Skip to content

Commit 3ea6b72

Browse files
committed
Add TOF Response parameters as a service
1 parent 6c47c04 commit 3ea6b72

File tree

6 files changed

+575
-409
lines changed

6 files changed

+575
-409
lines changed

Common/Core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ o2physics_add_library(AnalysisCore
2121
MetadataHelper.cxx
2222
CollisionTypeHelper.cxx
2323
FFitWeights.cxx
24-
PUBLIC_LINK_LIBRARIES O2::Framework O2::DataFormatsParameters ROOT::EG O2::CCDB ROOT::Physics O2::FT0Base O2::FV0Base)
24+
PUBLIC_LINK_LIBRARIES O2::Framework O2::DataFormatsParameters ROOT::EG O2::CCDB ROOT::Physics O2::FT0Base O2::FV0Base O2::DataFormatsTOF)
2525

2626
o2physics_target_root_dictionary(AnalysisCore
2727
HEADERS TrackSelection.h

Common/Core/PID/PIDTOF.cxx

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
///
1818

1919
#include "PIDTOF.h"
20+
21+
// O2Physics includes
22+
#include "Common/Core/CollisionTypeHelper.h"
23+
24+
// O2 includes
25+
#include "DataFormatsParameters/GRPLHCIFData.h"
26+
#include "Framework/Logger.h"
27+
28+
#include <map>
2029
#include <string>
2130

2231
namespace o2::pid::tof
@@ -102,3 +111,276 @@ float TOFResoParamsV3::getTimeShift(float eta, int16_t sign) const
102111
}
103112

