Skip to content

Commit a0a6e8c

Browse files
Add actua database variables
1 parent 10882cf commit a0a6e8c

12 files changed

Lines changed: 438 additions & 473 deletions

sbndcode/DatabaseInterface/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ file(GLOB lib_srcs *.cxx)
2929

3030
art_make_library( SOURCE ${lib_srcs} LIBRARIES PUBLIC ${LIB_LIBRARIES})
3131

32-
cet_build_plugin( PMTTimingCorrectionService art::service LIBRARIES PUBLIC ${SERVICE_LIBRARIES})
32+
cet_build_plugin( PMTCalibrationDatabaseService art::service LIBRARIES PUBLIC ${SERVICE_LIBRARIES})
3333

3434
install_headers()
3535
install_fhicl()

sbndcode/DatabaseInterface/IPMTTimingCorrectionService.h renamed to sbndcode/DatabaseInterface/IPMTCalibrationDatabaseService.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* @author Andrea Scarpelli (ascarpell@bnl.gov), Gianluca Petrillo (petrillo@slac.stanford.edu)
55
*/
66

7-
#ifndef ICARUSCODE_TIMING_IPMTTIMINGCORRECTIONSERVICE_H
8-
#define ICARUSCODE_TIMING_IPMTTIMINGCORRECTIONSERVICE_H
7+
#ifndef SBNDCCODE_DATABASEINTERFACE_IPMTCALIBRATIONDATABASESERVICE_H
8+
#define SBNDCCODE_DATABASEINTERFACE_IPMTCALIBRATIONDATABASESERVICE_H
99

1010
// ICARUS libraries
11-
#include "sbndcode/DatabaseInterface/PMTTimingCorrections.h"
11+
#include "sbndcode/DatabaseInterface/PMTCalibrationDatabase.h"
1212

1313
// LArSoft libraries
1414
#include "larcore/CoreUtils/ServiceProviderWrappers.h"
@@ -17,17 +17,17 @@
1717
// -----------------------------------------------------------------------------
1818
namespace sbndDB {
1919
/// The only thing this service does is to return its service provider of type
20-
/// `icarusDB::PMTTimingCorrections`.
21-
using IPMTTimingCorrectionService
22-
= lar::ServiceProviderInterfaceWrapper<PMTTimingCorrections>;
20+
/// `sbndDB::PMTCalibrationDatabase`.
21+
using IPMTCalibrationDatabaseService
22+
= lar::ServiceProviderInterfaceWrapper<PMTCalibrationDatabase>;
2323
}
2424

2525

2626
// -----------------------------------------------------------------------------
27-
DECLARE_ART_SERVICE_INTERFACE(sbndDB::IPMTTimingCorrectionService, SHARED)
27+
DECLARE_ART_SERVICE_INTERFACE(sbndDB::IPMTCalibrationDatabaseService, SHARED)
2828

2929

3030
// -----------------------------------------------------------------------------
3131

3232

