Belle II Software development
BeamBkgMixerModule.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/core/Module.h>
12#include <framework/datastore/StoreArray.h>
13#include <simulation/background/BeamBGTypes.h>
14#include <framework/dataobjects/BackgroundMetaData.h>
15#include <string>
16#include <map>
17
18#include <TChain.h>
19#include <TClonesArray.h>
20
21
22namespace Belle2 {
27
31 class BeamBkgMixerModule : public Module {
32
33 public:
34
39
44 virtual void initialize() override;
45
49 virtual void event() override;
50
55 virtual void terminate() override;
56
57 private:
58
62 struct BkgHits {
63 TClonesArray* PXD;
64 TClonesArray* SVD;
65 TClonesArray* CDC;
66 TClonesArray* TOP;
67 TClonesArray* ARICH;
68 TClonesArray* ECL;
69 TClonesArray* KLM;
70 TClonesArray* BeamBackHits;
71
76 PXD(0), SVD(0), CDC(0), TOP(0), ARICH(0), ECL(0), KLM(0),
78 {}
79 };
80
84 struct BkgFiles {
86 std::string type;
87 double realTime;
88 double scaleFactor;
89 std::vector<std::string> fileNames;
91 std::unique_ptr<TChain> tree;
92 unsigned numFiles;
93 unsigned numEvents;
94 unsigned eventCount;
95 double rate;
96 unsigned index;
97
103 tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(0)
104 {}
105
116 const std::string& bkgType,
117 const std::string& fileName,
118 double time,
119 double scaleFac,
121 unsigned indx = 0):
122 tag(bkgTag), type(bkgType), realTime(time), scaleFactor(scaleFac),
123 fileType(fileTyp),
124 tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(indx)
125 {
126 fileNames.push_back(fileName);
127 }
128 };
129
130
139 template<class SIMHIT>
141 TClonesArray* cloneArray,
142 double timeShift,
143 double minTime,
144 double maxTime)
145 {
146 if (!cloneArray) return;
147 if (!simHits.isValid()) return;
148
149 int numEntries = cloneArray->GetEntriesFast();
150 for (int i = 0; i < numEntries; i++) {
151 SIMHIT* bkgSimHit = static_cast<SIMHIT*>(cloneArray->AddrAt(i));
152 SIMHIT* simHit = simHits.appendNew(*bkgSimHit);
153 simHit->shiftInTime(timeShift);
154 if (simHit->getBackgroundTag() == 0) // should be properly set at bkg simulation
155 simHit->setBackgroundTag(BackgroundMetaData::bg_other);
156 if (m_wrapAround) {
157 double time = simHit->getGlobalTime();
158 if (time > maxTime) {
159 double windowSize = maxTime - minTime;
160 double shift = int((time - minTime) / windowSize) * windowSize;
161 simHit->shiftInTime(-shift);
162 }
163 }
164 }
165
166 }
167
176 template<class HIT>
177 void addBeamBackHits(StoreArray<HIT>& hits, TClonesArray* cloneArray,
178 double timeShift, double minTime, double maxTime)
179 {
180 // Match SubDet id from BeamBackHits to whether we keep it or not
181 bool keep[] = {false, m_PXD, m_SVD, m_CDC, m_ARICH, m_TOP, m_ECL, m_KLM};
182 if (!cloneArray) return;
183 if (!hits.isValid()) return;
184 // this is basically a copy of addSimHits but we only add the
185 // BeamBackHits from the specified sub detectors so we have to check
186 // each if it is from one of the enabled subdetectors
187 int numEntries = cloneArray->GetEntriesFast();
188 for (int i = 0; i < numEntries; i++) {
189 HIT* bkgHit = static_cast<HIT*>(cloneArray->AddrAt(i));
190 //Only keep selected
191 if (!keep[bkgHit->getSubDet()]) continue;
192 HIT* hit = hits.appendNew(*bkgHit);
193 hit->shiftInTime(timeShift);
194 //TODO: BeamBackHits does not have a setBackgroundTag so we do not
195 //check or set it
196 if (m_wrapAround) {
197 double time = hit->getTime();
198 if (time > maxTime) {
199 double windowSize = maxTime - minTime;
200 double shift = int((time - minTime) / windowSize) * windowSize;
201 hit->shiftInTime(-shift);
202 }
203 }
204 }
205 }
206
214 bool isComponentIncluded(std::vector<std::string>& components,
215 const std::string& component);
216
226 const std::string& bkgType,
227 const std::string& fileName,
228 double realTime,
230
236 bool acceptEvent(TClonesArray* cloneArrayECL);
237
238
239 std::vector<std::string> m_backgroundFiles;
241 std::vector<std::tuple<std::string, double> > m_scaleFactors;
242 double m_minTime;
243 double m_maxTime;
244 std::vector<std::string> m_components;
252
253 std::vector<BkgFiles> m_backgrounds;
255
256 bool m_PXD = false;
257 bool m_SVD = false;
258 bool m_CDC = false;
259 bool m_TOP = false;
260 bool m_ARICH = false;
261 bool m_ECL = false;
262 bool m_KLM = false;
263 bool m_BeamBackHits = false;
264
266
267 std::map<std::string, int> m_rejected;
268 std::map<std::string, int> m_reused;
270
271 };
272
274} // Belle2 namespace
275
Metadata information about the beam background file.
EFileType
Enum for BG file types.
BG_TAG
Enum for background tags.
@ bg_other
Other type of background.
std::map< std::string, int > m_reused
messages: rejused events
double m_maxEdepECL
maximal allowed deposited energy in ECL
std::vector< BkgFiles > m_backgrounds
container for background samples
double m_maxTimeECL
maximal time shift of background event for ECL
std::map< std::string, int > m_rejected
messages: rejected events
bool isComponentIncluded(std::vector< std::string > &components, const std::string &component)
Returns true if a component is found in components or the list is empty.
bool m_ECL
true if found in m_components
std::vector< std::string > m_components
detector components
void addBeamBackHits(StoreArray< HIT > &hits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add BeamBackHits to those in the DataStore
double m_minTimePXD
minimal time shift of background event for PXD
bool m_PXD
true if found in m_components
void addSimHits(StoreArray< SIMHIT > &simHits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add background SimHits to those in the DataStore
background::BeamBGTypes m_bgTypes
defined BG types
std::vector< std::string > m_backgroundFiles
names of beam background files
virtual void initialize() override
Initialize the Module.
bool m_CDC
true if found in m_components
double m_maxTime
maximal time shift of background event
virtual void event() override
Event processor.
double m_minTime
minimal time shift of background event
bool m_wrapAround
if true wrap around events in the tail after maxTime
double m_maxTimePXD
maximal time shift of background event for PXD
int m_rejectedCount
counter for suppressing "rejected event" messages
virtual void terminate() override
Termination action.
BkgHits m_simHits
input event buffer
int m_cacheSize
file cache size in Mbytes
bool m_BeamBackHits
if true add also background hits
bool m_ARICH
true if found in m_components
double m_overallScaleFactor
overall scale factor
void appendSample(BackgroundMetaData::BG_TAG bkgTag, const std::string &bkgType, const std::string &fileName, double realTime, BackgroundMetaData::EFileType fileTyp)
appends background sample to m_backgrounds
bool acceptEvent(TClonesArray *cloneArrayECL)
Checks for deposited energy of ECLHits and returns true if Edep < m_maxEdepECL.
double m_minTimeECL
minimal time shift of background event for ECL
std::vector< std::tuple< std::string, double > > m_scaleFactors
scale factors
bool m_KLM
true if found in m_components
bool m_SVD
true if found in m_components
bool m_TOP
true if found in m_components
Module()
Constructor.
Definition Module.cc:30
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition StoreArray.h:246
bool isValid() const
Check whether the array was registered.
Definition StoreArray.h:288
Class to define BG types and to convert between BG types and tags or v.v.
Definition BeamBGTypes.h:27
Abstract base class for different kinds of events.
std::vector< std::string > fileNames
file names
unsigned index
index of this element in the std::vector
double rate
background rate of the sample
std::unique_ptr< TChain > tree
tree pointer
BkgFiles(BackgroundMetaData::BG_TAG bkgTag, const std::string &bkgType, const std::string &fileName, double time, double scaleFac, BackgroundMetaData::EFileType fileTyp=BackgroundMetaData::c_Usual, unsigned indx=0)
useful constructor
BackgroundMetaData::BG_TAG tag
background tag
double scaleFactor
scale factor for the rate
double realTime
real time of BG samlpe
unsigned numEvents
number of events (tree entries) in the sample
unsigned numFiles
number of files connected to TChain
BackgroundMetaData::EFileType fileType
file type
unsigned eventCount
current event (tree entry)
An input event buffer definition for background SimHits.
TClonesArray * BeamBackHits
BeamBackHits from collision file.
TClonesArray * KLM
KLM SimHits from collision file.
TClonesArray * ARICH
ARICH SimHits from collision file.
TClonesArray * CDC
CDC SimHits from collision file.
TClonesArray * PXD
PXD SimHits from collision file.
TClonesArray * ECL
ECL SimHits from collision file.
TClonesArray * SVD
SVD SimHits from collision file.
TClonesArray * TOP
TOP SimHits from collision file.