104113
} // namespace o2::pid::tof
114+
115+
#include "Framework/CommonServices.h"
116+
#include "Framework/Plugins.h"
117+
#include "Framework/ServiceHandle.h"
118+
#include "Framework/ServiceSpec.h"
119+
120+
#include "TDatabasePDG.h"
121+
122+
using namespace o2::framework;
123+
124+
o2::pid::tof::TOFResoParamsV3 o2::pid::tof::TOFResponseImpl::parameters;
125+
o2::common::core::MetadataHelper o2::pid::tof::TOFResponseImpl::metadataInfo;
126+
bool o2::pid::tof::TOFResponseImpl::mIsInit = false;
127+
int o2::pid::tof::TOFResponseImpl::mLastRunNumber = -1;
128+
129+
void o2::pid::tof::TOFResponseImpl::inheritFromBaseTask(o2::framework::InitContext& initContext, const std::string task)
130+
{
131+
if (mIsInit) {
132+
LOG(fatal) << "TOFResponseImpl already initialized, cannot re-initialize";
133+
}
134+
getCfg(initContext, "ccdb-url", mUrl, task);
135+
getCfg(initContext, "ccdb-path-grplhcif", mPathGrpLhcIf, task);
136+
getCfg(initContext, "ccdb-timestamp", mTimestamp, task);
137+
getCfg(initContext, "timeShiftCCDBPathPos", mTimeShiftCCDBPathPos, task);
138+
getCfg(initContext, "timeShiftCCDBPathNeg", mTimeShiftCCDBPathNeg, task);
139+
getCfg(initContext, "timeShiftCCDBPathPosMC", mTimeShiftCCDBPathPosMC, task);
140+
getCfg(initContext, "timeShiftCCDBPathNegMC", mTimeShiftCCDBPathNegMC, task);
141+
getCfg(initContext, "paramFileName", mParamFileName, task);
142+
getCfg(initContext, "parametrizationPath", mParametrizationPath, task);
143+
getCfg(initContext, "reconstructionPass", mReconstructionPass, task);
144+
getCfg(initContext, "reconstructionPassDefault", mReconstructionPassDefault, task);
145+
getCfg(initContext, "fatalOnPassNotAvailable", mFatalOnPassNotAvailable, task);
146+
getCfg(initContext, "enableTimeDependentResponse", mEnableTimeDependentResponse, task);
147+
getCfg(initContext, "collisionSystem", mCollisionSystem, task);
148+
getCfg(initContext, "autoSetProcessFunctions", mAutoSetProcessFunctions, task);
149+
}
150+
151+
void o2::pid::tof::TOFResponseImpl::initSetup(o2::ccdb::BasicCCDBManager* ccdb,
152+
o2::framework::InitContext& initContext)
153+
{
154+
if (mIsInit) {
155+
LOG(fatal) << "TOFResponseImpl already initialized, cannot re-initialize";
156+
}
157+
158+
if (!ccdb) {
159+
LOG(fatal) << "CCDB manager is not set, cannot initialize TOFResponseImpl";
160+
}
161+
inheritFromBaseTask(initContext); // Gets the configuration parameters from the base task (tof-signal)
162+
mCcdb = ccdb; // Set the CCDB manager
163+
mCcdb->setURL(mUrl);
164+
mCcdb->setTimestamp(mTimestamp);
165+
mCcdb->setCaching(true);
166+
mCcdb->setLocalObjectValidityChecking();
167+
// Not later than now objects
168+
mCcdb->setCreatedNotAfter(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
169+
170+
mIsInit = true; // Set the initialization flag
171+
172+
// Then the information about the metadata
173+
if (mReconstructionPass == "metadata") {
174+
LOG(info) << "Getting pass from metadata";
175+
if (metadataInfo.isMC()) {
176+
mReconstructionPass = metadataInfo.get("AnchorPassName");
177+
} else {
178+
mReconstructionPass = metadataInfo.get("RecoPassName");
179+
}
180+
LOG(info) << "Passed autodetect mode for pass. Taking '" << mReconstructionPass << "'";
181+
}
182+
LOG(info) << "Using parameter collection, starting from pass '" << mReconstructionPass << "'";
183+
184+
if (!mParamFileName.empty()) { // Loading the parametrization from file
185+
LOG(info) << "Loading exp. sigma parametrization from file " << mParamFileName << ", using param: " << mParametrizationPath << " and pass " << mReconstructionPass;
186+
o2::tof::ParameterCollection paramCollection;
187+
paramCollection.loadParamFromFile(mParamFileName, mParametrizationPath);
188+
LOG(info) << "+++ Loaded parameter collection from file +++";
189+
if (!paramCollection.retrieveParameters(parameters, mReconstructionPass)) {
190+
if (mFatalOnPassNotAvailable) {
191+
LOG(fatal) << "Pass '" << mReconstructionPass << "' not available in the retrieved object from file";
192+
} else {
193+
LOG(warning) << "Pass '" << mReconstructionPass << "' not available in the retrieved object from file, fetching '" << mReconstructionPassDefault << "'";
194+
if (!paramCollection.retrieveParameters(parameters, mReconstructionPassDefault)) {
195+
paramCollection.print();
196+
LOG(fatal) << "Cannot get default pass for calibration " << mReconstructionPassDefault;
197+
} else {
198+
if (metadataInfo.isRun3()) {
199+
parameters.setResolutionParametrization(paramCollection.getPars(mReconstructionPassDefault));
200+
} else {
201+
parameters.setResolutionParametrizationRun2(paramCollection.getPars(mReconstructionPassDefault));
202+
}
203+
parameters.setMomentumChargeShiftParameters(paramCollection.getPars(mReconstructionPassDefault));
204+
}
205+
}
206+
} else { // Pass is available, load non standard parameters
207+
if (metadataInfo.isRun3()) {
208+
parameters.setResolutionParametrization(paramCollection.getPars(mReconstructionPass));
209+
} else {
210+
parameters.setResolutionParametrizationRun2(paramCollection.getPars(mReconstructionPass));
211+
}
212+
parameters.setMomentumChargeShiftParameters(paramCollection.getPars(mReconstructionPass));
213+
}
214+
} else if (!mEnableTimeDependentResponse) { // Loading it from CCDB
215+
LOG(info) << "Loading initial exp. sigma parametrization from CCDB, using path: " << mParametrizationPath << " for timestamp " << mTimestamp;
216+
o2::tof::ParameterCollection* paramCollection = mCcdb->getSpecific<o2::tof::ParameterCollection>(mParametrizationPath, mTimestamp);
217+
if (!paramCollection->retrieveParameters(parameters, mReconstructionPass)) { // Attempt at loading the parameters with the pass defined
218+
if (mFatalOnPassNotAvailable) {
219+
LOG(fatal) << "Pass '" << mReconstructionPass << "' not available in the retrieved CCDB object";
220+
} else {
221+
LOG(warning) << "Pass '" << mReconstructionPass << "' not available in the retrieved CCDB object, fetching '" << mReconstructionPassDefault << "'";
222+
if (!paramCollection->retrieveParameters(parameters, mReconstructionPassDefault)) {
223+
paramCollection->print();
224+
LOG(fatal) << "Cannot get default pass for calibration " << mReconstructionPassDefault;
225+
} else {
226+
if (metadataInfo.isRun3()) {
227+
parameters.setResolutionParametrization(paramCollection->getPars(mReconstructionPassDefault));
228+
} else {
229+
parameters.setResolutionParametrizationRun2(paramCollection->getPars(mReconstructionPassDefault));
230+
}
231+
parameters.setMomentumChargeShiftParameters(paramCollection->getPars(mReconstructionPassDefault));
232+
}
233+
}
234+
} else { // Pass is available, load non standard parameters
235+
if (metadataInfo.isRun3()) {
236+
parameters.setResolutionParametrization(paramCollection->getPars(mReconstructionPass));
237+
} else {
238+
parameters.setResolutionParametrizationRun2(paramCollection->getPars(mReconstructionPass));
239+
}
240+
parameters.setMomentumChargeShiftParameters(paramCollection->getPars(mReconstructionPass));
241+
}
242+
}
243+
244+
// Loading additional calibration objects
245+
std::map<std::string, std::string> metadata;
246+
if (!mReconstructionPass.empty()) {
247+
metadata["RecoPassName"] = mReconstructionPass;
248+
}
249+
250+
auto updateTimeShift = [&](const std::string& nameShift, bool isPositive) {
251+
if (nameShift.empty()) {
252+
return;
253+
}
254+
const bool isFromFile = nameShift.find(".root") != std::string::npos;
255+
if (isFromFile) {
256+
LOG(info) << "Initializing the time shift for " << (isPositive ? "positive" : "negative") << " from file '" << nameShift << "'";
257+
parameters.setTimeShiftParameters(nameShift, "ccdb_object", isPositive);
258+
} else if (!mEnableTimeDependentResponse) { // If the response is fixed fetch it at the init time
259+
LOG(info) << "Initializing the time shift for " << (isPositive ? "positive" : "negative")
260+
<< " from ccdb '" << nameShift << "' and timestamp " << mTimestamp
261+
<< " and pass '" << mReconstructionPass << "'";
262+
mCcdb->setFatalWhenNull(false);
263+
parameters.setTimeShiftParameters(mCcdb->getSpecific<TGraph>(nameShift, mTimestamp, metadata), isPositive);
264+
mCcdb->setFatalWhenNull(true);
265+
}
266+
LOG(info) << " test getTimeShift at 0 " << (isPositive ? "pos" : "neg") << ": "
267+
<< parameters.getTimeShift(0, isPositive);
268+
};
269+
270+
const std::string nameShiftPos = metadataInfo.isMC() ? mTimeShiftCCDBPathPosMC : mTimeShiftCCDBPathPos;
271+
updateTimeShift(nameShiftPos, true);
272+
const std::string nameShiftNeg = metadataInfo.isMC() ? mTimeShiftCCDBPathNegMC : mTimeShiftCCDBPathNeg;
273+
updateTimeShift(nameShiftNeg, false);
274+
275+
// Calibration object is defined
276+
LOG(info) << "Parametrization at init time:";
277+
parameters.printFullConfig();
278+
}
279+
280+
void o2::pid::tof::TOFResponseImpl::processSetup(const int runNumber, const int64_t timeStamp)
281+
{
282+
LOG(debug) << "Processing setup for run number " << runNumber << " from run " << mLastRunNumber;
283+
// First we check if this run number was already processed
284+
if (mLastRunNumber == runNumber) {
285+
return;
286+
}
287+
LOG(info) << "Updating the parametrization from last run " << mLastRunNumber << " to " << runNumber << " and timestamp from " << mTimestamp << " " << timeStamp;
288+
mLastRunNumber = runNumber;
289+
mTimestamp = timeStamp;
290+
291+
// Check the beam type
292+
if (mCollisionSystem == o2::common::core::CollisionSystemType::kCollSysUndef) {
293+
o2::parameters::GRPLHCIFData* grpo = mCcdb->getSpecific<o2::parameters::GRPLHCIFData>(mPathGrpLhcIf,
294+
mTimestamp);
295+
mCollisionSystem = CollisionSystemType::getCollisionTypeFromGrp(grpo);
296+
} else {
297+
LOG(debug) << "Not setting collisions system as already set to " << mCollisionSystem << " " << CollisionSystemType::getCollisionSystemName(mCollisionSystem);
298+
}
299+
300+
if (!mEnableTimeDependentResponse) {
301+
return;
302+
}
303+
LOG(info) << "Updating parametrization from path '" << mParametrizationPath << "' and timestamp " << mTimestamp << " and reconstruction pass '" << mReconstructionPass << "' for run number " << runNumber;
304+
if (mParamFileName.empty()) { // Not loading if parametrization was taken from file
305+
LOG(info) << "Updating parametrization from ccdb";
306+
const o2::tof::ParameterCollection* paramCollection = mCcdb->getSpecific<o2::tof::ParameterCollection>(mParametrizationPath, mTimestamp);
307+
if (!paramCollection->retrieveParameters(parameters, mReconstructionPass)) {
308+
if (mFatalOnPassNotAvailable) {
309+
LOGF(fatal, "Pass '%s' not available in the retrieved CCDB object", mReconstructionPass.data());
310+
} else {
311+
LOGF(warning, "Pass '%s' not available in the retrieved CCDB object, fetching '%s'", mReconstructionPass.data(), mReconstructionPassDefault.data());
312+
if (!paramCollection->retrieveParameters(parameters, mReconstructionPassDefault)) {
313+
paramCollection->print();
314+
LOG(fatal) << "Cannot get default pass for calibration " << mReconstructionPassDefault;
315+
} else { // Found the default case
316+
if (metadataInfo.isRun3()) {
317+
parameters.setResolutionParametrization(paramCollection->getPars(mReconstructionPassDefault));
318+
} else {
319+
parameters.setResolutionParametrizationRun2(paramCollection->getPars(mReconstructionPassDefault));
320+
}
321+
parameters.setMomentumChargeShiftParameters(paramCollection->getPars(mReconstructionPassDefault));
322+
}
323+
}
324+
} else { // Found the non default case
325+
if (metadataInfo.isRun3()) {
326+
parameters.setResolutionParametrization(paramCollection->getPars(mReconstructionPass));
327+
} else {
328+
parameters.setResolutionParametrizationRun2(paramCollection->getPars(mReconstructionPass));
329+
}
330+
parameters.setMomentumChargeShiftParameters(paramCollection->getPars(mReconstructionPass));
331+
}
332+
}
333+
334+
// Loading additional calibration objects
335+
std::map<std::string, std::string> metadata;
336+
if (!mReconstructionPass.empty()) {
337+
metadata["RecoPassName"] = mReconstructionPass;
338+
}
339+
340+
auto updateTimeShift = [&](const std::string& nameShift, bool isPositive) {
341+
if (nameShift.empty()) {
342+
return;
343+
}
344+
const bool isFromFile = nameShift.find(".root") != std::string::npos;
345+
if (isFromFile) {
346+
return;
347+
}
348+
LOG(info) << "Updating the time shift for " << (isPositive ? "positive" : "negative")
349+
<< " from ccdb '" << nameShift << "' and timestamp " << mTimestamp
350+
<< " and pass '" << mReconstructionPass << "'";
351+
mCcdb->setFatalWhenNull(false);
352+
parameters.setTimeShiftParameters(mCcdb->getSpecific<TGraph>(nameShift, mTimestamp, metadata), isPositive);
353+
mCcdb->setFatalWhenNull(true);
354+
LOG(info) << " test getTimeShift at 0 " << (isPositive ? "pos" : "neg") << ": "
355+
<< parameters.getTimeShift(0, isPositive);
356+
};
357+
358+
updateTimeShift(metadataInfo.isMC() ? mTimeShiftCCDBPathPosMC : mTimeShiftCCDBPathPos, true);
359+
updateTimeShift(metadataInfo.isMC() ? mTimeShiftCCDBPathNegMC : mTimeShiftCCDBPathNeg, false);
360+
361+
LOG(info) << "Parametrization at setup time:";
362+
parameters.printFullConfig();
363+
}
364+
365+
struct TOFSupport : o2::framework::ServicePlugin {
366+
o2::framework::ServiceSpec* create() final
367+
{
368+
return new ServiceSpec{
369+
.name = "tof-response",
370+
.init = [](ServiceRegistryRef, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
371+
auto* wrapper = new o2::pid::tof::TOFResponse();
372+
auto* ptr = new o2::pid::tof::TOFResponseImpl();
373+
wrapper->setInstance(ptr);
374+
return ServiceHandle{TypeIdHelpers::uniqueId<o2::pid::tof::TOFResponse>(), wrapper, ServiceKind::Serial, "database-pdg"};
375+
},
376+
.configure = CommonServices::noConfiguration(),
377+
.exit = [](ServiceRegistryRef, void* service) {
378+
auto* resp = reinterpret_cast<o2::pid::tof::TOFResponse*>(service);
379+
delete resp; },
380+
.kind = ServiceKind::Serial};
381+
}
382+
};
383+
384+
DEFINE_DPL_PLUGINS_BEGIN
385+
DEFINE_DPL_PLUGIN_INSTANCE(TOFSupport, CustomService);
386+
DEFINE_DPL_PLUGINS_END

0 commit comments

Comments
 (0)