12 #include <background/modules/BeamBkgHitRateMonitor/BeamBkgHitRateMonitorModule.h>
13 #include <background/modules/BeamBkgHitRateMonitor/PXDHitRateCounter.h>
14 #include <background/modules/BeamBkgHitRateMonitor/SVDHitRateCounter.h>
15 #include <background/modules/BeamBkgHitRateMonitor/CDCHitRateCounter.h>
16 #include <background/modules/BeamBkgHitRateMonitor/TOPHitRateCounter.h>
17 #include <background/modules/BeamBkgHitRateMonitor/ARICHHitRateCounter.h>
18 #include <background/modules/BeamBkgHitRateMonitor/ECLHitRateCounter.h>
19 #include <background/modules/BeamBkgHitRateMonitor/KLMHitRateCounter.h>
22 #include <framework/logging/Logger.h>
24 #include <framework/io/RootIOUtilities.h>
25 #include <framework/core/RandomNumbers.h>
26 #include <framework/core/Environment.h>
27 #include <framework/core/ModuleParam.templateDetails.h>
28 #include <framework/database/Database.h>
29 #include <framework/utilities/EnvironmentVariables.h>
31 #include <boost/python.hpp>
32 #include <boost/optional.hpp>
33 #include <boost/filesystem/path.hpp>
34 #include <boost/filesystem/operations.hpp>
35 #include <boost/algorithm/string.hpp>
59 setDescription(
"A module for off-line monitoring of beam background hit rates.");
66 addParam(
"outputFileName", m_outputFileName,
"output file name",
67 string(
"beamBkgHitRates.root"));
68 addParam(
"treeName", m_treeName,
"output tree name",
70 m_trgTypes.push_back(TRGSummary::TTYP_DPHY);
71 m_trgTypes.push_back(TRGSummary::TTYP_RAND);
72 addParam(
"trgTypes", m_trgTypes,
73 "trigger types for event selection (see TRGSummary.h for definitions). "
74 "Empty list means all trigger types.",
76 addParam(
"writeEmptyTimeStamps", m_writeEmptyTimeStamps,
77 "if true, write to ntuple also empty time stamps",
false);
78 addParam(
"topTimeOffset", m_topTimeOffset,
79 "TOP: time offset of hits (to be subtracted) [ns]", 25.0);
80 addParam(
"topTimeWindow", m_topTimeWindow,
81 "TOP: time window in which to count hits [ns]", 100.0);
82 addParam(
"svdShaperDigitsName", m_svdShaperDigitsName,
83 "SVDShaperDigits collection name",
string(
""));
84 addParam(
"svdThrCharge", m_svdThrCharge,
85 "Energy cur on SVD Cluster charge in electrons", 15000.);
86 addParam(
"svdIgnoreHotStripsPayload", m_svdIgnoreHotStripsPayload,
87 "If true, also SVD hot strips are counted as active",
false);
88 addParam(
"svdIgnoreMaskedStripsPayload", m_svdIgnoreMaskedStripsPayload,
89 "If true, also SVD FADC-masked strips are counted as active",
false);
90 addParam(
"additionalDataDescription", m_additionalDataDescription,
91 "Additional dictionary of "
92 "name->value pairs to be added to the file metadata to describe the data",
93 m_additionalDataDescription);
94 addParam(
"cdcTimeWindowLowerEdgeSmallCell", m_cdcTimeWindowLowerEdgeSmallCell,
95 "CDC: lower edge of the time window for small cells [tdc count = ns]",
97 addParam(
"cdcTimeWindowUpperEdgeSmallCell", m_cdcTimeWindowUpperEdgeSmallCell,
98 "CDC: upper edge of the time window for small cells [tdc count = ns]",
100 addParam(
"cdcTimeWindowLowerEdgeNormalCell", m_cdcTimeWindowLowerEdgeNormalCell,
101 "CDC: lower edge of the time window for normal cells [tdc count = ns]",
103 addParam(
"cdcTimeWindowUpperEdgeNormalCell", m_cdcTimeWindowUpperEdgeNormalCell,
104 "CDC: upper edge of the time window for normal cells [tdc count = ns]",
106 addParam(
"cdcEnableBadWireTreatment", m_cdcEnableBadWireTreatment,
107 "CDC: flag to enable the bad wire treatment",
true);
108 addParam(
"cdcEnableBackgroundHitFilter", m_cdcEnableBackgroundHitFilter,
109 "CDC: flag to enable the CDC background hit (crosstakl, noise) filter",
true);
110 addParam(
"cdcEnableMarkBackgroundHit", m_cdcEnableMarkBackgroundHit,
111 "CDC: flag to enable to mark background flag on CDCHit (set 0x100 bit for CDCHit::m_status).",
false);
115 BeamBkgHitRateMonitorModule::~BeamBkgHitRateMonitorModule()
117 for (
auto& monitor : m_monitors) {
118 if (monitor)
delete monitor;
122 void BeamBkgHitRateMonitorModule::initialize()
125 m_eventMetaData.isRequired();
126 if (m_trgTypes.empty()) {
127 m_trgSummary.isOptional();
129 m_trgSummary.isRequired();
131 m_fileMetaData.isOptional();
135 m_monitors.push_back(pxd);
137 m_svdIgnoreHotStripsPayload,
138 m_svdIgnoreMaskedStripsPayload);
139 m_monitors.push_back(svd);
141 m_cdcTimeWindowLowerEdgeNormalCell, m_cdcTimeWindowUpperEdgeNormalCell,
142 m_cdcEnableBadWireTreatment, m_cdcEnableBackgroundHitFilter,
143 m_cdcEnableMarkBackgroundHit);
144 m_monitors.push_back(cdc);
146 m_monitors.push_back(top);
148 m_monitors.push_back(arich);
150 m_monitors.push_back(ecl);
152 m_monitors.push_back(klm);
155 m_file = TFile::Open(m_outputFileName.c_str(),
"RECREATE");
157 B2FATAL(
"Cannot open output file '" << m_outputFileName <<
"' for writing");
161 m_tree =
new TTree(m_treeName.c_str(),
"hit rates of selected events");
164 m_persistent =
new TTree(
"persistent",
"persistent data");
165 m_persistent->Branch(
"FileMetaData", &m_outputFileMetaData);
168 m_tree->Branch(
"run", &m_run,
"run/I");
169 m_tree->Branch(
"numEvents", &m_numEvents,
"numEvents/I");
170 m_tree->Branch(
"timeStamp", &m_timeStamp,
"timeStamp/i");
171 m_tree->Branch(
"time", &m_time,
"time/I");
172 for (
auto& monitor : m_monitors) {
173 monitor->initialize(m_tree);
177 m_trgAll =
new TH1F(
"trgAll",
"trigger types of all events", 16, -0.5, 15.5);
178 m_trgAll->SetXTitle(
"type of trigger timing source");
179 m_trgSel =
new TH1F(
"trgSel",
"trigger types of selected events", 16, -0.5, 15.5);
180 m_trgSel->SetXTitle(
"type of trigger timing source");
184 void BeamBkgHitRateMonitorModule::beginRun()
187 for (
auto& monitor : m_monitors) {
190 m_eventCounts.clear();
193 m_numEventsSelected = 0;
194 m_trgTypesCount.clear();
197 m_run = m_eventMetaData->getRun();
200 unsigned utime = m_eventMetaData->getTime() / 1000000000;
201 m_utimeFirst = utime;
203 m_utimeMax = utime + 1;
207 void BeamBkgHitRateMonitorModule::event()
210 unsigned utime = m_eventMetaData->getTime() / 1000000000;
211 m_utimeMin = std::min(m_utimeMin, utime);
212 m_utimeMax = std::max(m_utimeMax, utime + 1);
215 collectFileMetaData();
218 if (not isEventSelected())
return;
219 m_numEventsSelected++;
222 for (
auto& monitor : m_monitors) {
223 monitor->accumulate(utime);
225 m_eventCounts[utime] += 1;
229 void BeamBkgHitRateMonitorModule::endRun()
232 for (
unsigned utime = m_utimeMin; utime < m_utimeMax; utime++) {
233 if (not m_writeEmptyTimeStamps) {
234 if (m_eventCounts.find(utime) == m_eventCounts.end())
continue;
236 m_numEvents = m_eventCounts[utime];
238 m_time = utime - m_utimeMin;
239 for (
auto& monitor : m_monitors) {
240 monitor->normalize(utime);
246 m_allEventsSelected += m_numEventsSelected;
250 for (
const auto& trgType : m_trgTypesCount) {
251 trigs +=
" trigger type " + std::to_string(trgType.first) +
": " +
252 std::to_string(trgType.second) +
" events\n";
254 B2INFO(
"Run " << m_run <<
": " << m_numEventsSelected
255 <<
" events selected for beam background hit rate monitoring.\n"
257 <<
LogVar(
"first event utime ", m_utimeMin)
258 <<
LogVar(
"start utime ", m_utimeMin)
259 <<
LogVar(
"stop utime ", m_utimeMax)
260 <<
LogVar(
"duration [seconds]", m_utimeMax - m_utimeMin)
264 void BeamBkgHitRateMonitorModule::terminate()
267 m_persistent->Fill();
274 B2INFO(
"Output file: " << m_outputFileName);
277 bool BeamBkgHitRateMonitorModule::isEventSelected()
279 auto trgType = TRGSummary::TTYP_NONE;
280 if (m_trgSummary.isValid()) trgType = m_trgSummary->getTimType();
281 m_trgAll->Fill(trgType);
283 if (m_trgTypes.empty()) {
284 m_trgTypesCount[trgType] += 1;
285 m_trgSel->Fill(trgType);
288 for (
auto type : m_trgTypes) {
289 if (trgType == type) {
290 m_trgTypesCount[trgType] += 1;
291 m_trgSel->Fill(trgType);
299 void BeamBkgHitRateMonitorModule::collectFileMetaData()
302 if (m_fileMetaData.isValid()) {
303 std::string lfn = m_fileMetaData->getLfn();
304 if (not lfn.empty() and (m_parentLfns.empty() or (m_parentLfns.back() != lfn))) {
305 m_parentLfns.push_back(lfn);
310 unsigned long experiment = m_eventMetaData->getExperiment();
311 unsigned long run = m_eventMetaData->getRun();
312 unsigned long event = m_eventMetaData->getEvent();
313 if (m_experimentLow > m_experimentHigh) {
314 m_experimentLow = m_experimentHigh = experiment;
315 m_runLow = m_runHigh = run;
316 m_eventLow = m_eventHigh = event;
318 if ((experiment < m_experimentLow) or ((experiment == m_experimentLow) and ((run < m_runLow) or ((run == m_runLow)
319 and (event < m_eventLow))))) {
320 m_experimentLow = experiment;
324 if ((experiment > m_experimentHigh) or ((experiment == m_experimentHigh) and ((run > m_runHigh) or ((run == m_runHigh)
325 and (event > m_eventHigh))))) {
326 m_experimentHigh = experiment;
335 void BeamBkgHitRateMonitorModule::setFileMetaData()
338 if (m_fileMetaData.isValid() and not m_fileMetaData->isMC()) {
339 m_outputFileMetaData.declareRealData();
342 m_outputFileMetaData.setNEvents(m_allEventsSelected);
344 if (m_experimentLow > m_experimentHigh) {
346 m_outputFileMetaData.setLow(-1, -1, 0);
347 m_outputFileMetaData.setHigh(-1, -1, 0);
349 m_outputFileMetaData.setLow(m_experimentLow, m_runLow, m_eventLow);
350 m_outputFileMetaData.setHigh(m_experimentHigh, m_runHigh, m_eventHigh);
353 m_outputFileMetaData.setParents(m_parentLfns);
354 RootIOUtilities::setCreationData(m_outputFileMetaData);
355 m_outputFileMetaData.setRandomSeed(RandomNumbers::getSeed());
356 m_outputFileMetaData.setSteering(Environment::Instance().getSteering());
357 auto mcEvents = Environment::Instance().getNumberOfMCEvents();
358 m_outputFileMetaData.setMcEvents(mcEvents);
359 m_outputFileMetaData.setDatabaseGlobalTag(Database::Instance().getGlobalTags());
361 for (
const auto& item : m_additionalDataDescription) {
362 m_outputFileMetaData.setDataDescription(item.first, item.second);
365 std::string lfn = m_file->GetName();
366 lfn = boost::filesystem::absolute(lfn, boost::filesystem::initial_path()).string();
367 std::string format = EnvironmentVariables::get(
"BELLE2_LFN_FORMATSTRING",
"");
368 if (!format.empty()) {
369 auto format_filename = boost::python::import(
"B2Tools.format").attr(
"format_filename");
370 lfn = boost::python::extract<std::string>(format_filename(format, m_outputFileName, m_outputFileMetaData.getJsonStr()));
372 m_outputFileMetaData.setLfn(lfn);