CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Utilities.h
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 #include <cassert>
4 #include <iomanip>
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 #include <map>
9 #include <cmath>
10 #include <TFile.h>
11 #include <TH1F.h>
12 #include <TF1.h>
13 #include <TStyle.h>
14 #include <TMath.h>
15 
16 void GetMPV(char name[100],TH1F* histo, TDirectory* Dir, double& peak, double& error, double& sigma, double& err_sigma);
17 void GetMEAN(TH1F* histo, double& peak, double& error, double& sigma);
18 void CalculateResponse(bool UseRatioForResponse, double x, double ex, double y, double ey, double& r, double& e);
19 void CalculateCorrection(bool UseRatioForResponse, double x, double ex, double y, double ey, double& c, double& e);
20 void Invert(TF1* f, double Min, double Max, double y, double& x);
21 bool HistoExists(std::vector<std::string> LIST, std::string hname);
22 int getBin(double x, std::vector<double> boundaries);
23 
25 {
26 public:
27  //
28  // construction/destruction
29  //
30  CommandLine();
31  ~CommandLine();
32 
33  //
34  // member functions
35  //
36  bool parse(int argc,char**argv);
37  bool check();
38  void print();
39 
40  template <class T> T getValue(const std::string& name);
41  template <class T> T getValue(const std::string& name, T default_value);
42 
43  template <class T> std::vector<T> getVector(const std::string& name);
44  template <class T> std::vector<T> getVector(const std::string& name,
45  const std::string& default_as_string);
46 
47 private:
48  bool parse_file(const std::string& file_name);
49 
50 private:
51  //
52  // internal typedefs
53  //
54  typedef std::map<std::string,std::pair<std::string,bool> > OptionMap_t;
55  typedef std::vector<std::string> StrVec_t;
56 
57 
58  //
59  // member data
60  //
61  std::string _exe;
65 
66 };
67 
68 //
69 // implemenentation of inline functions
70 //
71 
72 //______________________________________________________________________________
73 template <class T>
74 T CommandLine::getValue(const std::string& name)
75 {
76  T result;
77  OptionMap_t::iterator it=_options.find(name);
78  if (it!=_options.end()) {
79  it->second.second = true;
80  _ordered_options.push_back(name);
81  std::stringstream ss;
82  ss<<it->second.first;
83  ss>>result;
84  return result;
85  }
86  _unknowns.push_back(name);
87  return result;
88 }
89 
90 
91 //______________________________________________________________________________
92 template <class T>
93 T CommandLine::getValue(const std::string& name,T default_value)
94 {
95  OptionMap_t::const_iterator it=_options.find(name);
96  if (it!=_options.end()) return getValue<T>(name);
97  std::string default_as_string;
98  std::stringstream ss;
99  ss<<default_value;
100  ss>>default_as_string;
101  _options[name] = std::make_pair(default_as_string,true);
102  _ordered_options.push_back(name);
103  return default_value;
104 }
105 
106 
107 //______________________________________________________________________________
108 template <>
109 bool CommandLine::getValue<bool>(const std::string& name)
110 {
111  OptionMap_t::iterator it=_options.find(name);
112  if (it!=_options.end()) {
113  it->second.second = true;
114  _ordered_options.push_back(name);
115  std::string val_as_string = it->second.first;
116  if (val_as_string=="true") return true;
117  if (val_as_string=="false") return false;
118  int val_as_int;
119  std::stringstream ss;
120  ss<<val_as_string;
121  ss>>val_as_int;
122  return val_as_int;
123  }
124  _unknowns.push_back(name);
125  return false;
126 }
127 
128 
129 //______________________________________________________________________________
130 template <>
131 bool CommandLine::getValue(const std::string& name,bool default_value)
132 {
133  OptionMap_t::const_iterator it=_options.find(name);
134  if (it!=_options.end()) return getValue<bool>(name);
135  _options[name] = (default_value) ?
136  std::make_pair("true",true) : std::make_pair("false",true);
137  _ordered_options.push_back(name);
138  return default_value;
139 }
140 
141 
142 //______________________________________________________________________________
143 template <class T>
144 std::vector<T> CommandLine::getVector(const std::string& name)
145 {
146  std::vector<T> result;
147  OptionMap_t::iterator it=_options.find(name);
148  if (it!=_options.end()) {
149  it->second.second = true;
150  _ordered_options.push_back(name);
151  std::string tmp=it->second.first;
153  if (!tmp.empty()) {
154  do {
155  pos = tmp.find(",");
156  std::stringstream ss;
157  ss<<tmp.substr(0,pos);
158  tmp.erase(0,pos+1);
159  T element;
160  ss>>element;
161  result.push_back(element);
162  }
163  while (pos!=std::string::npos);
164  }
165  }
166  else {
167  _unknowns.push_back(name);
168  }
169  return result;
170 }
171 
172 //______________________________________________________________________________
173 template <class T>
174 std::vector<T> CommandLine::getVector(const std::string& name,
175  const std::string& default_as_string)
176 {
177  OptionMap_t::iterator it=_options.find(name);
178  if (it==_options.end()) _options[name] = std::make_pair(default_as_string,false);
179  return getVector<T>(name);
180 }
181 
183 // construction / destruction
185 //______________________________________________________________________________
187 {
188 
189 }
190 //______________________________________________________________________________
192 {
193 
194 }
196 // implementation of member functions
198 //______________________________________________________________________________
199 bool CommandLine::parse(int argc,char**argv)
200 {
201  _exe = argv[0];
202  _options.clear();
203  _ordered_options.clear();
204  _unknowns.clear();
205 
206  for (int i=1;i<argc;i++) {
207  std::string opt=argv[i];
208  if(0!=opt.find("-")) {
209  if (i==1) {
210  bool success = parse_file(opt);
211  if (!success) return false;
212  continue;
213  }
214  else {
215  std::cout<<"CommandLine ERROR: options must start with '-'!"<<std::endl;
216  return false;
217  }
218  }
219  opt.erase(0,1);
220  std::string next=argv[i+1];
221  if (/*0==next.find("-")||*/i+1>=argc) {
222  std::cout<<"ERROR: option '"<<opt<<"' requires value!"<<std::endl;
223  return false;
224  }
225  _options[opt] = std::make_pair(next,false);
226  i++;
227  if (i<argc-1) {
228  next=argv[i+1];
229  while (next.find("-")!=0) {
230  _options[opt].first += ","+next;
231  i++;
232  next = (i<argc-1) ? argv[i+1] : "-";
233  }
234  }
235  }
236 
237  return true;
238 }
239 //______________________________________________________________________________
241 {
242  bool result = true;
243  OptionMap_t::const_iterator it;
244  for (it = _options.begin();it!=_options.end();++it) {
245  if (!it->second.second) {
246  std::cout<<"CommandLine WARNING: unused option '"<<it->first<<"'!"<<std::endl;
247  result = false;
248  }
249  }
250 
251  if (_unknowns.size()>0) {
252  result = false;
253  std::cout<<"\nCommandLine WARNING: "<<_unknowns.size()
254  <<" the followingparameters *must* be provided:"<<std::endl;
255  for (StrVec_t::const_iterator it=_unknowns.begin();it!=_unknowns.end();++it)
256  std::cout<<(*it)<<std::endl;
257  std::cout<<std::endl;
258  }
259  return result;
260 }
261 //______________________________________________________________________________
263 {
264  std::cout<<"------------------------------------------------------------"<<std::endl;
265  std::cout<<_exe<<" options:"<<std::endl;
266  std::cout<<"------------------------------------------------------------"<<std::endl;
267  for (StrVec_t::const_iterator itvec=_ordered_options.begin();
268  itvec!=_ordered_options.end();++itvec) {
269  OptionMap_t::const_iterator it=_options.find(*itvec);
270  assert(it!=_options.end());
271  if (it->second.first.find(",")<std::string::npos) {
272  std::string tmp=it->second.first;
273  std::string::size_type length = tmp.length();
275  do {
276  pos = tmp.find(",");
277  if (tmp.length()==length) {
278  std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
279  <<it->first
280  <<std::resetiosflags(std::ios::left)
281  <<std::setw(3)<<"="
282  <<std::setiosflags(std::ios::right)<<std::setw(35)
283  <<tmp.substr(0,pos)
284  <<std::resetiosflags(std::ios::right)
285  <<std::endl;
286  }
287  else {
288  std::cout<<std::setiosflags(std::ios::right)<<std::setw(60)
289  <<tmp.substr(0,pos)
290  <<std::resetiosflags(std::ios::right)
291  <<std::endl;
292  }
293  tmp.erase(0,pos+1);
294  }
295  while (pos!=std::string::npos);
296  }
297  else {
298  std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
299  <<it->first
300  <<std::resetiosflags(std::ios::left)
301  <<std::setw(3)<<"="
302  <<std::setiosflags(std::ios::right)<<std::setw(35)
303  <<it->second.first
304  <<std::resetiosflags(std::ios::right)
305  <<std::endl;
306  }
307  }
308  std::cout<<"------------------------------------------------------------"<<std::endl;
309 }
310 //______________________________________________________________________________
311 bool CommandLine::parse_file(const std::string& file_name)
312 {
313  ifstream fin(file_name.c_str());
314  if (!fin.is_open()) {
315  std::cout<<"Can't open configuration file "<<file_name<<std::endl;
316  return false;
317  }
318 
319  std::stringstream ss;
320  bool filter(false);
321  while(!fin.eof()){
322  char next;
323  fin.get(next);
324  if (!filter&&next=='$') filter=true;
325  if(!filter) {
326  if (next=='=') ss<<" "<<next<<" ";
327  else ss<<next;
328  }
329  if (filter&&next=='\n') filter=false;
330  }
331 
332  std::string token,last_token,key,value;
333  ss>>token;
334  while (!ss.eof()) {
335  if (token=="=") {
336  if (key!=""&&value!="") _options[key] = std::make_pair(value,false);
337  key=last_token;
338  last_token="";
339  value="";
340  }
341  else if (last_token!="") {
342  if (last_token.find("\"")==0) {
343  if (last_token.rfind("\"")==last_token.length()-1) {
344  last_token=last_token.substr(1,last_token.length()-2);
345  value+=(value!="")?","+last_token:last_token;
346  last_token=token;
347  }
348  else last_token+=" "+token;
349  }
350  else {
351  value+=(value!="")?","+last_token:last_token;
352  last_token=(token=="=")?"":token;
353  }
354  }
355  else last_token=(token=="=")?"":token;
356  ss>>token;
357  }
358  if (last_token!="") {
359  if (last_token.find("\"")==0&&last_token.rfind("\"")==last_token.length()-1)
360  last_token=last_token.substr(1,last_token.length()-2);
361  value+=(value!="")?","+last_token:last_token;
362  }
363  if (key!=""&&value!="") _options[key] = std::make_pair(value,false);
364 
365  return true;
366 }
368 void GetMPV(char name[100],TH1F* histo, TDirectory* Dir, double& peak, double& error, double& sigma, double& err_sigma)
369 {
370  double norm,mean,rms,integral,lowlimit,highlimit,LowResponse,HighResponse,a;
371  int k;
372  LowResponse = histo->GetXaxis()->GetXmin();
373  HighResponse = histo->GetXaxis()->GetXmax();
374  Dir->cd();
375  TF1 *g;
376  TStyle *myStyle = new TStyle("mystyle","mystyle");
377  myStyle->Reset();
378  myStyle->SetOptFit(1111);
379  myStyle->SetOptStat(2200);
380  myStyle->SetStatColor(0);
381  myStyle->SetTitleFillColor(0);
382  myStyle->cd();
383  integral = histo->Integral();
384  mean = histo->GetMean();
385  rms = histo->GetRMS();
386  a = 1.5;
387  if (integral>0)
388  {
389  lowlimit = TMath::Max(LowResponse,mean-a*rms);
390  highlimit= TMath::Min(mean+a*rms,HighResponse);
391  norm = histo->GetMaximumStored();
392  peak = mean;
393  sigma = rms;
394  for (k=0; k<3; k++)
395  {
396  g = new TF1("g","gaus",lowlimit, highlimit);
397  g->SetParNames("N","#mu","#sigma");
398  g->SetParameter(0,norm);
399  g->SetParameter(1,peak);
400  g->SetParameter(2,sigma);
401  lowlimit = TMath::Max(LowResponse,peak-a*sigma);
402  highlimit= TMath::Min(peak+a*sigma,HighResponse);
403  g->SetRange(lowlimit,highlimit);
404  histo->Fit(g,"RQ");
405  norm = g->GetParameter(0);
406  peak = g->GetParameter(1);
407  sigma = g->GetParameter(2);
408  }
409  if (g->GetNDF()>5)
410  {
411  peak = g->GetParameter(1);
412  sigma = g->GetParameter(2);
413  error = g->GetParError(1);
414  err_sigma = g->GetParError(2);
415  }
416  else
417  {
418  std::cout<<"FIT FAILURE: histogram "<<name<<"...Using MEAN and RMS."<<std::endl;
419  peak = mean;
420  sigma = rms;
421  error = histo->GetMeanError();
422  err_sigma = histo->GetRMSError();
423  }
424  }
425  else
426  {
427  peak = 0;
428  sigma = 0;
429  error = 0;
430  err_sigma = 0;
431  }
432  histo->Write();
433 }
435 void GetMEAN(TH1F* histo, double& peak, double& error, double& sigma)
436 {
437  double N = histo->Integral();
438  if (N>2)
439  {
440  peak = histo->GetMean();
441  sigma = histo->GetRMS();
442  error = histo->GetMeanError();
443  }
444  else
445  {
446  peak = 0;
447  sigma = 0;
448  error = 0;
449  }
450 }
452 void CalculateResponse(bool UseRatioForResponse, double x, double ex, double y, double ey, double& r, double& e)
453 {
454  if (x>0 && fabs(y)>0)
455  {
456  if (UseRatioForResponse)
457  {
458  r = y;
459  e = ey;
460  }
461  else
462  {
463  r = (x+y)/x;
464  e = fabs(r-1.)*sqrt(pow(ey/y,2)+pow(ex/x,2));
465  }
466  }
467  else
468  {
469  r = 0;
470  e = 0;
471  }
472 }
474 void CalculateCorrection(bool UseRatioForResponse, double x, double ex, double y, double ey, double& c, double& e)
475 {
476  if (x>0 && fabs(y)>0)
477  {
478  if (UseRatioForResponse)
479  {
480  c = 1./y;
481  e = ey/(y*y);
482  }
483  else
484  {
485  c = x/(x+y);
486  e = (fabs(x*y)/pow(x+y,2))*sqrt(pow(ey/y,2)+pow(ex/x,2));
487  }
488  }
489  else
490  {
491  c = 0;
492  e = 0;
493  }
494 }
496 bool HistoExists(std::vector<std::string> LIST, std::string hname)
497 {
498  unsigned int i,N;
499  bool found(false);
500  N = LIST.size();
501  if (N==0)
502  std::cout<<"WARNING: empty file histogram list!!!!"<<std::endl;
503  else
504  for(i=0;i<N;i++)
505  if (hname==LIST[i])
506  found = true;
507  if (!found)
508  std::cout<<"Histogram: "<<hname<<" NOT FOUND!!! Check list of existing objects."<<std::endl;
509  return found;
510 }
512 int getBin(double x, std::vector<double> boundaries)
513 {
514  int i;
515  int n = boundaries.size()-1;
516  if (n<=0) return -1;
517  if (x<boundaries[0] || x>=boundaries[n])
518  return -1;
519  for(i=0;i<n;i++)
520  {
521  if (x>=boundaries[i] && x<boundaries[i+1])
522  return i;
523  }
524  return 0;
525 }
int i
Definition: DBlmapReader.cc:9
void CalculateResponse(bool UseRatioForResponse, double x, double ex, double y, double ey, double &r, double &e)
Definition: Utilities.h:452
bool check()
Definition: Utilities.h:240
std::string _exe
Definition: Utilities.h:61
std::vector< T > getVector(const std::string &name)
Definition: Utilities.h:144
OptionMap_t _options
Definition: Utilities.h:62
uint16_t size_type
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
void Invert(TF1 *f, double Min, double Max, double y, double &x)
std::map< std::string, std::pair< std::string, bool > > OptionMap_t
Definition: Utilities.h:54
T sqrt(T t)
Definition: SSEVec.h:46
tuple result
Definition: query.py:137
double f[11][100]
void print()
Definition: Utilities.h:262
T getValue(const std::string &name)
Definition: Utilities.h:74
int getBin(double x, std::vector< double > boundaries)
Definition: Utilities.h:512
void GetMPV(char name[100], TH1F *histo, TDirectory *Dir, double &peak, double &error, double &sigma, double &err_sigma)
Definition: Utilities.h:368
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:69
int k[5][pyjets_maxn]
std::vector< std::string > StrVec_t
Definition: Utilities.h:55
#define N
Definition: blowfish.cc:9
StrVec_t _unknowns
Definition: Utilities.h:64
tuple argc
Definition: dir2webdir.py:41
void GetMEAN(TH1F *histo, double &peak, double &error, double &sigma)
Definition: Utilities.h:435
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
list key
Definition: combine.py:13
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
x
Definition: VDTMath.h:216
void CalculateCorrection(bool UseRatioForResponse, double x, double ex, double y, double ey, double &c, double &e)
Definition: Utilities.h:474
long double T
bool parse_file(const std::string &file_name)
Definition: Utilities.h:311
bool parse(int argc, char **argv)
Definition: Utilities.h:199
StrVec_t _ordered_options
Definition: Utilities.h:63
bool HistoExists(std::vector< std::string > LIST, std::string hname)
Definition: Utilities.h:496
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40