ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
scan_point.cxx
Go to the documentation of this file.
1 #include "scan_point.hpp"
2 
3 #include <cstdlib>
4 
5 #include <string>
6 #include <iostream>
7 #include <iomanip>
8 #include <stdexcept>
9 #include <sstream>
10 #include <fstream>
11 #include <limits>
12 
13 #include <getopt.h>
14 
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "TSystem.h"
18 #include "TDirectory.h"
19 
20 #include "utilities.hpp"
21 #include "cross_sections.hpp"
22 
23 using namespace std;
24 
25 namespace{
26  string file_name = "";
27  bool do_signif = false;
28 }
29 
30 int main(int argc, char *argv[]){
31  GetOptions(argc, argv);
32  if(file_name == "") ERROR("Must supply an input file name");
33 
34  TFile file(file_name.c_str(), "read");
35  if(!file.IsOpen()) ERROR("Could not open "+file_name);
36 
37  string model = "T1tttt";
38  if(Contains(file_name, "T5tttt")) model = "T5tttt";
39  if(Contains(file_name, "T2tt")) model = "T2tt";
40  if(Contains(file_name, "T6ttWW")) model = "T6ttWW";
41 
43  int mglu, mlsp;
44  parseMasses(file_name, mglu, mlsp);
45  float xsec, xsec_unc;
46  if(model=="T1tttt" || model=="T5tttt") xsec::signalCrossSection(mglu, xsec, xsec_unc);
47  else xsec::stopCrossSection(mglu, xsec, xsec_unc);
48  string glu_lsp("mGluino-"+to_string(mglu)+"_mLSP-"+to_string(mlsp));
49 
50  string workdir = MakeDir("scan_point_"+glu_lsp);
51  //string workdir = "scan_point_"+model+"_"+glu_lsp+"/";
52  //gSystem->mkdir(workdir.c_str(), kTRUE);
53 
54  ostringstream command;
55  string done = " < /dev/null &> /dev/null; ";
56  done = "; ";
57  //Need to get modify these file names
58  string up_file_name = file_name; ReplaceAll(up_file_name, "xsecNom", "xsecUp");
59  string down_file_name = file_name; ReplaceAll(down_file_name, "xsecNom", "xsecDown");
60  command
61  << "export origdir=$(pwd); "
62  << "cd ~/cmssw/CMSSW_7_4_14/src; "
63  << "eval `scramv1 runtime -sh`; "
64  << "cd $origdir; "
65  << "ln -s $(readlink -f " << file_name << ") " << workdir << done
66  << "ln -s $(readlink -f " << up_file_name << ") " << workdir << done
67  << "ln -s $(readlink -f " << down_file_name << ") " << workdir << done
68  << "cd " << workdir << done
69  << "combine -M Asymptotic " << GetBaseName(file_name) << done
70  << "combine -M Asymptotic --run observed --name Up " << GetBaseName(up_file_name) << done
71  << "combine -M Asymptotic --run observed --name Down " << GetBaseName(down_file_name) << done;
72  if(do_signif){
73  command
74  << "combine -M ProfileLikelihood --significance --expectSignal=1 --verbose=999999 --rMin=-10. --uncapped=1 " << GetBaseName(file_name)
75  << " < /dev/null &> signif_obs.log; "
76  << "combine -M ProfileLikelihood --significance --expectSignal=1 -t -1 --verbose=999999 --rMin=-10. --uncapped=1 " << GetBaseName(file_name)
77  << " < /dev/null &> signif_exp.log; ";
78  }
79  command << flush;
80  execute(command.str());
81 
82  string limits_file_name = workdir+"/higgsCombineTest.Asymptotic.mH120.root";
83  TFile limits_file(limits_file_name.c_str(), "read");
84  if(!limits_file.IsOpen()) ERROR("Could not open limits file "+limits_file_name);
85  TTree *tree = static_cast<TTree*>(limits_file.Get("limit"));
86  if(tree == nullptr) ERROR("Could not get limits tree");
87  double limit;
88  tree->SetBranchAddress("limit", &limit);
89  int num_entries = tree->GetEntries();
90  if(num_entries != 6) ERROR("Expected 6 tree entries. Saw "+to_string(num_entries));
91  tree->GetEntry(1);
92  double exp_down = limit;
93  tree->GetEntry(2);
94  double exp = limit;
95  tree->GetEntry(3);
96  double exp_up = limit;
97  tree->GetEntry(5);
98  double obs = limit;
99  limits_file.Close();
100 
101  string up_limits_file_name = workdir+"/higgsCombineUp.Asymptotic.mH120.root";
102  TFile up_limits_file(up_limits_file_name.c_str(), "read");
103  if(!up_limits_file.IsOpen()) ERROR("No \"up\" file "+up_limits_file_name);
104  tree = static_cast<TTree*>(up_limits_file.Get("limit"));
105  if(tree == nullptr) ERROR("Could not get \"up\" limits tree");
106  tree->SetBranchAddress("limit", &limit);
107  num_entries = tree->GetEntries();
108  if(num_entries != 1) ERROR("Expected 1 \"up\" tree entry. Saw "+to_string(num_entries));
109  tree->GetEntry(0);
110  double obs_up = limit;
111  up_limits_file.Close();
112 
113  string down_limits_file_name = workdir+"/higgsCombineDown.Asymptotic.mH120.root";
114  TFile down_limits_file(down_limits_file_name.c_str(), "read");
115  if(!down_limits_file.IsOpen()) ERROR("No \"down\" file "+down_limits_file_name);
116  tree = static_cast<TTree*>(down_limits_file.Get("limit"));
117  if(tree == nullptr) ERROR("Could not get \"down\" limits tree");
118  tree->SetBranchAddress("limit", &limit);
119  num_entries = tree->GetEntries();
120  if(num_entries != 1) ERROR("Expected 1 \"down\" tree entry. Saw "+to_string(num_entries));
121  tree->GetEntry(0);
122  double obs_down = limit;
123  down_limits_file.Close();
124 
125  double sig_obs, sig_exp;
126  if(do_signif){
127  sig_obs = GetSignif(workdir+"/signif_obs.log");
128  sig_exp = GetSignif(workdir+"/signif_exp.log");
129  }
130 
131  execute("rm -rf "+workdir);
132 
133  cout
134  << setprecision(numeric_limits<double>::max_digits10)
135  << ' ' << mglu
136  << ' ' << mlsp
137  << ' ' << xsec
138  << ' ' << obs
139  << ' ' << obs_up
140  << ' ' << obs_down
141  << ' ' << exp
142  << ' ' << exp_up
143  << ' ' << exp_down;
144  if(do_signif){
145  cout
146  << ' ' << sig_obs
147  << ' ' << sig_exp;
148  }
149  cout << endl;
150 
151  string txtname(workdir+"/limits_"+model+"_"+glu_lsp+".txt");
152  ofstream txtfile(txtname);
153  txtfile
154  << setprecision(numeric_limits<double>::max_digits10)
155  << ' ' << mglu
156  << ' ' << mlsp
157  << ' ' << xsec
158  << ' ' << obs
159  << ' ' << obs_up
160  << ' ' << obs_down
161  << ' ' << exp
162  << ' ' << exp_up
163  << ' ' << exp_down;
164  if(do_signif){
165  txtfile
166  << ' ' << sig_obs
167  << ' ' << sig_exp;
168  }
169  txtfile << endl;
170 }
171 
172 double GetSignif(const string &filename){
173  double signif = 0.;
174  ifstream file(filename);
175  string line;
176  while(getline(file, line)){
177  auto pos = line.find("Significance: ");
178  if(pos != 0) continue;
179  string val = line.substr(14);
180  signif = stod(val);
181  }
182  return signif;
183 }
184 
185 string GetBaseName(const string &path){
186  auto pos = path.rfind("/");
187  if(pos == string::npos){
188  return path;
189  }else{
190  return path.substr(pos+1);
191  }
192 }
193 
194 double ExtractNumber(const string &results, const string &key){
195  auto pos = results.find(key);
196  if(pos != string::npos){
197  pos += key.size();
198  istringstream iss(results.substr(pos));
199  double result;
200  iss >> result;
201  return result;
202  }else{
203  return -1.;
204  }
205 }
206 
207 void GetOptions(int argc, char *argv[]){
208  while(true){
209  static struct option long_options[] = {
210  {"filename", required_argument, 0, 'f'},
211  {"signif", required_argument, 0, 's'},
212  {0, 0, 0, 0}
213  };
214 
215  char opt = -1;
216  int option_index;
217  opt = getopt_long(argc, argv, "f:s", long_options, &option_index);
218  if( opt == -1) break;
219 
220  string optname;
221  switch(opt){
222  case 'f':
223  file_name = optarg;
224  break;
225  case 's':
226  do_signif = true;
227  break;
228  default:
229  cerr << "Bad option! getopt_long returned character code " << static_cast<int>(opt) << endl;
230  break;
231  }
232  }
233 }
double ExtractNumber(const string &results, const string &key)
Definition: scan_point.cxx:194
string GetBaseName(const string &path)
Definition: scan_point.cxx:185
void parseMasses(const std::string &str, int &mglu, int &mlsp)
Definition: utilities.cpp:21
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:34
void stopCrossSection(int stop_mass, float &xsec, float &xsec_unc)
STL namespace.
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:26
std::string execute(const std::string &cmd)
Definition: utilities.cpp:87
#define ERROR(x)
Definition: utilities.hpp:15
void GetOptions(int argc, char *argv[])
Definition: scan_point.cxx:207
void signalCrossSection(int glu_mass, float &xsec, float &xsec_unc)
double GetSignif(const string &filename)
Definition: scan_point.cxx:172
int main(int argc, char *argv[])
Definition: scan_point.cxx:30
tuple mlsp
Definition: change_r.py:18
tuple mglu
Definition: change_r.py:17
string workdir
Definition: change_r.py:13
std::string MakeDir(std::string prefix)
Definition: utilities.cpp:126