Project
Loading...
Searching...
No Matches
TPCSectorDigitization.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
13#include <sstream>
14#include <string>
15#include <TChain.h>
17#include "Framework/Logger.h"
19#include "TPCBase/Sector.h"
20#include "TPCSimulation/Point.h"
32#include <filesystem>
33
34namespace o2::tpc
35{
36
37namespace
38{
39std::string getBranchNameLeft(int sector)
40{
41 std::stringstream branchnamestreamleft;
42 branchnamestreamleft << "TPCHitsShiftedSector" << int(o2::tpc::Sector::getLeft(o2::tpc::Sector(sector)));
43 return branchnamestreamleft.str();
44}
45
46std::string getBranchNameRight(int sector)
47{
48 std::stringstream branchnamestreamright;
49 branchnamestreamright << "TPCHitsShiftedSector" << sector;
50 return branchnamestreamright.str();
51}
52} // namespace
53
54std::vector<dataformats::RangeReference<int, int>> digitizeSector(Digitizer& digitizer, const steer::DigitizationContext& context,
55 const std::vector<TChain*>& simChains, int sector, ULong64_t timeframe,
56 SectorFlushBuffers& buffers, const std::function<void()>& onFlush)
57{
58 std::vector<dataformats::RangeReference<int, int>> events;
59 const bool isContinuous = digitizer.isContinuousReadout();
60 const auto& irecords = context.getEventRecords();
61 const auto& eventParts = context.getEventParts();
62
66
67 size_t digitCounter = 0;
68 auto flush = [&](bool finalFlush = false) {
69 buffers.digits.clear();
70 buffers.labels.clear();
71 buffers.commonMode.clear();
72 digitizer.flush(buffers.digits, buffers.labels, buffers.commonMode, finalFlush);
73 LOG(info) << "TPC: Flushed " << buffers.digits.size() << " digits, " << buffers.labels.getNElements() << " labels and " << buffers.commonMode.size() << " common mode entries";
74 onFlush();
75 digitCounter += buffers.digits.size();
76 };
77
78 if (isContinuous) {
79 auto& hbfu = o2::raw::HBFUtils::Instance();
80 double time = hbfu.getFirstIRofTF(o2::InteractionRecord(0, hbfu.orbitFirstSampled)).bc2ns() / 1000.;
82 if (!irecords.empty()) {
83 digitizer.setStartTime(irecords[0].getTimeNS() / 1000.f);
84 }
85 }
86
87 // loop over all composite collisions given from context
88 // (aka loop over all the interaction records)
89 for (int collID = 0; collID < irecords.size(); ++collID) {
90 const double eventTime = irecords[collID].getTimeNS() / 1000.f;
91 LOG(info) << "TPC: Event time " << eventTime << " us";
92 digitizer.setEventTime(eventTime);
93 if (!isContinuous) {
94 digitizer.setStartTime(eventTime);
95 }
96 size_t startSize = digitCounter;
97
98 // for each collision, loop over the constituents event and source IDs
99 // (background signal merging is basically taking place here)
100 for (auto& part : eventParts[collID]) {
101 const int eventID = part.entryID;
102 const int sourceID = part.sourceID;
103
104 // get the hits for this event and this source
105 std::vector<o2::tpc::HitGroup> hitsLeft;
106 std::vector<o2::tpc::HitGroup> hitsRight;
107 context.retrieveHits(simChains, getBranchNameLeft(sector).c_str(), part.sourceID, part.entryID, &hitsLeft);
108 context.retrieveHits(simChains, getBranchNameRight(sector).c_str(), part.sourceID, part.entryID, &hitsRight);
109 LOG(debug) << "TPC: Found " << hitsLeft.size() << " hit groups left and " << hitsRight.size() << " hit groups right in collision " << collID << " eventID " << part.entryID;
110
111 digitizer.process(hitsLeft, eventID, sourceID);
112 digitizer.process(hitsRight, eventID, sourceID);
113
114 flush();
115
116 if (!isContinuous) {
117 events.emplace_back(startSize, buffers.digits.size());
118 }
119 }
120 }
121
122 // final flushing step; getting everything not yet written out
123 if (isContinuous) {
124 LOG(info) << "TPC: Final flush";
125 flush(true);
126 events.emplace_back(0, digitCounter); // all digits are grouped to 1 super-event pseudo-triggered mode
127 }
128 return events;
129}
130
131std::vector<framework::ConfigParamSpec> TPCDigitizerConditions::options()
132{
134 return {
135 {"TPCtriggered", VariantType::Bool, false, {"Impose triggered RO mode (default: continuous)"}},
136 {"TPCuseCCDB", VariantType::Bool, false, {"true: load calibrations from CCDB; false: use random calibratoins"}},
137 {"meanLumiDistortions", VariantType::Float, -1.f, {"override lumi of distortion object if >=0"}},
138 {"meanLumiDistortionsDerivative", VariantType::Float, -1.f, {"override lumi of derivative distortion object if >=0"}},
139 {"do-not-recalculate-distortions", VariantType::Bool, false, {"Do not recalculate the distortions"}},
140 {"n-threads-distortions", VariantType::Int, 4, {"Number of threads used for the calculation of the distortions"}},
141 };
142}
143
144void TPCDigitizerConditions::addInputs(std::vector<framework::InputSpec>& inputs, int distortionType)
145{
148 inputs.emplace_back("gasparam", o2::header::gDataOriginTPC, "TPCGASPARAM", 0, Lifetime::Condition, framework::ccdbParamSpec(CDBTypeMap.at(CDBType::ParGas)));
149 inputs.emplace_back("gemparam", o2::header::gDataOriginTPC, "TPCGEMPARAM", 0, Lifetime::Condition, framework::ccdbParamSpec(CDBTypeMap.at(CDBType::ParGEM)));
150 if (distortionType) {
151 inputs.emplace_back("tpcdistortions", o2::header::gDataOriginTPC, "TPCDIST", 0, Lifetime::Condition, framework::ccdbParamSpec(CDBTypeMap.at(CDBType::DistortionMapMC), {}, 1)); // time-dependent
152 // load derivative map in case scaling was requested
153 if (distortionType == 2) {
154 inputs.emplace_back("tpcdistortionsderiv", o2::header::gDataOriginTPC, "TPCDISTDERIV", 0, Lifetime::Condition, framework::ccdbParamSpec(CDBTypeMap.at(CDBType::DistortionMapDerivMC), {}, 1)); // time-dependent
155 }
156 }
157}
158
160{
161 mDistortionType = distortionType;
162 mRecalcDistortions = !(ic.options().get<bool>("do-not-recalculate-distortions"));
163 SpaceCharge<float>::setNThreads(ic.options().get<int>("n-threads-distortions"));
164 mUseCalibrationsFromCCDB = ic.options().get<bool>("TPCuseCCDB");
165 mMeanLumiDistortions = ic.options().get<float>("meanLumiDistortions");
166 mMeanLumiDistortionsDerivative = ic.options().get<float>("meanLumiDistortionsDerivative");
167 LOG(info) << "TPC calibrations from CCDB: " << mUseCalibrationsFromCCDB;
168}
169
171{
173 using SC = SpaceCharge<float>;
174 if (mVDriftHelper.accountCCDBInputs(matcher, obj)) {
175 return true;
176 }
177 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "TPCDIST", 0)) {
178 LOGP(info, "Updating distortion map");
179 digitizer.setUseSCDistortions(static_cast<SC*>(obj));
180 if (mMeanLumiDistortions >= 0) {
181 digitizer.setMeanLumiDistortions(mMeanLumiDistortions);
182 }
183 return true;
184 }
185 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "TPCDISTDERIV", 0)) {
186 LOGP(info, "Updating reference distortion map");
187 digitizer.setSCDistortionsDerivative(static_cast<SC*>(obj));
188 if (mMeanLumiDistortionsDerivative >= 0) {
189 digitizer.setMeanLumiDistortionsDerivative(mMeanLumiDistortionsDerivative);
190 }
191 return true;
192 }
193 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "TPCGASPARAM", 0)) {
194 LOGP(info, "TPC gas param updated");
195 ParameterGas::Instance().printKeyValues(true, true);
196 return true;
197 }
198 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "TPCGEMPARAM", 0)) {
199 LOGP(info, "TPC GEM param updated");
200 ParameterGEM::Instance().printKeyValues(true, true);
201 return true;
202 }
203 return false;
204}
205
206void TPCDigitizerConditions::update(framework::ProcessingContext& pc, Digitizer& digitizer, const std::function<void(float, float)>& setVDrift)
207{
208 using SC = SpaceCharge<float>;
209 auto& cdb = CDBInterface::instance();
210 cdb.setUseDefaults(!mUseCalibrationsFromCCDB);
211 // whatever are global settings for CCDB usage, we have to extract the TPC vdrift from CCDB for anchored simulations
212 mVDriftHelper.extractCCDBInputs(pc);
213 pc.inputs().get<ParameterGas*>("gasparam");
214 pc.inputs().get<ParameterGEM*>("gemparam");
215 if (mDistortionType) {
216 pc.inputs().get<SC*>("tpcdistortions");
217 if (mDistortionType == 2) {
218 pc.inputs().get<SC*>("tpcdistortionsderiv");
219 digitizer.setLumiScaleFactor();
220 if (mRecalcDistortions) {
221 digitizer.recalculateDistortions();
222 }
223 }
224 }
225
226 if (mVDriftHelper.isUpdated()) {
227 const auto& vd = mVDriftHelper.getVDriftObject();
228 LOGP(info, "Updating TPC fast transform map with new VDrift factor of {} wrt reference {} and DriftTimeOffset correction {} wrt {} from source {}",
229 vd.corrFact, vd.refVDrift, vd.timeOffsetCorr, vd.refTimeOffset, mVDriftHelper.getSourceName());
230 setVDrift(vd.getVDrift(), vd.getTimeOffset());
231 mVDriftHelper.acknowledgeUpdate();
232 }
233
234 if (std::filesystem::exists("ThresholdMap.root")) {
235 LOG(info) << "TPC: Using zero suppression map from 'ThresholdMap.root'";
236 cdb.setThresholdMapFromFile("ThresholdMap.root");
237 }
238
239 if (std::filesystem::exists("GainMap.root")) {
240 LOG(info) << "TPC: Using gain map from 'GainMap.root'";
241 cdb.setGainMapFromFile("GainMap.root");
242 }
243}
244
245} // namespace o2::tpc
Simple interface to the CDB manager.
CDB Type definitions for TPC.
std::ostringstream debug
int16_t time
Definition RawEventData.h:4
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual sector
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char events
Timeframe settings Average distance in cm of events merged into timeframe
Definition of the parameter class for the GEM stack.
Definition of the parameter class for the detector gas.
Class for TPC Point.
This class contains the algorithms for calculation the distortions and corrections.
Digitizer digitizer
Definition of the ALICE TPC digitizer.
ConfigParamRegistry const & options()
Definition InitContext.h:33
decltype(auto) get(R binding, int part=0) const
InputRecord & inputs()
The inputs associated with this processing context.
std::vector< o2::InteractionTimeRecord > & getEventRecords(bool withQED=false)
std::vector< std::vector< o2::steer::EventPart > > & getEventParts(bool withQED=false)
void retrieveHits(std::vector< TChain * > const &chains, const char *brname, int sourceID, int entryID, std::vector< T > *hits) const
function reading the hits from a chain (previously initialized with initSimChains
static CDBInterface & instance()
void setSeedingTimeframe(ULong64_t timeframe)
Set the timeframe identifier that enters the sector seeding.
Definition Digitizer.h:156
void flush(std::vector< o2::tpc::Digit > &digits, o2::dataformats::MCTruthContainer< o2::MCCompLabel > &labels, std::vector< o2::tpc::CommonMode > &commonModeOutput, bool finalFlush=false)
void setSector(Sector sec)
Definition Digitizer.h:90
void setOutputDigitTimeOffset(double offset)
Set mOutputDigitTimeOffset.
Definition Digitizer.h:101
void setStartTime(double time)
void process(const std::vector< o2::tpc::HitGroup > &hits, const int eventID, const int sourceID=0)
void setEventTime(double time)
Definition Digitizer.h:105
void init()
Initializer.
bool isContinuousReadout()
Definition Digitizer.h:113
static Sector getLeft(const Sector s)
Definition Sector.h:145
static void setNThreads(const int nThreads)
set the number of threads used for some of the calculations
static void addInputs(std::vector< framework::InputSpec > &inputs, int distortionType)
CCDB inputs common to the TPC digitizer devices.
bool finaliseCCDB(framework::ConcreteDataMatcher &matcher, void *obj, Digitizer &digitizer)
pass a CCDB object to the digitizer; true if it was a TPC condition
static std::vector< framework::ConfigParamSpec > options()
options common to the TPC digitizer devices
void init(framework::InitContext &ic, int distortionType)
void update(framework::ProcessingContext &pc, Digitizer &digitizer, const std::function< void(float, float)> &setVDrift)
static void requestCCDBInputs(std::vector< o2::framework::InputSpec > &inputs, bool laser=true, bool itstpcTgl=true)
void extractCCDBInputs(o2::framework::ProcessingContext &pc, bool laser=true, bool itstpcTgl=true)
const VDriftCorrFact & getVDriftObject() const
bool accountCCDBInputs(const o2::framework::ConcreteDataMatcher &matcher, void *obj)
static std::string_view getSourceName(Source s)
bool isUpdated() const
constexpr o2::header::DataOrigin gDataOriginTPC
Definition DataHeader.h:579
uint8_t int statusCode int
Lifetime
Possible Lifetime of objects being exchanged by the DPL.
Definition Lifetime.h:18
std::vector< ConfigParamSpec > ccdbParamSpec(std::string const &path, int runDependent, std::vector< CCDBMetadata > metadata={}, int qrate=0)
Global TPC definitions and constants.
std::vector< dataformats::RangeReference< int, int > > digitizeSector(Digitizer &digitizer, const steer::DigitizationContext &context, const std::vector< TChain * > &simChains, int sector, ULong64_t timeframe, SectorFlushBuffers &buffers, const std::function< void()> &onFlush)
const std::unordered_map< CDBType, const std::string > CDBTypeMap
Storage name in CCDB for each calibration and parameter type.
Definition CDBTypes.h:98
@ ParGas
Parameter for Gas.
@ DistortionMapDerivMC
derivative distortions for MC used in the digitizer for scaling
@ DistortionMapMC
full distortions (static + IR dependant) for MC used in the digitizer
Output buffers of one flush of a sector.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"