Belle II Software  release-05-01-25
BeamBkgMixerModule.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/Module.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <simulation/background/BeamBGTypes.h>
16 #include <framework/dataobjects/BackgroundMetaData.h>
17 #include <string>
18 #include <map>
19 
20 #include "TChain.h"
21 #include "TClonesArray.h"
22 
23 
24 namespace Belle2 {
36  class BeamBkgMixerModule : public Module {
37 
38  public:
39 
44 
48  virtual ~BeamBkgMixerModule();
49 
54  virtual void initialize() override;
55 
60  virtual void beginRun() override;
61 
65  virtual void event() override;
66 
71  virtual void endRun() override;
72 
77  virtual void terminate() override;
78 
79  private:
80 
84  struct BkgHits {
85  TClonesArray* PXD;
86  TClonesArray* SVD;
87  TClonesArray* CDC;
88  TClonesArray* TOP;
89  TClonesArray* ARICH;
90  TClonesArray* ECL;
91  TClonesArray* BKLM;
92  TClonesArray* EKLM;
93  TClonesArray* BeamBackHits;
99  PXD(0), SVD(0), CDC(0), TOP(0), ARICH(0), ECL(0), BKLM(0), EKLM(0),
101  {}
102  };
103 
107  struct BkgFiles {
109  std::string type;
110  double realTime;
111  double scaleFactor;
112  std::vector<std::string> fileNames;
114  std::unique_ptr<TChain> tree;
115  unsigned numFiles;
116  unsigned numEvents;
117  unsigned eventCount;
118  double rate;
119  unsigned index;
126  tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(0)
127  {}
139  const std::string& bkgType,
140  const std::string& fileName,
141  double time,
142  double scaleFac,
144  unsigned indx = 0):
145  tag(bkgTag), type(bkgType), realTime(time), scaleFactor(scaleFac),
146  fileType(fileTyp),
147  tree(nullptr), numFiles(0), numEvents(0), eventCount(0), rate(0.0), index(indx)
148  {
149  fileNames.push_back(fileName);
150  }
151  };
152 
153 
162  template<class SIMHIT>
163  void addSimHits(StoreArray<SIMHIT>& simHits,
164  TClonesArray* cloneArray,
165  double timeShift,
166  double minTime,
167  double maxTime)
168  {
169  if (!cloneArray) return;
170  if (!simHits.isValid()) return;
171 
172  int numEntries = cloneArray->GetEntriesFast();
173  for (int i = 0; i < numEntries; i++) {
174  SIMHIT* bkgSimHit = static_cast<SIMHIT*>(cloneArray->AddrAt(i));
175  SIMHIT* simHit = simHits.appendNew(*bkgSimHit);
176  simHit->shiftInTime(timeShift);
177  if (simHit->getBackgroundTag() == 0) // should be properly set at bkg simulation
178  simHit->setBackgroundTag(BackgroundMetaData::bg_other);
179  if (m_wrapAround) {
180  double time = simHit->getGlobalTime();
181  if (time > maxTime) {
182  double windowSize = maxTime - minTime;
183  double shift = int((time - minTime) / windowSize) * windowSize;
184  simHit->shiftInTime(-shift);
185  }
186  }
187  }
188 
189  }
190 
199  template<class HIT>
200  void addBeamBackHits(StoreArray<HIT>& hits, TClonesArray* cloneArray,
201  double timeShift, double minTime, double maxTime)
202  {
203  // Match SubDet id from BeamBackHits to whether we keep it or not
204  // KLM is a single component, but it has different hits for BKLM and EKLM.
205  bool keep[] = {false, m_PXD, m_SVD, m_CDC, m_ARICH, m_TOP, m_ECL, m_KLM, m_KLM};
206  if (!cloneArray) return;
207  if (!hits.isValid()) return;
208  // this is basically a copy of addSimHits but we only add the
209  // BeamBackHits from the specified sub detectors so we have to check
210  // each if it is from one of the enabled subdetectors
211  int numEntries = cloneArray->GetEntriesFast();
212  for (int i = 0; i < numEntries; i++) {
213  HIT* bkgHit = static_cast<HIT*>(cloneArray->AddrAt(i));
214  //Only keep selected
215  if (!keep[bkgHit->getSubDet()]) continue;
216  HIT* hit = hits.appendNew(*bkgHit);
217  hit->shiftInTime(timeShift);
218  //TODO: BeamBackHits does not have a setBackgroundTag so we do not
219  //check or set it
220  if (m_wrapAround) {
221  double time = hit->getTime();
222  if (time > maxTime) {
223  double windowSize = maxTime - minTime;
224  double shift = int((time - minTime) / windowSize) * windowSize;
225  hit->shiftInTime(-shift);
226  }
227  }
228  }
229  }
230 
238  bool isComponentIncluded(std::vector<std::string>& components,
239  const std::string& component);
240 
250  const std::string& bkgType,
251  const std::string& fileName,
252  double realTime,
254 
260  bool acceptEvent(TClonesArray* cloneArrayECL);
261 
262 
263  std::vector<std::string> m_backgroundFiles;
264  double m_overallScaleFactor;
265  std::vector<std::tuple<std::string, double> > m_scaleFactors;
266  double m_minTime;
267  double m_maxTime;
268  std::vector<std::string> m_components;
269  bool m_wrapAround;
270  double m_minTimeECL;
271  double m_maxTimeECL;
272  double m_minTimePXD;
273  double m_maxTimePXD;
274  double m_maxEdepECL;
277  std::vector<BkgFiles> m_backgrounds;
280  bool m_PXD = false;
281  bool m_SVD = false;
282  bool m_CDC = false;
283  bool m_TOP = false;
284  bool m_ARICH = false;
285  bool m_ECL = false;
286  bool m_KLM = false;
287  bool m_BeamBackHits = false;
291  std::map<std::string, int> m_rejected;
292  std::map<std::string, int> m_reused;
293  int m_rejectedCount = 0;
295  };
296 
298 } // Belle2 namespace
299 
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::BeamBkgMixerModule::m_SVD
bool m_SVD
true if found in m_components
Definition: BeamBkgMixerModule.h:289
Belle2::BeamBkgMixerModule::BkgFiles::numFiles
unsigned numFiles
number of files connected to TChain
Definition: BeamBkgMixerModule.h:123
Belle2::BeamBkgMixerModule::appendSample
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
Definition: BeamBkgMixerModule.cc:538
Belle2::BeamBkgMixerModule::~BeamBkgMixerModule
virtual ~BeamBkgMixerModule()
Destructor.
Definition: BeamBkgMixerModule.cc:114
Belle2::BeamBkgMixerModule::BkgHits
An input event buffer definition for background SimHits.
Definition: BeamBkgMixerModule.h:92
Belle2::BeamBkgMixerModule::m_CDC
bool m_CDC
true if found in m_components
Definition: BeamBkgMixerModule.h:290
Belle2::BeamBkgMixerModule::BkgFiles::tree
std::unique_ptr< TChain > tree
tree pointer
Definition: BeamBkgMixerModule.h:122
Belle2::BeamBkgMixerModule::m_rejectedCount
int m_rejectedCount
counter for suppresing "rejected event" messages
Definition: BeamBkgMixerModule.h:301
Belle2::BeamBkgMixerModule::m_maxTimeECL
double m_maxTimeECL
maximal time shift of background event for ECL
Definition: BeamBkgMixerModule.h:279
Belle2::BeamBkgMixerModule::BkgFiles::BkgFiles
BkgFiles()
default constructor
Definition: BeamBkgMixerModule.h:132
Belle2::BeamBkgMixerModule::m_backgrounds
std::vector< BkgFiles > m_backgrounds
container for background samples
Definition: BeamBkgMixerModule.h:285
Belle2::BeamBkgMixerModule::BeamBkgMixerModule
BeamBkgMixerModule()
Constructor.
Definition: BeamBkgMixerModule.cc:66
Belle2::BeamBkgMixerModule::BkgFiles::tag
BackgroundMetaData::BG_TAG tag
background tag
Definition: BeamBkgMixerModule.h:116
Belle2::BeamBkgMixerModule::acceptEvent
bool acceptEvent(TClonesArray *cloneArrayECL)
Checks for deposited energy of ECLHits and returns true if Edep < m_maxEdepECL.
Definition: BeamBkgMixerModule.cc:560
Belle2::BeamBkgMixerModule::BkgHits::BKLM
TClonesArray * BKLM
BKLM SimHits from collision file.
Definition: BeamBkgMixerModule.h:99
Belle2::BeamBkgMixerModule::m_simHits
BkgHits m_simHits
input event buffer
Definition: BeamBkgMixerModule.h:286
Belle2::BeamBkgMixerModule::BkgHits::EKLM
TClonesArray * EKLM
EKLM SimHits from collision file.
Definition: BeamBkgMixerModule.h:100
Belle2::BeamBkgMixerModule::beginRun
virtual void beginRun() override
Called when entering a new run.
Definition: BeamBkgMixerModule.cc:353
Belle2::BeamBkgMixerModule::BkgHits::ECL
TClonesArray * ECL
ECL SimHits from collision file.
Definition: BeamBkgMixerModule.h:98
Belle2::BeamBkgMixerModule::m_maxTimePXD
double m_maxTimePXD
maximal time shift of background event for PXD
Definition: BeamBkgMixerModule.h:281
Belle2::BeamBkgMixerModule::m_TOP
bool m_TOP
true if found in m_components
Definition: BeamBkgMixerModule.h:291
Belle2::BeamBkgMixerModule::addSimHits
void addSimHits(StoreArray< SIMHIT > &simHits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add background SimHits to those in the DataStore
Definition: BeamBkgMixerModule.h:171
Belle2::BeamBkgMixerModule::m_PXD
bool m_PXD
true if found in m_components
Definition: BeamBkgMixerModule.h:288
Belle2::BeamBkgMixerModule::BkgHits::CDC
TClonesArray * CDC
CDC SimHits from collision file.
Definition: BeamBkgMixerModule.h:95
Belle2::BackgroundMetaData::EFileType
EFileType
Enum for BG file types.
Definition: BackgroundMetaData.h:77
Belle2::BeamBkgMixerModule::BkgHits::ARICH
TClonesArray * ARICH
ARICH SimHits from collision file.
Definition: BeamBkgMixerModule.h:97
Belle2::BeamBkgMixerModule::m_KLM
bool m_KLM
true if found in m_components
Definition: BeamBkgMixerModule.h:294
Belle2::BeamBkgMixerModule::event
virtual void event() override
Event processor.
Definition: BeamBkgMixerModule.cc:357
Belle2::BeamBkgMixerModule::BkgHits::SVD
TClonesArray * SVD
SVD SimHits from collision file.
Definition: BeamBkgMixerModule.h:94
Belle2::BeamBkgMixerModule::endRun
virtual void endRun() override
End-of-run action.
Definition: BeamBkgMixerModule.cc:502
Belle2::BeamBkgMixerModule::BkgHits::BkgHits
BkgHits()
default constructor
Definition: BeamBkgMixerModule.h:106
Belle2::BeamBkgMixerModule::m_scaleFactors
std::vector< std::tuple< std::string, double > > m_scaleFactors
scale factors
Definition: BeamBkgMixerModule.h:273
Belle2::BackgroundMetaData::bg_other
@ bg_other
Other type of background.
Definition: BackgroundMetaData.h:65
Belle2::BeamBkgMixerModule::BkgFiles::numEvents
unsigned numEvents
number of events (tree entries) in the sample
Definition: BeamBkgMixerModule.h:124
Belle2::BeamBkgMixerModule::BkgHits::PXD
TClonesArray * PXD
PXD SimHits from collision file.
Definition: BeamBkgMixerModule.h:93
Belle2::BeamBkgMixerModule::BkgFiles::realTime
double realTime
real time of BG samlpe
Definition: BeamBkgMixerModule.h:118
Belle2::BeamBkgMixerModule::isComponentIncluded
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.
Definition: BeamBkgMixerModule.cc:525
Belle2::BeamBkgMixerModule::m_rejected
std::map< std::string, int > m_rejected
messages: rejected events
Definition: BeamBkgMixerModule.h:299
Belle2::BeamBkgMixerModule::m_components
std::vector< std::string > m_components
detector components
Definition: BeamBkgMixerModule.h:276
Belle2::BeamBkgMixerModule::BkgFiles::eventCount
unsigned eventCount
current event (tree entry)
Definition: BeamBkgMixerModule.h:125
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BeamBkgMixerModule::BkgFiles::fileNames
std::vector< std::string > fileNames
file names
Definition: BeamBkgMixerModule.h:120
Belle2::BeamBkgMixerModule::initialize
virtual void initialize() override
Initialize the Module.
Definition: BeamBkgMixerModule.cc:118
Belle2::BeamBkgMixerModule::m_maxTime
double m_maxTime
maximal time shift of background event
Definition: BeamBkgMixerModule.h:275
Belle2::BeamBkgMixerModule::m_BeamBackHits
bool m_BeamBackHits
if true add also background hits
Definition: BeamBkgMixerModule.h:295
Belle2::BeamBkgMixerModule::m_backgroundFiles
std::vector< std::string > m_backgroundFiles
names of beam background files
Definition: BeamBkgMixerModule.h:271
Belle2::StoreArray::isValid
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:298
Belle2::BeamBkgMixerModule::m_reused
std::map< std::string, int > m_reused
messages: rejused events
Definition: BeamBkgMixerModule.h:300
Belle2::BeamBkgMixerModule::m_minTimeECL
double m_minTimeECL
minimal time shift of background event for ECL
Definition: BeamBkgMixerModule.h:278
Belle2::BeamBkgMixerModule::m_ARICH
bool m_ARICH
true if found in m_components
Definition: BeamBkgMixerModule.h:292
Belle2::background::BeamBGTypes
Class to define BG types and to convert between BG types and tags or v.v.
Definition: BeamBGTypes.h:37
Belle2::BeamBkgMixerModule::m_maxEdepECL
double m_maxEdepECL
maximal allowed deposited energy in ECL
Definition: BeamBkgMixerModule.h:282
Belle2::BackgroundMetaData
Metadata information about the beam background file.
Definition: BackgroundMetaData.h:34
Belle2::BeamBkgMixerModule::m_minTime
double m_minTime
minimal time shift of background event
Definition: BeamBkgMixerModule.h:274
Belle2::BeamBkgMixerModule::m_minTimePXD
double m_minTimePXD
minimal time shift of background event for PXD
Definition: BeamBkgMixerModule.h:280
Belle2::BeamBkgMixerModule::BkgFiles::index
unsigned index
index of this element in the std::vector
Definition: BeamBkgMixerModule.h:127
Belle2::BeamBkgMixerModule::BkgFiles::scaleFactor
double scaleFactor
scale factor for the rate
Definition: BeamBkgMixerModule.h:119
Belle2::BeamBkgMixerModule::m_overallScaleFactor
double m_overallScaleFactor
overall scale factor
Definition: BeamBkgMixerModule.h:272
Belle2::BeamBkgMixerModule::m_wrapAround
bool m_wrapAround
if true wrap around events in the tail after maxTime
Definition: BeamBkgMixerModule.h:277
Belle2::BeamBkgMixerModule::m_ECL
bool m_ECL
true if found in m_components
Definition: BeamBkgMixerModule.h:293
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::BeamBkgMixerModule::BkgFiles::fileType
BackgroundMetaData::EFileType fileType
file type
Definition: BeamBkgMixerModule.h:121
Belle2::BeamBkgMixerModule::addBeamBackHits
void addBeamBackHits(StoreArray< HIT > &hits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add BeamBackHits to those in the DataStore
Definition: BeamBkgMixerModule.h:208
Belle2::BeamBkgMixerModule::BkgFiles::type
std::string type
background type
Definition: BeamBkgMixerModule.h:117
Belle2::BeamBkgMixerModule::m_cacheSize
int m_cacheSize
file cache size in Mbytes
Definition: BeamBkgMixerModule.h:283
Belle2::BackgroundMetaData::c_Usual
@ c_Usual
usual BG file
Definition: BackgroundMetaData.h:77
Belle2::BeamBkgMixerModule::terminate
virtual void terminate() override
Termination action.
Definition: BeamBkgMixerModule.cc:506
Belle2::BeamBkgMixerModule::BkgHits::BeamBackHits
TClonesArray * BeamBackHits
BeamBackHits from collision file.
Definition: BeamBkgMixerModule.h:101
Belle2::BeamBkgMixerModule::BkgHits::TOP
TClonesArray * TOP
TOP SimHits from collision file.
Definition: BeamBkgMixerModule.h:96
Belle2::BeamBkgMixerModule::BkgFiles::rate
double rate
background rate of the sample
Definition: BeamBkgMixerModule.h:126
Belle2::BackgroundMetaData::BG_TAG
BG_TAG
Enum for background tags.
Definition: BackgroundMetaData.h:41
Belle2::BeamBkgMixerModule::m_bgTypes
background::BeamBGTypes m_bgTypes
defined BG types
Definition: BeamBkgMixerModule.h:297