GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations 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  delete _geoManager;
32 }
33 
34 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
35 
36 const std::string &GGSTFilesHandler::GetFileName(unsigned int iFile) {
37  static const std::string nullFile("");
38  if (iFile < GetNFiles())
39  return _files[iFile];
40  else
41  return nullFile;
42 }
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
45 
46 bool GGSTFilesHandler::_AddFile(const std::string &fileName, bool force) {
47  static const std::string routineName("GGSTFilesHandler::_AddFile");
48 
49  _files.push_back(fileName);
50 
51  // Check if file exists and can be opened
52  TFile *file = TFile::Open(fileName.data());
53  if (!file) {
54  GGSCOUT(WARNING) << "File " << fileName << " not found. File discarded." << GGSENDL;
55  _files.pop_back();
56  return false;
57  }
58 
59  if (file->IsZombie()) {
60  GGSCOUT(WARNING) << "File " << fileName << " cannot be opened. File discarded." << GGSENDL;
61  _files.pop_back();
62  delete file;
63  return false;
64  }
65 
66  // Do some consistency checks
67  if (!force) {
68  // Check if file contains GGS simulation data
69  std::unique_ptr<GGSTSimInfo> simInfo((GGSTSimInfo *)(file->Get("GGSSimInfo")));
70  if (simInfo == NULL) {
71  GGSCOUT(WARNING) << "GGS simulation info not present in file " << fileName << ". File discarded." << GGSENDL;
72  _files.pop_back();
73  delete file;
74  return false;
75  }
76  // Retrieve geometry and generator parameters (if they exist)
77  std::unique_ptr<GGSTGeoParams> geoParams((GGSTGeoParams *)(file->Get("GGSGeoParams")));
78  std::unique_ptr<GGSTParameters> genParams((GGSTParameters *)(file->Get("GGSGenParams")));
79  if (_files.size() == 1) {
80  // Save simulation info
81  _simInfo = *simInfo;
82  // Set geometry parameters info object if it exists
83  if (geoParams)
84  _geoParams = *geoParams;
85  // Set generator parameters info object if it exists
86  if (genParams)
87  _genParams = *genParams;
88  } else {
89  // Check if info from current file is consistent with others
90  if (!(simInfo->IsSameSimAs(_simInfo))) {
91  GGSCOUT(WARNING) << "Simulation info in file " << fileName << " are inconsistent with those in file "
92  << _files[0] << ". File discarded." << GGSENDL;
93  _files.pop_back();
94  } else if (!(geoParams->AreSameParamsAs(_geoParams))) {
95  GGSCOUT(WARNING) << "Geometry parameters in file " << fileName << " are inconsistent with those in file "
96  << _files[0] << ". File discarded." << GGSENDL;
97  _files.pop_back();
98  } else if (!(genParams->AreSameParamsAs(_genParams))) {
99  GGSCOUT(WARNING) << "Generator parameters in file " << fileName << " are inconsistent with those in file "
100  << _files[0] << ". File discarded." << GGSENDL;
101  _files.pop_back();
102  }
103  }
104  }
105  delete file;
106  return true;
107 }
108 
109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
110 
111 void GGSTFilesHandler::_SortFiles() {
112  static const std::string routineName("GGSTFilesHandler::_SortFiles");
113 
114  if (_chains.size() > 0) {
115  GGSCOUT(WARNING) << "Can't sort files after chain creation." << GGSENDL;
116  return;
117  }
118 
119  sort(_files.begin(), _files.end());
120  // Reset the simulation info to that of the first file
121  TFile *file0 = TFile::Open(_files[0].data());
122  _simInfo = *((GGSTSimInfo *)(file0->Get("GGSSimInfo")));
123  delete file0;
124 }
125 
126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
~GGSTFilesHandler()
Destructor.
GGSTFilesHandler()
Constructor.
#define GGSENDL
Definition: GGSSmartLog.h:131
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.