MINT2
ParticlePropertiesList.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:18:04 GMT
4 #include "Mint/NamedParameter.h"
5 
6 #include <string>
7 #include <fstream>
8 //#include <sstream>
9 #include <cstdio>
10 #include <stdlib.h>
11 
12 using namespace std;
13 using namespace MINT;
14 
16 std::string ParticlePropertiesList::_MintDalitzSpecialParticles="MintDalitzSpecialParticles.cvs";
17 
18 std::vector<std::string> ParticlePropertiesList::_dirList;
19 
20 
22  if(! ptr){
23  ptr = new ParticlePropertiesList();
24  }
25  if(0 == ptr){
26  cout << "ERROR in ParticlceProperties::getMe"
27  << " Couldn't get ParticlePropertiesList (i.e. myself)"
28  << "\n\t This is a serious problem. Will crash."
29  << endl;
30  throw "No ParticlePropertiesList";
31  }
32  return ptr;
33 }
34 
36  , const std::string& what){
37  cout << "WARNING in ParticlceProperties::" << where
38  << " Got ParticlePropertiesList, but couldn't find "
39  << what
40  << endl;
41 }
43  , int what){
44  cout << "WARNING in ParticlceProperties::" << where
45  << " Got ParticlePropertiesList, but couldn't find "
46  << what
47  << endl;
48 }
49 
50 double ParticlePropertiesList::mass(const std::string& name) {
51  const ParticleProperties* props = getMe()->get(name);
52  if(0 == props){
53  printCannotFindWarning("mass", name);
54  return -9999;
55  }
56  return props->mass();
57 }
59  const ParticleProperties* props = getMe()->get(pid);
60  if(0 == props){
61  printCannotFindWarning("mass", pid);
62  return -9999;
63  }
64  return props->mass();
65 }
66 double ParticlePropertiesList::width(const std::string& name) {
67  const ParticleProperties* props = getMe()->get(name);
68  if(0 == props){
69  printCannotFindWarning("width", name);
70  return -9999;
71  }
72  return props->width();
73 }
75  const ParticleProperties* props = getMe()->get(pid);
76  if(0 == props){
77  printCannotFindWarning("width", pid);
78  return -9999;
79  }
80  return props->width();
81 }
82 
83 
84 const std::vector<std::string>& ParticlePropertiesList::dirList(){
85  if(_dirList.empty()) fillDirList();
86  return _dirList;
87 }
88 
90  _dirList.clear();
91  NamedParameter<std::string> userDir("ParticlePropertiesList::ParticlePropertiesDir",
92  (char*)0, NamedParameterBase::QUIET);
93  for(int i=0; i < userDir.size(); i++){
94  if("" != userDir.getVal(i)){
95  _dirList.push_back(userDir.getVal(i) + "/");
96  // cout << "just pushed back " << userDir.getVal() << endl;
97  }
98  }
99 
100 // Get directory to MINTROOT
101 
102 
103  std::string MintRoot(".");
104  char * Mintevn(0);
105  Mintevn = getenv ("MINT2");
106  if (NULL != Mintevn){
107  MintRoot = Mintevn;
108  }
109 
110  std::string DecFilesRoot("UNKNOWN");
111  char * DecFiles;
112  DecFiles = getenv ("DECFILESROOT");
113  if (NULL != DecFiles){
114  DecFilesRoot = DecFiles;
115  }
116 
117  _dirList.push_back("");
118  _dirList.push_back("./");
119  _dirList.push_back(MintRoot+"/share/");
120  _dirList.push_back(DecFilesRoot+"/MintData/");
121 }
122 
123 
125  static int i=0;
126  // cout << " i = " << i << endl;
127  FILE* f(0);
128  if(0 == i){
129  f = findThisFile(_fname);
130  if(0 == f) findThisFile("mass_width.csv"); // making it backward compatible with old typo
131  if(0 == f)i++;
132  }
133  if(1 == i){
134  f = findThisFile(_MintDalitzSpecialParticles);
135  if(0 == f) i++;
136  }
137 
138  i = (i+1)%3;
139 
140  return f;
141 }
142 FILE* ParticlePropertiesList::findThisFile(const std::string& fname){
143  bool verbose=true;
144  for(std::vector<std::string>::const_iterator it = dirList().begin();
145  it != dirList().end();
146  it++){
147 
148  std::string tryThisDir = (*it);
149  std::string fullFname = tryThisDir + fname;
150  if(verbose)cout << "INFO in ParticlePropertiesList: trying: " << fullFname << endl;
151  FILE* f=fopen(fullFname.c_str(), "r");
152  if(0 != f){
153  cout << "INFO in ParticlePropertiesList: Using this file: "
154  << fullFname << endl;
155  return f;
156  }
157 
158  }
159  cout << "ERROR in ParticlePropertiesList::findFile()"
160  << "\n > having trouble finding file"
161  << "\n > with name " << fname
162  << "\n > in any directory I could guess."
163  << "\n > This is hardcoded in ParticleProperties.C."
164  << endl;
165  return (FILE*) 0;
166 }
167 
169  _fname=fname_in;
170 
171  if(! readFiles()){
172  std::cout << "ERROR! in ParticleProperties constructor:"
173  << " Problem reading file "
174  << _fname << std::endl;
175  }
176 
177 }
178 
180  bool shoutIfTwice=true;
181  int counter=0;
182  while(FILE* f = findFiles()){
183  cout << "reading " << counter << "th file" << endl;
184  if(0==f) return false;
185 
186  static const int lineLength=800;
187  char line[lineLength]={'\0'};
188 
189  while(fgets(line, lineLength, f)){
190  ParticleProperties P(line);
191  if(! P.isValid()) continue;
192 
193  theList.push_back(P);
194  if(P.hasDistinctAnti()){
195  P.antiThis();
196  theList.push_back(P);
197  }
198  }
199  fclose(f);
200  }
201 
202  // make maps for fast particle finding:
203  for(std::list<ParticleProperties>::iterator it= theList.begin();
204  it != theList.end(); it++){
205  byName[it->name()] = it;
206  if(shoutIfTwice && it->pdg_id() != 0){
207  map<int, list<ParticleProperties>::iterator >::iterator found = byID.find(it->pdg_id());
208  if(found != byID.end()){
209  cout << "WARNING in ParticlePropertiesList::readFiles "
210  << "pdg_id " << it->pdg_id() << " used twice, here: "
211  << (found->second)->name()
212  << ", and here:\n " << it->name()
213  << endl;
214  cout << " Important: Note that this is probably not be a MintDalitz error - "
215  << "\n some particles (the 'non-established' ones) have the same PDG code."
216  << "\n To use those realiably, you'll need to put your own copy of the"
217  << "\n mass_width.cvs file, put it into your run-directory, and edit the file"
218  << "\n so that your particle has a unique PID code."
219  << "\n -----------------------"
220  << endl;
221  }
222  }
223  byID[it->pdg_id()] = it;
224  }
225 
226  return true;
227 }
228 
229 void ParticlePropertiesList::print(std::ostream& out) const{
230  for(std::list<ParticleProperties>::const_iterator it= theList.begin();
231  it != theList.end(); it++){
232  it->print(out);
233  out << "\n";
234  }
235 }
236 
237 const ParticleProperties* ParticlePropertiesList::get(const std::string& name) const{
238  std::map<std::string, std::list<ParticleProperties>::iterator >::const_iterator it
239  = byName.find(name);
240  if(it == byName.end()) return 0;
241 
242  return &(*(it->second));
243 }
245  std::map<int, std::list<ParticleProperties>::iterator >::const_iterator it
246  = byID.find(id);
247  if(it == byID.end()) return 0;
248 
249  return &(*(it->second));
250 }
251 
252 std::ostream& operator<<(std::ostream& out, const ParticlePropertiesList& ppl){
253  ppl.print(out);
254  return out;
255 }
256 
257 //
static FILE * findThisFile(const std::string &fname)
ParticlePropertiesList(std::string fname_in="mass_width.cvs")
void print(std::ostream &out=std::cout) const
virtual int size() const
static double width(const std::string &name)
std::ostream & operator<<(std::ostream &out, const ParticlePropertiesList &ppl)
static const std::vector< std::string > & dirList()
const ParticleProperties * get(const std::string &name) const
static ParticlePropertiesList * ptr
static std::vector< std::string > _dirList
static void printCannotFindWarning(const std::string &where, const std::string &what)
static const ParticlePropertiesList * getMe()
static std::string _MintDalitzSpecialParticles
static double mass(const std::string &name)
double width() const
virtual const T & getVal(int i=0) const