Skip to content

Commit e2d0ede

Browse files
Merge pull request #673 from SBNSoftware/feature/acastill_pmtdatabaseinterface
Feature/acastill pmtdatabaseinterface
2 parents a2e4f0c + 62a58a6 commit e2d0ede

File tree

10 files changed

+478
-0
lines changed

10 files changed

+478
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_subdirectory(OpReco)
22
add_subdirectory(CRT)
33
add_subdirectory(DQM)
4+
add_subdirectory(PDSDatabaseInterface)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cet_enable_asserts()
2+
3+
set( MODULE_LIBRARIES
4+
art_root_io::TFileService_service
5+
art_root_io::tfile_support
6+
)
7+
set( LIB_LIBRARIES
8+
art::Framework_Services_Registry
9+
messagefacility::MF_MessageLogger
10+
lardata::Utilities
11+
larevt::CalibrationDBI_IOVData
12+
larevt::CalibrationDBI_Providers
13+
)
14+
set( SERVICE_LIBRARIES
15+
sbndcode_Calibration_PDSDatabaseInterface
16+
larcore::Geometry_Geometry_service
17+
lardata::DetectorClocksService
18+
)
19+
20+
file(GLOB lib_srcs *.cxx)
21+
22+
art_make_library( SOURCE ${lib_srcs} LIBRARIES PUBLIC ${LIB_LIBRARIES})
23+
24+
cet_build_plugin( PMTCalibrationDatabaseService art::service LIBRARIES PUBLIC ${SERVICE_LIBRARIES})
25+
26+
install_headers()
27+
install_fhicl()
28+
install_source()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @file icaruscode/Timing/IPMTTimingCorrectionService.h
3+
* @brief Wrapper class for 'PMTTimingCorrectionsProvider.h'
4+
* @author Andrea Scarpelli (ascarpell@bnl.gov), Gianluca Petrillo (petrillo@slac.stanford.edu)
5+
*/
6+
// Ported to SBND by Alejandro Sanchez-Castillo, Jan. 2025
7+
8+
#ifndef SBNDCCODE_DATABASEINTERFACE_IPMTCALIBRATIONDATABASESERVICE_H
9+
#define SBNDCCODE_DATABASEINTERFACE_IPMTCALIBRATIONDATABASESERVICE_H
10+
11+
// ICARUS libraries
12+
#include "sbndcode/Calibration/PDSDatabaseInterface/PMTCalibrationDatabase.h"
13+
14+
// LArSoft libraries
15+
#include "larcore/CoreUtils/ServiceProviderWrappers.h"
16+
17+
// -----------------------------------------------------------------------------
18+
namespace sbndDB {
19+
/// The only thing this service does is to return its service provider of type
20+
/// `sbndDB::PMTCalibrationDatabase`.
21+
using IPMTCalibrationDatabaseService =
22+
lar::ServiceProviderInterfaceWrapper<PMTCalibrationDatabase>;
23+
}
24+
25+
// -----------------------------------------------------------------------------
26+
DECLARE_ART_SERVICE_INTERFACE(sbndDB::IPMTCalibrationDatabaseService, SHARED)
27+
28+
// -----------------------------------------------------------------------------
29+
30+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#include "larcorealg/CoreUtils/UncopiableAndUnmovableClass.h"
16+
17+
#include "art/Framework/Principal/Run.h"
18+
#include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
19+
20+
namespace sbndDB {
21+
22+
class PMTCalibrationDatabase : lar::UncopiableClass {
23+
public:
24+
virtual ~PMTCalibrationDatabase() noexcept = default;
25+
virtual int getBreakoutBox(unsigned int channelID) const = 0;
26+
virtual int getCAENDigitizer(unsigned int channelID) const = 0;
27+
virtual int getCAENDigitizerChannel(unsigned int channelID) const = 0;
28+
virtual double getTotalTransitTime(unsigned int channelID) const = 0;
29+
virtual double getSPEAmplitude(unsigned int channelID) const = 0;
30+
virtual double getGaussFilterPower(unsigned int channelID) const = 0;
31+
virtual double getGaussFilterWC(unsigned int channelID) const = 0;
32+
virtual std::vector<double> getSER(unsigned int channelID) const = 0;
33+
}; // end class
34+
35+
} // end of namespace
36+
37+
DECLARE_ART_SERVICE_INTERFACE(sbndDB::PMTCalibrationDatabase, SHARED)
38+
39+
#endif
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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

Comments
 (0)