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
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
138 std::vector<std::tuple<int, int, int>> calcTimeframeIndices(long startOrbit, long orbitsPerTF, double orbitsEarly = 0.) const;
139
140 // Sample and fix interaction vertices (according to some distribution). Makes sure that same event ids
141 // have to have same vertex, as well as event ids associated to same collision.
143
144 // Function allowing to inject interaction vertixes from the outside.
145 // Useful when this is given from data for instance. The vertex vector needs to be of same
146 // size as the interaction record.
147 // Returns 0 if success. 1 if there is a problem.
148 int setInteractionVertices(std::vector<math_utils::Point3D<float>> const& vertices);
149
150 // helper functions to save and load a context
151 void saveToFile(std::string_view filename) const;
152
153 // Return the vector of interaction vertices associated with collisions
154 // The vector is empty if no vertices were provided or sampled. In this case, one
155 // may call "sampleInteractionVertices".
156 std::vector<math_utils::Point3D<float>> const& getInteractionVertices() const { return mInteractionVertices; }
157
158 static DigitizationContext* loadFromFile(std::string_view filename = "");
159
160 void setCTPDigits(std::vector<o2::ctp::CTPDigit> const* ctpdigits) const
161 {
162 mCTPTrigger = ctpdigits;
163 if (mCTPTrigger) {
164 mHasTrigger = true;
165 }
166 }
167
168 void setDigitizerInteractionRate(float intRate) { mDigitizerInteractionRate = intRate; }
169 float getDigitizerInteractionRate() const { return mDigitizerInteractionRate; }
170
171 std::vector<o2::ctp::CTPDigit> const* getCTPDigits() const { return mCTPTrigger; }
172 bool hasTriggerInput() const { return mHasTrigger; }
173
174 private:
175 int mNofEntries = 0;
176 int mMaxPartNumber = 0; // max number of parts in any given collision
177 uint32_t mFirstOrbitForSampling = 0; // 1st orbit to start sampling
178
179 float mMuBC; // probability of hadronic interaction per bunch
180
181 std::vector<o2::InteractionTimeRecord> mEventRecords;
182 // for each collision we record the constituents (which shall not exceed mMaxPartNumber)
183 std::vector<std::vector<o2::steer::EventPart>> mEventParts;
184
185 // for each collisionstd::vector<std::tuple<int,int,int>> &timeframeindice we may record/fix the interaction vertex (to be used in event generation)
186 std::vector<math_utils::Point3D<float>> mInteractionVertices;
187
188 // the collision records **with** QED interleaved;
189 std::vector<o2::InteractionTimeRecord> mEventRecordsWithQED;
190 std::vector<std::vector<o2::steer::EventPart>> mEventPartsWithQED;
191
192 o2::BunchFilling mBCFilling; // pattern of active BCs
193
194 std::vector<std::string> mSimPrefixes; // identifiers to the hit sim products; the key corresponds to the source ID of event record
195 std::string mQEDSimPrefix; // prefix for QED production/contribution
196 mutable o2::parameters::GRPObject* mGRP = nullptr;
197
198 mutable std::vector<o2::ctp::CTPDigit> const* mCTPTrigger = nullptr; // CTP trigger info associated to this digitization context
199 mutable bool mHasTrigger = false; //
200
201 // The global ALICE interaction hadronic interaction rate as applied in digitization.
202 // It should be consistent with mMuPerBC but it might be easier to handle.
203 // The value will be filled/inserted by the digitization workflow so that digiters can access it.
204 // There is no guarantee that the value is available elsewhere.
205 float mDigitizerInteractionRate{-1};
206
207 ClassDefNV(DigitizationContext, 6);
208};
209
211template <typename T>
212inline void DigitizationContext::retrieveHits(std::vector<TChain*> const& chains,
213 const char* brname,
214 int sourceID,
215 int entryID,
216 std::vector<T>* hits) const
217{
218 if (chains.size() <= sourceID) {
219 return;
220 }
221 auto br = chains[sourceID]->GetBranch(brname);
222 if (!br) {
223 LOG(error) << "No branch found with name " << brname;
224 return;
225 }
226 br->SetAddress(&hits);
227 auto maxEntries = br->GetEntries();
228 if (maxEntries) {
229 entryID %= maxEntries;
230 }
231 br->GetEntry(entryID);
232}
233
234} // namespace steer
235} // namespace o2
236
237#endif
definition of CTPDigit, CTPInputDigit
Header of the General Run Parameters object.
int getNBunches(int dir=-1) const
Static class with identifiers, bitmasks and names for ALICE detectors.
Definition DetID.h:58
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< 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)
std::vector< std::tuple< int, int, int > > calcTimeframeIndices(long startOrbit, long orbitsPerTF, double orbitsEarly=0.) const
get timeframe structure --> index markers where timeframe starts/ends/is_influenced_by
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
constexpr double LHCRevFreq
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"