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