Project
Loading...
Searching...
No Matches
CalibratordEdxSpec.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
17
18#include <vector>
19#include <memory>
20
21// o2 includes
22#include "CCDB/CcdbApi.h"
23#include "CCDB/CcdbObjectInfo.h"
24// #include "CommonUtils/NameConf.h"
26// #include "DataFormatsParameters/GRPObject.h"
28#include "Framework/Task.h"
36#include "TPCBase/CDBTypes.h"
37#include "TPCBase/Utils.h"
38
39using namespace o2::framework;
40
41namespace o2::tpc
42{
43
45{
46 public:
47 CalibratordEdxDevice(std::shared_ptr<o2::base::GRPGeomRequest> req, const o2::base::Propagator::MatCorrType matType) : mCCDBRequest(req), mMatType(matType) {}
49 {
51 const auto slotLengthTF = ic.options().get<uint32_t>("tf-per-slot");
52 const auto slotLengthSeconds = ic.options().get<int>("seconds-per-slot");
53 const auto maxDelay = ic.options().get<uint32_t>("max-delay");
54 const auto minEntries = ic.options().get<int>("min-entries");
55 mCalibIntervalExtensionMS = ic.options().get<uint32_t>("calib-interval-extension") * 1000l;
56
57 const auto minEntriesSector = ic.options().get<int>("min-entries-sector");
58 const auto minEntries1D = ic.options().get<int>("min-entries-1d");
59 const auto minEntries2D = ic.options().get<int>("min-entries-2d");
60 const auto fitPasses = ic.options().get<int>("fit-passes");
61 const auto fitThreshold = ic.options().get<float>("fit-threshold");
62 const auto fitThresholdLowFactor = ic.options().get<float>("fit-threshold-low-factor");
63
64 const auto dEdxBins = ic.options().get<int>("dedxbins");
65 const auto mindEdx = ic.options().get<float>("min-dedx");
66 const auto maxdEdx = ic.options().get<float>("max-dedx");
67 const auto angularBins = ic.options().get<int>("angularbins");
68 const auto fitSnp = ic.options().get<bool>("fit-snp");
69
70 const auto dumpData = ic.options().get<bool>("file-dump");
71 const auto dumpHistograms = ic.options().get<uint32_t>("dump-histograms");
72 const auto trackDebug = ic.options().get<bool>("track-debug");
73 const bool makeGaussianFits = !ic.options().get<bool>("disable-gaussian-fits");
74
75 mCalibrator = std::make_unique<tpc::CalibratordEdx>();
76 mCalibrator->setHistParams(dEdxBins, mindEdx, maxdEdx, angularBins, fitSnp);
77 mCalibrator->setApplyCuts(false);
78 mCalibrator->setFitThresholds(minEntriesSector, minEntries1D, minEntries2D);
79 mCalibrator->setMinEntries(minEntries);
80 mCalibrator->setSlotLength(slotLengthTF);
81 mCalibrator->setSlotLengthInSeconds(slotLengthSeconds);
82 mCalibrator->setMaxSlotsDelay(maxDelay);
83 mCalibrator->setElectronCut({fitThreshold, fitPasses, fitThresholdLowFactor});
84 mCalibrator->setMaterialType(mMatType);
85 mCalibrator->setDumpHistograms(dumpHistograms);
86 mCalibrator->setTrackDebug(trackDebug);
87 mCalibrator->setMakeGaussianFits(makeGaussianFits);
88
89 mCustomdEdxFileName = o2::gpu::GPUConfigurableParamGPUSettingsO2::Instance().dEdxCorrFile;
90 mDisableTimeGain = o2::gpu::GPUConfigurableParamGPUSettingsO2::Instance().dEdxDisableResidualGain;
91
92 if (mDisableTimeGain) {
93 LOGP(info, "TimeGain correction was disabled via GPU_global.dEdxDisableResidualGain=1");
94 }
95
96 if (!mDisableTimeGain && !mCustomdEdxFileName.empty()) {
97 std::unique_ptr<TFile> fdEdxCustom(TFile::Open(mCustomdEdxFileName.data()));
98 if (!fdEdxCustom || !fdEdxCustom->IsOpen() || fdEdxCustom->IsZombie()) {
99 LOGP(error, "Could not open custom TimeGain file {}", mCustomdEdxFileName);
100 } else {
101 const auto timeGain = fdEdxCustom->Get<o2::tpc::CalibdEdxCorrection>("CalibdEdxCorrection");
102 if (!timeGain) {
103 LOGP(error, "Could not load 'CalibdEdxCorrection' from file {}", mCustomdEdxFileName);
104 } else {
105 mTimeGain = *timeGain;
106 const auto meanParamTot = mTimeGain.getMeanParams(ChargeType::Tot);
107 LOGP(info, "Loaded custom TimeGain from file {} with {} dimensions and mean qTot Params {}", mCustomdEdxFileName, mTimeGain.getDims(), utils::elementsToString(meanParamTot));
108 }
109 }
110 }
111
112 if (dumpData) {
113 const auto dumpDataName = ic.options().get<std::string>("file-dump-name");
114 mCalibrator->enableDebugOutput(dumpDataName);
115 }
116 }
117
118 void finaliseCCDB(o2::framework::ConcreteDataMatcher& matcher, void* obj) final
119 {
121 return;
122 }
123 if ((mDisableTimeGain == 0) && mCustomdEdxFileName.empty() && (matcher == ConcreteDataMatcher("TPC", "TIMEGAIN", 0))) {
124 mTimeGain = *(o2::tpc::CalibdEdxCorrection*)obj;
125 const auto meanParamTot = mTimeGain.getMeanParams(ChargeType::Tot);
126 LOGP(info, "Updating TimeGain with {} dimensions and mean qTot Params {}", mTimeGain.getDims(), utils::elementsToString(meanParamTot));
127 return;
128 }
129 }
130
131 void run(ProcessingContext& pc) final
132 {
133 o2::base::TFIDInfoHelper::fillTFIDInfo(pc, mCalibrator->getCurrentTFInfo());
135 checkUpdates(pc);
136 static bool slotLengthSet = false;
137 if (!slotLengthSet) {
138 // Important, otherwise the call to getSlotForTF below will be wrong, must be called after GRPGeomHelper update to get the correct TF length
139 mCalibrator->checkSlotLength();
140 slotLengthSet = true;
141 }
142
143 auto& slotTF = mCalibrator->getSlotForTF(mCalibrator->getCurrentTFInfo().tfCounter);
144 auto calib = slotTF.getContainer();
145 const auto stackID = StackID{0, GEMstack::IROCgem};
146 const auto calibInEnties = calib->getCalibrationInput().getEntries(stackID, ChargeType::Tot);
147 calib->setCalibrationInput(mTimeGain);
148 if (calibInEnties != mTimeGain.getEntries(stackID, ChargeType::Tot)) {
149 const auto meanParamTot = calib->getCalibrationInput().getMeanParams(ChargeType::Tot);
150 LOGP(info, "Updating TimeGain with {} dimensions and mean qTot Params {} for slot with TF range {} <=TF<= {}",
151 calib->getCalibrationInput().getDims(), utils::elementsToString(meanParamTot), slotTF.getTFStart(), slotTF.getTFEnd());
152 }
153
154 const auto tracks = pc.inputs().get<gsl::span<tpc::TrackTPC>>("tracks");
155 LOGP(detail, "Processing TF {} with {} tracks", mCalibrator->getCurrentTFInfo().tfCounter, tracks.size());
156 mRunNumber = mCalibrator->getCurrentTFInfo().runNumber;
157 mCalibrator->process(tracks);
158 sendOutput(pc.outputs());
159
160 const auto& infoVec = mCalibrator->getTFinterval();
161 LOGP(detail, "Created {} objects for TF {}", infoVec.size(), mCalibrator->getCurrentTFInfo().tfCounter);
162 }
163
165 {
166 LOGP(info, "Finalizing calibration");
167 mCalibrator->checkSlotsToFinalize(o2::calibration::INFINITE_TF);
168 sendOutput(eos.outputs());
169
170 if (mCalibrator->hasDebugOutput()) {
171 mCalibrator->finalizeDebugOutput();
172 }
173 }
174
175 private:
176 void sendOutput(DataAllocator& output)
177 {
178 const auto& calibrations = mCalibrator->getCalibs();
179 const auto& intervals = mCalibrator->getTimeIntervals();
180
181 assert(calibrations.size() == intervals.size());
182 for (unsigned int i = 0; i < calibrations.size(); i++) {
183 const auto& object = calibrations[i];
184 o2::ccdb::CcdbObjectInfo info(CDBTypeMap.at(CDBType::CalTimeGain), std::string{}, std::string{}, std::map<std::string, std::string>{{"runNumber", std::to_string(mRunNumber)}}, intervals[i].first, intervals[i].second + mCalibIntervalExtensionMS + 1);
185 auto image = o2::ccdb::CcdbApi::createObjectImage(&object, &info);
186 LOGP(info, "Sending object {} / {} of size {} bytes, valid for {} : {} ", info.getPath(), info.getFileName(), image->size(), info.getStartValidityTimestamp(), info.getEndValidityTimestamp());
187 output.snapshot(Output{o2::calibration::Utils::gDataOriginCDBPayload, "TPC_CalibdEdx", i}, *image.get()); // vector<char>
188 output.snapshot(Output{o2::calibration::Utils::gDataOriginCDBWrapper, "TPC_CalibdEdx", i}, info); // root-serialized
189 }
190 mCalibrator->initOutput(); // empty the outputs after they are send
191 }
192
193 void checkUpdates(ProcessingContext& pc) const
194 {
195 if (pc.inputs().isValid("tpctimegain")) {
196 pc.inputs().get<o2::tpc::CalibdEdxCorrection*>("tpctimegain");
197 } else {
198 return;
199 }
200 }
201
202 std::unique_ptr<CalibratordEdx> mCalibrator;
203 const o2::base::Propagator::MatCorrType mMatType{};
204 std::shared_ptr<o2::base::GRPGeomRequest> mCCDBRequest;
205 uint32_t mRunNumber{0};
206 long mCalibIntervalExtensionMS{0};
207 o2::tpc::CalibdEdxCorrection mTimeGain{};
208 bool mDisableTimeGain{false};
209 std::string mCustomdEdxFileName{};
210};
211
213{
214 const bool enableAskMatLUT = matType == o2::base::Propagator::MatCorrType::USEMatCorrLUT;
215 std::vector<OutputSpec> outputs;
216 outputs.emplace_back(ConcreteDataTypeMatcher{o2::calibration::Utils::gDataOriginCDBPayload, "TPC_CalibdEdx"}, Lifetime::Sporadic);
217 outputs.emplace_back(ConcreteDataTypeMatcher{o2::calibration::Utils::gDataOriginCDBWrapper, "TPC_CalibdEdx"}, Lifetime::Sporadic);
218 std::vector<InputSpec> inputs{{"tracks", "TPC", "MIPS", Lifetime::Sporadic}};
219 inputs.emplace_back("tpctimegain", "TPC", "TIMEGAIN", 0, Lifetime::Condition, ccdbParamSpec(o2::tpc::CDBTypeMap.at(o2::tpc::CDBType::CalTimeGain), {}, 1)); // time-dependent
220
221 auto ccdbRequest = std::make_shared<o2::base::GRPGeomRequest>(true, // orbitResetTime
222 true, // GRPECS=true
223 false, // GRPLHCIF
224 true, // GRPMagField
225 enableAskMatLUT, // askMatLUT
227 inputs,
228 true,
229 true);
230 return DataProcessorSpec{
231 "tpc-calibrator-dEdx",
232 inputs,
233 outputs,
234 adaptFromTask<CalibratordEdxDevice>(ccdbRequest, matType),
235 Options{
236 {"tf-per-slot", VariantType::UInt32, 6000u, {"number of TFs per calibration time slot, is overwritten by seconds-per-slot if > 0"}},
237 {"seconds-per-slot", VariantType::Int, 180, {"seconds per calibration time slot, overwrites tf-per-slot if > 0"}},
238 {"max-delay", VariantType::UInt32, 10u, {"number of slots in past to consider"}},
239 {"min-entries", VariantType::Int, 10000, {"minimum entries per stack to fit a single time slot"}},
240 {"calib-interval-extension", VariantType::UInt32, 3600u, {"seconds by which to extend the calibration interval beyond the end of the time slot"}},
241
242 {"min-entries-sector", VariantType::Int, 1000, {"min entries per GEM stack to enable sector by sector correction. Below this value we only perform one fit per ROC type (IROC, OROC1, ...; no side nor sector information)."}},
243 {"min-entries-1d", VariantType::Int, 10000, {"minimum entries per stack to fit 1D correction"}},
244 {"min-entries-2d", VariantType::Int, 50000, {"minimum entries per stack to fit 2D correction"}},
245 {"fit-passes", VariantType::Int, 3, {"number of fit iterations"}},
246 {"fit-threshold", VariantType::Float, 0.2f, {"dEdx width around the MIP peak used in the fit"}},
247 {"fit-threshold-low-factor", VariantType::Float, 1.5f, {"factor for low dEdx width around the MIP peak used in the fit"}},
248
249 {"dedxbins", VariantType::Int, 70, {"number of dEdx bins"}},
250 {"min-dedx", VariantType::Float, 10.0f, {"minimum value for dEdx histograms"}},
251 {"max-dedx", VariantType::Float, 90.0f, {"maximum value for dEdx histograms"}},
252 {"angularbins", VariantType::Int, 36, {"number of angular bins: Tgl and Snp"}},
253 {"fit-snp", VariantType::Bool, false, {"enable Snp correction"}},
254
255 {"dump-histograms", VariantType::UInt32, 0u, {"dump calibration histograms bitmask: 0x1 = as THn; 0x2 as TTree"}},
256 {"file-dump", VariantType::Bool, false, {"directly dump calibration to file"}},
257 {"file-dump-name", VariantType::String, "calibratordEdx.root", {"name of the file dump output file"}},
258 {"track-debug", VariantType::Bool, false, {"track dEdx debugging"}},
259 {"disable-gaussian-fits", VariantType::Bool, false, {"disable calibration with gaussian fits and use mean instead"}},
260 }};
261}
262
263} // namespace o2::tpc
CDB Type definitions for TPC.
Workflow for time based dE/dx calibration.
This file provides the time dependent dE/dx calibrator, tracking the MIP position over time.
Utils and constants for calibration and related workflows.
int32_t i
Helper for geometry and GRP related CCDB requests.
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
void checkUpdates(o2::framework::ProcessingContext &pc)
static GRPGeomHelper & instance()
void setRequest(std::shared_ptr< GRPGeomRequest > req)
static std::unique_ptr< std::vector< char > > createObjectImage(const T *obj, CcdbObjectInfo *info=nullptr)
Definition CcdbApi.h:103
bool isValid(std::string const &s) const
Helper method to be used to check if a given part of the InputRecord is present.
decltype(auto) get(R binding, int part=0) const
InputRecord & inputs()
The inputs associated with this processing context.
const std::array< float, ParamSize > getMeanParams(ChargeType charge) const
Parameters averaged over all stacks.
void init(framework::InitContext &ic) final
void endOfStream(EndOfStreamContext &eos) final
void finaliseCCDB(o2::framework::ConcreteDataMatcher &matcher, void *obj) final
void run(ProcessingContext &pc) final
CalibratordEdxDevice(std::shared_ptr< o2::base::GRPGeomRequest > req, const o2::base::Propagator::MatCorrType matType)
GLeglImageOES image
Definition glcorearb.h:4021
constexpr TFType INFINITE_TF
Definition TimeSlot.h:30
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< ConfigParamSpec > ccdbParamSpec(std::string const &path, int runDependent, std::vector< CCDBMetadata > metadata={}, int qrate=0)
std::vector< ConfigParamSpec > Options
std::string elementsToString(Iterator begin, Iterator end, const std::string separator=", ")
Definition Utils.h:74
Global TPC definitions and constants.
Definition SimTraits.h:167
@ IROCgem
Definition Defs.h:53
@ Tot
Definition Defs.h:72
const std::unordered_map< CDBType, const std::string > CDBTypeMap
Storage name in CCDB for each calibration and parameter type.
Definition CDBTypes.h:94
o2::framework::DataProcessorSpec getCalibratordEdxSpec(const o2::base::Propagator::MatCorrType matType)
create a processor spec
@ CalTimeGain
Gain variation over time.
static void fillTFIDInfo(o2::framework::ProcessingContext &pc, o2::dataformats::TFIDInfo &ti)
static constexpr o2::header::DataOrigin gDataOriginCDBWrapper
Definition Utils.h:44
static constexpr o2::header::DataOrigin gDataOriginCDBPayload
Definition Utils.h:43
GEM stack identification.
Definition Defs.h:77