GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
Data Structures | Public Member Functions
GGSTRootReader Class Reference

Class to manage ROOT output readers. More...

#include <GGSTRootReader.h>

Inheritance diagram for GGSTRootReader:
Inheritance graph
[legend]

Public Member Functions

 GGSTRootReader ()
 Constructor.
 
 ~GGSTRootReader ()
 Destructor.
 
GGSTFilesHandlerOpen (const std::string &fileName, GGSTFilesHandler *filesHandler=nullptr, bool force=false)
 Open ROOT files. More...
 
GGSTFilesHandlerOpen (const char *fileName, GGSTFilesHandler *filesHandler=nullptr, bool force=false)
 Open ROOT files. More...
 
bool OpenFile (const char *fileName)
 Opens a ROOT file. More...
 
template<class T >
T * GetReader (GGSTFilesHandler *filesHandler=nullptr, const TString &treeName="")
 Get a chain reader. More...
 
void GetEntry (Long64_t entry)
 Reads an event. More...
 
Long64_t GetEntries ()
 The total number of events. More...
 
Long64_t GetReadEntry ()
 Returns the current entry (event). More...
 
const GGSTSimInfoGetSimInfo (const GGSTFilesHandler *filesHandler=nullptr)
 Returns the simulation informations. More...
 
const GGSTGeoParamsGetGeoParams (const GGSTFilesHandler *filesHandler=nullptr)
 Returns the geometry parameters. More...
 
const GGSTParametersGetGenParams (const GGSTFilesHandler *filesHandler=nullptr)
 Returns the generator parameters. More...
 
const TGeoManager * GetROOTGeometry (const GGSTFilesHandler *filesHandler=nullptr)
 Returns the simulation geometry in TGeo format. More...
 

Detailed Description

Class to manage ROOT output readers.

GGS actions which write output on a ROOT file generally write each one on its own tree. So for each of these trees there should be a reader class to read back the data. GGSTRootReader simplifies the handling of multiple tree reader classes, by providing facilities for opening ROOT files and reading entries for all the trees. The main method of this class is the GetReader method. See its documentation for further details.

Definition at line 38 of file GGSTRootReader.h.

Member Function Documentation

Long64_t GGSTRootReader::GetEntries ( )

The total number of events.

This method returns the number of events in the event chain. Since all the chains are enforced to contain the same number of event when they are created, there is no ambiguity on the returned value even in presence of multiple chains. If no reader has already been retrieved by GetReader when this method is called then there is still no chain. This is because chains are created when calling GetReader to connect each reader to its chain and branches. In this case a "blind" number of entries is returned: the files belonging to the files handler created by the first call to Open are scanned and when the first TTree object is found a chain is created and the number of its entries is used as the return value. This value might be incorrect in some corner cases, e.g. if two handlers containing a different number of events are opened and the second one is the one which will be effectively used for the analysis.

Returns
The number of entries.

Definition at line 297 of file GGSTRootReader.cpp.

