EventAnalysis  1.3.0
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  _newEventAliasesBook; // _newEventAliasesBook is for booking new global aliases found during an
134  // event, and will be merged to _eventBook at the end of the event
135 
136  // Global
137  struct GlobObjInfo : public ObjInfo {
138  bool saved; // Flag for saved global objects
139 
140  GlobObjInfo(const std::string &storeName) : ObjInfo(storeName), saved(false) {}
141  };
142  using GlobObjectsBook = std::unordered_map<std::string, GlobObjInfo>;
144  _newGlobalAliasesBook; // _newGlobalAliasesBook is for booking new global aliases found during an
145  // event, and will be merged to _globalBook at the end of the event
146 
147  /* ******* Helper method for booking objects ******* */
148  bool BookEventObjectImpl(const std::string &objName, const std::string &objStore, EvObjectsBook &book);
149  bool BookGlobalObjectImpl(const std::string &objName, const std::string &objStore, GlobObjectsBook &book);
150 
151  /* ******* Pointers to Root output objects ******* */
152  TFile *_outputFile;
153  TTree *_eventTree;
154 
155  template <typename T>
156  bool _ResolveGlobAndBook(std::unordered_map<std::string, T> &objBook, ObjectCategory objCategory);
157  bool _EndOfEventForEventObject(const std::string &objName, EvObjInfo &objInfo);
158  bool _EndOfEventForGlobalObject(const std::string &objName, GlobObjInfo &objInfo);
159  bool _SaveGlobalObject(const std::string &objName, GlobObjInfo &objInfo);
160 };
161 
162 template <typename T>
163 bool RootPersistenceService::_ResolveGlobAndBook(std::unordered_map<std::string, T> &objBook,
164  ObjectCategory objCategory) {
165  const std::string routineName("RootPersistenceService::_ResolveGlobAndBook");
166  typename std::remove_reference<decltype(objBook)>::type newBook;
167  for (auto &objInfoElem : objBook) {
168  const std::string &objName = objInfoElem.first;
169  T &objInfo = objInfoElem.second;
170  if (!(objInfo.dataStore)) {
172  try {
173  if (objCategory == ObjectCategory::GLOBAL) {
174  objInfo.dataStore = dsManager->GetGlobalDataStore(objInfo.dataStoreName);
175  } else {
176  objInfo.dataStore = dsManager->GetEventDataStore(objInfo.dataStoreName);
177  }
178  } catch (Retrieval::NotFound &exc) {
179  COUT(ERROR) << objCategory << " object store " << objInfo.dataStoreName << " not found." << ENDL;
180  return false;
181  }
182  }
183 
184  if (objInfo.isGlob) {
185  // The name is a glob expression. Search all the matching names and aliases and book each of them separately
186  std::regex regEx(StringUtils::RegexFromGlob(objName));
187  std::smatch match;
188  auto storeKnownObjs = objInfo.dataStore->GetKnownObjects();
189  for (auto knownObj : storeKnownObjs) {
190  if (std::regex_match(knownObj.name, match, regEx)) {
191  objInfo.isMatched = true;
192  auto insertionResult = newBook.insert(std::pair<std::string, T>(knownObj.name, objInfo));
193  if (!(insertionResult.second)) {
194  COUT(ERROR) << "Can't insert the " << std::nouppercase << objCategory << " object name " << knownObj.name
195  << " matching the booked glob " << objName << " among the booked objects" << ENDL;
196  return false;
197  }
198  insertionResult.first->second.isGlob = false;
199  insertionResult.first->second.isAlias = false;
200  insertionResult.first->second.aliasToBeChecked = false;
201  }
202  for (auto &alias : knownObj.aliases) {
203  if (std::regex_match(alias, match, regEx)) {
204  objInfo.isMatched = true;
205  auto insertionResult = newBook.insert(std::pair<std::string, T>(alias, objInfo));
206  if (!(insertionResult.second)) {
207  COUT(ERROR) << "Can't insert the " << objCategory << " object alias " << alias
208  << " matching the booked glob " << objName << " among the booked objects" << ENDL;
209  return false;
210  }
211  insertionResult.first->second.isGlob = false;
212  insertionResult.first->second.isAlias = true;
213  insertionResult.first->second.aliasToBeChecked = false;
214  }
215  }
216  }
217  }
218 
219  // Keep also the original entry (even if it is a glob in order to save unknown objects with matching name, will be
220  // handled in _Save{Global,Event}Object)
221  auto insertionResult = newBook.insert(objInfoElem);
222  if (!(insertionResult.second)) {
223  COUT(ERROR) << "Can't insert the " << objCategory << " object " << objInfoElem.first
224  << " among the booked objects" << ENDL;
225  return false;
226  }
227  }
228 
229  objBook.swap(newBook);
230  return true;
231 }
232 
233 } // namespace EA
234 
235 #endif /* ROOTPERSISTENCESERVICE_H_ */
bool BookEventObject(const std::string &objName, const std::string &objStore) override
Books an event object for persistence.
Definition: RootPersistenceService.cpp:55
bool BookEventObjectImpl(const std::string &objName, const std::string &objStore, EvObjectsBook &book)
Definition: RootPersistenceService.cpp:58
std::set< std::string > actualNames
Definition: RootPersistenceService.h:102
bool _EndOfEventForEventObject(const std::string &objName, EvObjInfo &objInfo)
Definition: RootPersistenceService.cpp:221
std::string dataStoreName
Definition: RootPersistenceService.h:94
bool isGlob
Definition: RootPersistenceService.h:96
bool _SaveGlobalObject(const std::string &objName, GlobObjInfo &objInfo)
Definition: RootPersistenceService.cpp:585
bool BookPassObject(const std::string &objName, const std::string &objStore) override
Definition: RootPersistenceService.cpp:75
Definition: RootPersistenceService.h:137
std::vector< BookedObject > GetBookedGlobalObjects() override
Gets the list of booked global objects.
Definition: RootPersistenceService.cpp:109
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:500
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:120
EvObjectsBook _newEventAliasesBook
Definition: RootPersistenceService.h:132
#define ENDL
Definition: SmartLog.h:117
bool BookGlobalObject(const std::string &objName, const std::string &objStore) override
Definition: RootPersistenceService.cpp:77
void * object
Definition: RootPersistenceService.h:115
bool _ResolveGlobAndBook(std::unordered_map< std::string, T > &objBook, ObjectCategory objCategory)
Definition: RootPersistenceService.h:163
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:107
ObjectCategory
Category of objects.
Definition: ObjectCategory.h:17
bool BeginOfProcessing() override
Definition: RootPersistenceService.cpp:487
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:643
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:484
GlobObjectsBook _newGlobalAliasesBook
Definition: RootPersistenceService.h:143
void * defaultObject
Definition: RootPersistenceService.h:116
TTree * _eventTree
Definition: RootPersistenceService.h:153
GlobObjInfo(const std::string &storeName)
Definition: RootPersistenceService.h:140
bool flag
Definition: RootPersistenceService.h:114
TBranch * nameForEvBranch
Definition: RootPersistenceService.h:100
bool BookGlobalObjectImpl(const std::string &objName, const std::string &objStore, GlobObjectsBook &book)
Definition: RootPersistenceService.cpp:81
EvObjInfo(const std::string &storeName)
Definition: RootPersistenceService.h:122
bool saved
Definition: RootPersistenceService.h:138
GlobObjectsBook _globalBook
Definition: RootPersistenceService.h:143
bool EndOfPass() override
Definition: RootPersistenceService.cpp:485
Definition: RootPersistenceService.h:112
bool _EndOfEventForGlobalObject(const std::string &objName, GlobObjInfo &objInfo)
Definition: RootPersistenceService.cpp:420
TClass * objClass
Definition: RootPersistenceService.h:117
TFile * _outputFile
Definition: RootPersistenceService.h:152
bool isAlias
Definition: RootPersistenceService.h:98
std::vector< BookedObject > GetBookedEventObjects() override
Gets the list of booked event objects.
Definition: RootPersistenceService.cpp:98
bool BeginOfEvent() override
Definition: RootPersistenceService.cpp:118
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:142
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