10#include <background/modules/BeamBkgMixer/BeamBkgMixerModule.h>
15#include <framework/datastore/DataStore.h>
16#include <framework/datastore/StoreArray.h>
17#include <framework/datastore/StoreObjPtr.h>
18#include <framework/datastore/RelationArray.h>
21#include <framework/core/ModuleParam.templateDetails.h>
22#include <framework/logging/Logger.h>
25#include <pxd/dataobjects/PXDSimHit.h>
26#include <svd/dataobjects/SVDSimHit.h>
27#include <cdc/dataobjects/CDCSimHit.h>
28#include <top/dataobjects/TOPSimHit.h>
29#include <arich/dataobjects/ARICHSimHit.h>
30#include <ecl/dataobjects/ECLHit.h>
31#include <klm/dataobjects/KLMSimHit.h>
32#include <simulation/dataobjects/BeamBackHit.h>
35#include <framework/dataobjects/EventMetaData.h>
36#include <framework/dataobjects/BackgroundInfo.h>
61 setDescription(
"Beam background mixer at SimHit level that uses beam background"
62 " simulation output directly (collision files) and not ROF files. "
63 "Each background event is shifted in time randomly within "
64 "a time window specified with minTime and maxTime.");
68 "List of background (collision) files (wildcards not allowed - "
69 "use python glob.glob() to expand to list of files)");
71 "Time window lower edge in nano seconds", -1000.0);
73 "Time window upper edge in nano seconds", 800.0);
75 "Overall factor to scale rates of backgrounds", 1.0);
77 "Factors to scale rates of backgrounds. "
81 "Detector components to be included, empty list means all components",
84 "if true wrap around events passing time window upper edge",
true);
86 "Time window lower edge for ECL in nano seconds", -17600.0);
88 "Time window upper edge for ECL in nano seconds", 8500.0);
90 "Time window lower edge for PXD in nano seconds", -10000.0);
92 "Time window upper edge for PXD in nano seconds", 10000.0);
95 "If true also add the BeamBackHits collection for the selected "
96 "subdetectors to the output file",
false);
99 "maximal deposited energy of ECLHit to accept BG event for mixing"
100 "(0 means accept all events)", 1.0);
103 "file cache size in Mbytes. If negative, use root default", 0);
139 if (!components.empty()) {
141 for (
unsigned i = 0; i < components.size(); ++i) str = str +
" " + components[i];
142 B2WARNING(
"Unknown components:" << str);
150 if (TString(file.c_str()).Contains(
"*")) {
151 B2ERROR(file <<
": wildcards are not allowed");
156 TFile* f = TFile::Open(file.c_str(),
"READ");
158 B2ERROR(file <<
": file not found");
162 B2ERROR(file <<
": can't open file");
167 TChain persistent(
"persistent");
168 int nFiles = persistent.Add(file.c_str());
170 B2ERROR(file <<
": no such files");
173 if (persistent.GetEntries() == 0) {
174 B2ERROR(file <<
": tree 'persistent' has no entries");
178 TObject* bkgMetaData = 0;
179 TBranch* branchBMD = persistent.GetBranch(
"BackgroundMetaData");
181 B2ERROR(file <<
": branch 'BackgroundMetaData' not found");
184 branchBMD->SetAddress(&bkgMetaData);
186 std::vector<BackgroundMetaData::BG_TAG> tags;
187 std::vector<std::string> types;
188 std::vector<BackgroundMetaData::EFileType> fileTypes;
190 for (
unsigned k = 0; k < persistent.GetEntries(); k++) {
191 persistent.GetEntry(k);
199 B2ERROR(file <<
": invalid realTime: " << realTime);
202 for (
unsigned i = 1; i < tags.size(); ++i) {
203 if (tags[i] != tags[0]) {
204 B2ERROR(file <<
": files with mixed background types not supported");
208 for (
unsigned i = 1; i < fileTypes.size(); ++i) {
209 if (fileTypes[i] != fileTypes[0]) {
210 B2ERROR(file <<
": files with mixed file types not supported");
215 appendSample(tags[0], types[0], file, realTime, fileTypes[0]);
223 std::string type = std::get<0>(scaleFactor);
225 B2ERROR(
"Unknown beam background type found in 'scaleFactors': " << type <<
"\n"
229 bkg.scaleFactor *= std::get<1>(scaleFactor);
238 bkg.tree.reset(
new TChain(
"tree"));
239 for (
unsigned i = 0; i < bkg.fileNames.size(); ++i) {
240 bkg.numFiles += bkg.tree->Add(bkg.fileNames[i].c_str());
243 bkg.numEvents = bkg.tree->GetEntries();
244 bkg.rate = bkg.numEvents / bkg.realTime * bkg.scaleFactor;
248 if (
m_PXD and bkg.tree->GetBranch(
"PXDSimHits"))
249 bkg.tree->SetBranchAddress(
"PXDSimHits", &
m_simHits.
PXD);
250 if (
m_SVD and bkg.tree->GetBranch(
"SVDSimHits"))
251 bkg.tree->SetBranchAddress(
"SVDSimHits", &
m_simHits.
SVD);
252 if (
m_CDC and bkg.tree->GetBranch(
"CDCSimHits"))
253 bkg.tree->SetBranchAddress(
"CDCSimHits", &
m_simHits.
CDC);
254 if (
m_TOP and bkg.tree->GetBranch(
"TOPSimHits"))
255 bkg.tree->SetBranchAddress(
"TOPSimHits", &
m_simHits.
TOP);
256 if (
m_ARICH and bkg.tree->GetBranch(
"ARICHSimHits"))
258 if (
m_ECL and bkg.tree->GetBranch(
"ECLHits"))
260 if (
m_KLM and bkg.tree->GetBranch(
"KLMSimHits"))
261 bkg.tree->SetBranchAddress(
"KLMSimHits", &
m_simHits.
KLM);
267 std::string unit(
" ns");
268 double realTime = bkg.realTime;
269 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" us";}
270 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" ms";}
271 if (realTime >= 1000.0) {realTime /= 1000.0; unit =
" s";}
273 B2INFO(
"BeamBkgMixer: " << bkg.type <<
274 " tag=" << bkg.tag <<
275 " files=" << bkg.numFiles <<
276 " events=" << bkg.numEvents <<
277 " realTime=" << realTime << unit <<
278 " scaleFactor=" << bkg.scaleFactor <<
279 " rate=" << bkg.rate * 1000 <<
" MHz");
328 descr.type = bkg.type;
329 descr.fileType = bkg.fileType;
330 descr.fileNames = bkg.fileNames;
331 descr.realTime = bkg.realTime;
332 descr.numEvents = bkg.numEvents;
333 descr.scaleFactor = bkg.scaleFactor;
334 descr.rate = bkg.rate;
336 bkgInfo->appendBackgroundDescr(descr);
362 int nev = gRandom->Poisson(mean);
364 for (
int iev = 0; iev < nev; iev++) {
366 bkg.tree->GetEntry(bkg.eventCount);
380 std::string message =
"BeamBkgMixer: event " + to_string(bkg.eventCount)
381 +
" of " + bkg.type +
" rejected due to large energy deposit in ECL";
385 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL");
387 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL "
388 <<
"(message will be suppressed now)");
393 if (bkg.eventCount >= bkg.numEvents) {
395 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be re-used";
397 if (
m_reused[message] == 1) B2INFO(message);
398 bkgInfo->incrementReusedCounter(bkg.index);
409 int nev = gRandom->Poisson(mean);
411 for (
int iev = 0; iev < nev; iev++) {
414 bkg.tree->GetEntry(bkg.eventCount);
427 std::string message =
"BeamBkgMixer: event " + to_string(bkg.eventCount)
428 +
" of " + bkg.type +
" rejected due to large energy deposit in ECL";
432 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL");
434 B2INFO(
"BeamBkgMixer: event rejected due to large energy deposit in ECL "
435 <<
"(message will be suppressed now)");
440 if (bkg.eventCount >= bkg.numEvents) {
442 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be re-used";
444 if (
m_reused[message] == 1) B2INFO(message);
445 bkgInfo->incrementReusedCounter(bkg.index);
457 int nev = gRandom->Poisson(mean);
459 for (
int iev = 0; iev < nev; iev++) {
462 bkg.tree->GetEntry(bkg.eventCount);
474 if (bkg.eventCount >= bkg.numEvents) {
476 std::string message =
"BeamBkgMixer: events of " + bkg.type +
" will be re-used";
478 if (
m_reused[message] == 1) B2INFO(message);
479 bkgInfo->incrementReusedCounter(bkg.index);
495 B2INFO(
"BeamBkgMixer - reused samples:");
496 for (
const auto& message :
m_reused) {
497 B2INFO(message.first <<
"(occured " << message.second <<
" times)");
499 B2INFO(
"BeamBkgMixer - rejected events:");
501 B2INFO(message.first <<
"(occured " << message.second <<
" times)");
512 const std::string& component)
515 auto iterator = std::find(components.begin(), components.end(), component);
516 if (iterator != components.end()) {
517 components.erase(iterator);
525 const std::string& type,
526 const std::string& fileName,
531 if (tag == bkg.tag and fileType == bkg.fileType) {
532 bkg.fileNames.push_back(fileName);
533 bkg.realTime += realTime;
537 std::string ftype = type;
548 if (!cloneArrayECL)
return true;
551 int numEntries = cloneArrayECL->GetEntriesFast();
552 for (
int i = 0; i < numEntries; i++) {
553 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 suppresing "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.