Belle II Software development
CalibrationCollectorModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <calibration/CalibrationCollectorModule.h>
9#include <framework/pcore/ProcHandler.h>
10
11using namespace std;
12using namespace Belle2;
13using namespace Calibration;
14
17 m_dir(nullptr),
18 m_manager(),
19 m_runRange(nullptr),
20 m_expRunEvents(),
21 m_eventsCollectedInRun(nullptr)
22{
24 addParam("granularity", m_granularity,
25 "Granularity of data collection. Data is separated by runs (=run) or not separated at all (=all)", std::string("run"));
26
27 addParam("maxEventsPerRun", m_maxEventsPerRun,
28 "Maximum number of events that will be collected per run. Effectively the code in the collect() function is only "
29 "run for this number of events on each run. Then the collect() function is switched off until a new "
30 "run that hasn't collected the maximum yet begins. -1 is the default and means that the collector runs over all events."
31 "\n\nNote that this is useful for debugging and hard limiting the number of events passed to the collected. However "
32 "you should be limiting the collected data yourself! Check if your collected data object has enough entries for an algorithm "
33 "to complete and then stop filling. Controlling this limit via a module param is encouraged.", int(-1));
34
35 addParam("preScale", m_preScale,
36 "This controls the rate at which events are actually passed to the collect() function. An event passing through this module "
37 "will only have the collect() function run on it it passes a random selection scaled by this parameter i.e. For preScale=1.0 "
38 "all events are collected, but for preScale=0.5 only 50 percent will be. Since this is based on a random choice, you should set the "
39 "random seed to a fixed value if you want repeatable results.\n\n"
40 "Should be a float in range [0.0,1.0], default=1.0", float(1.0));
41
42}
43
45{
46 m_evtMetaData.isRequired();
47 REG_HISTOGRAM
48 prepare();
49}
50
51
53{
54 // Should we collect data this event based on the number collected in the run?
56 // If yes, does our preScale return true?
57 if (getPreScaleChoice()) {
58 collect();
59 // Since we collected, do we care about incrementing the number of events collected?
60 if (m_maxEventsPerRun > -1) {
61 (*m_eventsCollectedInRun) += 1;
62 // Now that we incremented, have we exceeded our maximum collected events in this run?
64 // If we have, we should skip collection until further notice
65 B2INFO("Reached maximum number of events processed by collector for this run ("
67 << " >= "
69 << "). Turning off collection.");
70 m_runCollectOnRun = false;
71 }
72 }
73 }
74 }
75}
76
78{
83 // Current (Exp,Run)
84 ExpRun expRun = make_pair(m_emd->getExperiment(), m_emd->getRun());
85 m_runRange->add(expRun.first, expRun.second);
86
87 // Do we care about the number of events collected in each (input data) ExpRun?
88 // If so, we want to create values for the events collected map
89 if (m_maxEventsPerRun > -1) {
90 // Do we have a count for this ExpRun yet? If not create one
91 auto i_eventsInExpRun = m_expRunEvents.find(expRun);
92 if (i_eventsInExpRun == m_expRunEvents.end()) {
93 m_expRunEvents[expRun] = 0;
94 }
95
96 // Set our pointer to the correct location for this ExpRun
98 // Want to reset our flag to start collection if necessary
100 B2INFO("New run has had less events than the maximum collected so far ("
102 << " < "
104 << "). Turning on collection.");
105 m_runCollectOnRun = true;
106 } else {
107 B2INFO("New run has had more events than the maximum collected so far ("
109 << " >= "
111 << "). Turning off collection.");
112 m_runCollectOnRun = false;
113 }
114 }
115 // Granularity=all removes data splitting by runs by setting
116 // always the same exp, run for calibration data objects
117 if (m_granularity == "all") {
118 m_expRun = { -1, -1};
119 } else {
120 m_expRun = expRun;
121 }
123 // Run the user's startRun() implementation if there is one
124 startRun();
125}
126
128{
130 m_dir = gDirectory->mkdir(getName().c_str(), "", true);
132 B2INFO("Saving output to TDirectory " << m_dir->GetPath());
133 B2DEBUG(100, "Creating directories for individual collector objects.");
135 m_runRange = new RunRange();
137 m_runRange->SetName(Calibration::RUN_RANGE_OBJ_NAME.c_str());
138 m_dir->Add(m_runRange);
139 }
141}
142
144{
145 closeRun();
146 // Moving between runs possibly creates new objects if getObjectPtr is called and granularity is run
147 // So we should write and clear the current memory objects.
148 if (m_granularity == "run") {
149 ExpRun expRun = make_pair(m_emd->getExperiment(), m_emd->getRun());
152 }
153}
154
156{
157 finish();
158 // actually this should be done by the write() called by HistoManager....
159
160 // Haven't written objects yet if collecting with granularity == all
161 // Write them now that everything is done.
162// if (m_granularity == "all") {
163// m_manager.writeCurrentObjects(m_expRun);
164// m_manager.clearCurrentObjects(m_expRun);
165// }
167}
void setDirectory(TDirectory *dir)
Change the directory that we will be using to find/store all our objects, we don't own it.
bool m_runCollectOnRun
Whether or not we will run the collect() at all this run, basically skips the event() function if fal...
virtual void startRun()
Replacement for beginRun(). Do anything you would normally do in beginRun here.
Calibration::ExpRun m_expRun
Current ExpRun for object retrieval (becomes -1,-1 for granularity=all)
CalibObjManager m_manager
Controls the creation, collection and access to calibration objects.
void endRun() final
Write the current collector objects to a file and clear their memory.
std::string m_granularity
Granularity of data collection = run|all(= no granularity, exp,run=-1,-1)
void initialize() final
Set up a default RunRange object in datastore and call prepare()
void beginRun() final
Reset the m_runCollectOnRun flag, if necessary, to begin collection again.
void event() final
Check current experiment and run and update if needed, fill into RunRange and collect()
RunRange * m_runRange
Overall list of runs processed.
virtual void prepare()
Replacement for initialize(). Register calibration dataobjects here as well.
TDirectory * m_dir
The top TDirectory that collector objects for this collector will be stored beneath.
virtual void closeRun()
Replacement for endRun(). Do anything you would normally do in endRun here.
StoreObjPtr< EventMetaData > m_evtMetaData
Required input for EventMetaData.
float m_preScale
Prescale module parameter, this fraction of events will have collect() run on them [0....
virtual void inDefineHisto()
Replacement for defineHisto(). Do anything you would normally do in defineHisto here.
void defineHisto() final
Runs due to HistoManager, allows us to discover the correct file.
int * m_eventsCollectedInRun
Will point at correct value in m_expRunEvents.
virtual void collect()
Replacement for event(). Fill you calibration data objects here.
virtual void finish()
Replacement for terminate(). Do anything you would normally do in terminate here.
StoreObjPtr< EventMetaData > m_emd
Current EventMetaData.
int m_maxEventsPerRun
Maximum number of events to be collected at the start of each run (-1 = no maximum)
CalibrationCollectorModule()
Constructor. Sets the default prefix for calibration dataobjects.
void terminate() final
Write the final objects to the file.
bool getPreScaleChoice()
I'm a little worried about floating point precision when comparing to 0.0 and 1.0 as special values.
std::map< Calibration::ExpRun, int > m_expRunEvents
How many events processed for each ExpRun so far, stops counting up once max is hit Only used/increme...
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
const std::string & getName() const
Returns the name of the module.
Definition: Module.h:187
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition: Module.h:83
static bool isWorkerProcess()
Return true if the process is a worker process.
Definition: ProcHandler.cc:230
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
Mergeable object holding (unique) set of (exp,run) pairs.
Definition: RunRange.h:25
void add(int exp, int run)
Add an experiment and run number to the set.
Definition: RunRange.h:58
void setGranularity(const std::string &granularity)
Set the m_granularity to an allowed value.
Definition: RunRange.h:100
void clearCurrentObjects(const Calibration::ExpRun &expRun)
Deletes all in-memory objects in the exprun directories for all the collector objects we know about.
void writeCurrentObjects(const Calibration::ExpRun &expRun)
For each templated object we know about, we find an in memory object for this exprun and write to the...
void deleteHeldObjects()
Clears the map of templated objects -> causing their destruction.
void createExpRunDirectories(Calibration::ExpRun &expRun) const
For each templated object, we create a new TDirectory for this exprun.
void createDirectories()
Each object gets its own TDirectory under the main manager directory to store its objects.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
Abstract base class for different kinds of events.
STL namespace.
Struct containing exp number and run number.
Definition: Splitter.h:51