33-
#endif // ICARUSCODE_TIMING_IPMTTIMINGCORRECTIONSERVICE_H
33+
#endif // ICARUSCODE_DATABASEINTERFACE_IPMTCALIBRATIONDATABASESERVICE_H
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
///////////////////////////////////////////////////////////////////////
2+
///
3+
/// Interface class between the calibration database and the PMT time corrections
4+
///
5+
/// A. Scarpelli
6+
///
7+
/// \mailto ascarpell@bnl.gov
8+
///
9+
////////////////////////////////////////////////////////////////////////
10+
// Ported to SBND by Alejandro Sanchez-Castillo, Jan. 2025
11+
12+
#ifndef SBNDCCODE_DATABASEINTERFACE_PMTCALIBRATIONDATABASE_H
13+
#define SBNDCCODE_DATABASEINTERFACE_PMTCALIBRATIONDATABASE_H
14+
15+
16+
#include "larcorealg/CoreUtils/UncopiableAndUnmovableClass.h"
17+
18+
#include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
19+
#include "art/Framework/Principal/Run.h"
20+
21+
namespace sbndDB {
22+
23+
class PMTCalibrationDatabase: lar::UncopiableClass
24+
{
25+
public:
26+
27+
virtual ~PMTCalibrationDatabase() noexcept = default;
28+
virtual int getBreakoutBox( unsigned int channelID ) const = 0;
29+
virtual int getCAENDigitizer( unsigned int channelID ) const = 0;
30+
virtual int getCAENDigitizerChannel( unsigned int channelID ) const = 0;
31+
virtual double getTotalTransitTime( unsigned int channelID ) const = 0;
32+
virtual double getSPEAmplitude( unsigned int channelID ) const = 0;
33+
virtual double getGaussFilterPower( unsigned int channelID ) const = 0;
34+
virtual double getGaussFilterWC( unsigned int channelID ) const = 0;
35+
virtual std::vector<double> getSER( unsigned int channelID ) const = 0;
36+
}; // end class
37+
38+
}// end of namespace
39+
40+
DECLARE_ART_SERVICE_INTERFACE(sbndDB::PMTCalibrationDatabase, SHARED)
41+
42+
#endif
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Service for the PMT timing corrections.
3+
* Andrea Scarpelli (ascarpell@bnl.gov), Matteo Vicenzi (mvicenzi@bnl.gov)
4+
*/
5+
// Ported to SBND by Alejandro Sanchez-Castillo, Jan. 2025
6+
7+
#ifndef SBNDCODE_DATABASEINTERFACE_PMTCALIBRATIONDATABASEPROVIDER_H
8+
#define SBNDCODE_DATABASEINTERFACE_PMTCALIBRATIONDATABASEPROVIDER_H
9+
10+
// Framework includes
11+
#include "art/Framework/Services/Registry/ServiceDefinitionMacros.h"
12+
#include "art/Framework/Services/Registry/ActivityRegistry.h"
13+
#include "art/Framework/Services/Registry/ServiceHandle.h"
14+
#include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
15+
#include "art/Framework/Principal/Run.h"
16+
#include "messagefacility/MessageLogger/MessageLogger.h"
17+
#include "cetlib_except/exception.h"
18+
#include "fhiclcpp/ParameterSet.h"
19+
20+
// Local
21+
#include "sbndcode/DatabaseInterface/PMTCalibrationDatabase.h"
22+
23+
// C/C++ standard libraries
24+
#include <string>
25+
#include <map>
26+
#include <stdint.h>
27+
28+
29+
namespace sbndDB{ class PMTCalibrationDatabaseProvider; }
30+
/**
31+
* @brief
32+
*
33+
* This module reads the PMT timing corrections from the database.
34+
* Corrections are picked according to the run number being processed.
35+
*
36+
* All time corrections are offsets (in microseconds) that need to be _added_ to the uncorrected time.
37+
*
38+
* Configuration parameters
39+
* -------------------------
40+
* * `CorrectionTags`: tags to select the correction versions:
41+
* * `CablesTag` (default: `v1r0`): correction for cable delay.
42+
* * `LaserTag` (default: `v1r0`): first order PMT time correction, from laser data.
43+
* * `CosmicsTag` (default: `v1r0`): second order PMT time correction, from cosmic rays.
44+
* * `Verbose` (default: `false`): Print-out the corrections read from the database.
45+
* * `LogCategory` (default: `PMTTimingCorrection")
46+
*
47+
*/
48+
class sbndDB::PMTCalibrationDatabaseProvider : public PMTCalibrationDatabase {
49+
50+
public:
51+
52+
PMTCalibrationDatabaseProvider(const fhicl::ParameterSet& pset);
53+
54+
/// Read timing corrections from the database
55+
void readPMTCalibrationDatabase(const art::Run& run);
56+
57+
/// Get time delay on the trigger line
58+
int getBreakoutBox( unsigned int channelID ) const override {
59+
return getChannelCorrOrDefault(channelID).breakoutBox;
60+
};
61+
int getCAENDigitizer( unsigned int channelID ) const override {
62+
return getChannelCorrOrDefault(channelID).caenDigitizer;
63+
};
64+
int getCAENDigitizerChannel( unsigned int channelID ) const override {
65+
return getChannelCorrOrDefault(channelID).caenDigitizerChannel;
66+
};
67+
double getTotalTransitTime( unsigned int channelID ) const override {
68+
return getChannelCorrOrDefault(channelID).totalTransitTime;
69+
};
70+
double getSPEAmplitude( unsigned int channelID ) const override {
71+
return getChannelCorrOrDefault(channelID).spe_amplitude;
72+
};
73+
double getGaussFilterPower( unsigned int channelID ) const override {
74+
return getChannelCorrOrDefault(channelID).gauss_wc_power;
75+
};
76+
double getGaussFilterWC( unsigned int channelID ) const override {
77+
return getChannelCorrOrDefault(channelID).gauss_wc;
78+
};
79+
std::vector<double> getSER( unsigned int channelID ) const override {
80+
return getChannelCorrOrDefault(channelID).ser;
81+
};
82+
private:
83+
84+
bool fVerbose = false; ///< Whether to print the configuration we read.
85+
std::string fLogCategory; ///< Category tag for messages.
86+
std::string fCablesTag; ///< Tag for cable corrections database.
87+
long fDatabaseTimeStamp;
88+
std::string fLaserTag; ///< Tag for laser corrections database.
89+
std::string fCosmicsTag; ///< Tag for cosmics corrections database.
90+
std::string fTableName;
91+
std::string fVariabletoread;
92+
size_t fSERLength;
93+
94+
/// Structure for single channel corrections
95+
struct PMTCalibrationDB {
96+
97+
size_t breakoutBox=0;
98+
size_t caenDigitizer=0;
99+
size_t caenDigitizerChannel=0;
100+
size_t totalTransitTime=0;
101+
double spe_amplitude=0.;
102+
double gauss_wc_power=0.;
103+
double gauss_wc=0.;
104+
std::vector<double> ser={};
105+
};
106+
107+
const PMTCalibrationDB CorrectionDefaults = {0, 0, 0, 0, 0.0, 0.0, 0.0, {}};
108+
109+
/// Map of corrections by channel
110+
std::map<unsigned int, PMTCalibrationDB> fPMTCalibrationData;
111+
112+
/// Internal access to the channel correction record; returns defaults if not present.
113+
PMTCalibrationDB const& getChannelCorrOrDefault
114+
(unsigned int channelID) const
115+
{
116+
auto const it = fPMTCalibrationData.find(channelID);
117+
return (it == fPMTCalibrationData.end())? CorrectionDefaults: it->second;
118+
}
119+
120+
/// Convert run number to internal database
121+
uint64_t RunToDatabaseTimestamp(uint32_t run) const;
122+
123+
void ReadPMTCalibration(uint32_t run);
124+
125+
126+
}; // services class
127+
128+
#endif

0 commit comments

Comments
 (0)