ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
scatter_mj_mt.cxx
Go to the documentation of this file.
1 #include "scatter_mj_mt.hpp"
2 
3 #include <cstdlib>
4 #include <iostream>
5 
6 #include <string>
7 #include <sstream>
8 #include <set>
9 #include <vector>
10 
11 #include <unistd.h>
12 #include <getopt.h>
13 
14 #include "TColor.h"
15 #include "TPaveText.h"
16 #include "TMath.h"
17 #include "TGraph.h"
18 #include "TH2D.h"
19 #include "TCanvas.h"
20 #include "TLine.h"
21 #include "TLegend.h"
22 #include "TRandom3.h"
23 #include "TError.h" // Turns off "no dictionary for class" warnings
24 #include "TSystem.h"
25 
26 #include "small_tree_quick.hpp"
27 #include "timer.hpp"
28 #include "styles.hpp"
29 #include "utilities.hpp"
30 #include "utilities_macros.hpp"
31 
32 using namespace std;
33 
34 namespace{
35  double met_min = 200.;
36  double met_max = 0.;
37  int njets_min = 7;
38  int njets_max = 0;
39  int seed = 3239;
40  bool merge_ttbar = false;
41  bool compressed = false;
42  bool no_signal = false;
43  bool full_stats = false;
44 }
45 
46 //Not sure why I can't get the colors from utilities_macros...
47 TColor c_tt_2l(1006, 86/255.,160/255.,211/255.);
48 TColor c_tt_1l(1000, 1/255.,57/255.,166/255.);
49 
50 int main(int argc, char *argv[]){
51  //gErrorIgnoreLevel=6000; // Turns off errors due to missing branches
52  GetOptions(argc, argv);
53  TRandom3 rand3(seed);
54 
55  styles style("2Dnobar");
56  style.setDefaultStyle();
57 
58  string folder = "/cms5r0/ald77/archive/2015_05_25/skim/";
59  //folder = "/afs/cern.ch/user/m/manuelf/work/ucsb/2015_05_25/skim/";
60  string sig_name = compressed ? "*T1tttt*1200*800*":"*T1tttt*1500*100*";
61  small_tree_quick st_sig(folder+sig_name);
62  small_tree_quick st_bkg(folder+"*TTJets*");
63 
64  double mj_max = 1000.;
65  double mt_max = 400.;
66 
67  TGraph g_sig, g_bkg, g_bkg1, g_bkg2;
68  TGraph g_sig_full, g_bkg_full, g_bkg1_full, g_bkg2_full;
69  TH2D h("h", ";M_{J} [GeV];m_{T} [GeV]", 1, 0., mj_max, 1, 0., mt_max);
70 
71  int line_width = 4;
72  TLine l_mj(400.,0.,400.,mt_max);
73  TLine l_mt(0.,140.,mj_max,140.);
74  l_mj.SetLineWidth(line_width);
75  l_mt.SetLineWidth(line_width);
76 
77  double ttbar_norm = 1.;
78  double sig_norm = 1.;
79  if(full_stats){
80  ttbar_norm = -1.;
81  sig_norm = 100.;
82  }
83  set<size_t> indices_sig = GetRandomIndices(st_sig, sig_norm, rand3);
84  set<size_t> indices_bkg = GetRandomIndices(st_bkg, ttbar_norm, rand3);
85 
86  Process(st_sig, g_sig, g_sig_full, 2, 20, 1, indices_sig);
87  if(merge_ttbar){
88  Process(st_bkg, g_bkg, g_bkg_full, 1006, 21, 1, indices_bkg);
89  }else{
90  Process(st_bkg, g_bkg1, g_bkg1_full, 1000, 23, 1, indices_bkg, 1);
91  Process(st_bkg, g_bkg2, g_bkg2_full, 1006, 22, 1, indices_bkg, 2);
92  }
93 
94  double rho_sig = g_sig_full.GetCorrelationFactor();
95 
96  double rho_bkg = g_bkg_full.GetCorrelationFactor();
97  double rho_bkg1 = g_bkg1_full.GetCorrelationFactor();
98  double rho_bkg2 = g_bkg2_full.GetCorrelationFactor();
99 
100  TLegend l(style.PadLeftMargin, 1.-style.PadTopMargin, 1.-style.PadRightMargin, 1.0);
101  if(merge_ttbar){
102  l.SetNColumns(2);
103  }else{
104  l.SetNColumns(3);
105  }
106  l.SetFillColor(0);
107  l.SetFillStyle(4000);
108  l.SetBorderSize(0);
109  if(merge_ttbar){
110  l.AddEntry(&g_bkg, GetLabel("ttbar",rho_bkg).c_str(), "p");
111  }else{
112  l.AddEntry(&g_bkg1, GetLabel("t#bar{t} (1l)",rho_bkg1).c_str(), "p");
113  l.AddEntry(&g_bkg2, GetLabel("t#bar{t} (2l)",rho_bkg2).c_str(), "p");
114  }
115  if(!no_signal){
116  l.AddEntry(&g_sig, GetLabel((compressed?"T1tttt(1200,800)":"T1tttt(1500,100)"),rho_sig).c_str(), "p");
117  }
118 
119  double height = 0.125;
120  double width = 0.125;
121  TPaveText l1(style.PadLeftMargin, style.PadBottomMargin,
122  style.PadLeftMargin+width, style.PadBottomMargin+height, "NDCNB");
123  TPaveText l2(1.-style.PadRightMargin-width, style.PadBottomMargin,
124  1.-style.PadRightMargin, style.PadBottomMargin+height, "NDCNB");
125  TPaveText l3(style.PadLeftMargin, 1.-style.PadTopMargin-1.5*height,
126  style.PadLeftMargin+width, 1.-style.PadTopMargin-0.5*height, "NDCNB");
127  TPaveText l4(1.-style.PadRightMargin-width, 1.-style.PadTopMargin-1.5*height,
128  1.-style.PadRightMargin, 1.-style.PadTopMargin-0.5*height, "NDCNB");
129  TPaveText lcms(style.PadLeftMargin+0.01, 1.-style.PadTopMargin-0.5*height-0.01,
130  style.PadLeftMargin+0.01+2.*width, 1.-style.PadTopMargin-0.01, "NDCNB");
131 
132  l1.AddText("R1");
133  l2.AddText("R2");
134  l3.AddText("R3");
135  l4.AddText("R4");
136  lcms.AddText("#font[62]{CMS Simulation}");
137 
138  SetStyle(l1);
139  SetStyle(l2);
140  SetStyle(l3);
141  SetStyle(l4);
142  SetStyle(lcms);
143  lcms.SetTextColorAlpha(1,1.);
144 
145  TCanvas c;
146  h.Draw();
147  if(merge_ttbar){
148  g_bkg.Draw("psame");
149  }else{
150  g_bkg1.Draw("psame");
151  g_bkg2.Draw("psame");
152  }
153  if(!no_signal){
154  g_sig.Draw("psame");
155  }
156  l_mj.Draw("same");
157  l_mt.Draw("same");
158  l.Draw("same");
159  l1.Draw("same");
160  l2.Draw("same");
161  l3.Draw("same");
162  l4.Draw("same");
163  lcms.Draw("same");
164 
165  ostringstream outname;
166  outname << "plots/scat_mj_mt_met_"
167  << met_min << '_' << met_max
168  << "_njets_" << njets_min << '_' << njets_max
169  << "_seed" << seed
170  << (merge_ttbar?"_merged":"_split")
171  << (no_signal ? "_no_signal" : (compressed ? "_T1tttt_1200_800" : "_T1tttt_1500_100"))
172  << (full_stats ? "_shapes" : "_lumi")
173  << ".pdf";
174  c.Print(outname.str().c_str());
175 }
176 
177 set<size_t> GetRandomIndices(small_tree_quick &st, double norm, TRandom3 &rand3){
178  int num_entries = st.GetEntries();
179  if(num_entries<1) return set<size_t>();
180  st.GetEntry(0);
181  double weight = st.weight();
182  int num_points = std::min((norm<=0.
183  ?num_entries
184  :TMath::Nint(10.*weight*num_entries*norm)),
185  num_entries);
186  cout << "Selecting " << num_points << " out of " << num_entries << "..." << endl;
187  set<size_t> indices;
188  while(indices.size() < static_cast<size_t>(num_points)){
189  indices.insert(rand3.Integer(num_entries));
190  }
191  return indices;
192 }
193 
194 void Process(small_tree_quick &st, TGraph &g, TGraph &g_full,
195  int color, int marker, int size,
196  const set<size_t> &indices, int nleps){
197  g = TGraph(0);
198  g_full = TGraph(0);
199  int num_entries = st.GetEntries();
200  Timer timer(num_entries, 1.);
201  timer.Start();
202  int n2(0), n4(0);
203  for(int entry = 0; entry < num_entries; ++entry){
204  timer.Iterate();
205  st.GetEntry(entry);
206 
207  if(false
208  || st.nbm()<2
209  || st.njets()<njets_min
210  || (njets_max > 0 && st.njets()>njets_max)
211  || st.met()<=met_min
212  || (met_max > 0. && st.met()>met_max)
213  || st.ht()<=500.
214  || (st.nmus()+st.nels())!=1
215  || ((nleps == 1 && st.ntruleps()>1) || (nleps == 2 && st.ntruleps()<2))
216  ) continue;
217 
218  double mj = std::min(999.9f, st.mj());
219  double mt = std::min(399.9f, st.mt());
220 
221  AddPoint(g_full, mj, mt);
222  if(indices.find(entry) == indices.end()) continue;
223  AddPoint(g, mj, mt);
224  if(color==2) {
225  //cout<<entry<<": mj "<<mj<<", mt "<<mt<<endl;
226  if(mt<=140&&mj>400) n2++;
227  if(mt>140&&mj>400) n4++;
228  }
229  }
230  if(color==2)cout<<"Nsig in R2 is "<<n2<<" and in R4 is "<<n4<<endl;
231  g.SetLineColor(color);
232  g.SetFillColor(color);
233  g.SetMarkerColor(color);
234  g.SetMarkerStyle(marker);
235  g.SetMarkerSize(size);
236 }
237 
238 string GetLabel(const string &str, double rho){
239  ostringstream oss;
240  oss.precision(2);
241  oss << str << ", #rho=" << RoundNumber(rho,2) << flush;
242  return oss.str();
243 }
244 
245 void GetOptions(int argc, char *argv[]){
246  while(true){
247  static struct option long_options[] = {
248  {"met_min", required_argument, 0, 0},
249  {"met_max", required_argument, 0, 0},
250  {"njets_min", required_argument, 0, 0},
251  {"njets_max", required_argument, 0, 0},
252  {"seed", required_argument, 0, 0},
253  {"merge_ttbar", no_argument, 0, 0},
254  {"compressed", no_argument, 0, 0},
255  {"no_signal", no_argument, 0, 0},
256  {"full_stats", no_argument, 0, 0},
257  {0, 0, 0, 0}
258  };
259 
260  char opt = -1;
261  int option_index;
262  opt = getopt_long(argc, argv, "", long_options, &option_index);
263  if(opt == -1) break;
264 
265  string optname;
266  switch(opt){
267  case 0:
268  optname = long_options[option_index].name;
269  if(optname == "met_min"){
270  met_min = atof(optarg);
271  }else if(optname == "met_max"){
272  met_max = atof(optarg);
273  }else if(optname == "njets_min"){
274  njets_min = atoi(optarg);
275  }else if(optname == "seed"){
276  seed = atoi(optarg);
277  }else if(optname == "njets_max"){
278  njets_max = atoi(optarg);
279  }else if(optname == "merge_ttbar"){
280  merge_ttbar = true;
281  }else if(optname == "compressed"){
282  compressed = true;
283  }else if(optname == "no_signal"){
284  no_signal = true;
285  }else if(optname == "full_stats"){
286  full_stats = true;
287  }
288  break;
289  default: break;
290  }
291  }
292 }
293 
294 void SetStyle(TPaveText &pt){
295  pt.SetFillColor(0);
296  pt.SetFillStyle(4000);
297  pt.SetBorderSize(0);
298  pt.SetTextColorAlpha(1,0.5);
299 }
virtual int const & ntruleps() const
void SetStyle(TPaveText &pt)
Definition: timer.hpp:6
void AddPoint(TGraph &graph, const double x, const double y)
Definition: utilities.cpp:267
int const & nbm() const
Definition: small_tree.cpp:550
float const & weight() const
Definition: small_tree.cpp:506
void setDefaultStyle()
Definition: styles.cpp:36
int const & nmus() const
Definition: small_tree.cpp:605
string GetLabel(const string &str, double rho)
float PadTopMargin
Definition: styles.hpp:36
STL namespace.
set< size_t > GetRandomIndices(small_tree_quick &st, double norm, TRandom3 &rand3)
int main(int argc, char *argv[])
int const & nels() const
Definition: small_tree.cpp:572
float const & met() const
Definition: small_tree.cpp:462
virtual float const & mj() const
TString RoundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cpp:191
TColor c_tt_2l(1006, 86/255., 160/255., 211/255.)
float const & mt() const
Definition: small_tree.cpp:484
int const & njets() const
Definition: small_tree.cpp:583
TColor c_tt_1l(1000, 1/255., 57/255., 166/255.)
virtual void GetEntry(const long entry)
float PadRightMargin
Definition: styles.hpp:36
TFile * f
long GetEntries() const
Definition: small_tree.cpp:400
float PadLeftMargin
Definition: styles.hpp:36
void Iterate()
Definition: timer.cpp:28
void Start()
Definition: timer.cpp:22
void GetOptions(int argc, char *argv[])
void Process(small_tree_quick &st, TGraph &g, TGraph &g_full, int color, int marker, int size, const set< size_t > &indices, int nleps)
float const & ht() const
Definition: small_tree.cpp:451
float PadBottomMargin
Definition: styles.hpp:36