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