Belle II Software  release-05-02-19
BGOverlayExecutorModule.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 <framework/dataobjects/DigitBase.h>
16 #include <string>
17 #include <map>
18 
19 namespace Belle2 {
31  class BGOverlayExecutorModule : public Module {
32 
33  public:
34 
39 
43  virtual ~BGOverlayExecutorModule();
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 
76  std::string m_PXDDigitsName;
77  std::string m_SVDShaperDigitsName;
78  std::string m_CDCHitsName;
79  std::string m_TOPDigitsName;
80  std::string m_ARICHDigitsName;
81  std::string m_KLMDigitsName;
83  std::string m_extensionName;
84  std::string m_BackgroundInfoInstanceName = "";
89  template <class Digit>
90  void registerDigits(const std::string& name)
91  {
92  StoreArray<Digit> digits(name);
93  digits.isOptional();
94  StoreArray<Digit> tmp; // just to get the default name
95  std::string nameBG = tmp.getName() + m_extensionName;
96  StoreArray<Digit> bgDigits(nameBG);
97  bgDigits.isOptional();
98  B2DEBUG(100, "optional input: " << digits.getName() << " " << bgDigits.getName());
99  }
100 
105  template <class Digit>
106  void addBGDigits(const std::string& name)
107  {
108  // simulated digits
109  StoreArray<Digit> digits(name);
110  if (!digits.isValid()) {
111  B2DEBUG(100, digits.getName() << " are not valid");
112  return;
113  }
114 
115  // background digits
116  StoreArray<Digit> tmp; // just to get the default name
117  std::string nameBG = tmp.getName() + m_extensionName;
118  StoreArray<Digit> bgDigits(nameBG);
119  if (!bgDigits.isValid()) {
120  B2DEBUG(100, bgDigits.getName() << " are not valid");
121  return;
122  }
123 
124  // map unique channel ID with simulated digits
125  std::multimap<unsigned, Digit*> multimap;
126  for (auto& digit : digits) {
127  unsigned id = digit.getUniqueChannelID();
128  // multimap.emplace(id, &digit); /* Clang doesn't know this member! */
129  multimap.insert(std::pair<unsigned, Digit*>(id, &digit));
130  }
131 
132  int size = digits.getEntries(); // for debug print
133 
134  // add BG digits to simulated ones
135  typedef typename std::multimap<unsigned, Digit*>::iterator Iterator;
136  for (const auto& bgDigit : bgDigits) {
137  bool pileup = false;
138  unsigned id = bgDigit.getUniqueChannelID();
139  std::pair<Iterator, Iterator> range = multimap.equal_range(id);
140  for (Iterator it = range.first; it != range.second; ++it) {
141  auto* digit = it->second;
142  pileup = digit->addBGDigit(&bgDigit) == DigitBase::c_DontAppend;
143  if (pileup) break; // BG digit merged with simulated one
144  }
145  if (!pileup) digits.appendNew(bgDigit); // BG digit not merged, therefore append
146  }
147 
148  // debug printout
149  int diff = size + bgDigits.getEntries() - digits.getEntries();
150  B2DEBUG(100, digits.getName() << ": before " << size
151  << " + " << bgDigits.getEntries() << "(BG)"
152  << ", after " << digits.getEntries()
153  << ", merged " << diff);
154 
155  }
156 
157  };
158 
160 } // Belle2 namespace
161 
Belle2::BGOverlayExecutorModule::m_SVDShaperDigitsName
std::string m_SVDShaperDigitsName
name of SVD collection to overlay with BG
Definition: BGOverlayExecutorModule.h:85
Belle2::BGOverlayExecutorModule::m_ARICHDigitsName
std::string m_ARICHDigitsName
name of ARICH collection to overlay with BG
Definition: BGOverlayExecutorModule.h:88
Belle2::BGOverlayExecutorModule::m_BackgroundInfoInstanceName
std::string m_BackgroundInfoInstanceName
name BackgroundInfo name
Definition: BGOverlayExecutorModule.h:92
Belle2::BGOverlayExecutorModule::m_PXDDigitsName
std::string m_PXDDigitsName
name of PXD collection to overlay with BG
Definition: BGOverlayExecutorModule.h:84
Belle2::BGOverlayExecutorModule::~BGOverlayExecutorModule
virtual ~BGOverlayExecutorModule()
Destructor.
Definition: BGOverlayExecutorModule.cc:73
Belle2::BGOverlayExecutorModule::addBGDigits
void addBGDigits(const std::string &name)
Add BG digits to simulated ones.
Definition: BGOverlayExecutorModule.h:114
Belle2::BGOverlayExecutorModule::beginRun
virtual void beginRun() override
Called when entering a new run.
Definition: BGOverlayExecutorModule.cc:101
Belle2::Iterator
map< unsigned, const TOPSampleTimes * >::const_iterator Iterator
Iteratior for m_map.
Definition: TOPCalTimebase.cc:25
Belle2::BGOverlayExecutorModule::m_KLMDigitsName
std::string m_KLMDigitsName
name of KLM collection to overlay with BG
Definition: BGOverlayExecutorModule.h:89
Belle2::BGOverlayExecutorModule::event
virtual void event() override
Event processor.
Definition: BGOverlayExecutorModule.cc:105
Belle2::BGOverlayExecutorModule::endRun
virtual void endRun() override
End-of-run action.
Definition: BGOverlayExecutorModule.cc:120
Belle2::DigitBase::c_DontAppend
@ c_DontAppend
do not append BG digit to digits
Definition: DigitBase.h:43
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BGOverlayExecutorModule::registerDigits
void registerDigits(const std::string &name)
Register simulated and BG digits (both as optional input)
Definition: BGOverlayExecutorModule.h:98
Belle2::BGOverlayExecutorModule::initialize
virtual void initialize() override
Initialize the Module.
Definition: BGOverlayExecutorModule.cc:77
Belle2::BGOverlayExecutorModule::BGOverlayExecutorModule
BGOverlayExecutorModule()
Constructor.
Definition: BGOverlayExecutorModule.cc:49
Belle2::BGOverlayExecutorModule::m_extensionName
std::string m_extensionName
name added to default branch names
Definition: BGOverlayExecutorModule.h:91
Belle2::BGOverlayExecutorModule::m_CDCHitsName
std::string m_CDCHitsName
name of CDC collection to overlay with BG
Definition: BGOverlayExecutorModule.h:86
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::BGOverlayExecutorModule::terminate
virtual void terminate() override
Termination action.
Definition: BGOverlayExecutorModule.cc:124
Belle2::BGOverlayExecutorModule::m_TOPDigitsName
std::string m_TOPDigitsName
name of TOP collection to overlay with BG
Definition: BGOverlayExecutorModule.h:87