Belle II Software  release-05-02-19
CaTestModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: tadeas *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <calibration/example_caf_lib/modules/CaTest/CaTestModule.h>
12 
13 #include <string>
14 
15 #include <TTree.h>
16 #include <TH1F.h>
17 #include <TRandom.h>
18 
19 #include <alignment/dataobjects/MilleData.h>
20 #include <framework/pcore/ProcHandler.h>
21 
22 using namespace Belle2;
23 using namespace std;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_MODULE(CaTest)
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
35 {
36  // Set module properties
37  setDescription("Test Module for saving big data in CAF");
38  // Parameter definitions
39  addParam("entriesPerEvent", m_entriesPerEvent,
40  "Number of entries that we fill into the saved tree per event. As we increase this we start storing larger amonuts of data in a smaller number of events to test the limits.",
41  int(10));
42  addParam("spread", m_spread,
43  "Spread of gaussian (mean=42) filling of test histogram (range=<0,100>) - probability of algo iterations depend on it", int(17.));
44 }
45 
47 {
48  describeProcess("CaTest::prepare");
49  std::string objectName = "MyTree";
50  // Data object creation --------------------------------------------------
51  TTree* tree = new TTree(objectName.c_str(), "");
52  tree->Branch<int>("event", &m_evt);
53  tree->Branch<int>("run", &m_run);
54  tree->Branch<int>("exp", &m_exp);
55  tree->Branch<double>("hitX", &m_hitX);
56  tree->Branch<double>("hitY", &m_hitY);
57  tree->Branch<double>("hitZ", &m_hitZ);
58  tree->Branch<double>("trackX", &m_trackX);
59  tree->Branch<double>("trackY", &m_trackY);
60  tree->Branch<double>("trackZ", &m_trackZ);
61  tree->Branch<double>("chisq", &m_chisq);
62  tree->Branch<double>("pvalue", &m_pvalue);
63  tree->Branch<double>("dof", &m_dof);
64 
65  // We register the objects so that our framework knows about them.
66  // Don't try and hold onto the pointers or fill these objects directly
67  // Use the getObjectPtr functions to access collector objects
68  registerObject<TTree>(objectName, tree);
69 
70  auto hist = new TH1F("histo", "Test histogram, which mean value should be found by test calibration algo", 100, 0., 100.);
71  registerObject<TH1F>("MyHisto", hist);
72 
73  auto mille = new MilleData();
74  registerObject<MilleData>("test_mille", mille);
75 }
76 
78 {
79  describeProcess("CaTest::inDefineHisto()");
80 }
81 
83 {
84  describeProcess("CaTest::startRun()");
85 }
86 
88 {
89  describeProcess("CaTest::closeRun()");
90  // We close the file at end of run, producing
91  // one file per run (and process id) which is more
92  // convenient than one large binary block.
93  auto mille = getObjectPtr<MilleData>("test_mille");
94  if (mille->isOpen()) {
95  for (auto& fileName : mille->getFiles()) {
96  B2DEBUG(100, "Stored Mille binary file: " << fileName);
97  }
98  mille->close();
99  }
100  //getObjectPtr<TT/ree>("MyTree")->GetDirectory()->ls();
101 }
102 
104 {
105  describeProcess("CaTest::collect()");
106  m_evt = m_emdata->getEvent();
107  m_run = m_emdata->getRun();
108  m_exp = m_emdata->getExperiment();
109 
110  std::string objectName = "MyTree";
111  auto tree = getObjectPtr<TTree>(objectName);
112  auto hist = getObjectPtr<TH1F>("MyHisto");
113 
114  for (int i = 0; i < m_entriesPerEvent; ++i) {
115  m_hitX = gRandom->Gaus();
116  m_hitY = gRandom->Gaus();
117  m_hitZ = gRandom->Gaus();
118  m_trackX = gRandom->Gaus();
119  m_trackY = gRandom->Gaus();
120  m_trackZ = gRandom->Gaus();
121  m_chisq = gRandom->Gaus();
122  m_pvalue = gRandom->Gaus();
123  m_dof = gRandom->Gaus();
124  tree->Fill();
125  hist->Fill(gRandom->Gaus(42., m_spread));
126  }
127 
128  auto mille = getObjectPtr<MilleData>("test_mille");
129  // Open new file on request (at start or after being closed)
130  if (!mille->isOpen()) {
131  string newFileName = to_string(m_exp) + "-" + to_string(m_run) + "-sevt-" + to_string(m_evt) + "-pid" + std::to_string(
132  ProcHandler::EvtProcID()) + ".mille";
133  B2INFO("Opening new binary file " << newFileName);
134  mille->open(newFileName);
135  }
136 }
137 
139 {
140  describeProcess("CaTest::finish()");
141 }
142 
143 void CaTestModule::describeProcess(const string& functionName)
144 {
145  B2DEBUG(100, "Running " + functionName + " function from a Process of type " + ProcHandler::getProcessName()
146  + "\nParallel Processing Used = " + to_string(ProcHandler::parallelProcessingUsed())
147  + "\nThis EvtProcID Id = " + to_string(ProcHandler::EvtProcID())
148  + "\nThe gDirectory is " + gDirectory->GetPath());
149 }
Belle2::CalibrationCollectorModule
Calibration collector module base class.
Definition: CalibrationCollectorModule.h:44
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::CaTestModule::finish
virtual void finish() override
Replacement for terminate(). Do anything you would normally do in terminate here.
Definition: CaTestModule.cc:138
Belle2::CaTestModule::prepare
virtual void prepare() override
Replacement for initialize(). Register calibration dataobjects here as well.
Definition: CaTestModule.cc:46
Belle2::CaTestModule::collect
virtual void collect() override
Replacement for event(). Fill you calibration data objects here.
Definition: CaTestModule.cc:103
Belle2::ProcHandler::getProcessName
static std::string getProcessName()
Get a name for this process.
Definition: ProcHandler.cc:247
Belle2::MilleData
Mergeable class holding list of so far opened mille binaries and providing the binaries.
Definition: MilleData.h:17
Belle2::CaTestModule::closeRun
virtual void closeRun() override
Replacement for endRun(). Do anything you would normally do in endRun here.
Definition: CaTestModule.cc:87
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CaTestModule::inDefineHisto
virtual void inDefineHisto() override
Runs during the defineHisto() function.
Definition: CaTestModule.cc:77
Belle2::CaTestModule
Testing module for collection of calibration data.
Definition: CaTestModule.h:31
Belle2::ProcHandler::EvtProcID
static int EvtProcID()
Return ID of the current process.
Definition: ProcHandler.cc:243
Belle2::ProcHandler::parallelProcessingUsed
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:221
Belle2::CaTestModule::startRun
virtual void startRun() override
Replacement for beginRun(). Do anything you would normally do in beginRun here.
Definition: CaTestModule.cc:82