12 #include <background/modules/BGOverlayInput/BGOverlayInputModule.h>
17 #include <framework/datastore/DataStore.h>
18 #include <framework/datastore/StoreObjPtr.h>
21 #include <framework/logging/Logger.h>
24 #include <framework/dataobjects/BackgroundInfo.h>
27 #include <framework/io/RootIOUtilities.h>
28 #include <TClonesArray.h>
42 using namespace RootIOUtilities;
58 setDescription(
"Input for BG overlay, either in form of Digits or raw data.");
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(
""));
68 BGOverlayInputModule::~BGOverlayInputModule()
72 void BGOverlayInputModule::initialize()
77 if (m_inputFileNames.empty()) {
78 B2FATAL(
"No valid files specified!");
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);
88 auto* persistent =
static_cast<TTree*
>(f->Get(
"persistent"));
89 if (!persistent) B2ERROR(
"No 'persistent' tree found in " + fileName);
91 TBranch* branch = persistent->GetBranch(
"BackgroundMetaData");
92 if (branch) B2ERROR(fileName <<
": wrong sample, this one is aimed for BG mixing");
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());
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;
109 bool ok = connectBranches();
111 B2ERROR(
"No branches found to be connected");
116 bkgInfo.registerInDataStore();
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);
130 void BGOverlayInputModule::beginRun()
135 void BGOverlayInputModule::event()
139 for (
auto entry : m_storeEntries) {
140 entry->resetForGetEntry();
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);
149 m_tree->GetEntry(m_eventCount);
151 if (m_eventCount >= m_numEvents) {
155 for (
auto entry : m_storeEntries) {
157 entry->ptr = entry->object;
159 entry->recoverFromNullObject();
167 void BGOverlayInputModule::endRun()
171 void BGOverlayInputModule::terminate()
179 bool BGOverlayInputModule::connectBranches()
182 auto durability = DataStore::c_Event;
183 auto storeFlags = DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered;
184 auto& map = DataStore::Instance().getStoreEntryMap(durability);
186 const TObjArray* branches = m_tree->GetListOfBranches();
187 if (!branches)
return false;
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();
194 TObject* objectPtr = 0;
195 branch->SetAddress(&objectPtr);
197 std::string objName = branch->GetClassName();
199 if (objName ==
"TClonesArray") {
200 TClass* objClass = (
static_cast<TClonesArray*
>(objectPtr))->GetClass();
201 branch->ResetAddress();
204 const std::string className = objClass->GetName();
205 if (className.find(
"Belle2::") == std::string::npos) {
206 m_tree->SetBranchStatus(branchName.c_str(), 0);
210 std::string name = branchName + m_extensionName;
211 bool ok = DataStore::Instance().registerEntry(name, durability, objClass,
214 m_tree->SetBranchStatus(branchName.c_str(), 0);
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;
222 bool ok = DataStore::Instance().registerEntry(name, durability, objectPtr->IsA(),
224 branch->ResetAddress();
228 m_tree->SetBranchStatus(branchName.c_str(), 0);
232 m_tree->SetBranchAddress(branchName.c_str(), &(entry.
object));
233 m_storeEntries.push_back(&entry);
236 m_tree->SetBranchStatus(branchName.c_str(), 0);
237 branch->ResetAddress();
243 return !m_storeEntries.empty();