10#include <background/modules/BeamBkgMixer/BeamBkgMixerModule.h>
13#include <framework/datastore/DataStore.h>
14#include <framework/datastore/StoreArray.h>
15#include <framework/datastore/StoreObjPtr.h>
18#include <framework/core/ModuleParam.templateDetails.h>
19#include <framework/logging/Logger.h>
22#include <pxd/dataobjects/PXDSimHit.h>
23#include <svd/dataobjects/SVDSimHit.h>
24#include <cdc/dataobjects/CDCSimHit.h>
25#include <top/dataobjects/TOPSimHit.h>
26#include <arich/dataobjects/ARICHSimHit.h>
27#include <ecl/dataobjects/ECLHit.h>
28#include <klm/dataobjects/KLMSimHit.h>
29#include <simulation/dataobjects/BeamBackHit.h>
32#include <framework/dataobjects/BackgroundInfo.h>
57 setDescription(
"Beam background mixer at SimHit level that uses beam background"
58 " simulation output directly (collision files) and not ROF files. "
59 "Each background event is shifted in time randomly within "
60 "a time window specified with minTime and maxTime.");
64 "List of background (collision) files (wildcards not allowed - "
65 "use python glob.glob() to expand to list of files)");
67 "Time window lower edge in nano seconds", -1000.0);
69 "Time window upper edge in nano seconds", 800.0);
71 "Overall factor to scale rates of backgrounds", 1.0);
73 "Factors to scale rates of backgrounds. "
77 "Detector components to be included, empty list means all components",
80 "if true wrap around events passing time window upper edge",
true);
82 "Time window lower edge for ECL in nano seconds", -17600.0);
84 "Time window upper edge for ECL in nano seconds", 8500.0);
86 "Time window lower edge for PXD in nano seconds", -10000.0);
88 "Time window upper edge for PXD in nano seconds", 10000.0);
91 "If true also add the BeamBackHits collection for the selected "
92 "subdetectors to the output file",
false);
95 "maximal deposited energy of ECLHit to accept BG event for mixing"
96 "(0 means accept all events)", 1.0);
99 "file cache size in Mbytes. If negative, use root default", 0);
135 if (!components.empty()) {
137 for (
unsigned i = 0; i < components.size(); ++i) str = str +
" " + components[i];
138 B2WARNING(
"Unknown components:" << str);
146 if (TString(file.c_str()).Contains(
"*")) {
147 B2ERROR(file <<
": wildcards are not allowed");
152 TFile* f = TFile::Open(file.c_str(),
"READ");
154 B2ERROR(file <<
": file not found");
158 B2ERROR(file <<
": can't open file");
163 TChain persistent(
"persistent");
164 int nFiles = persistent.Add(file.c_str());
166 B2ERROR(file <<
": no such files");
169 if (persistent.GetEntries() == 0) {
170 B2ERROR(file <<
": tree 'persistent' has no entries");
174 TObject* bkgMetaData = 0;
175 TBranch* branchBMD = persistent.GetBranch(
"BackgroundMetaData");
177 B2ERROR(file <<
": branch 'BackgroundMetaData' not found");
180 branchBMD->SetAddress(&bkgMetaData);
182 std::vector<BackgroundMetaData::BG_TAG> tags;
183 std::vector<std::string> types;
184 std::vector<BackgroundMetaData::EFileType> fileTypes;
186 for (
unsigned k = 0; k < persistent.GetEntries(); k++) {
187 persistent.GetEntry(k);
195 B2ERROR(file <<
": invalid realTime: " << realTime);
198 for (
unsigned i = 1; i < tags.size(); ++i) {
199 if (tags[i] != tags[0]) {
200 B2ERROR(file <<
": files with mixed background types not supported");
204 for (
unsigned i = 1; i < fileTypes.size(); ++i) {
205 if (fileTypes[i] != fileTypes[0]) {
206 B2ERROR(file <<
": files with mixed file types not supported");
211 appendSample(tags[0], types[0], file, realTime, fileTypes[0]);
219 std::string type = std::get<0>(scaleFactor);
221 B2ERROR(
"Unknown beam background type found in 'scaleFactors': " << type <<
"\n"
225 bkg.scaleFactor *= std::get<1>(scaleFactor);
234 bkg.tree.reset(
new TChain(
"tree"));
235 for (
unsigned i = 0; i < bkg.fileNames.size(); ++i) {
236 bkg.numFiles += bkg.tree->Add(bkg.fileNames[i].c_str());
239 bkg.numEvents = bkg.tree->GetEntries();
240 bkg.rate = bkg.numEvents / bkg.realTime * bkg.scaleFactor;
244 if (
m_PXD and bkg.tree->GetBranch(
"PXDSimHits"))
245 bkg.tree->SetBranchAddress(
"PXDSimHits", &
m_simHits.
PXD);
246 if (
m_SVD and bkg.tree->GetBranch(
"SVDSimHits"))
247 bkg.tree->SetBranchAddress(
"SVDSimHits", &
m_simHits.
SVD);
248 if (
m_CDC and bkg.tree->GetBranch(
"CDCSimHits"))
249 bkg.tree->SetBranchAddress(
"CDCSimHits", &
m_simHits.
CDC);
250 if (
m_TOP and bkg.tree->GetBranch(
"TOPSimHits"))
251 bkg.tree->SetBranchAddress(
"TOPSimHits", &
m_simHits.
TOP);
252 if (
m_ARICH and bkg.tree->GetBranch(
"ARICHSimHits"))
254 if (
m_ECL and bkg.tree->GetBranch(
"ECLHits"))
256 if (
m_KLM and bkg.tree->GetBranch(
"KLMSimHits"))
257 bkg.tree->SetBranchAddress(
"KLMSimHits", &
m_simHits.
KLM);
263 std::string unit(
" ns");
264 double realTime = bkg.realTime;
265 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" us";}
266 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" ms";}
267 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" s";}
269 B2INFO(
"BeamBkgMixer: " << bkg.type <<
270 " tag=" << bkg.tag <<
271 " files=" << bkg.numFiles <<
272 " events=" << bkg.numEvents <<
273 " realTime=" << realTime << unit <<
274 " scaleFactor=" << bkg.scaleFactor <<
275 " rate=" << bkg.rate * 1000 <<
" MHz");
324 descr.type = bkg.type;
325 descr.fileType = bkg.fileType;
326 descr.fileNames = bkg.fileNames;
327 descr.realTime = bkg.realTime;
328 descr.numEvents = bkg.numEvents;
329 descr.scaleFactor = bkg.scaleFactor;
330 descr.rate = bkg.rate;
332 bkgInfo->appendBackgroundDescr(descr);
358 int nev = gRandom->Poisson(mean);
360 for (
int iev = 0; iev < nev; iev++) {
362 bkg.tree->GetEntry(bkg.eventCount);
376 std::string message =
"BeamBkgMixer: event " + to_string(bkg.eventCount)
377 +
" of " + bkg.type +
" rejected due to large energy deposit in ECL";
381 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL");
383 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL "
384 <<
"(message will be suppressed now)");
389 if (bkg.eventCount >= bkg.numEvents) {
391 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be reused";
393 if (
m_reused[message] == 1) B2INFO(message);
394 bkgInfo->incrementReusedCounter(bkg.index);
405 int nev = gRandom->Poisson(mean);
407 for (
int iev = 0; iev < nev; iev++) {
410 bkg.tree->GetEntry(bkg.eventCount);
423 std::string message =
"BeamBkgMixer: event " + to_string(bkg.eventCount)
424 +
" of " + bkg.type +
" rejected due to large energy deposit in ECL";
428 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL");
430 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL "
431 <<
"(message will be suppressed now)");
436 if (bkg.eventCount >= bkg.numEvents) {
438 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be reused";
440 if (
m_reused[message] == 1) B2INFO(message);
441 bkgInfo->incrementReusedCounter(bkg.index);
453 int nev = gRandom->Poisson(mean);
455 for (
int iev = 0; iev < nev; iev++) {
458 bkg.tree->GetEntry(bkg.eventCount);
470 if (bkg.eventCount >= bkg.numEvents) {
472 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be reused";
474 if (
m_reused[message] == 1) B2INFO(message);
475 bkgInfo->incrementReusedCounter(bkg.index);
491 B2INFO(
"BeamBkgMixer - reused samples:");
492 for (
const auto& message :
m_reused) {
493 B2INFO(message.first <<
"(occurred " << message.second <<
" times)");
495 B2INFO(
"BeamBkgMixer - rejected events:");
497 B2INFO(message.first <<
"(occurred " << message.second <<
" times)");
508 const std::string& component)
511 auto iterator = std::find(components.begin(), components.end(), component);
512 if (iterator != components.end()) {
513 components.erase(iterator);
521 const std::string& type,
522 const std::string& fileName,
527 if (tag == bkg.tag and fileType == bkg.fileType) {
528 bkg.fileNames.push_back(fileName);
529 bkg.realTime += realTime;
533 std::string ftype = type;
544 if (!cloneArrayECL)
return true;
547 int numEntries = cloneArrayECL->GetEntriesFast();
548 for (
int i = 0; i < numEntries; i++) {
549 ECLHit* simHit =
static_cast<ECLHit*
>(cloneArrayECL->AddrAt(i));
std::map< std::string, int > m_reused
messages: rejused events
double m_maxEdepECL
maximal allowed deposited energy in ECL
std::vector< BkgFiles > m_backgrounds
container for background samples
virtual ~BeamBkgMixerModule()
Destructor.
double m_maxTimeECL
maximal time shift of background event for ECL
std::map< std::string, int > m_rejected
messages: rejected events
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.
bool m_ECL
true if found in m_components
std::vector< std::string > m_components
detector components
void addBeamBackHits(StoreArray< HIT > &hits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add BeamBackHits to those in the DataStore
double m_minTimePXD
minimal time shift of background event for PXD
bool m_PXD
true if found in m_components
void addSimHits(StoreArray< SIMHIT > &simHits, TClonesArray *cloneArray, double timeShift, double minTime, double maxTime)
functions that add background SimHits to those in the DataStore
background::BeamBGTypes m_bgTypes
defined BG types
std::vector< std::string > m_backgroundFiles
names of beam background files
virtual void initialize() override
Initialize the Module.
bool m_CDC
true if found in m_components
double m_maxTime
maximal time shift of background event
virtual void event() override
Event processor.
double m_minTime
minimal time shift of background event
bool m_wrapAround
if true wrap around events in the tail after maxTime
virtual void endRun() override
End-of-run action.
double m_maxTimePXD
maximal time shift of background event for PXD
int m_rejectedCount
counter for suppressing "rejected event" messages
virtual void terminate() override
Termination action.
BkgHits m_simHits
input event buffer
int m_cacheSize
file cache size in Mbytes
bool m_BeamBackHits
if true add also background hits
virtual void beginRun() override
Called when entering a new run.
bool m_ARICH
true if found in m_components
double m_overallScaleFactor
overall scale factor
BeamBkgMixerModule()
Constructor.
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
bool acceptEvent(TClonesArray *cloneArrayECL)
Checks for deposited energy of ECLHits and returns true if Edep < m_maxEdepECL.
double m_minTimeECL
minimal time shift of background event for ECL
std::vector< std::tuple< std::string, double > > m_scaleFactors
scale factors
bool m_KLM
true if found in m_components
bool m_SVD
true if found in m_components
bool m_TOP
true if found in m_components
@ c_Persistent
Object is available during entire execution time.
Class to store simulated hits which equate to average of ECLSImHit on crystals input for digitization...
double getEnergyDep() const
Get deposit energy.
void setDescription(const std::string &description)
Sets the description of the module.
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.
Accessor to arrays stored in the data store.
Type-safe access to single objects in the data store.
std::string getBGTypes() const
Return all defined BG types as a string.
BackgroundMetaData::BG_TAG getTag(const std::string &bgType)
Return BG tag for a given BG type.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#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.
structure to hold samples of a particular background type
TClonesArray * BeamBackHits
BeamBackHits from collision file.
TClonesArray * KLM
KLM SimHits from collision file.
TClonesArray * ARICH
ARICH SimHits from collision file.
TClonesArray * CDC
CDC SimHits from collision file.
TClonesArray * PXD
PXD SimHits from collision file.
TClonesArray * ECL
ECL SimHits from collision file.
TClonesArray * SVD
SVD SimHits from collision file.
TClonesArray * TOP
TOP SimHits from collision file.