GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSTFilesHandler.cpp
Go to the documentation of this file.
1 /*
2  * GGSTFilesHandler.cpp
3  *
4  * Created on: 31 Jul 2014
5  * Author: Nicola mori
6  */
7 
10 // C++ headers
11 #include <algorithm>
12 #include <iostream>
13 
14 // ROOT headers
15 #include "TFile.h"
16 
17 // GGS headers
19 #include "utils/GGSSmartLog.h"
20 
21 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
22 
23 GGSTFilesHandler::GGSTFilesHandler() : _geoManager{nullptr} {}
24 
25 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
26 
28  for (unsigned int iChain = 0; iChain < _chains.size(); iChain++) {
29  delete _chains[iChain];
30  }
31 }
32 
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
34 
35 const std::string &GGSTFilesHandler::GetFileName(unsigned int iFile) {
36  static const std::string nullFile("");
37  if (iFile < GetNFiles())
38  return _files[iFile];
39  else
40  return nullFile;
41 }
42 
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
44 
45 void GGSTFilesHandler::_AddFile(const std::string &fileName, bool force) {
46  static const std::string routineName("GGSTFilesHandler::_AddFile");
47 
48  _files.push_back(fileName);
49 
50  // Check if file can be opened
51  TFile *file = TFile::Open(fileName.data());
52  if (file->IsZombie()) {
53  COUT(WARNING) << "File " << fileName << " cannot be opened. File discarded." << ENDL;
54  _files.pop_back();
55  delete file;
56  return;
57  }
58 
59  // Do some consistency checks
60  if (!force) {
61  // Check if file contains GGS simulation data
62  GGSTSimInfo *simInfo = (GGSTSimInfo *)(file->Get("GGSSimInfo"));
63  if (simInfo == NULL) {
64  COUT(WARNING) << "GGS simulation info not present in file " << fileName << ". File discarded." << ENDL;
65  _files.pop_back();
66  delete file;
67  return;
68  }
69  // Retrieve geometry and generator parameters (if they exist)
70  GGSTGeoParams *geoParams = (GGSTGeoParams *)(file->Get("GGSGeoParams"));
71  GGSTParameters *genParams = (GGSTParameters *)(file->Get("GGSGenParams"));
72  TGeoManager *geoManager = (TGeoManager *)(file->Get("GGSGeometry"));
73  if (_files.size() == 1) {
74  // Save simulation info
75  _simInfo = *simInfo;
76  // Set geometry parameters info object if it exists
77  if (geoParams)
78  _geoParams = *geoParams;
79  // Set generator parameters info object if it exists
80  if (genParams)
81  _genParams = *genParams;
82  // Set ROOT geometry object if it exists
83  if (geoManager)
84  _geoManager = geoManager;
85  } else {
86  // Check if info from current file is consistent with others
87  if (!(simInfo->IsSameSimAs(_simInfo))) {
88  COUT(WARNING) << "Simulation info in file " << fileName << " are inconsistent with those in file " << _files[0]
89  << ". File discarded." << ENDL;
90  _files.pop_back();
91  } else if (!(geoParams->AreSameParamsAs(_geoParams))) {
92  COUT(WARNING) << "Geometry parameters in file " << fileName << " are inconsistent with those in file "
93  << _files[0] << ". File discarded." << ENDL;
94  _files.pop_back();
95  } else if (!(genParams->AreSameParamsAs(_genParams))) {
96  COUT(WARNING) << "Generator parameters in file " << fileName << " are inconsistent with those in file "
97  << _files[0] << ". File discarded." << ENDL;
98  _files.pop_back();
99  }
100  }
101  }
102  delete file;
103 }
104 
105 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
106 
107 void GGSTFilesHandler::_SortFiles() {
108  static const std::string routineName("GGSTFilesHandler::_SortFiles");
109 
110  if (_chains.size() > 0) {
111  COUT(WARNING) << "Can't sort files after chain creation." << ENDL;
112  return;
113  }
114 
115  sort(_files.begin(), _files.end());
116  // Reset the simulation info to that of the first file
117  TFile *file0 = TFile::Open(_files[0].data());
118  _simInfo = *((GGSTSimInfo *)(file0->Get("GGSSimInfo")));
119  delete file0;
120 }
121 
122 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
bool AreSameParamsAs(const GGSParameters &params) const
Compares two parameter objects.
~GGSTFilesHandler()
Destructor.
bool IsSameSimAs(const GGSTSimInfo &simInfo)
Equality operator.
Definition: GGSTSimInfo.cpp:21
GGSTFilesHandler()
Constructor.
#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
bool AreSameParamsAs(const GGSTGeoParams &params) const
Compares two geometry parameter objects.
Class for storing the geometry parameters on Root output file.
Definition: GGSTGeoParams.h:18
unsigned int GetNFiles()
The number of files handled by this object.
A class to store simulation informations.
Definition: GGSTSimInfo.h:21
const std::string & GetFileName(unsigned int iFile)
Fetch the name of a handled file.
Class for writing parameters into the output Root file.