10 #include <background/modules/BGOverlayInput/BGOverlayInputModule.h>
13 #include <framework/dataobjects/BackgroundInfo.h>
14 #include <framework/dataobjects/EventMetaData.h>
15 #include <framework/dataobjects/FileMetaData.h>
16 #include <framework/datastore/DataStore.h>
17 #include <framework/datastore/StoreObjPtr.h>
18 #include <framework/io/RootFileInfo.h>
19 #include <framework/io/RootIOUtilities.h>
20 #include <framework/logging/Logger.h>
23 #include <TClonesArray.h>
47 setDescription(
"Input for BG overlay, either in form of Digits or raw data.");
50 addParam(
"inputFileNames", m_inputFileNames,
51 "List of files with measured beam background ");
52 addParam(
"extensionName", m_extensionName,
53 "Name added to default branch names",
string(
"_beamBG"));
54 addParam(
"bkgInfoName", m_BackgroundInfoInstanceName,
"Name of the BackgroundInfo StoreObjPtr",
string(
""));
55 addParam(
"skipExperimentCheck", m_skipExperimentCheck,
56 "If true, skip the check on the experiment number consistency between the basf2 process and the beam background files. By default, it is set to false, since the check should be skipped only by experts.",
60 BGOverlayInputModule::~BGOverlayInputModule()
64 void BGOverlayInputModule::initialize()
66 if (m_skipExperimentCheck)
67 B2WARNING(R
"RAW(The BGOverlayInput module will skip the check on the experiment number
68 consistency between the basf2 process and the beam background files.
70 This should be done only if you are extremely sure about what you are doing.
72 Be aware that you are not protected by the possible usage of beam background
73 files not suitabile for the experiment number you selected.)RAW");
76 m_inputFileNames = RootIOUtilities::expandWordExpansions(m_inputFileNames);
77 if (m_inputFileNames.empty()) {
78 B2FATAL(
"No valid files specified!");
83 int experiment{eventMetaData->getExperiment()};
86 for (
const string& fileName : m_inputFileNames) {
89 const std::set<std::string>& branchNames = fileInfo.getBranchNames(
true);
90 if (branchNames.count(
"BackgroundMetaData"))
91 B2FATAL(
"The BG sample used is aimed for BG mixing, not for BG mixing."
92 <<
LogVar(
"File name", fileName));
93 if (not m_skipExperimentCheck) {
94 const FileMetaData& fileMetaData = fileInfo.getFileMetaData();
97 B2FATAL(
"The BG sample used is aimed for a different experiment number. Please check what you are doing."
98 <<
LogVar(
"File name", fileName)
99 <<
LogVar(
"Experiment number of the basf2 process", experiment)
102 }
catch (
const std::invalid_argument& e) {
103 B2FATAL(
"One of the BG files can not be opened."
104 <<
LogVar(
"File name", fileName));
105 }
catch (
const std::runtime_error& e) {
106 B2FATAL(
"Something went wrong with one of the BG files."
107 <<
LogVar(
"File name", fileName)
108 <<
LogVar(
"Issue", e.what()));
113 m_tree =
new TChain(RootIOUtilities::c_treeNames[DataStore::c_Event].c_str());
114 for (
const string& fileName : m_inputFileNames) {
115 m_tree->AddFile(fileName.c_str());
117 m_numEvents = m_tree->GetEntries();
118 if (m_numEvents == 0) B2ERROR(RootIOUtilities::c_treeNames[DataStore::c_Event] <<
" has no entires");
119 m_firstEvent = gRandom->Integer(m_numEvents);
120 B2INFO(
"BGOverlayInput: starting with event " << m_firstEvent);
121 m_eventCount = m_firstEvent;
124 bool ok = connectBranches();
126 B2ERROR(
"No branches found to be connected");
133 bkgInfo->setMethod(BackgroundInfo::c_Overlay);
135 descr.tag = BackgroundMetaData::bg_other;
136 descr.type = string(
"RandomTrigger");
137 descr.fileNames = m_inputFileNames;
138 descr.numEvents = m_numEvents;
139 m_index = bkgInfo->appendBackgroundDescr(descr);
140 bkgInfo->setExtensionName(m_extensionName);
144 void BGOverlayInputModule::beginRun()
149 void BGOverlayInputModule::event()
153 for (
auto entry : m_storeEntries) {
154 entry->resetForGetEntry();
157 if (m_eventCount == m_firstEvent and !m_start) {
158 B2INFO(
"BGOverlayInput: events for BG overlay will be re-used");
159 bkgInfo->incrementReusedCounter(m_index);
163 m_tree->GetEntry(m_eventCount);
165 if (m_eventCount >= m_numEvents) {
169 for (
auto entry : m_storeEntries) {
171 entry->ptr = entry->object;
173 entry->recoverFromNullObject();
181 void BGOverlayInputModule::endRun()
185 void BGOverlayInputModule::terminate()
193 bool BGOverlayInputModule::connectBranches()
196 auto durability = DataStore::c_Event;
197 auto storeFlags = DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered;
198 auto& map = DataStore::Instance().getStoreEntryMap(durability);
201 const std::set<std::string> objPtrNames = {
"Belle2::ECLWaveforms",
"Belle2::PXDInjectionBGTiming",
"Belle2::EventLevelTriggerTimeInfo"};
203 const TObjArray* branches = m_tree->GetListOfBranches();
204 if (!branches)
return false;
206 for (
int jj = 0; jj < branches->GetEntriesFast(); jj++) {
207 TBranch* branch =
static_cast<TBranch*
>(branches->At(jj));
208 if (!branch)
continue;
209 const std::string branchName = branch->GetName();
211 TObject* objectPtr = 0;
212 branch->SetAddress(&objectPtr);
214 std::string objName = branch->GetClassName();
216 if (objName ==
"TClonesArray") {
217 TClass* objClass = (
static_cast<TClonesArray*
>(objectPtr))->GetClass();
218 branch->ResetAddress();
221 const std::string className = objClass->GetName();
222 if (className.find(
"Belle2::") == std::string::npos) {
223 m_tree->SetBranchStatus(branchName.c_str(), 0);
227 std::string name = branchName + m_extensionName;
228 bool ok = DataStore::Instance().registerEntry(name, durability, objClass,
231 m_tree->SetBranchStatus(branchName.c_str(), 0);
235 m_tree->SetBranchAddress(branchName.c_str(), &(entry.
object));
236 m_storeEntries.push_back(&entry);
237 }
else if (objPtrNames.find(objName) != objPtrNames.end()) {
238 std::string name = branchName;
239 bool ok = DataStore::Instance().registerEntry(name, durability, objectPtr->IsA(),
241 branch->ResetAddress();
245 m_tree->SetBranchStatus(branchName.c_str(), 0);
249 m_tree->SetBranchAddress(branchName.c_str(), &(entry.
object));
250 m_storeEntries.push_back(&entry);
253 m_tree->SetBranchStatus(branchName.c_str(), 0);
254 branch->ResetAddress();
260 return !m_storeEntries.empty();
Helper class to factorize some necessary tasks when working with Belle2 output files.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
Class to store variables with their name which were sent to the logging service.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
Structure for background description.
Wraps a stored array/object, stored under unique (name, durability) key.
TObject * object
The pointer to the actual object.