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