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(
"additionalDataDescription", m_additionalDataDescription,
87 "Additional dictionary of "
88 "name->value pairs to be added to the file metadata to describe the data",
89 m_additionalDataDescription);
90 addParam(
"cdcTimeWindowLowerEdgeSmallCell", m_cdcTimeWindowLowerEdgeSmallCell,
91 "CDC: lower edge of the time window for small cells [tdc count = ns]",
93 addParam(
"cdcTimeWindowUpperEdgeSmallCell", m_cdcTimeWindowUpperEdgeSmallCell,
94 "CDC: upper edge of the time window for small cells [tdc count = ns]",
96 addParam(
"cdcTimeWindowLowerEdgeNormalCell", m_cdcTimeWindowLowerEdgeNormalCell,
97 "CDC: lower edge of the time window for normal cells [tdc count = ns]",
99 addParam(
"cdcTimeWindowUpperEdgeNormalCell", m_cdcTimeWindowUpperEdgeNormalCell,
100 "CDC: upper edge of the time window for normal cells [tdc count = ns]",
102 addParam(
"cdcEnableBadWireTreatment", m_cdcEnableBadWireTreatment,
103 "CDC: flag to enable the bad wire treatment",
true);
104 addParam(
"cdcEnableBackgroundHitFilter", m_cdcEnableBackgroundHitFilter,
105 "CDC: flag to enable the CDC background hit (crosstakl, noise) filter",
true);
106 addParam(
"cdcEnableMarkBackgroundHit", m_cdcEnableMarkBackgroundHit,
107 "CDC: flag to enable to mark background flag on CDCHit (set 0x100 bit for CDCHit::m_status).",
false);
111 BeamBkgHitRateMonitorModule::~BeamBkgHitRateMonitorModule()
113 for (
auto& monitor : m_monitors) {
114 if (monitor)
delete monitor;
118 void BeamBkgHitRateMonitorModule::initialize()
121 m_eventMetaData.isRequired();
122 if (m_trgTypes.empty()) {
123 m_trgSummary.isOptional();
125 m_trgSummary.isRequired();
127 m_fileMetaData.isOptional();
131 m_monitors.push_back(pxd);
133 m_monitors.push_back(svd);
135 m_cdcTimeWindowLowerEdgeNormalCell, m_cdcTimeWindowUpperEdgeNormalCell,
136 m_cdcEnableBadWireTreatment, m_cdcEnableBackgroundHitFilter,
137 m_cdcEnableMarkBackgroundHit);
138 m_monitors.push_back(cdc);
140 m_monitors.push_back(top);
142 m_monitors.push_back(arich);
144 m_monitors.push_back(ecl);
146 m_monitors.push_back(klm);
149 m_file = TFile::Open(m_outputFileName.c_str(),
"RECREATE");
151 B2FATAL(
"Cannot open output file '" << m_outputFileName <<
"' for writing");
155 m_tree =
new TTree(m_treeName.c_str(),
"hit rates of selected events");
158 m_persistent =
new TTree(
"persistent",
"persistent data");
159 m_persistent->Branch(
"FileMetaData", &m_outputFileMetaData);
162 m_tree->Branch(
"run", &m_run,
"run/I");
163 m_tree->Branch(
"numEvents", &m_numEvents,
"numEvents/I");
164 m_tree->Branch(
"timeStamp", &m_timeStamp,
"timeStamp/i");
165 m_tree->Branch(
"time", &m_time,
"time/I");
166 for (
auto& monitor : m_monitors) {
167 monitor->initialize(m_tree);
171 m_trgAll =
new TH1F(
"trgAll",
"trigger types of all events", 16, -0.5, 15.5);
172 m_trgAll->SetXTitle(
"type of trigger timing source");
173 m_trgSel =
new TH1F(
"trgSel",
"trigger types of selected events", 16, -0.5, 15.5);
174 m_trgSel->SetXTitle(
"type of trigger timing source");
178 void BeamBkgHitRateMonitorModule::beginRun()
181 for (
auto& monitor : m_monitors) {
184 m_eventCounts.clear();
187 m_numEventsSelected = 0;
188 m_trgTypesCount.clear();
191 m_run = m_eventMetaData->getRun();
194 unsigned utime = m_eventMetaData->getTime() / 1000000000;
195 m_utimeFirst = utime;
197 m_utimeMax = utime + 1;
201 void BeamBkgHitRateMonitorModule::event()
204 unsigned utime = m_eventMetaData->getTime() / 1000000000;
205 m_utimeMin = std::min(m_utimeMin, utime);
206 m_utimeMax = std::max(m_utimeMax, utime + 1);
209 collectFileMetaData();
212 if (not isEventSelected())
return;
213 m_numEventsSelected++;
216 for (
auto& monitor : m_monitors) {
217 monitor->accumulate(utime);
219 m_eventCounts[utime] += 1;
223 void BeamBkgHitRateMonitorModule::endRun()
226 for (
unsigned utime = m_utimeMin; utime < m_utimeMax; utime++) {
227 if (not m_writeEmptyTimeStamps) {
228 if (m_eventCounts.find(utime) == m_eventCounts.end())
continue;
230 m_numEvents = m_eventCounts[utime];
232 m_time = utime - m_utimeMin;
233 for (
auto& monitor : m_monitors) {
234 monitor->normalize(utime);
240 m_allEventsSelected += m_numEventsSelected;
244 for (
const auto& trgType : m_trgTypesCount) {
245 trigs +=
" trigger type " + std::to_string(trgType.first) +
": " +
246 std::to_string(trgType.second) +
" events\n";
248 B2INFO(
"Run " << m_run <<
": " << m_numEventsSelected
249 <<
" events selected for beam background hit rate monitoring.\n"
251 <<
LogVar(
"first event utime ", m_utimeMin)
252 <<
LogVar(
"start utime ", m_utimeMin)
253 <<
LogVar(
"stop utime ", m_utimeMax)
254 <<
LogVar(
"duration [seconds]", m_utimeMax - m_utimeMin)
258 void BeamBkgHitRateMonitorModule::terminate()
261 m_persistent->Fill();
268 B2INFO(
"Output file: " << m_outputFileName);
271 bool BeamBkgHitRateMonitorModule::isEventSelected()
273 auto trgType = TRGSummary::TTYP_NONE;
274 if (m_trgSummary.isValid()) trgType = m_trgSummary->getTimType();
275 m_trgAll->Fill(trgType);
277 if (m_trgTypes.empty()) {
278 m_trgTypesCount[trgType] += 1;
279 m_trgSel->Fill(trgType);
282 for (
auto type : m_trgTypes) {
283 if (trgType == type) {
284 m_trgTypesCount[trgType] += 1;
285 m_trgSel->Fill(trgType);
293 void BeamBkgHitRateMonitorModule::collectFileMetaData()
296 if (m_fileMetaData.isValid()) {
297 std::string lfn = m_fileMetaData->getLfn();
298 if (not lfn.empty() and (m_parentLfns.empty() or (m_parentLfns.back() != lfn))) {
299 m_parentLfns.push_back(lfn);
304 unsigned long experiment = m_eventMetaData->getExperiment();
305 unsigned long run = m_eventMetaData->getRun();
306 unsigned long event = m_eventMetaData->getEvent();
307 if (m_experimentLow > m_experimentHigh) {
308 m_experimentLow = m_experimentHigh = experiment;
309 m_runLow = m_runHigh = run;
310 m_eventLow = m_eventHigh = event;
312 if ((experiment < m_experimentLow) or ((experiment == m_experimentLow) and ((run < m_runLow) or ((run == m_runLow)
313 and (event < m_eventLow))))) {
314 m_experimentLow = experiment;
318 if ((experiment > m_experimentHigh) or ((experiment == m_experimentHigh) and ((run > m_runHigh) or ((run == m_runHigh)
319 and (event > m_eventHigh))))) {
320 m_experimentHigh = experiment;
329 void BeamBkgHitRateMonitorModule::setFileMetaData()
332 if (m_fileMetaData.isValid() and not m_fileMetaData->isMC()) {
333 m_outputFileMetaData.declareRealData();
336 m_outputFileMetaData.setNEvents(m_allEventsSelected);
338 if (m_experimentLow > m_experimentHigh) {
340 m_outputFileMetaData.setLow(-1, -1, 0);
341 m_outputFileMetaData.setHigh(-1, -1, 0);
343 m_outputFileMetaData.setLow(m_experimentLow, m_runLow, m_eventLow);
344 m_outputFileMetaData.setHigh(m_experimentHigh, m_runHigh, m_eventHigh);
347 m_outputFileMetaData.setParents(m_parentLfns);
348 RootIOUtilities::setCreationData(m_outputFileMetaData);
349 m_outputFileMetaData.setRandomSeed(RandomNumbers::getSeed());
350 m_outputFileMetaData.setSteering(Environment::Instance().getSteering());
351 auto mcEvents = Environment::Instance().getNumberOfMCEvents();
352 m_outputFileMetaData.setMcEvents(mcEvents);
353 m_outputFileMetaData.setDatabaseGlobalTag(Database::Instance().getGlobalTags());
355 for (
const auto& item : m_additionalDataDescription) {
356 m_outputFileMetaData.setDataDescription(item.first, item.second);
359 std::string lfn = m_file->GetName();
360 lfn = boost::filesystem::absolute(lfn, boost::filesystem::initial_path()).string();
361 std::string format = EnvironmentVariables::get(
"BELLE2_LFN_FORMATSTRING",
"");
362 if (!format.empty()) {
363 auto format_filename = boost::python::import(
"B2Tools.format").attr(
"format_filename");
364 lfn = boost::python::extract<std::string>(format_filename(format, m_outputFileName, m_outputFileMetaData.getJsonStr()));
366 m_outputFileMetaData.setLfn(lfn);