Belle II Software  release-05-02-19
BGOverlayInputModule.cc
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 // Own include
12 #include <background/modules/BGOverlayInput/BGOverlayInputModule.h>
13 
14 
15 
16 // framework - DataStore
17 #include <framework/datastore/DataStore.h>
18 #include <framework/datastore/StoreObjPtr.h>
19 
20 // framework aux
21 #include <framework/logging/Logger.h>
22 
23 // MetaData
24 #include <framework/dataobjects/BackgroundInfo.h>
25 
26 // root
27 #include <framework/io/RootIOUtilities.h>
28 #include <TClonesArray.h>
29 #include <TFile.h>
30 #include <TRandom.h>
31 
32 #include <iostream>
33 
34 using namespace std;
35 
36 namespace Belle2 {
42  using namespace RootIOUtilities;
43 
44  //-----------------------------------------------------------------
45  // Register module
46  //-----------------------------------------------------------------
47 
48  REG_MODULE(BGOverlayInput)
49 
50  //-----------------------------------------------------------------
51  // Implementation
52  //-----------------------------------------------------------------
53 
55 
56  {
57  // module description
58  setDescription("Input for BG overlay, either in form of Digits or raw data.");
59 
60  // Add parameters
61  addParam("inputFileNames", m_inputFileNames,
62  "List of files with measured beam background ");
63  addParam("extensionName", m_extensionName,
64  "name added to default branch names", string("_beamBG"));
65  addParam("bkgInfoName", m_BackgroundInfoInstanceName, "name of the BackgroundInfo StoreObjPtr", string(""));
66  }
67 
68  BGOverlayInputModule::~BGOverlayInputModule()
69  {
70  }
71 
72  void BGOverlayInputModule::initialize()
73  {
74 
75  // expand possible wildcards
76  m_inputFileNames = expandWordExpansions(m_inputFileNames);
77  if (m_inputFileNames.empty()) {
78  B2FATAL("No valid files specified!");
79  }
80 
81  // check files
82  TDirectory* dir = gDirectory;
83  for (const string& fileName : m_inputFileNames) {
84  TFile* f = TFile::Open(fileName.c_str(), "READ");
85  if (!f or !f->IsOpen()) {
86  B2FATAL("Couldn't open input file " + fileName);
87  }
88  auto* persistent = static_cast<TTree*>(f->Get("persistent"));
89  if (!persistent) B2ERROR("No 'persistent' tree found in " + fileName);
90  // check and issue error if file is for BG mixing
91  TBranch* branch = persistent->GetBranch("BackgroundMetaData");
92  if (branch) B2ERROR(fileName << ": wrong sample, this one is aimed for BG mixing");
93  delete f;
94  }
95  dir->cd();
96 
97  // get event TTree
98  m_tree = new TChain(c_treeNames[DataStore::c_Event].c_str());
99  for (const string& fileName : m_inputFileNames) {
100  m_tree->AddFile(fileName.c_str());
101  }
102  m_numEvents = m_tree->GetEntries();
103  if (m_numEvents == 0) B2ERROR(c_treeNames[DataStore::c_Event] << " has no entires");
104  m_firstEvent = gRandom->Integer(m_numEvents);
105  B2INFO("BGOverlayInput: starting with event " << m_firstEvent);
106  m_eventCount = m_firstEvent;
107 
108  // connect selected branches to DataStore
109  bool ok = connectBranches();
110  if (!ok) {
111  B2ERROR("No branches found to be connected");
112  }
113 
114  // add BackgroundInfo to persistent tree
115  StoreObjPtr<BackgroundInfo> bkgInfo(m_BackgroundInfoInstanceName, DataStore::c_Persistent);
116  bkgInfo.registerInDataStore();
117  bkgInfo.create();
118  bkgInfo->setMethod(BackgroundInfo::c_Overlay);
120  descr.tag = BackgroundMetaData::bg_other;
121  descr.type = string("RandomTrigger");
122  descr.fileNames = m_inputFileNames;
123  descr.numEvents = m_numEvents;
124  m_index = bkgInfo->appendBackgroundDescr(descr);
125  bkgInfo->setExtensionName(m_extensionName);
126 
127  }
128 
129 
130  void BGOverlayInputModule::beginRun()
131  {
132  }
133 
134 
135  void BGOverlayInputModule::event()
136  {
137  StoreObjPtr<BackgroundInfo> bkgInfo("", DataStore::c_Persistent);
138 
139  for (auto entry : m_storeEntries) {
140  entry->resetForGetEntry();
141  }
142 
143  if (m_eventCount == m_firstEvent and !m_start) {
144  B2INFO("BGOverlayInput: events for BG overlay will be re-used");
145  bkgInfo->incrementReusedCounter(m_index);
146  }
147  m_start = false;
148 
149  m_tree->GetEntry(m_eventCount);
150  m_eventCount++;
151  if (m_eventCount >= m_numEvents) {
152  m_eventCount = 0;
153  }
154 
155  for (auto entry : m_storeEntries) {
156  if (entry->object) {
157  entry->ptr = entry->object;
158  } else {
159  entry->recoverFromNullObject();
160  entry->ptr = 0;
161  }
162  }
163 
164  }
165 
166 
167  void BGOverlayInputModule::endRun()
168  {
169  }
170 
171  void BGOverlayInputModule::terminate()
172  {
173 
174  delete m_tree;
175 
176  }
177 
178 
179  bool BGOverlayInputModule::connectBranches()
180  {
181 
182  auto durability = DataStore::c_Event;
183  auto storeFlags = DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered;
184  auto& map = DataStore::Instance().getStoreEntryMap(durability);
185 
186  const TObjArray* branches = m_tree->GetListOfBranches();
187  if (!branches) return false;
188 
189  for (int jj = 0; jj < branches->GetEntriesFast(); jj++) {
190  TBranch* branch = static_cast<TBranch*>(branches->At(jj));
191  if (!branch) continue;
192  const std::string branchName = branch->GetName();
193 
194  TObject* objectPtr = 0;
195  branch->SetAddress(&objectPtr);
196  branch->GetEntry();
197  std::string objName = branch->GetClassName();
198 
199  if (objName == "TClonesArray") {
200  TClass* objClass = (static_cast<TClonesArray*>(objectPtr))->GetClass();
201  branch->ResetAddress();
202  delete objectPtr;
203 
204  const std::string className = objClass->GetName();
205  if (className.find("Belle2::") == std::string::npos) { // only Belle2 classes
206  m_tree->SetBranchStatus(branchName.c_str(), 0);
207  continue;
208  }
209 
210  std::string name = branchName + m_extensionName;
211  bool ok = DataStore::Instance().registerEntry(name, durability, objClass,
212  true, storeFlags);
213  if (!ok) {
214  m_tree->SetBranchStatus(branchName.c_str(), 0);
215  continue;
216  }
217  DataStore::StoreEntry& entry = (map.find(name))->second;
218  m_tree->SetBranchAddress(branchName.c_str(), &(entry.object));
219  m_storeEntries.push_back(&entry);
220  } else if (objName == "Belle2::ECLWaveforms" or objName == "Belle2::PXDInjectionBGTiming") {
221  std::string name = branchName;// + m_extensionName;
222  bool ok = DataStore::Instance().registerEntry(name, durability, objectPtr->IsA(),
223  false, storeFlags);
224  branch->ResetAddress();
225  delete objectPtr;
226 
227  if (!ok) {
228  m_tree->SetBranchStatus(branchName.c_str(), 0);
229  continue;
230  }
231  DataStore::StoreEntry& entry = (map.find(name))->second;
232  m_tree->SetBranchAddress(branchName.c_str(), &(entry.object));
233  m_storeEntries.push_back(&entry);
234 
235  } else {
236  m_tree->SetBranchStatus(branchName.c_str(), 0);
237  branch->ResetAddress();
238  delete objectPtr;
239  }
240 
241  }
242 
243  return !m_storeEntries.empty();
244  }
245 
246 
248 } // end Belle2 namespace
249 
Belle2::StoreEntry::object
TObject * object
The pointer to the actual object.
Definition: StoreEntry.h:41
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:15
Belle2::RootIOUtilities::c_treeNames
const std::string c_treeNames[]
Names of trees.
Definition: RootIOUtilities.cc:20
Belle2::BackgroundInfo::BackgroundDescr
Structure for background description.
Definition: BackgroundInfo.h:57
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::RootIOUtilities::expandWordExpansions
std::vector< std::string > expandWordExpansions(const std::vector< std::string > &filenames)
Performs wildcard expansion using wordexp(), returns matches.
Definition: RootIOUtilities.cc:107
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::BGOverlayInputModule
Beam BG data input, either in form of Digits or raw data.
Definition: BGOverlayInputModule.h:36