TuneId.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: Marco Roda <Marco.Roda \at liverpool.ac.uk>
8  University of Liverpool
9 
10  Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk
11  University of Liverpool & STFC Rutherford Appleton Lab
12 */
13 //____________________________________________________________________________
14 
15 //#include <sstream>
16 
17 #include "TPRegexp.h"
18 #include "TObjArray.h"
19 #include "TObjString.h"
20 
21 #include "Framework/Utils/TuneId.h"
22 
26 
28 
29 //using std::ostringstream;
30 
31 using namespace genie;
32 
33 //____________________________________________________________________________
34 namespace genie
35 {
36  ostream & operator << (ostream & stream, const TuneId & id)
37  {
38  id.Print(stream);
39  return stream;
40  }
41  //..........................................................................
42  bool operator == (const TuneId & id1, const TuneId & id2)
43  {
44  return id1.Compare(id2);
45  }
46  //..........................................................................
47  bool operator != (const TuneId & id1, const TuneId & id2)
48  {
49  return !id1.Compare(id2);
50  }
51 }
52 //____________________________________________________________________________
53 TuneId::TuneId(const string & id_str, bool failOnInvalid)
54  : fName(genie::utils::str::TrimSpaces(id_str)) // remove any lead/trailing
55  , fIsConfigured(false)
56  , fIsValidated(false)
57 {
58  Build(fName);
59  if ( failOnInvalid && ! fIsValidated ) {
60  // status & 0377 is returned to parent on exit() call e.g. [0:255]
61  // SYSEXITS(3) FreeBSD Library Functions Manual
62  // According to style(9), it is not a good practice to call exit(3) with
63  // arbitrary values to indicate a failure condition when ending a program.
64  // Instead, the pre-defined exit codes from sysexits should be used, so the
65  // caller of the process can get a rough estimation about the failure class
66  // without looking up the source code.
67  // EX_USAGE (64) The command was used incorrectly, e.g., with the
68  // wrong number of arguments, a bad flag, a bad syntax
69  // in a parameter, or whatever.
70  // EX_UNAVAILABLE (69) A service is unavailable. This can occur if a supĀ­
71  // port program or file does not exist. This can also
72  // be used as a catchall message when something you
73  // wanted to do doesn't work, but you don't know why.
74 
75  // use 64 when failed Decode name (i.e. ! fIsConfigured )
76  // use 69 when failed to find directory (i.e. ! fIsValidated )
77  if ( fIsConfigured ) exit(69);
78  else exit(64);
79  }
80 }
81 //____________________________________________________________________________
83 {
84  this->Copy(id);
85 
86  if ( ! CheckDirectory() ) {
87  LOG("TuneId", pWARN) << "No valid subdirectory associated with " << Name() ;
88  }
89 }
90 //____________________________________________________________________________
91 string TuneId::CMC(void) const {
92 
93  string cmc = fPrefix ;
94  cmc += fYear ;
95  cmc += "_" ;
96  cmc += ModelId() ;
97 
98  return cmc ;
99 }
100 //____________________________________________________________________________
101 string TuneId::Tail(void) const {
102 
103  string tail = fTunedParamSetId ;
104  tail += "_" + fFitDataSetId ;
105  return tail ;
106 }
107 //____________________________________________________________________________
108 string TuneId::CMCDirectory(void) const {
109 
110  string dir = fBaseDirectory ;
111  dir += "/" + CMC() ;
112 
113  return dir ;
114 
115 }
116 //____________________________________________________________________________
117 string TuneId::TuneDirectory (void) const {
118 
119  string dir = CMCDirectory() ;
120  if ( ! OnlyConfiguration() ) dir += "/" + Name() ;
121 
122  return dir ;
123 }
124 //____________________________________________________________________________
125 void TuneId::Build(const string & name ) {
126  LOG("TuneId",pDEBUG)<<"Building tune "<<name;
127  if ( name.size() > 0 ) fName = name ;
128 
129  this -> Decode( fName );
130  if ( ! fIsConfigured ) return; // no point going on
131 
132  if ( this -> CheckDirectory() ) {
133  LOG("TuneId", pINFO) << Name() <<" Tune configured " ;
134  fIsValidated = true;
135  } else {
136  LOG("TuneId", pFATAL) << "No valid tune directory associated with " << Name() ;
137  fIsValidated = false;
138  }
139 }
140 //____________________________________________________________________________
141 void TuneId::Decode(string id_str)
142 {
143  static TPRegexp pattern("^([A-Za-z]+)(\\d{2})_(\\d{2})([a-z])_([a-z0-9]{2})_([a-z0-9]{3})$");
144  // TPRegexp pattern("([A-Za-z]+)(\\d{2})_(\\d{2})([a-z])_(\\d{2})_(\\d{3})");
145 
146  TString tstr(id_str.c_str());
147  TObjArray * matches = pattern.MatchS(tstr);
148  if ( matches -> GetEntries() != 7) {
149  LOG("TuneId", pFATAL) << "Bad tune pattern "<<id_str<<" - form is eg G18_01a_00_000";
150  fIsConfigured = false;
151  return;
152  } else {
153  fIsConfigured = true;
154  }
155 
156  this -> fPrefix = ((TObjString*)matches->At(1))->String().Data();
157  this -> fYear = ((TObjString*)matches->At(2))->String().Data();
158  this -> fMajorModelId = ((TObjString*)matches->At(3))->String().Data();
159  this -> fMinorModelId = ((TObjString*)matches->At(4))->String().Data();
160  this -> fTunedParamSetId = ((TObjString*)matches->At(5))->String().Data();
161  this -> fFitDataSetId = ((TObjString*)matches->At(6))->String().Data();
162 
163  delete matches;
164 }
165 //____________________________________________________________________________
166 void TuneId::Copy(const TuneId & id)
167 {
168  this->fName = id.Name();
169  this->fPrefix = id.Prefix();
170  this->fYear = id.Year();
171  this->fMajorModelId = id.MajorModelId();
172  this->fMinorModelId = id.MinorModelId();
173  this->fTunedParamSetId = id.TunedParamSetId();
174  this->fFitDataSetId = id.FitDataSetId();
175 
176  this->fIsConfigured = id.IsConfigured();
177  this->fIsValidated = id.IsValidated();
178 }
179 //____________________________________________________________________________
180 bool TuneId::Compare(const TuneId & id) const
181 {
182  return (this->Name() == id.Name());
183 }
184 //____________________________________________________________________________
185 void TuneId::Print(ostream & stream) const
186 {
187  std::string status = "Standard";
188  if ( IsCustom() ) status = "Custom";
189  if ( ! IsValidated() ) status = "BadDirectory";
190  if ( ! IsConfigured() ) status = "BadConfig";
191  stream << status << " GENIE tune: " << this -> Name() << std::endl;
192  stream << " - Prefix ............... : " << this->Prefix() << std::endl;
193  stream << " - Year ................. : " << this->Year() << std::endl;
194  stream << " - Major model ID ....... : " << this->MajorModelId() << std::endl;
195  stream << " - Minor model ID ....... : " << this->MinorModelId() << std::endl;
196  stream << " - Tuned param set ID ... : " << this->TunedParamSetId() << std::endl;
197  stream << " - Fit dataset ID ....... : " << this->FitDataSetId() << std::endl;
198  stream << " - Tune directory ....... : " << this->TuneDirectory() << std::endl;
199  stream << " - Base directory ....... : " << this->fBaseDirectory << std::endl;
200  if ( IsCustom() )
201  stream << " - Custom directory ..... : " << this -> fCustomSource << std::endl;
202 
203  stream << std::flush;
204 }
205 //____________________________________________________________________________
207 
208  std::string pathlist = utils::xml::GetXMLPathList(false) ;
209  std::vector<std::string> paths = utils::str::Split(pathlist,":;,");
210 
211  string top_path = gSystem->ExpandPathName( paths[0].c_str() ) ;
212  string def_path = gSystem->ExpandPathName( utils::xml::GetXMLDefaultPath().c_str() ) ;
213 
214  if ( top_path != def_path ) {
215  fCustomSource = top_path ;
216  }
217 
218  fBaseDirectory = "" ;
219  LOG("TuneId",pDEBUG) << "Base dir validation " ;
220 
221  for ( size_t i=0; i< paths.size(); ++i ) {
222  const char* tmppath = paths[i].c_str();
223  std::string onepath = gSystem->ExpandPathName(tmppath);
224  string test = onepath + "/" + CMC() ;
225  LOG("TuneId", pDEBUG) << " Testing " << test << " directory" ;
226  if ( utils::system::DirectoryExists( test.c_str() ) ) {
227  fBaseDirectory = onepath ;
228  break ;
229  }
230  }
231 
232  if ( fBaseDirectory.size() == 0 ) {
233  LOG("TuneId", pWARN) << " No " << CMC() << " subdirectory found in pathlist";
234  return false ;
235  }
236 
237  if ( ! OnlyConfiguration() ) {
238  if ( ! utils::system::DirectoryExists( TuneDirectory().c_str() ) ) {
239  LOG("TuneId", pWARN) << "No " << Name() << " subdirectory found in " << CMC() ;
240  return false ;
241  }
242  }
243 
244  LOG("TuneId",pDEBUG) << fBaseDirectory ;
245 
246  return true ;
247 }
string fMajorModelId
Definition: TuneId.h:97
string fYear
Definition: TuneId.h:95
const XML_Char * name
Definition: expat.h:151
string GetXMLPathList(bool add_tune=true)
string Name(void) const
Definition: TuneId.h:47
void Copy(const TuneId &id)
Definition: TuneId.cxx:166
int status
Definition: fabricate.py:1613
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
bool fIsConfigured
Definition: TuneId.h:105
void Decode(string id_str)
Definition: TuneId.cxx:141
bool operator!=(const TuneId &id1, const TuneId &id2)
Definition: TuneId.cxx:47
string MajorModelId(void) const
Definition: TuneId.h:51
string CMCDirectory(void) const
Definition: TuneId.cxx:108
#define pFATAL
Definition: Messenger.h:57
string Year(void) const
Definition: TuneId.h:49
string FitDataSetId(void) const
Definition: TuneId.h:54
bool IsCustom(void) const
Definition: TuneId.h:63
bool OnlyConfiguration() const
Definition: TuneId.h:70
cout<< t1-> GetEntries()
Definition: plottest35.C:29
bool IsValidated(void) const
Definition: TuneId.h:60
string fTunedParamSetId
Definition: TuneId.h:99
string fBaseDirectory
Definition: TuneId.h:102
string fCustomSource
Definition: TuneId.h:103
bool fIsValidated
Definition: TuneId.h:106
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
string TuneDirectory(void) const
Definition: TuneId.cxx:117
string fMinorModelId
Definition: TuneId.h:98
string MinorModelId(void) const
Definition: TuneId.h:52
#define pINFO
Definition: Messenger.h:63
bool operator==(const TuneId &id1, const TuneId &id2)
Definition: TuneId.cxx:42
string fPrefix
Definition: TuneId.h:94
string ModelId(void) const
Definition: TuneId.h:50
#define pWARN
Definition: Messenger.h:61
bool IsConfigured(void) const
Definition: TuneId.h:56
string TrimSpaces(string input)
Definition: StringUtils.cxx:24
string Tail(void) const
Definition: TuneId.cxx:101
string fName
Definition: TuneId.h:92
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool CheckDirectory()
Definition: TuneId.cxx:206
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
bool DirectoryExists(const char *path)
TDirectory * dir
Definition: macro.C:5
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
string Prefix(void) const
Definition: TuneId.h:48
string TunedParamSetId(void) const
Definition: TuneId.h:53
exit(0)
def tail(path, window=20)
Definition: log_trawl.py:24
GENIE tune ID.
Definition: TuneId.h:38
string CMC(void) const
Definition: TuneId.cxx:91
string fFitDataSetId
Definition: TuneId.h:100
string GetXMLDefaultPath()
void Build(const string &name="")
Definition: TuneId.cxx:125
void Print(ostream &stream) const
Definition: TuneId.cxx:185
bool Compare(const TuneId &id) const
Definition: TuneId.cxx:180
#define pDEBUG
Definition: Messenger.h:64