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 {
31 class BeamBkgMixerModule : public Module {
32
33 public:
34
39
43 virtual ~BeamBkgMixerModule();
44
49 virtual void initialize() override;
50
55 virtual void beginRun() override;
56
60 virtual void event() override;
61
66 virtual void endRun() override;
67
72 virtual void terminate() override;
73
74 private:
75
79 struct BkgHits {
80 TClonesArray* PXD;
81 TClonesArray* SVD;
82 TClonesArray* CDC;
83 TClonesArray* TOP;
84 TClonesArray* ARICH;
85 TClonesArray* ECL;
86 TClonesArray* KLM;
87 TClonesArray* BeamBackHits;
93 PXD(0), SVD(0), CDC(0), TOP(0), ARICH(0), ECL(0), KLM(0),
95 {}
96 };
97
101 struct BkgFiles {
103 std::string type;
104 double realTime;
105 double scaleFactor;
106 std::vector<std::string> fileNames;
108 std::unique_ptr<TChain> tree;
109 unsigned numFiles;
110 unsigned numEvents;
111 unsigned eventCount;
112 double rate;
113 unsigned index;
120 tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(0)
121 {}
133 const std::string& bkgType,
134 const std::string& fileName,
135 double time,
136 double scaleFac,
138 unsigned indx = 0):
139 tag(bkgTag), type(bkgType), realTime(time), scaleFactor(scaleFac),
140 fileType(fileTyp),
141 tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(indx)
142 {
143 fileNames.push_back(fileName);
144 }
145 };
146
147
156 template<class SIMHIT>
158 TClonesArray* cloneArray,
159 double timeShift,
160 double minTime,
161 double maxTime)
162 {
163 if (!cloneArray) return;
164 if (!simHits.isValid()) return;
165
166 int numEntries = cloneArray->GetEntriesFast();
167 for (int i = 0; i < numEntries; i++) {
168 SIMHIT* bkgSimHit = static_cast<SIMHIT*>(cloneArray->AddrAt(i));
169 SIMHIT* simHit = simHits.appendNew(*bkgSimHit);
170 simHit->shiftInTime(timeShift);
171 if (simHit->getBackgroundTag() == 0) // should be properly set at bkg simulation
172 simHit->setBackgroundTag(BackgroundMetaData::bg_other);
173 if (m_wrapAround) {
174 double time = simHit->getGlobalTime();
175 if (time > maxTime) {
176 double windowSize = maxTime - minTime;
177 double shift = int((time - minTime) / windowSize) * windowSize;
178 simHit->shiftInTime(-shift);
179 }
180 }
181 }
182
183 }
184
193 template<class HIT>
194 void addBeamBackHits(StoreArray<HIT>& hits, TClonesArray* cloneArray,
195 double timeShift, double minTime, double maxTime)
196 {
197 // Match SubDet id from BeamBackHits to whether we keep it or not
198 bool keep[] = {false, m_PXD, m_SVD, m_CDC, m_ARICH, m_TOP, m_ECL, m_KLM};
199 if (!cloneArray) return;
200 if (!hits.isValid()) return;
201 // this is basically a copy of addSimHits but we only add the
202 // BeamBackHits from the specified sub detectors so we have to check
203 // each if it is from one of the enabled subdetectors
204 int numEntries = cloneArray->GetEntriesFast();
205 for (int i = 0; i < numEntries; i++) {
206 HIT* bkgHit = static_cast<HIT*>(cloneArray->AddrAt(i));
207 //Only keep selected
208 if (!keep[bkgHit->getSubDet()]) continue;
209 HIT* hit = hits.appendNew(*bkgHit);
210 hit->shiftInTime(timeShift);
211 //TODO: BeamBackHits does not have a setBackgroundTag so we do not
212 //check or set it
213 if (m_wrapAround) {
214 double time = hit->getTime();
215 if (time > maxTime) {
216 double windowSize = maxTime - minTime;
217 double shift = int((time - minTime) / windowSize) * windowSize;
218 hit->shiftInTime(-shift);
219 }
220 }
221 }
222 }
223
231 bool isComponentIncluded(std::vector<std::string>& components,
232 const std::string& component);
233
243 const std::string& bkgType,
244 const std::string& fileName,
245 double realTime,
247
253 bool acceptEvent(TClonesArray* cloneArrayECL);
254
255
256 std::vector<std::string> m_backgroundFiles;
258 std::vector<std::tuple<std::string, double> > m_scaleFactors;
259 double m_minTime;
260 double m_maxTime;
261 std::vector<std::string> m_components;
270 std::vector<BkgFiles> m_backgrounds;
273 bool m_PXD = false;
274 bool m_SVD = false;
275 bool m_CDC = false;
276 bool m_TOP = false;
277 bool m_ARICH = false;
278 bool m_ECL = false;
279 bool m_KLM = false;
280 bool m_BeamBackHits = false;
284 std::map<std::string, int> m_rejected;
285 std::map<std::string, int> m_reused;
288 };
289
291} // Belle2 namespace
292
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.
New beam background mixer; this one doesn't need ROF files.
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
virtual ~BeamBkgMixerModule()
Destructor.
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
virtual void endRun() override
End-of-run action.
double m_maxTimePXD
maximal time shift of background event for PXD
int m_rejectedCount
counter for suppresing "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
virtual void beginRun() override
Called when entering a new run.
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
Base class for Modules.
Definition: Module.h:72
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 wether 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.
structure to hold samples of a particular background type
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)
usefull 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.