Belle II Software  release-08-01-10
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 
22 namespace Belle2 {
34  class BeamBkgMixerModule : public Module {
35 
36  public:
37 
42 
46  virtual ~BeamBkgMixerModule();
47 
52  virtual void initialize() override;
53 
58  virtual void beginRun() override;
59 
63  virtual void event() override;
64 
69  virtual void endRun() override;
70 
75  virtual void terminate() override;
76 
77  private:
78 
82  struct BkgHits {
83  TClonesArray* PXD;
84  TClonesArray* SVD;
85  TClonesArray* CDC;
86  TClonesArray* TOP;
87  TClonesArray* ARICH;
88  TClonesArray* ECL;
89  TClonesArray* KLM;
90  TClonesArray* BeamBackHits;
96  PXD(0), SVD(0), CDC(0), TOP(0), ARICH(0), ECL(0), KLM(0),
97  BeamBackHits(0)
98  {}
99  };
100 
104  struct BkgFiles {
106  std::string type;
107  double realTime;
108  double scaleFactor;
109  std::vector<std::string> fileNames;
111  std::unique_ptr<TChain> tree;
112  unsigned numFiles;
113  unsigned numEvents;
114  unsigned eventCount;
115  double rate;
116  unsigned index;
122  fileType(BackgroundMetaData::c_Usual),
123  tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(0)
124  {}
136  const std::string& bkgType,
137  const std::string& fileName,
138  double time,
139  double scaleFac,
141  unsigned indx = 0):
142  tag(bkgTag), type(bkgType), realTime(time), scaleFactor(scaleFac),
143  fileType(fileTyp),
144  tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(indx)
145  {
146  fileNames.push_back(fileName);
147  }
148  };
149 
150 
159  template<class SIMHIT>
161  TClonesArray* cloneArray,
162  double timeShift,
163  double minTime,
164  double maxTime)
165  {
166  if (!cloneArray) return;
167  if (!simHits.isValid()) return;
168 
169  int numEntries = cloneArray->GetEntriesFast();
170  for (int i = 0; i < numEntries; i++) {
171  SIMHIT* bkgSimHit = static_cast<SIMHIT*>(cloneArray->AddrAt(i));
172  SIMHIT* simHit = simHits.appendNew(*bkgSimHit);
173  simHit->shiftInTime(timeShift);
174  if (simHit->getBackgroundTag() == 0) // should be properly set at bkg simulation
175  simHit->setBackgroundTag(BackgroundMetaData::bg_other);
176  if (m_wrapAround) {
177  double time = simHit->getGlobalTime();
178  if (time > maxTime) {
179  double windowSize = maxTime - minTime;
180  double shift = int((time - minTime) / windowSize) * windowSize;
181  simHit->shiftInTime(-shift);
182  }
183  }
184  }
185 
186  }
187 
196  template<class HIT>
197  void addBeamBackHits(StoreArray<HIT>& hits, TClonesArray* cloneArray,
198  double timeShift, double minTime, double maxTime)
199  {
200  // Match SubDet id from BeamBackHits to whether we keep it or not
201  bool keep[] = {false, m_PXD, m_SVD, m_CDC, m_ARICH, m_TOP, m_ECL, m_KLM};
202  if (!cloneArray) return;
203  if (!hits.isValid()) return;
204  // this is basically a copy of addSimHits but we only add the
205  // BeamBackHits from the specified sub detectors so we have to check
206  // each if it is from one of the enabled subdetectors
207  int numEntries = cloneArray->GetEntriesFast();
208  for (int i = 0; i < numEntries; i++) {
209  HIT* bkgHit = static_cast<HIT*>(cloneArray->AddrAt(i));
210  //Only keep selected
211  if (!keep[bkgHit->getSubDet()]) continue;
212  HIT* hit = hits.appendNew(*bkgHit);
213  hit->shiftInTime(timeShift);
214  //TODO: BeamBackHits does not have a setBackgroundTag so we do not
215  //check or set it
216  if (m_wrapAround) {
217  double time = hit->getTime();
218  if (time > maxTime) {
219  double windowSize = maxTime - minTime;
220  double shift = int((time - minTime) / windowSize) * windowSize;
221  hit->shiftInTime(-shift);
222  }
223  }
224  }
225  }
226 
234  bool isComponentIncluded(std::vector<std::string>& components,
235  const std::string& component);
236 
246  const std::string& bkgType,
247  const std::string& fileName,
248  double realTime,
250 
256  bool acceptEvent(TClonesArray* cloneArrayECL);
257 
258 
259  std::vector<std::string> m_backgroundFiles;
261  std::vector<std::tuple<std::string, double> > m_scaleFactors;
262  double m_minTime;
263  double m_maxTime;
264  std::vector<std::string> m_components;
266  double m_minTimeECL;
267  double m_maxTimeECL;
268  double m_minTimePXD;
269  double m_maxTimePXD;
270  double m_maxEdepECL;
273  std::vector<BkgFiles> m_backgrounds;
276  bool m_PXD = false;
277  bool m_SVD = false;
278  bool m_CDC = false;
279  bool m_TOP = false;
280  bool m_ARICH = false;
281  bool m_ECL = false;
282  bool m_KLM = false;
283  bool m_BeamBackHits = false;
287  std::map<std::string, int> m_rejected;
288  std::map<std::string, int> m_reused;
289  int m_rejectedCount = 0;
291  };
292 
294 } // Belle2 namespace
295 
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.