1+ /*
2+ * Service for the PMT Calibration Database.
3+ * Andrea Scarpelli (ascarpell@bnl.gov), Matteo Vicenzi (mvicenzi@bnl.gov)
4+ */
5+ // Ported from icaruscode to SBND by Alejandro Sanchez-Castillo, Jan. 2025
6+
7+ // Framework includes
8+ #include " art/Framework/Principal/Run.h"
9+ #include " art/Framework/Services/Registry/ServiceDefinitionMacros.h"
10+ #include " cetlib_except/exception.h"
11+ #include " messagefacility/MessageLogger/MessageLogger.h"
12+
13+ // Local
14+ #include " sbndcode/Calibration/PDSDatabaseInterface/PMTCalibrationDatabase.h"
15+ #include " sbndcode/Calibration/PDSDatabaseInterface/PMTCalibrationDatabaseProvider.h"
16+
17+ // Database interface helpers
18+ #include " larevt/CalibrationDBI/IOVData/TimeStampDecoder.h"
19+ #include " larevt/CalibrationDBI/Providers/DBFolder.h"
20+
21+ // C/C++ standard libraries
22+ #include < string>
23+ #include < vector>
24+
25+ // --------------------------------------------------------------------------------
26+
27+ sbndDB::PMTCalibrationDatabaseProvider::PMTCalibrationDatabaseProvider (
28+ const fhicl::ParameterSet& pset)
29+ : fVerbose{pset.get <bool >(" Verbose" , false )}
30+ , fLogCategory {pset.get <std::string>(" LogCategory" , " PMTTimingCorrection" )}
31+ {
32+ fhicl::ParameterSet const tags{pset.get <fhicl::ParameterSet>(" CorrectionTags" )};
33+ fPMTCalibrationDatabaseTag = tags.get <std::string>(" PMTCalibrationDatabaseTag" );
34+ fDatabaseTimeStamp = tags.get <long >(" DatabaseTimeStamp" );
35+ fTableName = tags.get <std::string>(" TableName" );
36+ fSERLength = tags.get <size_t >(" SERLength" );
37+ if (fVerbose )
38+ mf::LogInfo (fLogCategory ) << " Database tags for timing corrections:\n "
39+ << " Cables corrections " << fPMTCalibrationDatabaseTag << " \n " ;
40+ }
41+
42+ // -------------------------------------------------------------------------------
43+
44+ uint64_t sbndDB::PMTCalibrationDatabaseProvider::RunToDatabaseTimestamp (uint32_t run) const
45+ {
46+
47+ // Run number to timestamp used in the db
48+ // DBFolder.h only takes 19 digit (= timestamp in nano second),
49+ // but SBND tables are currently using run numbers
50+ // Step 1) Add 1000000000 to the run number; e.g., run XXXXX -> 10000XXXXX
51+ // Step 2) Multiply 1000000000
52+ uint64_t runNum = uint64_t (run);
53+ uint64_t timestamp = runNum + 1000000000 ;
54+ timestamp *= 1000000000 ;
55+
56+ if (fVerbose )
57+ mf::LogInfo (fLogCategory ) << " Run " << runNum << " corrections from DB timestamp " << timestamp;
58+
59+ return timestamp;
60+ }
61+
62+ // -------------------------------------------------------------------------------
63+
64+ // / Function to look up the calibration database at the table holding the pmt hardware cables corrections
65+ void sbndDB::PMTCalibrationDatabaseProvider::ReadPMTCalibration (uint32_t run)
66+ {
67+ const std::string dbname (fTableName );
68+ lariov::DBFolder db (dbname, " " , " " , fPMTCalibrationDatabaseTag , true , false );
69+
70+ bool ret = db.UpdateData (fDatabaseTimeStamp ); // select table based on timestamp (this is temporary, once we generate db based on run numbers this should be changed)
71+ mf::LogDebug (fLogCategory ) << dbname + " corrections" << (ret ? " " : " not" )
72+ << " updated for run " << run;
73+ mf::LogTrace (fLogCategory )
74+ << " Fetched IoV [ " << db.CachedStart ().DBStamp () << " ; " << db.CachedEnd ().DBStamp ()
75+ << " ] to cover t=" << RunToDatabaseTimestamp (run)
76+ << " [=" << lariov::TimeStampDecoder::DecodeTimeStamp (RunToDatabaseTimestamp (run)).DBStamp ()
77+ << " ]" ;
78+
79+ std::vector<unsigned int > channelList;
80+ if (int res = db.GetChannelList (channelList); res != 0 ) {
81+ throw cet::exception (" PMTTimingCorrectionsProvider" )
82+ << " GetChannelList() returned " << res << " on run " << run << " query in " << dbname << " \n " ;
83+ }
84+
85+ if (channelList.empty ()) {
86+ throw cet::exception (" PMTTimingCorrectionsProvider" )
87+ << " Got an empty channel list for run " << run << " in " << dbname << " \n " ;
88+ }
89+
90+ for (auto channel : channelList) {
91+ // Read breakout box
92+ long _breakoutbox = 0 ;
93+ int error = db.GetNamedChannelData (channel, " breakout_box" , _breakoutbox);
94+ if (error)
95+ throw cet::exception (" PMTTimingCorrectionsProvider" )
96+ << " Encountered error (code " << error
97+ << " ) while trying to access 'breakout_box' on table " << dbname << " \n " ;
98+ fPMTCalibrationData [channel].breakoutBox = static_cast <int >(_breakoutbox);
99+
100+ // Read caen digitizer
101+ long _caen_digitizer = 0 ;
102+ error = db.GetNamedChannelData (channel, " caen_digitizer" , _caen_digitizer);
103+ if (error)
104+ throw cet::exception (" PMTTimingCorrectionsProvider" )
105+ << " Encountered error (code " << error
106+ << " ) while trying to access 'caen_digitizer' on table " << dbname << " \n " ;
107+ fPMTCalibrationData [channel].caenDigitizer = static_cast <int >(_caen_digitizer);
108+
109+ // Read caen digitizer channel
110+ long _caen_digitizer_channel = 0 ;
111+ error = db.GetNamedChannelData (channel, " caen_digitizer_channel" , _caen_digitizer_channel);
112+ if (error)
113+ throw cet::exception (" PMTTimingCorrectionsProvider" )
114+ << " Encountered error (code " << error
115+ << " ) while trying to access 'caen_digitizer_channel' on table " << dbname << " \n " ;
116+ fPMTCalibrationData [channel].caenDigitizerChannel = static_cast <int >(_caen_digitizer_channel);
117+
118+ // Read total transit time
119+ double _total_transit_time = 0 .;
120+ error = db.GetNamedChannelData (channel, " total_transit_time" , _total_transit_time);
121+ if (error)
122+ throw cet::exception (" PMTTimingCorrectionsProvider" )
123+ << " Encountered error (code " << error
124+ << " ) while trying to access 'total_transit_time' on table " << dbname << " \n " ;
125+ fPMTCalibrationData [channel].totalTransitTime = _total_transit_time;
126+
127+ // Read spe amplitude
128+ double _spe_amplitude = 0 .;
129+ error = db.GetNamedChannelData (channel, " spe_amp" , _spe_amplitude);
130+ if (error)
131+ throw cet::exception (" PMTTimingCorrectionsProvider" )
132+ << " Encountered error (code " << error
133+ << " ) while trying to access 'spe_amplitude' on table " << dbname << " \n " ;
134+ fPMTCalibrationData [channel].spe_amplitude = _spe_amplitude;
135+
136+ // Read gauss filter power
137+ double _gauss_w_wc_power = 0 ;
138+ error = db.GetNamedChannelData (channel, " gauss_w_wc_power" , _gauss_w_wc_power);
139+ if (error)
140+ throw cet::exception (" PMTTimingCorrectionsProvider" )
141+ << " Encountered error (code " << error
142+ << " ) while trying to access 'gauss_w_wc_power' on table " << dbname << " \n " ;
143+ fPMTCalibrationData [channel].gauss_wc_power = _gauss_w_wc_power;
144+
145+ // Read gauss filter wc
146+ double _gauss_wc = 0 .;
147+ error = db.GetNamedChannelData (channel, " gauss_wc" , _gauss_wc);
148+ if (error)
149+ throw cet::exception (" PMTTimingCorrectionsProvider" )
150+ << " Encountered error (code " << error << " ) while trying to access 'gauss_wc' on table "
151+ << dbname << " \n " ;
152+ fPMTCalibrationData [channel].gauss_wc = _gauss_wc;
153+
154+ // Read SER
155+ std::vector<double > _ser;
156+ std::string name_base = " ser_vec_" ;
157+ for (size_t i = 0 ; i < fSERLength ; i++) {
158+ std::string entry_num = std::to_string (i);
159+ std::string entry_name = name_base + entry_num;
160+ double _ser_component = 0 .;
161+ error = db.GetNamedChannelData (channel, entry_name, _ser_component);
162+ if (error)
163+ throw cet::exception (" PMTTimingCorrectionsProvider" )
164+ << " Encountered error (code " << error << " ) while trying to access 'ser_vec' on table "
165+ << dbname << " \n " ;
166+ _ser.push_back (_ser_component);
167+ }
168+ fPMTCalibrationData [channel].ser = _ser;
169+ }
170+ }
171+
172+ // -----------------------------------------------------------------------------
173+
174+ // / Read all the corrections from the database and save them inside a map, whose index
175+ // / is the PMT channel number
176+ void sbndDB::PMTCalibrationDatabaseProvider::readPMTCalibrationDatabase (const art::Run& run)
177+ {
178+
179+ // Clear before the run
180+ fPMTCalibrationData .clear ();
181+
182+ ReadPMTCalibration (run.id ().run ());
183+
184+ if (fVerbose ) {
185+ mf::LogInfo (fLogCategory ) << " Dump information from database " << std::endl;
186+ mf::LogVerbatim (fLogCategory )
187+ << " channel, trigger cable delay, reset cable delay, laser corrections, muons corrections"
188+ << std::endl;
189+ for (auto const & [key, value] : fPMTCalibrationData ) {
190+ mf::LogVerbatim (fLogCategory ) << key << " " << value.breakoutBox << " ," << std::endl;
191+ }
192+ }
193+ }
0 commit comments