297  {
298  static const std::string routineName("GGSTRootReader::GetEntries");
299 
300  if (_readers.size() > 0)
301  return _readers[0]->chain->GetEntries();
302  else {
303  COUT(WARNING) << "GetEntries may return a wrong number of entries when called before GetReader." << ENDL;
304  return _GetEntriesBlind();
305  }
306 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
void GGSTRootReader::GetEntry ( Long64_t  entry)

Reads an event.

This method reads the event specified by entry. It calls GetEntry for every chain associated with the already instantiated readers (

See Also
GetReader), so that every reader makes reference to the current event.
Parameters
entryThe event to be read.

Definition at line 289 of file GGSTRootReader.cpp.

289  {
290 
291  for (unsigned int i = 0; i < _readers.size(); i++) {
292  _readers[i]->reader->GetEntry(entry);
293  }
294  _currentEv = entry;
295 }
const GGSTParameters * GGSTRootReader::GetGenParams ( const GGSTFilesHandler filesHandler = nullptr)

Returns the generator parameters.

Parameters
filesHandlerThe handler to the file(s) containing the desired info object. If nullptr then the handler returned by the first invocation of Open will be automatically used.
Returns
pointer to the geometry parameters object.
nullptr if an invalid filesHandler is used as argument, if no file(s) has still been opened or if the geometry parameters are not available.

Definition at line 332 of file GGSTRootReader.cpp.

332  {
333 
334  if (filesHandler == nullptr) {
335  if (_filesHandlers.size() > 0)
336  filesHandler = _filesHandlers[0];
337  else
338  return nullptr;
339  }
340 
341  return &(filesHandler->_genParams);
342 }
const GGSTGeoParams * GGSTRootReader::GetGeoParams ( const GGSTFilesHandler filesHandler = nullptr)

Returns the geometry parameters.

Parameters
filesHandlerThe handler to the file(s) containing the desired info object. If nullptr then the handler returned by the first invocation of Open will be automatically used.
Returns
pointer to the geometry parameters object.
nullptr if an invalid filesHandler is used as argument, if no file(s) has still been opened or if the geometry parameters are not available.

Definition at line 320 of file GGSTRootReader.cpp.

320  {
321 
322  if (filesHandler == nullptr) {
323  if (_filesHandlers.size() > 0)
324  filesHandler = _filesHandlers[0];
325  else
326  return nullptr;
327  }
328 
329  return &(filesHandler->_geoParams);
330 }
Long64_t GGSTRootReader::GetReadEntry ( )
inline

Returns the current entry (event).

Returns
The current entry.

Definition at line 161 of file GGSTRootReader.h.

161 { return _currentEv; }
template<class T >
T * GGSTRootReader::GetReader ( GGSTFilesHandler filesHandler = nullptr,
const TString &  treeName = "" 
)

Get a chain reader.

The template argument T is the class name of a reader class, which must inherit from GGSTChainReader. The method will return a pointer to a reader of class T for the specified tree, eventually chaining all the files handled by the GGSTFilesHandler passed as first argument. The reader will be automatically set to current entry.

If a nullptr value is passed for GGSTFilesHandler then the first handler (i.e. the file(s) opened with the first call to Open) will be used. If an empty std::string is passed for treeName, then the default tree name will be used.

Parameters
filesHandlerHandler to the Root files to be read by the reader.
treeNameThe name of the tree to be read by the reader T.
Returns
Pointer to the reader (or nullptr if tree could not be found in files handled by the handler).

Definition at line 233 of file GGSTRootReader.h.

233  {
234 
235  static const char *routineName = "GGSTRootReader::GetReader";
236 
237  // Set filesHandler to first handler if no handler is provided
238  if (filesHandler == nullptr) {
239  if (_filesHandlers.size() > 0)
240  filesHandler = _filesHandlers[0];
241  }
242 
243  // Set default tree name if no name is provided
244  TString tName(treeName);
245  if (tName == "") {
246  tName = "GGSEventsTree";
247  }
248 
249  // Search the tree in stored trees (i.e., already requested by a reader)
250  for (unsigned int iReader = 0; iReader < _readers.size(); iReader++) {
251  if (_readers[iReader]->filesHandler == filesHandler && _readers[iReader]->chain->GetName() == tName) {
252  // Check if found reader is of the desired type
253  T *retVal = dynamic_cast<T *>(_readers[iReader]->reader);
254  if (retVal) {
255  return retVal;
256  }
257  }
258  }
259 
260  // Search for chain
261  TChain *chain = _FindChain(tName, filesHandler);
262  if (chain) {
263  T *reader = new T;
264  // Read first entry if no entry has been read yet.
265  // This is necessary in order to make branches available so that readers can check for branch match in their
266  // SetChain methods.
267  if (chain->GetReadEntry() < 0)
268  chain->GetEntry(0);
269  if (reader->SetChain(chain)) {
270  // Reset the read entry to -1, to avoid missing to read the first event with some readers.
271  chain->GetEntry(-1);
272  for (unsigned int iReader = 0; iReader < _readers.size(); iReader++) {
273  if (chain->GetEntries() != _readers[iReader]->chain->GetEntries()) {
274  COUT(WARNING) << routineName << "Event number for the requested reader (" << chain->GetEntries()
275  << ") and for reader no. " << iReader << " (" << _readers[iReader]->chain->GetEntries()
276  << ") do not match. The reader has not been created." << std::endl;
277  delete reader;
278  return nullptr;
279  }
280  }
281  if (_currentEv != -1)
282  reader->GetEntry(_currentEv);
283  _readers.push_back(new ReadersInfo());
284  _readers.back()->reader = reader;
285  _readers.back()->chain = chain;
286  _readers.back()->filesHandler = filesHandler;
287  return reader;
288  } else {
289  COUT(WARNING) << "Tree " << tName << " cannot be read by the requested reader. The reader has not been created."
290  << std::endl;
291  chain->GetEntry(-1);
292  delete reader;
293  return nullptr;
294  }
295  } else {
296  COUT(WARNING) << "Can't find tree " << tName << ". The reader has not been created." << ENDL;
297  return nullptr;
298  }
299 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
const TGeoManager * GGSTRootReader::GetROOTGeometry ( const GGSTFilesHandler filesHandler = nullptr)

Returns the simulation geometry in TGeo format.

Parameters
filesHandlerThe handler to the file(s) containing the desired info object. If nullptr then the handler returned by the first invocation of Open will be automatically used.
Returns
pointer to the geometry manager.
nullptr if an invalid filesHandler is used as argument, if no file(s) has still been opened or if the geometry manager is not available.

Definition at line 344 of file GGSTRootReader.cpp.

344  {
345  if (filesHandler == nullptr) {
346  if (_filesHandlers.size() > 0)
347  filesHandler = _filesHandlers[0];
348  else
349  return nullptr;
350  }
351 
352  return filesHandler->_geoManager;
353 }
const GGSTSimInfo * GGSTRootReader::GetSimInfo ( const GGSTFilesHandler filesHandler = nullptr)

Returns the simulation informations.

Since this class can manage many handlers, the user can specify from which of them the simulation informations must be read. If no handler is specified, then the one obtained with the first call to Open will be used. All the files belonging to the same handler are forced to have the same GGSTSimInfo object, except for the initial seeds.

Parameters
filesHandlerThe handler to the file(s) containing the desired info object. If nullptr then the handler returned by the first invocation of Open will be automatically used.
Returns
Pointer to simulation informations object.
nullptr if an invalid filesHandler is used as argument or if no file(s) has still been opened.

Definition at line 308 of file GGSTRootReader.cpp.

308  {
309 
310  if (filesHandler == nullptr) {
311  if (_filesHandlers.size() > 0)
312  filesHandler = _filesHandlers[0];
313  else
314  return nullptr;
315  }
316 
317  return &(filesHandler->_simInfo);
318 }
GGSTFilesHandler * GGSTRootReader::Open ( const std::string &  fileName,
GGSTFilesHandler filesHandler = nullptr,
bool  force = false 
)

Open ROOT files.

The argument of this method can either be the name of a single file or multiple files, in turn specified using wildcards (*, ? and []) or the name of a txt file containing the full paths to the desired files (one per row). The reader will handle the files and return a pointer to a GGSTFilesHandler object handling all the files that have been opened.

When opening with wildcards the files will be sorted in alphabetical order, while when using the txt file the order will not be modified.

If the file(s) do not exist or in case of an error, a nullptr value will be returned. When opening multiple files a consistency check is done in order to assure that all the files have been produced in the same way. The check is based on the content of the GGSTSimInfo stored in each file, and currently it only checks that the content of each member of the GGSTSimInfo objects exactly match those of the GGSTSimInfo object contained in the first file. This means that two file produced with two datacards with different names will be considered as inconsistent, even if the content of the datacards was actually the same. Files which are inconsistent with respecto to the first one will be ignored. The consistency check can be turned off using the #force argument.

The pointer to the handler can eventually be passed to GetReader to specify which files the desired reader should use (see GetReader).

After a handler has been created by calling Open, more files can be added to it by passing it as the second argument to subsequent calls of Open. In this case the returned address will be equal to that of the argument. If an error occurs the method will return nullptr and the handler will not be modified (i.e. no files will be appended).

The reader retains the ownership of the handler objects it creates and will adopt eventual handlers instantiated explicitly by the user and passed to Open as the second argument (e.g. the user must not delete the handler in these cases)

Parameters
fileNamePath to Root file (eventually with wildcards) or to a txt file containing the list of desired Root files.
filesHandlerThe files handler to which the newly opened file will be appended. If nullptr a new handler will be created.
forceif true, no consistency check between different files will be done.
Returns
A pointer to a GGSTFilesHandler object handling the opened files (nullptr if the files do not exist or if an error has occurred).

Definition at line 36 of file GGSTRootReader.cpp.

36  {
37  static const std::string routineName("GGSTRootReader::Open");
38 
39  GGSTFilesHandler *handler = nullptr;
40  if (filesHandler == nullptr)
41  handler = new GGSTFilesHandler;
42  else {
43  handler = filesHandler;
44  if (find(_filesHandlers.begin(), _filesHandlers.end(), handler) == _filesHandlers.end())
45  _filesHandlers.push_back(handler);
46  }
47 
48  if (fileName.find('?') != std::string::npos || fileName.find('*') != std::string::npos) {
49  // ******* Name with wildcards *******
50 
51  // ++++ Convert fileName into a regular expression ++++
52  std::string regExpFiles(fileName);
53  // 1. escape all .
54  std::string::size_type pos = 0;
55  while ((pos = regExpFiles.find('.', pos)) != std::string::npos) {
56  regExpFiles.replace(pos, 1, "\\.");
57  pos += 2;
58  }
59  // 2. convert all * to .*
60  pos = 0;
61  while ((pos = regExpFiles.find('*', pos)) != std::string::npos) {
62  regExpFiles.replace(pos, 1, ".*");
63  pos += 2;
64  }
65  // 3. convert all ? to .
66  std::replace(regExpFiles.begin(), regExpFiles.end(), '?', '.');
67 
68  // ++++ Search files using Boost.FileSystem ++++
69  std::string basePathName(fileName);
70  // Extract path
71  size_t posOfLastSlash = basePathName.find_last_of('/');
72  if (posOfLastSlash != std::string::npos)
73  basePathName.erase(posOfLastSlash);
74  else
75  basePathName = boost::filesystem::current_path().string();
76 
77  boost::filesystem::path basePath(basePathName);
78  boost::filesystem::absolute(basePath);
79 
80  boost::filesystem::directory_iterator endItr; // Default constructor yields past-the-end
81  boost::filesystem::directory_iterator dirIter(basePath);
82  if (dirIter == endItr) {
83  if (filesHandler == nullptr)
84  delete handler;
85  return nullptr;
86  }
87 
88  for (; dirIter != endItr; ++dirIter) {
89  // Skip if not a file
90  if (!is_regular_file(dirIter->status()))
91  continue;
92  boost::smatch what;
93  // Skip if no match
94  if (!boost::regex_match(dirIter->path().string(), what, boost::regex(regExpFiles)))
95  continue;
96  // Skip if not a Root file
97  if (dirIter->path().string().substr(dirIter->path().string().size() - 5) != ".root")
98  continue;
99  // File matches, store it
100  handler->_AddFile(dirIter->path().string(), force);
101  }
102  if (handler->GetNFiles() == 0) {
103  COUT(WARNING) << fileName << ": no file found." << ENDL;
104  delete handler;
105  return nullptr;
106  }
107  handler->_SortFiles();
108 
109  } else if (fileName.substr(fileName.size() - 4) == ".txt") {
110  // ******* Text file with file names *******
111  if (boost::filesystem::exists(fileName)) {
112  std::ifstream file(fileName.data(), std::ios::in);
113  std::string currFile;
114  while (file.good()) {
115  getline(file, currFile);
116  if (boost::filesystem::exists(currFile) && currFile.substr(currFile.size() - 5) == ".root")
117  handler->_AddFile(currFile, force);
118  }
119  } else {
120  if (filesHandler == nullptr)
121  delete handler;
122  return nullptr;
123  }
124  } else if (fileName.substr(fileName.size() - 5) == ".root") {
125  // ******* Single root file *******
126  if (boost::filesystem::exists(fileName))
127  handler->_AddFile(fileName, force);
128  else {
129  if (filesHandler == nullptr)
130  delete handler;
131  return nullptr;
132  }
133  } else {
134  if (filesHandler == nullptr)
135  delete handler;
136  return nullptr;
137  }
138 
139  if (find(_filesHandlers.begin(), _filesHandlers.end(), handler) == _filesHandlers.end())
140  _filesHandlers.push_back(handler);
141  return handler;
142 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
Class to handle group of Root files.
unsigned int GetNFiles()
The number of files handled by this object.
GGSTFilesHandler* GGSTRootReader::Open ( const char *  fileName,
GGSTFilesHandler filesHandler = nullptr,
bool  force = false 
)
inline

Open ROOT files.

Overload with const char *.

Parameters
fileNamePath to Root file (eventually with wildcards) or to a txt file containing the list of desired Root files.
filesHandlerThe files handler to which the newly opened file will be appended. If nullptr a new handler will be created.
forceif true, no consistency check between different files will be done.
Returns
A pointer to a GGSTFilesHandler object handling the opened files (nullptr if file(s) do(es) not exist or if an error has occurred).

Definition at line 101 of file GGSTRootReader.h.

101  {
102  return Open(std::string(fileName), filesHandler, force);
103  }
GGSTFilesHandler * Open(const std::string &fileName, GGSTFilesHandler *filesHandler=nullptr, bool force=false)
Open ROOT files.
bool GGSTRootReader::OpenFile ( const char *  fileName)
inline

Opens a ROOT file.

This method is kept for backward compatibility. It opens a single Root file. The usage of this method is discouraged and it will be deprecated in some future release. The suggested method is Open.

Parameters
fileNamePath to file.
Returns
true if file opening was successful, false otherwise.

Definition at line 113 of file GGSTRootReader.h.

113 { return Open(fileName); }
GGSTFilesHandler * Open(const std::string &fileName, GGSTFilesHandler *filesHandler=nullptr, bool force=false)
Open ROOT files.

The documentation for this class was generated from the following files: