Project
Loading...
Searching...
No Matches
CellRecalibratorSpec.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
12#include <algorithm>
13#include <filesystem>
14#include <gsl/span>
23
24using namespace o2::emcal;
25
26CellRecalibratorSpec::CellRecalibratorSpec(uint32_t outputspec, LEDEventSettings ledsettings, bool badChannelCalib, bool timeCalib, bool gainCalib, bool isMC, std::shared_ptr<o2::emcal::CalibLoader> calibHandler) : mOutputSubspec(outputspec), mIsMC(isMC), mLEDsettings(ledsettings), mCalibrationHandler(calibHandler)
27{
28 setRunBadChannelCalibration(badChannelCalib);
29 setRunTimeCalibration(timeCalib);
30 setRunGainCalibration(gainCalib);
31}
32
34{
35 // Display calibrations which are enabled:
36 LOG(info) << "Bad channel calibration: " << (isRunBadChannlCalibration() ? "enabled" : "disabled");
37 LOG(info) << "Time calibration: " << (isRunTimeCalibration() ? "enabled" : "disabled");
38 LOG(info) << "Gain calibration: " << (isRunGainCalibration() ? "enabled" : "disabled");
39 std::string ledsettingstitle;
40 switch (mLEDsettings) {
42 ledsettingstitle = "keep";
43 break;
45 ledsettingstitle = "drop";
46 break;
48 ledsettingstitle = "redirect to EMC/CELLS/10";
49 break;
50 };
51 LOG(info) << "Handling of LED events: " << ledsettingstitle;
52 if (mIsMC) {
53 LOG(info) << "MC mode - removing labels for masked cells from MC label container";
54 }
55}
56
58{
59 auto inputcells = ctx.inputs().get<gsl::span<o2::emcal::Cell>>("cells");
60 auto intputtriggers = ctx.inputs().get<gsl::span<o2::emcal::TriggerRecord>>("triggerrecords");
61 LOG(info) << "Received " << inputcells.size() << " cells from " << intputtriggers.size() << " triggers";
62 std::unique_ptr<const o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>> inputMCLabels = nullptr;
63 std::optional<o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>> outputMCLabels;
64 if (mIsMC) {
65 inputMCLabels = ctx.inputs().get<o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>*>("cellmclabels");
66 outputMCLabels = std::optional<o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>>(o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>());
67 LOG(info) << "Received " << inputMCLabels->getIndexedSize() << " MC Label entries";
68 }
69
70 mCalibrationHandler->checkUpdates(ctx);
71 updateCalibObjects();
72
73 std::vector<o2::emcal::Cell> outputcells;
74 std::vector<o2::emcal::TriggerRecord> outputtriggers;
75
76 // Prepare containers for LED triggers (in case they should be redirected to a different output)
77 std::vector<o2::emcal::Cell> ledcells;
78 std::vector<o2::emcal::TriggerRecord> ledtriggers;
79
80 uint32_t currentfirst = outputcells.size(),
81 currentledfirst = ledcells.size();
82 for (const auto& trg : intputtriggers) {
83 if (trg.getTriggerBits() & o2::trigger::Cal) {
84 switch (mLEDsettings) {
86 // LED events stay always uncalibrated
87 writeTrigger(inputcells.subspan(trg.getFirstEntry(), trg.getNumberOfObjects()), trg, outputcells, outputtriggers);
88 continue;
90 // LED events will be dropped, simply discard them
91 continue;
93 // LED events stay always uncalibrated
94 writeTrigger(inputcells.subspan(trg.getFirstEntry(), trg.getNumberOfObjects()), trg, ledcells, ledtriggers);
95 continue;
96 }
97 };
98 }
99 if (!trg.getNumberOfObjects()) {
100 outputtriggers.emplace_back(trg.getBCData(), outputcells.size(), trg.getNumberOfObjects()).setTriggerBits(trg.getTriggerBits());
101 continue;
102 }
103 auto [calibratedCells, cellIndices] = mCellRecalibrator.getCalibratedCells(gsl::span<const o2::emcal::Cell>(inputcells.data() + trg.getFirstEntry(), trg.getNumberOfObjects()));
104 writeTrigger(calibratedCells, trg, outputcells, outputtriggers);
105 if (mIsMC) {
106 writeMCLabels(*inputMCLabels, outputMCLabels.value(), cellIndices, trg.getFirstEntry());
107 }
108 }
109
110 LOG(info) << "Timeframe: " << inputcells.size() << " cells read, " << outputcells.size() << " cells kept";
111 if (mLEDsettings == LEDEventSettings::REDIRECT) {
112 LOG(info) << "Redirecting " << ledcells.size() << " LED cells from " << ledtriggers.size() << " LED triggers";
113 }
114
115 // send recalibrated objects
116 ctx.outputs().snapshot(o2::framework::Output{o2::header::gDataOriginEMC, "CELLS", mOutputSubspec}, outputcells);
117 ctx.outputs().snapshot(o2::framework::Output{o2::header::gDataOriginEMC, "CELLSTRGR", mOutputSubspec}, outputtriggers);
118 if (outputMCLabels.has_value()) {
119 LOG(info) << "Timeframe: " << inputMCLabels->getIndexedSize() << " label entries read, " << outputMCLabels->getIndexedSize() << " label entries kept";
120 ctx.outputs().snapshot(o2::framework::Output{o2::header::gDataOriginEMC, "CELLSMCTR", mOutputSubspec}, outputMCLabels.value());
121 }
122 if (mLEDsettings == LEDEventSettings::REDIRECT) {
124 ctx.outputs().snapshot(o2::framework::Output{o2::header::gDataOriginEMC, "CELLSTRGR", 10}, ledtriggers);
125 }
126}
127
128void CellRecalibratorSpec::writeTrigger(const gsl::span<const o2::emcal::Cell> selectedCells, const o2::emcal::TriggerRecord& currenttrigger, std::vector<o2::emcal::Cell>& outputcontainer, std::vector<o2::emcal::TriggerRecord>& outputtriggers)
129{
130 std::size_t currentfirst = outputcontainer.size();
131 if (selectedCells.size()) {
132 std::copy(selectedCells.begin(), selectedCells.end(), std::back_inserter(outputcontainer));
133 }
134 outputtriggers.emplace_back(currenttrigger.getBCData(), currentfirst, selectedCells.size()).setTriggerBits(currenttrigger.getTriggerBits());
135}
136
137void CellRecalibratorSpec::writeMCLabels(const o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>& inputlabels, o2::dataformats::MCTruthContainer<o2::emcal::MCLabel>& outputContainer, const std::vector<int>& keptIndices, int firstindex)
138{
139 for (auto keptindex : keptIndices) {
140 int globalInputIndex = firstindex + keptindex;
141 auto labelsToKeep = inputlabels.getLabels(globalInputIndex);
142 outputContainer.addElements(outputContainer.getIndexedSize(), labelsToKeep);
143 }
144}
145
147{
148 LOG(info) << "Handling new Calibration objects";
149 if (mCalibrationHandler->finalizeCCDB(matcher, obj)) {
150 return;
151 }
152}
153
154void CellRecalibratorSpec::updateCalibObjects()
155{
157 if (mCalibrationHandler->hasUpdateBadChannelMap()) {
158 LOG(info) << "updateCalibObjects: Bad channel map changed";
159 mCellRecalibrator.setBadChannelMap(mCalibrationHandler->getBadChannelMap());
160 }
161 }
162 if (isRunTimeCalibration()) {
163 if (mCalibrationHandler->hasUpdateTimeCalib()) {
164 LOG(info) << "updateCalibObjects: Time calib params changed";
165 mCellRecalibrator.setTimeCalibration(mCalibrationHandler->getTimeCalibration());
166 }
167 }
168 if (isRunGainCalibration()) {
169 if (mCalibrationHandler->hasUpdateGainCalib()) {
170 LOG(info) << "updateCalibObjects: Time calib params changed";
171 mCellRecalibrator.setGainCalibration(mCalibrationHandler->getGainCalibration());
172 }
173 }
174}
175
176o2::framework::DataProcessorSpec o2::emcal::getCellRecalibratorSpec(uint32_t inputSubspec, uint32_t outputSubspec, uint32_t ledsettings, bool badChannelCalib, bool timeCalib, bool gainCalib, bool isMC)
177{
178 auto calibhandler = std::make_shared<o2::emcal::CalibLoader>();
179 calibhandler->enableBadChannelMap(badChannelCalib);
180 calibhandler->enableTimeCalib(timeCalib);
181 calibhandler->enableGainCalib(gainCalib);
182 std::vector<o2::framework::InputSpec>
183 inputs = {{"cells", o2::header::gDataOriginEMC, "CELLS", inputSubspec, o2::framework::Lifetime::Timeframe},
184 {"triggerrecords", o2::header::gDataOriginEMC, "CELLSTRGR", inputSubspec, o2::framework::Lifetime::Timeframe}};
186 switch (ledsettings) {
187 case 0:
189 break;
190 case 1:
192 break;
193 case 2:
195 break;
196 default:
197 LOG(fatal) << "Undefined handling of LED events";
198 }
199 std::vector<o2::framework::OutputSpec> outputs = {{o2::header::gDataOriginEMC, "CELLS", outputSubspec, o2::framework::Lifetime::Timeframe},
200 {o2::header::gDataOriginEMC, "CELLSTRGR", outputSubspec, o2::framework::Lifetime::Timeframe}};
202 outputs.push_back({o2::header::gDataOriginEMC, "CELLS", 10, o2::framework::Lifetime::Timeframe});
203 outputs.push_back({o2::header::gDataOriginEMC, "CELLSTRGR", 10, o2::framework::Lifetime::Timeframe});
204 }
205 if (isMC) {
206 inputs.push_back({"cellmclabels", o2::header::gDataOriginEMC, "CELLSMCTR", inputSubspec, o2::framework::Lifetime::Timeframe});
207 outputs.push_back({o2::header::gDataOriginEMC, "CELLSMCTR", outputSubspec, o2::framework::Lifetime::Timeframe});
208 }
209 calibhandler->defineInputSpecs(inputs);
210
211 return o2::framework::DataProcessorSpec{"EMCALCellRecalibrator",
212 inputs,
213 outputs,
214 o2::framework::adaptFromTask<o2::emcal::CellRecalibratorSpec>(outputSubspec, taskledsettings, badChannelCalib, timeCalib, gainCalib, isMC, calibhandler)};
215}
Definition of the 32 Central Trigger System (CTS) Trigger Types defined in https://twiki....
A container to hold and manage MC truth information/labels.
gsl::span< TruthElement > getLabels(uint32_t dataindex)
void addElements(uint32_t dataindex, gsl::span< CompatibleLabel > elements)
LEDEventSettings
Dedicated handling for LED events.
@ REDIRECT
Redirect LED events to dedicated output.
@ KEEP
Keep LED events in timeframe (uncalibrated)
void setRunGainCalibration(bool doRun)
Switch for the gain calibration.
void setRunTimeCalibration(bool doRun)
Switch for time calibration.
void finaliseCCDB(framework::ConcreteDataMatcher &matcher, void *obj) final
Fetching cell objects and assigning them to the internal handlers.
void setRunBadChannelCalibration(bool doRun)
Switch for bad channel calibration.
CellRecalibratorSpec(uint32_t outputspec, LEDEventSettings ledsettings, bool badChannelCalib, bool timeCalib, bool gainCalib, bool isMC, std::shared_ptr< CalibLoader >(calibHandler))
Constructor.
bool isRunBadChannlCalibration() const
Check if the bad channel calibration is enabled.
void run(framework::ProcessingContext &ctx) final
Run recalibration of cells for a new timeframe.
bool isRunTimeCalibration() const
Check if the time calibration is enabled.
bool isRunGainCalibration() const
Check if the gain calibration is enabled.
void init(framework::InitContext &ctx) final
Initialize recalibrator.
std::tuple< std::vector< T >, std::vector< int > > getCalibratedCells(const gsl::span< const T > inputcells)
Get list of calibrated cells based on a cell input collection.
void setGainCalibration(const GainCalibrationFactors *gcf)
Set the gain calibration params.
void setTimeCalibration(const TimeCalibrationParams *tcp)
Set the time calibration params.
void setBadChannelMap(const BadChannelMap *bcm)
Set the bad channel map.
Header for data corresponding to the same hardware trigger.
uint32_t getTriggerBits() const
const BCData & getBCData() const
void snapshot(const Output &spec, T const &object)
decltype(auto) get(R binding, int part=0) const
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
constexpr o2::header::DataOrigin gDataOriginEMC
Definition DataHeader.h:565
framework::DataProcessorSpec getCellRecalibratorSpec(uint32_t inputSubspec, uint32_t outputSubspec, uint32_t ledsettings, bool badChannelCalib, bool timeCalib, bool gainCalib, bool isMC)
Create CellRecalibrator processor spec.
constexpr uint32_t Cal
Definition Triggers.h:32
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"