GGS(GenericGEANT4Simulation)Software  2.6.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=NULL, bool force=false)
 Open ROOT files. More...
 
GGSTFilesHandlerOpen (const char *fileName, GGSTFilesHandler *filesHandler=NULL, bool force=false)
 Open ROOT files. More...
 
bool OpenFile (const char *fileName)
 Opens a ROOT file. More...
 
template<class T >
T * GetReader (GGSTFilesHandler *filesHandler=NULL, 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=NULL)
 Returns the simulation informations. More...
 
const GGSTGeoParamsGetGeoParams (const GGSTFilesHandler *filesHandler=NULL)
 Returns the geometry parameters. 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 37 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 327 of file GGSTRootReader.cpp.

327  {
328  static const std::string routineName("GGSTRootReader::GetEntries");
329 
330  if (_readers.size() > 0)
331  return _readers[0]->chain->GetEntries();
332  else {
333  COUT(WARNING) << "GetEntries may return a wrong number of entries when called before GetReader." << ENDL;
334  return _GetEntriesBlind();
335  }
336 }
#define ENDL
Definition: GGSSmartLog.h:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
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 317 of file GGSTRootReader.cpp.

317  {
318 
319  for (unsigned int i = 0; i < _readers.size(); i++) {
320  _readers[i]->reader->GetEntry(entry);
321  }
322  _currentEv = entry;
323 }
const GGSTGeoParams * GGSTRootReader::GetGeoParams ( const GGSTFilesHandler filesHandler = NULL)

Returns the geometry parameters.

Parameters
filesHandlerThe handler to the file(s) containing the desired info object. If NULL then the handler returned by the first invocation of Open will be automatically used.
Returns
pointer to the geometry parameters object.
NULL 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 354 of file GGSTRootReader.cpp.

354  {
355 
356  if (filesHandler == NULL) {
357  if (_filesHandlers.size() > 0)
358  filesHandler = _filesHandlers[0];
359  else
360  return NULL;
361  }
362 
363  return &(filesHandler->_geoParams);
364 }
Long64_t GGSTRootReader::GetReadEntry ( )
inline

Returns the current entry (event).

Returns
The current entry.

Definition at line 162 of file GGSTRootReader.h.

162  {
163  return _currentEv;
164  }
template<class T >
T * GGSTRootReader::GetReader ( GGSTFilesHandler filesHandler = NULL,
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 NULL 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 NULL if tree could not be found in files handled by the handler).

Definition at line 221 of file GGSTRootReader.h.

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

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 NULL then the handler returned by the first invocation of Open will be automatically used.
Returns
Pointer to simulation informations object.
NULL if an invalid filesHandler is used as argument or if no file(s) has still been opened.

Definition at line 340 of file GGSTRootReader.cpp.

340  {
341 
342  if (filesHandler == NULL) {
343  if (_filesHandlers.size() > 0)
344  filesHandler = _filesHandlers[0];
345  else
346  return NULL;
347  }
348 
349  return &(filesHandler->_simInfo);
350 }
GGSTFilesHandler * GGSTRootReader::Open ( const std::string &  fileName,
GGSTFilesHandler filesHandler = NULL,
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 NULL 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 NULL 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 NULL 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 (NULL if the files do not exist or if an error has occurred).

Definition at line 45 of file GGSTRootReader.cpp.

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

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