Belle II Software development
RawSecMapRootInterface.h
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#pragma once
10
11#include <tracking/trackFindingVXD/sectorMapTools/FilterValueDataSet.h>
12
13#include <framework/logging/Logger.h>
14
15#include <framework/pcore/RootMergeable.h>
16#include <framework/datastore/StoreObjPtr.h>
17
18#include <framework/pcore/ProcHandler.h>
19
20#include <TFile.h>
21#include <TTree.h>
22#include <string>
23
24
25namespace Belle2 {
33 protected:
34
36 TFile* m_file;
37
39 std::string m_name;
40
43
46
49
52
53
54 public:
55
56
58 RawSecMapRootInterface(const std::string& mapName, const std::string& tag, const std::string& outputdir = "./") :
59 m_name(mapName),
60 m_tree2Hit((m_name + std::string("2Hit")), DataStore::c_Persistent),
61 m_data2Hit({}),
62 m_tree3Hit((m_name + std::string("3Hit")), DataStore::c_Persistent),
63 m_data3Hit({})
64
65 {
66 // the "create" option results in the file not being opened if already existing
67 std::string fileName = outputdir + "/" + mapName + "_" + tag + ".root";
68 m_file = new TFile((fileName).c_str(), "CREATE");
69 if (!m_file->IsOpen()) {
70 B2FATAL("File was not opened! File name: " << fileName << " (maybe it already exists!?)");
71 }
72 m_file->cd();
73 }
74
77
78
80 RawSecMapRootInterface(const RawSecMapRootInterface& rawSecMapInterFace) = delete;
83 m_file(nullptr),
84 m_name(std::move(other.m_name)),
85 m_tree2Hit(std::move(other.m_tree2Hit)),
86 m_data2Hit(std::move(other.m_data2Hit)),
87 m_tree3Hit(std::move(other.m_tree3Hit)),
88 m_data3Hit(std::move(other.m_data3Hit))
89 {
90 // the new object takes ownership of the file
91 m_file = other.m_file;
92 other.m_file = nullptr;
93 }
94
96 RawSecMapRootInterface& operator = (const RawSecMapRootInterface& rawSecMapInterFace) = delete;
97
98
100 void initialize2Hit(std::vector<std::string> filterNames)
101 {
102 B2DEBUG(20, "RawSecMapRootInterface::initialize2Hit: start - got " << filterNames.size() << " filters");
103 B2DEBUG(20, "and root file got size of: " << m_file->GetSize());
104 m_file->cd();
105
106 // preparing StoreObjPtr:
107 bool registered = m_tree2Hit.registerInDataStore((m_name + std::string("2Hit")), DataStore::c_ErrorIfAlreadyRegistered);
108 bool constructed = m_tree2Hit.construct((m_name + std::string("2Hit")).c_str(), "Raw data of two-hit-combinations for a sectorMap");
109 B2DEBUG(20, "RawSecMapRootInterface::initialize2Hit: isRegistered/isConstructed: " << registered << "/" << constructed);
110
111 // preparing data-mask for 2-hit-combinations:
113
114 m_tree2Hit->get().Branch("expNo", &(m_data2Hit.expNo));
115 m_tree2Hit->get().Branch("runNo", &(m_data2Hit.runNo));
116 m_tree2Hit->get().Branch("evtNo", &(m_data2Hit.evtNo));
117 m_tree2Hit->get().Branch("trackNo", &(m_data2Hit.trackNo));
118 m_tree2Hit->get().Branch("pdg", &(m_data2Hit.pdg));
119 m_tree2Hit->get().Branch("outerSecID", &(m_data2Hit.secIDs.outer));
120 m_tree2Hit->get().Branch("innerSecID", &(m_data2Hit.secIDs.inner));
121
122 B2DEBUG(20, "RawSecMapRootInterface::initialize2Hit: adding " << filterNames.size() << " filters as branches to ttree ");
123 for (auto& name : filterNames) {
124 double* valuePtr = m_data2Hit.getValuePtr(name);
125 if (valuePtr != nullptr) {
126 B2DEBUG(20, "RawSecMapRootInterface::initialize2Hit: adding now branch with name " << name);
127 m_tree2Hit->get().Branch(name.c_str(), valuePtr);
128 } else {
129 B2ERROR("RawSecMapRootInterface::initialize2Hit: filterName " << name <<
130 " not known! this is unintended behavior - skipping filter");
131 }
132
133 }
134 B2DEBUG(20, "RawSecMapRootInterface::initialize2Hit: nBranches/nEntries: " << m_tree2Hit->get().GetNbranches() << "/" <<
135 m_tree2Hit->get().GetEntries());
136 }
137
138
139
141 void initialize3Hit(std::vector<std::string> filterNames)
142 {
143 B2DEBUG(20, "RawSecMapRootInterface::initialize3Hit: start");
144 m_file->cd();
145
146 // preparing StoreObjPtr:
147 bool registered = m_tree3Hit.registerInDataStore((m_name + std::string("3Hit")), DataStore::c_ErrorIfAlreadyRegistered);
148 bool constructed = m_tree3Hit.construct((m_name + std::string("3Hit")).c_str(),
149 "Raw data of three-hit-combinations for a sectorMap");
150 B2DEBUG(20, "RawSecMapRootInterface::initialize3Hit: isRegistered/isConstructed: " << registered << "/" << constructed);
151
152 // preparing data-mask for 2-hit-combinations:
154
155 m_tree3Hit->get().Branch("expNo", &(m_data3Hit.expNo));
156 m_tree3Hit->get().Branch("runNo", &(m_data3Hit.runNo));
157 m_tree3Hit->get().Branch("evtNo", &(m_data3Hit.evtNo));
158 m_tree3Hit->get().Branch("trackNo", &(m_data3Hit.trackNo));
159 m_tree3Hit->get().Branch("pdg", &(m_data3Hit.pdg));
160 m_tree3Hit->get().Branch("outerSecID", &(m_data3Hit.secIDs.outer));
161 m_tree3Hit->get().Branch("centerSecID", &(m_data3Hit.secIDs.center));
162 m_tree3Hit->get().Branch("innerSecID", &(m_data3Hit.secIDs.inner));
163
164 B2DEBUG(20, "RawSecMapRootInterface::initialize3Hit: adding " << filterNames.size() << " filters as branches to ttree ");
165 for (auto& name : filterNames) {
166 double* valuePtr = m_data3Hit.getValuePtr(name);
167 if (valuePtr != nullptr) {
168 B2DEBUG(20, "RawSecMapRootInterface::initialize3Hit: adding now branch with name " << name);
169 m_tree3Hit->get().Branch(name.c_str(), valuePtr);
170 } else {
171 B2ERROR("RawSecMapRootInterface::initialize3Hit: filterName " << name <<
172 " not known! this is unintended behavior - skipping filter");
173 }
174
175 }
176 B2DEBUG(20, "RawSecMapRootInterface::initialize3Hit: nBranches/nEntries: " << m_tree3Hit->get().GetNbranches() << "/" <<
177 m_tree3Hit->get().GetEntries());
178 }
179
182
183
186
187
188
191 void fill2Hit()
192 {
193 if (!m_data2Hit.isValid()) {
194 B2ERROR("RawSecMapRootInterface::fill2Hit: attempt to fill invalid data in the tree! -> unintended behavior, data will not be filled.");
195 m_data2Hit.reset();
196 return;
197 }
198 m_tree2Hit->get().Fill();
199 m_data2Hit.reset();
200 }
201
202
205 void fill3Hit()
206 {
207 if (!m_data3Hit.isValid()) {
208 B2ERROR("RawSecMapRootInterface::fill3Hit: attempt to fill invalid data in the tree! -> unintended behavior, data will not be filled.");
209 m_data3Hit.reset();
210 return;
211 }
212 m_tree3Hit->get().Fill();
213 m_data3Hit.reset();
214 }
215
216
218 void write()
219 {
220 B2DEBUG(20, "RawSecMapRootInterface::write: start");
221 m_file->cd();
222
224 //use TFile you created in initialize()
225 m_tree2Hit->write(m_file);
226 m_tree3Hit->write(m_file);
227
228 }
229 }
230
231 };
233}
234
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
contains the relevant information needed for filling a TTree containing train-data for the secMap.
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:232
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
To be used as an interface to root-stuff.
FilterValueDataSet< SecIDPair > m_data2Hit
Mask for storing dataSets to be piped into 2hit-tree.
RawSecMapRootInterface(const std::string &mapName, const std::string &tag, const std::string &outputdir="./")
Constructor - prepares TTree.
RawSecMapRootInterface(RawSecMapRootInterface &&other)
but it may be moved
StoreObjPtr< RootMergeable< TTree > > m_tree3Hit
interface to the TTree storing three-hit-variables.
void fill3Hit()
fill three-hit-combinations in tree, triggers an Error if values not set yet.
void initialize3Hit(std::vector< std::string > filterNames)
initialize the RawSecMapRootInterface for three-hit-combinations (to be called in Module::initialize(...
FilterValueDataSet< SecIDTriplet > & get3HitDataSet()
returns a reference to the 3-hit-dataset so one can set the relevant values.
StoreObjPtr< RootMergeable< TTree > > m_tree2Hit
interface to the TTree storing two-hit-variables.
void fill2Hit()
fill two-hit-combinations in tree, triggers an Error if values not set yet.
TFile * m_file
a pointer to the file where the Tree shall be stored.
void write()
write all trees to file at end of processing.
void initialize2Hit(std::vector< std::string > filterNames)
initialize the RawSecMapRootInterface for two-hit-combinations (to be called in Module::initialize().
RawSecMapRootInterface(const RawSecMapRootInterface &rawSecMapInterFace)=delete
That class shall not be copied.
FilterValueDataSet< SecIDTriplet > m_data3Hit
Mask for storing dataSets to be piped into 3hit-tree.
FilterValueDataSet< SecIDPair > & get2HitDataSet()
returns a reference to the 2-hit-dataset so one can set the relevant values.
~RawSecMapRootInterface()
destructor deleting the rootFile.
RawSecMapRootInterface & operator=(const RawSecMapRootInterface &rawSecMapInterFace)=delete
That class shall not be copied.
std::string m_name
name of the StoreObjPtr.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Abstract base class for different kinds of events.
STL namespace.