Project
Loading...
Searching...
No Matches
DigitizationContext.h
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#ifndef ALICEO2_SIMULATIONDATAFORMAT_RUNCONTEXT_H
13#define ALICEO2_SIMULATIONDATAFORMAT_RUNCONTEXT_H
14
15#include <vector>
16#include <TChain.h>
17#include <TBranch.h>
22#include <GPUCommonLogger.h>
23#include <unordered_map>
24#include <MathUtils/Cartesian.h>
27
28namespace o2
29{
30namespace steer
31{
32// a structure describing EventPart
33// (an elementary constituent of a collision)
34
35constexpr static int QEDSOURCEID = 99;
36
37struct EventPart {
38 EventPart() = default;
39 EventPart(int s, int e) : sourceID(s), entryID(e) {}
40 int sourceID = 0; // the ID of the source (0->backGround; > 1 signal source)
41 // the sourceID should correspond to the chain ID
42 int entryID = 0; // the event/entry ID inside the chain corresponding to sourceID
43
44 static bool isSignal(EventPart e) { return e.sourceID > 1 && e.sourceID != QEDSOURCEID; }
45 static bool isBackGround(EventPart e) { return !isSignal(e); }
46 static bool isQED(EventPart e) { return e.sourceID == QEDSOURCEID; }
48};
49
50// Class fully describing the Collision context or timeframe structure.
51// The context fixes things such as times (orbits and bunch crossings)
52// at which collision happen inside a timeframe and how they are composed
53// in terms of MC events.
55{
56 public:
57 DigitizationContext() : mNofEntries{0}, mMaxPartNumber{0}, mEventRecords(), mEventParts() {}
58
59 uint32_t getFirstOrbitForSampling() const { return mFirstOrbitForSampling; }
60 void setFirstOrbitForSampling(uint32_t o) { mFirstOrbitForSampling = o; }
61
62 int getNCollisions() const { return mNofEntries; }
63 void setNCollisions(int n) { mNofEntries = n; }
64
65 void setMaxNumberParts(int maxp) { mMaxPartNumber = maxp; }
66 int getMaxNumberParts() const { return mMaxPartNumber; }
67
68 std::vector<o2::InteractionTimeRecord>& getEventRecords(bool withQED = false) { return withQED ? mEventRecordsWithQED : mEventRecords; }
69 std::vector<std::vector<o2::steer::EventPart>>& getEventParts(bool withQED = false) { return withQED ? mEventPartsWithQED : mEventParts; }
70
71 const std::vector<o2::InteractionTimeRecord>& getEventRecords(bool withQED = false) const { return withQED ? mEventRecordsWithQED : mEventRecords; }
72 const std::vector<std::vector<o2::steer::EventPart>>& getEventParts(bool withQED = false) const { return withQED ? mEventPartsWithQED : mEventParts; }
73
74 // returns a collection of (first) collision indices that have this "source" included
75 std::unordered_map<int, int> getCollisionIndicesForSource(int source) const;
76
77 bool isQEDProvided() const { return !mEventRecordsWithQED.empty(); }
78
79 void setBunchFilling(o2::BunchFilling const& bf) { mBCFilling = bf; }
80 const o2::BunchFilling& getBunchFilling() const { return (const o2::BunchFilling&)mBCFilling; }
81
82 void setMuPerBC(float m) { mMuBC = m; }
83 float getMuPerBC() const { return mMuBC; }
84
86 float getCalculatedInteractionRate() const { return getMuPerBC() * getBunchFilling().getNBunches() * o2::constants::lhc::LHCRevFreq; }
87
88 void printCollisionSummary(bool withQED = false, int truncateOutputTo = -1) const;
89
90 // we need a method to fill the file names
91 void setSimPrefixes(std::vector<std::string> const& p);
92 std::vector<std::string> const& getSimPrefixes() const { return mSimPrefixes; }
93 // returns the source for a given simprefix ... otherwise -1 if not found
94 int findSimPrefix(std::string const& prefix) const;
95
97 void fillQED(std::string_view QEDprefix, int max_events, double qedrate);
98
101 void fillQED(std::string_view QEDprefix, std::vector<o2::InteractionTimeRecord> const& irecord, int max_events = -1, bool fromKinematics = true);
102
106 bool initSimChains(o2::detectors::DetID detid, std::vector<TChain*>& simchains) const;
107
111 bool initSimKinematicsChains(std::vector<TChain*>& simkinematicschains) const;
112
114 bool checkVertexCompatibility(bool verbose = false) const;
115
119 DigitizationContext extractSingleTimeframe(int timeframeid, std::vector<std::tuple<int, int, int>> const& timeframeindices, std::vector<int> const& sources_to_offset);
120
123 template <typename T>
124 void retrieveHits(std::vector<TChain*> const& chains,
125 const char* brname,
126 int sourceID,
127 int entryID,
128 std::vector<T>* hits) const;
129
131 o2::parameters::GRPObject const& getGRP() const;
132
133 // apply collision number cuts and potential relabeling of eventID, (keeps collisions which fall into the orbitsEarly range for the next timeframe)
134 // needs a timeframe index structure (determined by calcTimeframeIndices), which is adjusted during the process to reflect the filtering
135 void applyMaxCollisionFilter(std::vector<std::tuple<int, int, int>>& timeframeindices, long startOrbit, long orbitsPerTF, int maxColl, double orbitsEarly = 0.);
136
142 std::vector<std::tuple<int, int, int>> calcTimeframeIndices(long startOrbit, long orbitsPerTF, double orbitsEarly = 0., long nTimeframes = -1) const;
143
144 // Sample and fix interaction vertices (according to some distribution). Makes sure that same event ids
145 // have to have same vertex, as well as event ids associated to same collision.
147
148 // Function allowing to inject interaction vertixes from the outside.
149 // Useful when this is given from data for instance. The vertex vector needs to be of same
150 // size as the interaction record.
151 // Returns 0 if success. 1 if there is a problem.
152 int setInteractionVertices(std::vector<math_utils::Point3D<float>> const& vertices);
153
154 // helper functions to save and load a context
155 void saveToFile(std::string_view filename) const;
156
157 // Return the vector of interaction vertices associated with collisions
158 // The vector is empty if no vertices were provided or sampled. In this case, one
159 // may call "sampleInteractionVertices".
160 std::vector<math_utils::Point3D<float>> const& getInteractionVertices() const { return mInteractionVertices; }
161
162 static DigitizationContext* loadFromFile(std::string_view filename = "");
163
164 void setCTPDigits(std::vector<o2::ctp::CTPDigit> const* ctpdigits) const
165 {
166 mCTPTrigger = ctpdigits;
167 if (mCTPTrigger) {
168 mHasTrigger = true;
169 }
170 }
171
172 void setDigitizerInteractionRate(float intRate) { mDigitizerInteractionRate = intRate; }
173 float getDigitizerInteractionRate() const { return mDigitizerInteractionRate; }
174
175 std::vector<o2::ctp::CTPDigit> const* getCTPDigits() const { return mCTPTrigger; }
176 bool hasTriggerInput() const { return mHasTrigger; }
177
178 private:
179 int mNofEntries = 0;
180 int mMaxPartNumber = 0; // max number of parts in any given collision
181 uint32_t mFirstOrbitForSampling = 0; // 1st orbit to start sampling
182
183 float mMuBC; // probability of hadronic interaction per bunch
184
185 std::vector<o2::InteractionTimeRecord> mEventRecords;
186 // for each collision we record the constituents (which shall not exceed mMaxPartNumber)
187 std::vector<std::vector<o2::steer::EventPart>> mEventParts;
188
189 // for each collisionstd::vector<std::tuple<int,int,int>> &timeframeindice we may record/fix the interaction vertex (to be used in event generation)
190 std::vector<math_utils::Point3D<float>> mInteractionVertices;
191
192 // the collision records **with** QED interleaved;
193 std::vector<o2::InteractionTimeRecord> mEventRecordsWithQED;
194 std::vector<std::vector<o2::steer::EventPart>> mEventPartsWithQED;
195
196 o2::BunchFilling mBCFilling; // pattern of active BCs
197
198 std::vector<std::string> mSimPrefixes; // identifiers to the hit sim products; the key corresponds to the source ID of event record
199 std::string mQEDSimPrefix; // prefix for QED production/contribution
200 mutable o2::parameters::GRPObject* mGRP = nullptr;
201
202 mutable std::vector<o2::ctp::CTPDigit> const* mCTPTrigger = nullptr; // CTP trigger info associated to this digitization context
203 mutable bool mHasTrigger = false; //
204
205 // The global ALICE interaction hadronic interaction rate as applied in digitization.
206 // It should be consistent with mMuPerBC but it might be easier to handle.
207 // The value will be filled/inserted by the digitization workflow so that digiters can access it.
208 // There is no guarantee that the value is available elsewhere.
209 float mDigitizerInteractionRate{-1};
210
211 ClassDefNV(DigitizationContext, 6);
212};
213
215template <typename T>
216inline void DigitizationContext::retrieveHits(std::vector<TChain*> const& chains,
217 const char* brname,
218 int sourceID,
219 int entryID,
220 std::vector<T>* hits) const
221{
222 if (chains.size() <= sourceID) {
223 return;
224 }
225 auto br = chains[sourceID]->GetBranch(brname);
226 if (!br) {
227 LOG(error) << "No branch found with name " << brname;
228 return;
229 }
230 br->SetAddress(&hits);
231 auto maxEntries = br->GetEntries();
232 if (maxEntries) {
233 entryID %= maxEntries;
234 }
235 br->GetEntry(entryID);
236}
237
238} // namespace steer
239} // namespace o2
240
241#endif
definition of CTPDigit, CTPInputDigit
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 s
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 e
Header of the General Run Parameters object.
char error[CURL_ERROR_SIZE]
std::vector< TChain * > chains
int getNBunches(int dir=-1) const
Static class with identifiers, bitmasks and names for ALICE detectors.
Definition DetID.h:60
DigitizationContext extractSingleTimeframe(int timeframeid, std::vector< std::tuple< int, int, int > > const &timeframeindices, std::vector< int > const &sources_to_offset)
bool checkVertexCompatibility(bool verbose=false) const
Check collision parts for vertex consistency.
void fillQED(std::string_view QEDprefix, int max_events, double qedrate)
add QED contributions to context, giving prefix; maximal event number and qed interaction rate
bool initSimChains(o2::detectors::DetID detid, std::vector< TChain * > &simchains) const
std::unordered_map< int, int > getCollisionIndicesForSource(int source) const
void printCollisionSummary(bool withQED=false, int truncateOutputTo=-1) const
std::vector< math_utils::Point3D< float > > const & getInteractionVertices() const
const std::vector< o2::InteractionTimeRecord > & getEventRecords(bool withQED=false) const
int findSimPrefix(std::string const &prefix) const
float getCalculatedInteractionRate() const
returns the main (hadronic interaction rate) associated to this digitization context
void applyMaxCollisionFilter(std::vector< std::tuple< int, int, int > > &timeframeindices, long startOrbit, long orbitsPerTF, int maxColl, double orbitsEarly=0.)
void setCTPDigits(std::vector< o2::ctp::CTPDigit > const *ctpdigits) const
const std::vector< std::vector< o2::steer::EventPart > > & getEventParts(bool withQED=false) const
int setInteractionVertices(std::vector< math_utils::Point3D< float > > const &vertices)
void setSimPrefixes(std::vector< std::string > const &p)
std::vector< std::tuple< int, int, int > > calcTimeframeIndices(long startOrbit, long orbitsPerTF, double orbitsEarly=0., long nTimeframes=-1) const
std::vector< o2::InteractionTimeRecord > & getEventRecords(bool withQED=false)
bool initSimKinematicsChains(std::vector< TChain * > &simkinematicschains) const
o2::parameters::GRPObject const & getGRP() const
returns the GRP object associated to this context
void sampleInteractionVertices(o2::dataformats::MeanVertexObject const &v)
std::vector< std::vector< o2::steer::EventPart > > & getEventParts(bool withQED=false)
std::vector< o2::ctp::CTPDigit > const * getCTPDigits() const
std::vector< std::string > const & getSimPrefixes() const
const o2::BunchFilling & getBunchFilling() const
void saveToFile(std::string_view filename) const
void setBunchFilling(o2::BunchFilling const &bf)
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
void setDigitizerInteractionRate(float intRate)
static DigitizationContext * loadFromFile(std::string_view filename="")
GLdouble n
Definition glcorearb.h:1982
const GLfloat * m
Definition glcorearb.h:4066
const GLdouble * v
Definition glcorearb.h:832
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string filename()
ClassDefNV(EventPart, 1)
static bool isBackGround(EventPart e)
static bool isSignal(EventPart e)
static bool isQED(EventPart e)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"