GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSimFiles.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <cstdlib>
12 
13 #include "libxml/parser.h"
14 #include "libxml/xmlmemory.h"
15 #include "libxml/xmlreader.h"
16 
20 
21 using std::endl;
22 
23 using namespace genie;
24 
25 //____________________________________________________________________________
26 namespace genie {
27  ostream & operator << (ostream & stream, const GSimFiles & f)
28  {
29  f.Print(stream);
30  return stream;
31  }
32 }
33 //____________________________________________________________________________
34 GSimFiles::GSimFiles(bool chain, const int nmaxmodels)
35 {
36  fDoChain = chain;
37  this->Init(nmaxmodels);
38 }
39 //____________________________________________________________________________
41 {
42  this->CleanUp();
43 }
44 //____________________________________________________________________________
45 int GSimFiles::NModels(void) const
46 {
47  return fNModels;
48 }
49 //____________________________________________________________________________
50 int GSimFiles::FindModelID(string tag) const
51 {
52  int imodel = 0;
53  vector<string>::const_iterator it = fModelTag->begin();
54  for( ; it != fModelTag->end(); ++it) {
55  if(*it == tag) return imodel;
56  imodel++;
57  }
58  return -1;
59 }
60 //____________________________________________________________________________
61 string GSimFiles::ModelTag(int imodel) const
62 {
63  return (*fModelTag)[imodel];
64 }
65 //____________________________________________________________________________
66 TFile* GSimFiles::XSecFile(int imodel) const
67 {
68  return (*fXSecFile)[imodel];
69 }
70 //____________________________________________________________________________
71 string GSimFiles::XSecFileName(int imodel) const
72 {
73  return (*fXSecFileName)[imodel];
74 }
75 //____________________________________________________________________________
76 TChain* GSimFiles::EvtChain(int imodel) const
77 {
78  return (*fEvtChain)[imodel];
79 }
80 //____________________________________________________________________________
81 vector<string> & GSimFiles::EvtFileNames(int imodel) const
82 {
83  return (*fEvtFileNames)[imodel];
84 }
85 //____________________________________________________________________________
86 const string & GSimFiles::PathToXMLFile(void) const
87 {
88  return fPath2XMLFile;
89 }
90 //____________________________________________________________________________
91 bool GSimFiles::LoadFromFile(string xmlfile)
92 {
93  LOG("GSimFiles", pNOTICE) << "Loading: " << xmlfile;
94 
95  vector<string> & model_tag = *fModelTag;
96  vector<TFile*> & xsec_file = *fXSecFile;
97  vector<string> & xsec_filename = *fXSecFileName;
98  vector<TChain*> & evt_chain = *fEvtChain;
99  vector< vector<string> > & evt_filenames = *fEvtFileNames;
100 
101  xmlTextReaderPtr reader = xmlNewTextReaderFilename(xmlfile.c_str());
102  if(reader == NULL) {
103  return false;
104  }
105 
106  const int kNodeTypeStartElement = 1;
107  const int kNodeTypeEndElement = 15;
108 
109  int imodel = 0;
110  bool is_xsec_file = false;
111  bool is_evt_file = false;
112  bool is_ghep_evt_file = false;
113  bool is_gst_evt_file = false;
114  bool have_ghep_files = false;
115  bool have_gst_files = false;
116 
117  if (reader != NULL) {
118  int ret = xmlTextReaderRead(reader);
119  while (ret == 1) {
120  xmlChar * name = xmlTextReaderName (reader);
121  xmlChar * value = xmlTextReaderValue (reader);
122  int type = xmlTextReaderNodeType (reader);
123  int depth = xmlTextReaderDepth (reader);
124 
125  bool start_element = (type==kNodeTypeStartElement);
126  bool end_element = (type==kNodeTypeEndElement);
127 
128  if(depth==0 && start_element) {
129  LOG("GSimFiles", pDEBUG) << "Root element = " << name;
130  if(xmlStrcmp(name, (const xmlChar *) "genie_simulation_outputs")) {
131  LOG("GSimFiles", pERROR)
132  << "\nXML doc. has invalid root element! [filename: "
133  << xmlfile << "]";
134  return false;
135  }
136  }
137 
138  if( (!xmlStrcmp(name, (const xmlChar *) "model")) && start_element) {
139  xmlChar * xname = xmlTextReaderGetAttribute(reader,(const xmlChar*)"name");
140  string sname = utils::str::TrimSpaces((const char *)xname);
141  model_tag[imodel] = sname;
142  LOG("GSimFiles", pNOTICE)
143  << "Adding files for model ID: "
144  << imodel << " (" << model_tag[imodel] << ")";
145  xmlFree(xname);
146  }
147  if( (!xmlStrcmp(name, (const xmlChar *) "model")) && end_element) {
148  LOG("GSimFiles", pNOTICE)
149  << "Done adding files for model ID: " << imodel;
150  imodel++;
151  }
152  if( (!xmlStrcmp(name, (const xmlChar *) "xsec_file")) && start_element) {
153  is_xsec_file = true;
154  }
155  if( (!xmlStrcmp(name, (const xmlChar *) "xsec_file")) && end_element) {
156  is_xsec_file = false;
157  }
158  if( (!xmlStrcmp(name, (const xmlChar *) "evt_file")) && start_element) {
159  is_evt_file = true;
160  is_ghep_evt_file = false;
161  is_gst_evt_file = false;
162  xmlChar * xfmt = xmlTextReaderGetAttribute(reader,(const xmlChar*)"format");
163  string sfmt = utils::str::TrimSpaces((const char *)xfmt);
164  if (sfmt.find("gst") != string::npos)
165  {
166  is_gst_evt_file = true;
167  if(!have_gst_files) { have_gst_files = true; }
168  }
169  else
170  if (sfmt.find("ghep") != string::npos)
171  {
172  is_ghep_evt_file = true;
173  if(!have_ghep_files) { have_ghep_files = true; }
174  }
175  if(have_gst_files && have_ghep_files) {
176  LOG("GSimFiles", pFATAL)
177  << "Oops! You shouldn't mix GHEP and GST event files in GSimFiles";
178  LOG("GSimFiles", pFATAL)
179  << "Please correct XML file: " << xmlfile;
180  gAbortingInErr = true;;
181  exit(1);
182  }
183  xmlFree(xfmt);
184  }
185  if( (!xmlStrcmp(name, (const xmlChar *) "evt_file")) && end_element) {
186  is_evt_file = false;
187  }
188  if( (!xmlStrcmp(name, (const xmlChar *) "#text")) && depth==3) {
189  string filename = utils::str::TrimSpaces((const char *)value);
190  if(is_evt_file) {
191  LOG("GSimFiles", pNOTICE)
192  << " * Adding event file: " << filename;
193  // chain the event trees, if requested
194  if(fDoChain) {
195  if(!evt_chain[imodel] && is_gst_evt_file) {
196  evt_chain[imodel] = new TChain("gst");
197  } else
198  if(!evt_chain[imodel] && is_ghep_evt_file) {
199  evt_chain[imodel] = new TChain("gtree");
200  }
201  if(evt_chain[imodel]) {
202  evt_chain[imodel]->Add(filename.c_str());
203  }
204  }//chain?
205  evt_filenames[imodel].push_back(filename);
206  }
207  if(is_xsec_file) {
208  LOG("GSimFiles", pNOTICE)
209  << " * Adding cross section file: " << filename;
210  xsec_file [imodel] = new TFile(filename.c_str(), "read");
211  xsec_filename[imodel] = filename;
212  if(!xsec_file[imodel]) {
213  exit(1);
214  }
215  }
216  }
217 
218  xmlFree(name);
219  xmlFree(value);
220 
221  ret = xmlTextReaderRead(reader);
222 
223  }//ret==1
224 
225  xmlFreeTextReader(reader);
226 
227  }//reader!=null
228 
229  fNModels = imodel;
230 
231  fPath2XMLFile = xmlfile;
232 
233  return true;
234 }
235 //____________________________________________________________________________
236 void GSimFiles::Print(ostream & stream) const
237 {
238  stream << endl;
239  stream << "loaded from path: " << fPath2XMLFile << endl;
240  for(int imodel=0; imodel < this->NModels(); imodel++) {
241  stream << "model tag: [" << this->ModelTag(imodel) << "]" << endl;
242  if(this->XSecFile(imodel)) {
243  stream << " xsec file : " << this->XSecFileName(imodel) << endl;
244  }
245  const vector<string> & filenames = this->EvtFileNames(imodel);
246  vector<string>::const_iterator iter = filenames.begin();
247  for( ; iter != filenames.end(); ++iter) {
248  string filename = *iter;
249  stream << " event file : " << filename << endl;
250  }
251  }
252 }
253 //____________________________________________________________________________
254 void GSimFiles::Init(const int nmaxmodels)
255 {
256  fNModels = 0;
257  fModelTag = new vector<string> (nmaxmodels);
258  fXSecFile = new vector<TFile*> (nmaxmodels);
259  fXSecFileName = new vector<string> (nmaxmodels);
260  fEvtChain = new vector<TChain*> (nmaxmodels);
261  fEvtFileNames = new vector<vector<string> > (nmaxmodels);
262 
263  for(int i=0; i<nmaxmodels; i++) {
264  (*fModelTag) [i] = "";
265  (*fXSecFile) [i] = 0;
266  (*fEvtChain) [i] = 0;
267  }
268 
269  fPath2XMLFile = "";
270 
271 }
272 //____________________________________________________________________________
274 {
275 
276  fPath2XMLFile = "";
277 
278 }
279 //____________________________________________________________________________
vector< TFile * > * fXSecFile
Definition: GSimFiles.h:91
#define pERROR
Definition: Messenger.h:59
string ModelTag(int imodel) const
Definition: GSimFiles.cxx:61
vector< TChain * > * fEvtChain
Definition: GSimFiles.h:93
void Init(const int nmaxmodels)
Definition: GSimFiles.cxx:254
vector< string > * fModelTag
Definition: GSimFiles.h:90
#define pFATAL
Definition: Messenger.h:56
vector< string > & EvtFileNames(int imodel) const
Definition: GSimFiles.cxx:81
int NModels(void) const
Definition: GSimFiles.cxx:45
TChain * EvtChain(int imodel) const
Definition: GSimFiles.cxx:76
const string & PathToXMLFile(void) const
Definition: GSimFiles.cxx:86
string XSecFileName(int imodel) const
Definition: GSimFiles.cxx:71
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
bool LoadFromFile(string xmlfile)
Definition: GSimFiles.cxx:91
void Print(ostream &stream) const
Definition: GSimFiles.cxx:236
int FindModelID(string tag) const
Definition: GSimFiles.cxx:50
string fPath2XMLFile
Definition: GSimFiles.h:95
void CleanUp(void)
Definition: GSimFiles.cxx:273
vector< vector< string > > * fEvtFileNames
Definition: GSimFiles.h:94
string TrimSpaces(string input)
Definition: StringUtils.cxx:18
GSimFiles(bool chain=true, const int nmaxmodels=10)
Definition: GSimFiles.cxx:34
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
TFile * XSecFile(int imodel) const
Definition: GSimFiles.cxx:66
#define pNOTICE
Definition: Messenger.h:61
vector< string > * fXSecFileName
Definition: GSimFiles.h:92
bool gAbortingInErr
Definition: Messenger.cxx:34
#define pDEBUG
Definition: Messenger.h:63