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 // helper functions to save and load a context
145 void saveToFile(std::string_view filename) const;
146
147 // Return the vector of interaction vertices associated with collisions
148 // The vector is empty if no vertices were provided or sampled. In this case, one
149 // may call "sampleInteractionVertices".
150 std::vector<math_utils::Point3D<float>> const& getInteractionVertices() const { return mInteractionVertices; }
151
152 static DigitizationContext* loadFromFile(std::string_view filename = "");
153
154 void setCTPDigits(std::vector<o2::ctp::CTPDigit> const* ctpdigits) const
155 {
156 mCTPTrigger = ctpdigits;
157 if (mCTPTrigger) {
158 mHasTrigger = true;
159 }
160 }
161
162 void setDigitizerInteractionRate(float intRate) { mDigitizerInteractionRate = intRate; }
163 float getDigitizerInteractionRate() const { return mDigitizerInteractionRate; }
164
165 std::vector<o2::ctp::CTPDigit> const* getCTPDigits() const { return mCTPTrigger; }
166 bool hasTriggerInput() const { return mHasTrigger; }
167
168 private:
169 int mNofEntries = 0;
170 int mMaxPartNumber = 0; // max number of parts in any given collision
171 uint32_t mFirstOrbitForSampling = 0; // 1st orbit to start sampling
172
173 float mMuBC; // probability of hadronic interaction per bunch
174
175 std::vector<o2::InteractionTimeRecord> mEventRecords;
176 // for each collision we record the constituents (which shall not exceed mMaxPartNumber)
177 std::vector<std::vector<o2::steer::EventPart>> mEventParts;
178
179 // for each collisionstd::vector<std::tuple<int,int,int>> &timeframeindice we may record/fix the interaction vertex (to be used in event generation)
180 std::vector<math_utils::Point3D<float>> mInteractionVertices;
181
182 // the collision records **with** QED interleaved;
183 std::vector<o2::InteractionTimeRecord> mEventRecordsWithQED;
184 std::vector<std::vector<o2::steer::EventPart>> mEventPartsWithQED;
185
186 o2::BunchFilling mBCFilling; // pattern of active BCs
187
188 std::vector<std::string> mSimPrefixes; // identifiers to the hit sim products; the key corresponds to the source ID of event record
189 std::string mQEDSimPrefix; // prefix for QED production/contribution
190 mutable o2::parameters::GRPObject* mGRP = nullptr;
191
192 mutable std::vector<o2::ctp::CTPDigit> const* mCTPTrigger = nullptr; // CTP trigger info associated to this digitization context
193 mutable bool mHasTrigger = false; //
194
195 // The global ALICE interaction hadronic interaction rate as applied in digitization.
196 // It should be consistent with mMuPerBC but it might be easier to handle.
197 // The value will be filled/inserted by the digitization workflow so that digiters can access it.
198 // There is no guarantee that the value is available elsewhere.
199 float mDigitizerInteractionRate{-1};
200
201 ClassDefNV(DigitizationContext, 6);
202};
203
205template <typename T>
206inline void DigitizationContext::retrieveHits(std::vector<TChain*> const& chains,
207 const char* brname,
208 int sourceID,
209 int entryID,
210 std::vector<T>* hits) const
211{
212 if (chains.size() <= sourceID) {
213 return;
214 }
215 auto br = chains[sourceID]->GetBranch(brname);
216 if (!br) {
217 LOG(error) << "No branch found with name " << brname;
218 return;
219 }
220 br->SetAddress(&hits);
221 br->GetEntry(entryID);
222}
223
224} // namespace steer
225} // namespace o2
226
227#endif
definition of CTPDigit, CTPInputDigit
bool o
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
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"