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