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