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