EventAnalysis  1.2.2
RootPersistenceService.h
Go to the documentation of this file.
1 /*
2  * RootPersistenceService.h
3  *
4  * Created on: 12 May 2017
5  * Author: Nicola Mori
6  */
7 
8 #ifndef ROOTPERSISTENCESERVICE_H_
9 #define ROOTPERSISTENCESERVICE_H_
10 
12 
13 class TFile;
14 class TObject;
15 class TTree;
16 class TBranch;
17 class TClass;
18 class RootOutputStruct;
19 
20 #include <set>
21 #include <unordered_map>
22 
23 namespace EA {
24 
26 public:
27  RootPersistenceService(const std::string &name, const std::string &output);
28 
29  bool Connect() override;
30  bool Disconnect() override;
31 
32  bool BookEventObject(const std::string &objName, const std::string &objStore) override;
33  bool BookPassObject(const std::string &objName, const std::string &objStore) override;
34  bool BookGlobalObject(const std::string &objName, const std::string &objStore) override;
35 
40  std::vector<BookedObject> GetBookedEventObjects() override;
41 
46  std::vector<BookedObject> GetBookedPassObjects() override;
47 
52  std::vector<BookedObject> GetBookedGlobalObjects() override;
53 
54  bool BeginOfEvent() override;
55  bool EndOfEvent() override;
56  bool BeginOfPass() override;
57  bool EndOfPass() override;
58  bool BeginOfProcessing() override;
59  bool EndOfProcessing() override;
60 
70  std::unique_ptr<PersistenceService> Copy(const std::string &name, const std::string &output) override;
71 
72 protected:
80 
88 
89 private:
90  /* ******* Bookkeeping structs for output data objects ******* */
91 
92  // Base
93  struct ObjInfo {
94  std::string dataStoreName;
96  bool isGlob; // True if the object name is a glob expression
97  bool isMatched; // True if the glob has been matched
98  bool isAlias; // True if the object name is an alias
99  bool aliasToBeChecked; // Set to false after checking if the object name is an alias
100  TBranch *nameForEvBranch; // Branch storing the name of aliased global object for each event
101  std::string nameForThisEv;
102  std::set<std::string> actualNames; // Set of all the names assumed by the aliased object
103 
104  ObjInfo(const std::string &storeName)
105  : dataStoreName(storeName), dataStore(nullptr), isGlob(false), isMatched(false), isAlias(false),
106  aliasToBeChecked(true), nameForEvBranch(nullptr) {}
107  };
108 
109  // Event
110  struct EvObjInfo;
111  using EvObjectsBook = std::unordered_map<std::string, EvObjInfo>;
112  struct EvObjInfo : public ObjInfo {
113  TBranch *outputBranch; // Output branch
114  bool flag; // True if the object is present for current event
115  void *object;
117  TClass *objClass;
118  void *prevObject; // Address used for previous fill
119 
120  std::unique_ptr<EvObjectsBook> aliasedObjsBook; // Sub-book for aliased objects
121 
122  EvObjInfo(const std::string &storeName)
123  : ObjInfo(storeName), outputBranch(nullptr), flag(true), object(nullptr), defaultObject(nullptr),
124  objClass(nullptr), prevObject(nullptr), aliasedObjsBook(new EvObjectsBook) {}
125 
126  EvObjInfo(const EvObjInfo &otherObjInfo)
127  : ObjInfo(otherObjInfo), outputBranch(otherObjInfo.outputBranch), flag(otherObjInfo.flag),
128  object(otherObjInfo.object), defaultObject(otherObjInfo.defaultObject), objClass(otherObjInfo.objClass),
129  prevObject(otherObjInfo.prevObject), aliasedObjsBook(new EvObjectsBook(*(otherObjInfo.aliasedObjsBook))) {}
130  };
131 
133 
134  // Global
135  struct GlobObjInfo : public ObjInfo {
136  bool saved; // Flag for saved global objects
137 
138  GlobObjInfo(const std::string &storeName) : ObjInfo(storeName), saved(false) {}
139  };
140  using GlobObjectsBook = std::unordered_map<std::string, GlobObjInfo>;
142 
143  /* ******* Pointers to Root output objects ******* */
144  TFile *_outputFile;
145  TTree *_eventTree;
146 
147  template <typename T>
148  bool _ResolveGlobAndBook(std::unordered_map<std::string, T> &objBook, ObjectCategory objCategory);
149  bool _EndOfEventForEventObject(const std::string &objName, EvObjInfo &objInfo);
150  bool _EndOfEventForGlobalObject(const std::string &objName, GlobObjInfo &objInfo);
151  bool _SaveGlobalObject(const std::string &objName, GlobObjInfo &objInfo);
152 };
153 
154 template <typename T>
155 bool RootPersistenceService::_ResolveGlobAndBook(std::unordered_map<std::string, T> &objBook,
156  ObjectCategory objCategory) {
157  const std::string routineName("RootPersistenceService::_ResolveGlobAndBook");
158  typename std::remove_reference<decltype(objBook)>::type newBook;
159  for (auto &objInfoElem : objBook) {
160  const std::string &objName = objInfoElem.first;
161  T &objInfo = objInfoElem.second;
162  if (!(objInfo.dataStore)) {
164  try {
165  if (objCategory == ObjectCategory::GLOBAL) {
166  objInfo.dataStore = dsManager->GetGlobalDataStore(objInfo.dataStoreName);
167  } else {
168  objInfo.dataStore = dsManager->GetEventDataStore(objInfo.dataStoreName);
169  }
170  } catch (Retrieval::NotFound &exc) {
171  COUT(ERROR) << objCategory << " object store " << objInfo.dataStoreName << " not found." << ENDL;
172  return false;
173  }
174  }
175 
176  if (objInfo.isGlob) {
177  // The name is a glob expression. Search all the matching names and aliases and book each of them separately
178  std::regex regEx(StringUtils::RegexFromGlob(objName));
179  std::smatch match;
180  auto storeKnownObjs = objInfo.dataStore->GetKnownObjects();
181  for (auto knownObj : storeKnownObjs) {
182  if (std::regex_match(knownObj.name, match, regEx)) {
183  objInfo.isMatched = true;
184  auto insertionResult = newBook.insert(std::pair<std::string, T>(knownObj.name, objInfo));
185  if (!(insertionResult.second)) {
186  COUT(ERROR) << "Can't insert the " << std::nouppercase << objCategory << " object name " << knownObj.name
187  << " matching the booked glob " << objName << " among the booked objects" << ENDL;
188  return false;
189  }
190  insertionResult.first->second.isGlob = false;
191  insertionResult.first->second.isAlias = false;
192  insertionResult.first->second.aliasToBeChecked = false;
193  }
194  for (auto &alias : knownObj.aliases) {
195  if (std::regex_match(alias, match, regEx)) {
196  objInfo.isMatched = true;
197  auto insertionResult = newBook.insert(std::pair<std::string, T>(alias, objInfo));
198  if (!(insertionResult.second)) {
199  COUT(ERROR) << "Can't insert the " << objCategory << " object alias " << alias
200  << " matching the booked glob " << objName << " among the booked objects" << ENDL;
201  return false;
202  }
203  insertionResult.first->second.isGlob = false;
204  insertionResult.first->second.isAlias = true;
205  insertionResult.first->second.aliasToBeChecked = false;
206  }
207  }
208  }
209  }
210 
211  // Keep also the original entry (even if it is a glob in order to save unknown objects with matching name, will be
212  // handled in _Save{Global,Event}Object)
213  auto insertionResult = newBook.insert(objInfoElem);
214  if (!(insertionResult.second)) {
215  COUT(ERROR) << "Can't insert the " << objCategory << " object " << objInfoElem.first
216  << " among the booked objects" << ENDL;
217  return false;
218  }
219  }
220 
221  objBook.swap(newBook);
222  return true;
223 }
224 
225 } // namespace EA
226 
227 #endif /* ROOTPERSISTENCESERVICE_H_ */
bool BookEventObject(const std::string &objName, const std::string &objStore) override
Books an event object for persistence.
Definition: RootPersistenceService.cpp:56
std::set< std::string > actualNames
Definition: RootPersistenceService.h:102
bool _EndOfEventForEventObject(const std::string &objName, EvObjInfo &objInfo)
Definition: RootPersistenceService.cpp:174
std::string dataStoreName
Definition: RootPersistenceService.h:94
bool isGlob
Definition: RootPersistenceService.h:96
bool _SaveGlobalObject(const std::string &objName, GlobObjInfo &objInfo)
Definition: RootPersistenceService.cpp:479
bool BookPassObject(const std::string &objName, const std::string &objStore) override
Definition: RootPersistenceService.cpp:72
Definition: RootPersistenceService.h:135
std::vector< BookedObject > GetBookedGlobalObjects() override
Gets the list of booked global objects.
Definition: RootPersistenceService.cpp:101
Definition: PersistenceService.h:20
void * prevObject
Definition: RootPersistenceService.h:118
bool isMatched
Definition: RootPersistenceService.h:97
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
#define COUT(level)
Definition: SmartLog.h:88
observer_ptr< TTree > GetEventTree()
Getter method for the event tree.
Definition: RootPersistenceService.h:87
Base struct for Root output.
Definition: RootOutputStruct.h:24
bool EndOfProcessing() override
Definition: RootPersistenceService.cpp:394
bool Connect() override
Definition: RootPersistenceService.cpp:28
observer_ptr< TFile > GetOutputFile()
Getter method for the output file.
Definition: RootPersistenceService.h:79
Definition: RootPersistenceService.h:93
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::string nameForThisEv
Definition: RootPersistenceService.h:101
TBranch * outputBranch
Definition: RootPersistenceService.h:113
EvObjectsBook _eventBook
Definition: RootPersistenceService.h:132
Exception for unknown object.
Definition: RetrievalExceptions.h:39
EvObjInfo(const EvObjInfo &otherObjInfo)
Definition: RootPersistenceService.h:126
bool EndOfEvent() override
Save event objects on persistence medium.
Definition: RootPersistenceService.cpp:112
#define ENDL
Definition: SmartLog.h:117
bool BookGlobalObject(const std::string &objName, const std::string &objStore) override
Definition: RootPersistenceService.cpp:74
void * object
Definition: RootPersistenceService.h:115
bool _ResolveGlobAndBook(std::unordered_map< std::string, T > &objBook, ObjectCategory objCategory)
Definition: RootPersistenceService.h:155
ObjInfo(const std::string &storeName)
Definition: RootPersistenceService.h:104
Objects with values defined on a per-pass basis.
std::vector< BookedObject > GetBookedPassObjects() override
Gets the list of booked pass objects.
Definition: RootPersistenceService.cpp:99
ObjectCategory
Category of objects.
Definition: ObjectCategory.h:17
bool BeginOfProcessing() override
Definition: RootPersistenceService.cpp:381
std::string RegexFromGlob(const std::string &str)
Build a regex starting from a glob expression.
Definition: StringUtils.cpp:152
std::unique_ptr< PersistenceService > Copy(const std::string &name, const std::string &output) override
Copies the persistence service.
Definition: RootPersistenceService.cpp:517
observer_ptr< DataStoreManager > GetDataStoreManager()
Getter for the Data store manager.
Definition: DataStoreUser.h:42
StorePtr dataStore
Definition: RootPersistenceService.h:95
bool aliasToBeChecked
Definition: RootPersistenceService.h:99
bool BeginOfPass() override
Definition: RootPersistenceService.cpp:378
void * defaultObject
Definition: RootPersistenceService.h:116
TTree * _eventTree
Definition: RootPersistenceService.h:145
GlobObjInfo(const std::string &storeName)
Definition: RootPersistenceService.h:138
bool flag
Definition: RootPersistenceService.h:114
TBranch * nameForEvBranch
Definition: RootPersistenceService.h:100
EvObjInfo(const std::string &storeName)
Definition: RootPersistenceService.h:122
bool saved
Definition: RootPersistenceService.h:136
GlobObjectsBook _globalBook
Definition: RootPersistenceService.h:141
bool EndOfPass() override
Definition: RootPersistenceService.cpp:379
Definition: RootPersistenceService.h:112
bool _EndOfEventForGlobalObject(const std::string &objName, GlobObjInfo &objInfo)
Definition: RootPersistenceService.cpp:330
TClass * objClass
Definition: RootPersistenceService.h:117
TFile * _outputFile
Definition: RootPersistenceService.h:144
bool isAlias
Definition: RootPersistenceService.h:98
std::vector< BookedObject > GetBookedEventObjects() override
Gets the list of booked event objects.
Definition: RootPersistenceService.cpp:90
bool BeginOfEvent() override
Definition: RootPersistenceService.cpp:110
RootPersistenceService(const std::string &name, const std::string &output)
Definition: RootPersistenceService.cpp:25
bool Disconnect() override
Definition: RootPersistenceService.cpp:48
std::unordered_map< std::string, GlobObjInfo > GlobObjectsBook
Definition: RootPersistenceService.h:140
Definition: RootPersistenceService.h:25
Aliased object is an alias.
std::unordered_map< std::string, EvObjInfo > EvObjectsBook
Definition: RootPersistenceService.h:111
std::unique_ptr< EvObjectsBook > aliasedObjsBook
Definition: RootPersistenceService.h:120