ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/root/toyMC_ejterm.C
Revision: 1.2
Committed: Tue Mar 30 01:32:32 2010 UTC (15 years, 1 month ago) by msegala
Content type: text/plain
Branch: MAIN
CVS Tags: V00-03-01, ZMorph_BASE_20100408, gak040610_morphing, HEAD
Branch point for: ZMorph-V00-03-01
Changes since 1.1: +73 -109 lines
Log Message:
msegala32910

File Contents

# User Rev Content
1 kukartse 1.1 #include <iostream>
2     #include <utility>
3     #include "TH1F.h"
4     #include "TObjArray.h"
5     #include "TFractionFitter.h"
6     #include "TFile.h"
7     #include "TTree.h"
8     #include "TChain.h"
9     #include "TRandom3.h"
10     #include "TCanvas.h"
11     #include "TSlider.h"
12     #include "TF1.h"
13    
14     using namespace std;
15    
16     class toyMC
17     {
18     public:
19    
20     toyMC();
21     toyMC(int nbins);
22     ~toyMC();
23    
24     void load_templates();
25     void load_syst_templates();
26     void load_ejets_templates();
27    
28     double print_chisq(TH1 * h1, TH1 * h2);
29    
30     //
31     //_____ Fix bkg2 fraction to qcd_frac _________________________________
32     // Do not fix if qcd_frac is negative
33     //
34     int do_fit(TH1F * _data, double qcd_frac=-1.0);
35     int generate_toy(int n_sig, int n_phys, int n_qcd);
36     int generate_toy_from_syst(int n_sig, int n_phys, int n_qcd);
37     void set_overflow_bins(TH1F * h);
38     void debug_hist(TH1F * h);
39     int fit_data(void);
40     int fit_data_stacked(void);
41    
42     void set_frame_style(TH1 * frame, TVirtualPad * pad, Float_t scale = 1.0);
43    
44     // returns pair<mean,width>
45     std::pair<double,double> do_toys(double n_sig, double n_bkg1, double n_bkg2, int n_exp=100);
46    
47     TH1F * data;
48     TH1F * ttbar;
49     TH1F * wzjets;
50     TH1F * qcd;
51    
52     // templates for systematics
53     TH1F * ttbar_s;
54     TH1F * wzjets_s;
55     TH1F * qcd_s;
56    
57     int n_bins;
58    
59     TObjArray * mc;
60     TFractionFitter * fit;
61     //
62     //_____ ensemble data __________________________________________________
63     //
64     int n_gen_sig,n_gen_bkg1,n_gen_bkg2;
65     TH1F * toy_data;
66     std::vector<double> v_mean;
67     std::vector<double> v_error;
68     std::vector<double> v_pull;
69    
70     private:
71     TRandom3 _random;
72     Float_t labelSize;
73     };
74    
75    
76     toyMC::toyMC(){
77     data = 0;
78     ttbar = 0;
79     wzjets = 0;
80     qcd = 0;
81     ttbar_s = 0;
82     wzjets_s = 0;
83     qcd_s = 0;
84     mc = 0;
85     fit = 0;
86     toy_data = 0;
87     n_bins = 50;
88     labelSize = 0.09;
89     }
90    
91    
92     toyMC::toyMC(int nbins){
93     data = 0;
94     ttbar = 0;
95     wzjets = 0;
96     qcd = 0;
97     ttbar_s = 0;
98     wzjets_s = 0;
99     qcd_s = 0;
100     mc = 0;
101     fit = 0;
102     toy_data = 0;
103     n_bins = nbins;
104     labelSize = 0.09;
105     }
106    
107    
108     toyMC::~toyMC(){
109     delete data;
110     delete ttbar;
111     delete wzjets;
112     delete qcd;
113     delete mc;
114     delete fit;
115     delete toy_data;
116     }
117    
118    
119     void toyMC::load_templates(){
120 msegala 1.2 /*
121 kukartse 1.1 TFile * data_file = new TFile("./ejterm/TMVApp-fake-data.root","READ");
122     TTree * t_data = (TTree *)data_file->Get("classifier");
123     //
124     //_____ nominal templates
125     //
126     TFile * ttbar_phys_template_file = new TFile("./ejterm/TMVA.root","READ");
127     TFile * qcd_template_file = new TFile("./ejterm/TMVA.root","READ");
128     //
129     TTree * t_ttbar = (TTree *)ttbar_phys_template_file->Get("TestTree");
130     TTree * t_phys = (TTree *)ttbar_phys_template_file->Get("TestTree");
131     TTree * t_qcd = (TTree *)qcd_template_file->Get("TestTree");
132 msegala 1.2 */
133    
134    
135     TFile * data_file = new TFile("/uscms_data/d2/mike1886/LeptonNeutrinoSwitch/AnalyzedSamplesFor7TeV/200pbTemplates/TMVApp-data-200pb.root","READ");
136    
137     TFile * phys_template_file = new TFile("/uscms_data/d2/mike1886/LeptonNeutrinoSwitch/AnalyzedSamplesFor7TeV/MinNumberofJets3/200pbTemplates/TMVApp-WZJets-200pbZMassMorphing_ONLYWJETS.root");
138    
139     TFile * ttbar_phys_template_file = new TFile("/uscms/home/mike1886/test/CMSSW_2_2_6/src/LJMet/MultivariateAnalysis/macros/TMVA.root","READ");
140     //TFile * qcd_template_file = new TFile("/uscms_data/d2/mike1886/LeptonNeutrinoSwitch/LeptonSwitchMVA/QCDApp.root","READ");
141    
142     TTree * t_ttbar = (TTree *)ttbar_phys_template_file->Get("TestTree");
143     TTree * t_phys = (TTree *)phys_template_file->Get("classifier");
144     //TTree * t_phys = (TTree *)ttbar_phys_template_file->Get("TestTree");
145    
146     TTree * t_data = (TTree *)data_file->Get("classifier");
147    
148    
149    
150    
151    
152    
153    
154    
155    
156    
157    
158    
159 kukartse 1.1
160     delete data;
161     delete ttbar;
162     delete wzjets;
163 msegala 1.2 //delete qcd;
164 kukartse 1.1 data = new TH1F("data" ,"data" ,n_bins, -0.8, 0.8);
165     ttbar = new TH1F("ttbar" ,"ttbar" ,n_bins, -0.8, 0.8);
166     wzjets = new TH1F("wzjets","wzjets",n_bins, -0.8, 0.8);
167 msegala 1.2 //qcd = new TH1F("qcd" ,"qcd" ,n_bins, -0.8, 0.8);
168 kukartse 1.1
169     //
170     data->SetMarkerStyle(21);
171     data->SetMarkerSize(0.7);
172     ttbar->SetFillColor(42);
173     wzjets->SetFillColor(46);
174 msegala 1.2 //qcd->SetFillColor(48);
175 kukartse 1.1
176     t_data -> Draw("MVA_BDT>>data","","goff");
177     t_ttbar -> Draw("MVA_BDT>>ttbar","type==1","goff");
178 msegala 1.2 //t_phys -> Draw("MVA_BDT>>wzjets","type==0","goff");
179     t_phys -> Draw("MVA_BDT>>wzjets","","goff");
180     //t_qcd -> Draw("MVA_BDT>>qcd","type==0","goff");
181 kukartse 1.1
182     set_overflow_bins(data);
183     set_overflow_bins(ttbar);
184     set_overflow_bins(wzjets);
185 msegala 1.2 //set_overflow_bins(qcd);
186 kukartse 1.1
187    
188    
189     TCanvas * c1 = new TCanvas("canvas1","canvas1",800,600);
190     c1->Divide(2,2);
191     TVirtualPad * pad = c1->cd(1);
192     data->SetTitle("");
193     data->SetNdivisions(5,"X");
194     data->SetNdivisions(5,"Y");
195     data->GetXaxis()->SetLabelSize( labelSize );
196     data->GetYaxis()->SetLabelSize( labelSize );
197     set_frame_style( data, pad, 1.2 );
198     data -> Draw();
199     pad->SaveAs("mu_data.eps");
200     pad = c1->cd(2);
201     ttbar->SetTitle("");
202     ttbar->SetNdivisions(5,"X");
203     ttbar->SetNdivisions(5,"Y");
204     ttbar->GetXaxis()->SetLabelSize( labelSize );
205     ttbar->GetYaxis()->SetLabelSize( labelSize );
206     set_frame_style( ttbar, pad, 1.2 );
207     ttbar -> Draw();
208     pad->SaveAs("mu_ttbar_template.eps");
209     pad = c1->cd(3);
210     wzjets->SetTitle("");
211     wzjets->SetNdivisions(5,"X");
212     wzjets->SetNdivisions(5,"Y");
213     wzjets->GetXaxis()->SetLabelSize( labelSize );
214     wzjets->GetYaxis()->SetLabelSize( labelSize );
215     set_frame_style( wzjets, pad, 1.2 );
216     wzjets -> Draw();
217     pad->SaveAs("mu_wzjets_template.eps");
218 msegala 1.2 //pad = c1->cd(4);
219     //qcd->SetTitle("");
220     //qcd->SetNdivisions(5,"X");
221     //qcd->SetNdivisions(5,"Y");
222     //qcd->GetXaxis()->SetLabelSize( labelSize );
223     //qcd->GetYaxis()->SetLabelSize( labelSize );
224     //set_frame_style( qcd, pad, 1.2 );
225     //qcd -> Draw();
226     //pad->SaveAs("mu_qcd_template.eps");
227 kukartse 1.1
228     // templates for the fitter
229     delete mc;
230     mc = new TObjArray(3); // MC histograms are put in this array
231     mc->Add(ttbar);
232     mc->Add(wzjets);
233 msegala 1.2 //mc->Add(qcd);
234 kukartse 1.1 }
235    
236    
237     void toyMC::load_syst_templates(){
238 msegala 1.2
239     /*
240 kukartse 1.1 TFile * ttbar_template_file = new TFile("TMVApp-TTJets-jes-down-04jul2009.root");
241     TFile * phys_template_file = new TFile("TMVApp-phys-jes-down-04jul2009.root");
242     TFile * qcd_template_file = new TFile("TMVApp-qcd-jes-down-04jul2009.root");
243 msegala 1.2
244 kukartse 1.1 TTree * t_ttbar = (TTree *)ttbar_template_file->Get("classifier");
245     TTree * t_phys = (TTree *)phys_template_file->Get("classifier");
246     TTree * t_qcd = (TTree *)qcd_template_file->Get("classifier");
247 msegala 1.2 */
248    
249    
250     TFile * ttbar_template_file = new TFile("/uscms/home/mike1886/test/CMSSW_2_2_6/src/LJMet/MultivariateAnalysis/macros/TMVA.root","READ");
251    
252     TFile * phys_template_file = new TFile("/uscms_data/d2/mike1886/LeptonNeutrinoSwitch/AnalyzedSamplesFor7TeV/MinNumberofJets3/200pbTemplates/TMVApp-WZJets-LeptonToNeutrinoSwitch_200pbZMassMorphing_ONLYWJETS.root");
253    
254    
255     TTree * t_ttbar = (TTree *)ttbar_template_file->Get("TestTree");
256     TTree * t_phys = (TTree *)phys_template_file->Get("classifier");
257    
258 kukartse 1.1
259     delete ttbar_s;
260     delete wzjets_s;
261 msegala 1.2 //delete qcd_s;
262 kukartse 1.1 ttbar_s = new TH1F("ttbar_s" ,"ttbar_s" ,n_bins, -0.8, 0.8);
263     wzjets_s = new TH1F("wzjets_s","wzjets_s",n_bins, -0.8, 0.8);
264 msegala 1.2 //qcd_s = new TH1F("qcd_s" ,"qcd_s" ,n_bins, -0.8, 0.8);
265 kukartse 1.1
266     //
267     ttbar_s->SetFillColor(42);
268     //ttbar_s->SetFillStyle(4050);
269     wzjets_s->SetFillColor(46);
270     //wzjets_s->SetFillStyle(4050);
271 msegala 1.2 //qcd_s->SetFillColor(48);
272 kukartse 1.1 //TSlider *slider = 0;
273     //
274    
275 msegala 1.2 t_ttbar -> Draw("MVA_BDT>>ttbar_s","type==1","goff");
276     //t_ttbar -> Draw("MVA_BDT>>ttbar_s","","goff");
277 kukartse 1.1 //t_phys -> Draw("MVA_BDT>>wzjets_s","type==0","goff");
278     t_phys -> Draw("MVA_BDT>>wzjets_s","","goff");
279 msegala 1.2 //t_qcd -> Draw("MVA_BDT>>qcd_s","","goff");
280 kukartse 1.1
281     set_overflow_bins(ttbar_s);
282     set_overflow_bins(wzjets_s);
283 msegala 1.2 //set_overflow_bins(qcd_s);
284    
285 kukartse 1.1 TCanvas * c1 = new TCanvas("canvas1","canvas1",800,600);
286     c1->Divide(2,2);
287     TVirtualPad * pad = c1->cd(1);
288     //
289     pad = c1->cd(2);
290     ttbar_s -> Draw();
291     pad->SaveAs("mu_ttbar_s_template.eps");
292     //
293     pad = c1->cd(3);
294     wzjets_s -> Draw();
295     pad->SaveAs("mu_wzjets_s_template.eps");
296     //
297 msegala 1.2 //pad = c1->cd(4);
298     //qcd_s -> Draw();
299     //pad->SaveAs("mu_qcd_s_template.eps");
300    
301 kukartse 1.1 }
302    
303    
304     void toyMC::load_ejets_templates(){
305     TFile * data_file = new TFile("./TMVApp-data-electron-18may2009.root","READ");
306     //TFile * data_file = new TFile("./TMVApp-data-electron_noqcd.root","READ");
307     //TFile * data_file = new TFile("./TMVApp-data-electron_wzfastsim-08may2009.root","READ");
308     //TFile * data_file = new TFile("./TMVApp-data-electron_wzfastsim-08may2009.root","READ");
309     TFile * ttbar_phys_template_file = new TFile("./TMVA_wjets.root","READ");
310     TFile * qcd_template_file = new TFile("TMVApp-qcd-electron-18may2009.root","READ");
311    
312     //TChain * t_data = new TChain("classifier");
313     //t_data->Add("./TMVApp-data-electron_noqcd.root");
314     //t_data->Add("./TMVApp-data-electron_qcdonly.root");
315     //t_data->Add("./TMVApp-data-electron_200.root");
316    
317     TTree * t_data = (TTree *)data_file->Get("classifier");
318     TTree * t_ttbar_phys = (TTree *)ttbar_phys_template_file->Get("TestTree");
319     TTree * t_qcd = (TTree *)qcd_template_file->Get("classifier");
320    
321     delete data;
322     delete ttbar;
323     delete wzjets;
324     delete qcd;
325    
326     data = new TH1F("data" ,"data" ,n_bins, -0.8, 0.8);
327     ttbar = new TH1F("ttbar" ,"ttbar" ,n_bins, -0.8, 0.8);
328     wzjets = new TH1F("wzjets","wzjets",n_bins, -0.8, 0.8);
329     qcd = new TH1F("qcd" ,"qcd" ,n_bins, -0.8, 0.8);
330    
331     //
332     data->SetMarkerStyle(21);
333     data->SetMarkerSize(0.7);
334     //data->SetFillStyle(4050);
335     ttbar->SetFillColor(42);
336     //ttbar->SetFillStyle(4050);
337     wzjets->SetFillColor(46);
338     //wzjets->SetFillStyle(4050);
339     qcd->SetFillColor(48);
340     //TSlider *slider = 0;
341     //
342    
343     TCanvas * c1 = new TCanvas("canvas1","canvas1",800,600);
344     c1->Divide(2,2);
345     TVirtualPad * pad = c1->cd(1);
346     t_data -> Draw("MVA_BDT>>data");
347     pad->SaveAs("e_data.eps");
348     pad = c1->cd(2);
349     t_ttbar_phys -> Draw("MVA_BDT>>ttbar","type==1");
350     pad->SaveAs("e_ttbar_template.eps");
351     pad = c1->cd(3);
352     t_ttbar_phys -> Draw("MVA_BDT>>wzjets","type==0");
353     pad->SaveAs("e_wzjets_template.eps");
354     pad = c1->cd(4);
355     t_qcd -> Draw("MVA_BDT>>qcd");
356     pad->SaveAs("e_qcd_template.eps");
357    
358     //data_file->Close();
359     //ttbar_phys_template_file->Close();
360     //qcd_template_file->Close();
361    
362     delete mc;
363     mc = new TObjArray(3); // MC histograms are put in this array
364     mc->Add(ttbar);
365     mc->Add(wzjets);
366     mc->Add(qcd);
367     }
368    
369    
370     int toyMC::do_fit(TH1F * _data, double qcd_frac){
371     delete fit;
372     fit = new TFractionFitter(_data, mc); // initialise
373    
374     fit->Constrain(1,0.0,1.0); // constrain fraction 1 to be between 0 and 1
375     fit->Constrain(2,0.0,1.0);
376     if (qcd_frac<0.0){
377     fit->Constrain(3,0.0,1.0);
378     }
379     else{
380     fit->Constrain(3,qcd_frac-0.000001,qcd_frac+0.000001); // constrain fraction 1 to be between 0 and 1
381     }
382     fit->SetRangeX(1,n_bins); // use only some bins to fit
383     Int_t status = fit->Fit(); // perform the fit
384     if (status!=0) status = fit->Fit();
385     cout << "fit status: " << status << endl;
386     return status;
387     }
388    
389     /*
390     int toyMC::fit1(TH1F * _data, double bkg2_low = 0.0, bkg2_high = 1.0){
391     delete fit;
392     fit = new TFractionFitter(_data, mc); // initialise
393    
394     fit->Constrain(1,data_low,data_high); // constrain fraction 1 to be between 0 and 1
395     fit->Constrain(2,bkg1_low,bkg1_high); // constrain fraction 1 to be between 0 and 1
396     fit->Constrain(3,bkg2_low,bkg2_high); // constrain fraction 1 to be between 0 and 1
397     fit->SetRangeX(first_bin,last_bin); // use only some bins to fit
398     Int_t status = fit->Fit(); // perform the fit
399     if (status!=0) status = fit->Fit(); // make one more attempt to fit if the first one fails
400     cout << "fit status: " << status << endl;
401     return status;
402     }
403     */
404    
405     //
406     //_____ generates a pseudoexperiment, given n_events, smears by Poisson _
407     //
408     int toyMC::generate_toy(int n_sig, int n_bg, int n_qcd){
409     int n_events=0;
410     delete toy_data;
411    
412     toy_data = new TH1F("toy_data" ,"toy_data" ,n_bins, -0.8, 0.8);
413    
414     int _nsig = _random.Poisson(n_sig);
415     int _nbg = _random.Poisson(n_bg);
416     int _nqcd = _random.Poisson(n_qcd);
417     //int _nqcd = n_qcd;
418    
419     n_gen_sig = _nsig;
420     n_gen_bkg1 = _nbg;
421     n_gen_bkg2 = _nqcd;
422     n_events = _nsig+_nbg+_nqcd;
423    
424     for (int i=0;i<_nsig;i++){
425     toy_data->Fill(ttbar->GetRandom());
426     }
427     for (int i=0;i<_nbg;i++){
428     toy_data->Fill(wzjets->GetRandom());
429     }
430    
431     for (int i=0;i<_nqcd;i++){
432     toy_data->Fill(qcd->GetRandom());
433     }
434     //toy_data->Draw();
435     set_overflow_bins(toy_data);
436     return n_events;
437     //return _nsig;
438     }
439    
440     //
441     //_____ generates a pseudoexperiment, given n_events, smears by Poisson _
442     //
443     int toyMC::generate_toy_from_syst(int n_sig, int n_bg, int n_qcd){
444     int n_events=0;
445     delete toy_data;
446    
447     toy_data = new TH1F("toy_data" ,"toy_data" ,n_bins, -0.8, 0.8);
448    
449     int _nsig = _random.Poisson(n_sig);
450     int _nbg = _random.Poisson(n_bg);
451     int _nqcd = _random.Poisson(n_qcd);
452     //int _nqcd = n_qcd;
453    
454     n_gen_sig = _nsig;
455     n_gen_bkg1 = _nbg;
456     n_gen_bkg2 = _nqcd;
457     n_events = _nsig+_nbg+_nqcd;
458    
459     for (int i=0;i<_nsig;i++){
460     toy_data->Fill(ttbar_s->GetRandom());
461     }
462     for (int i=0;i<_nbg;i++){
463     toy_data->Fill(wzjets_s->GetRandom());
464     }
465    
466     for (int i=0;i<_nqcd;i++){
467     toy_data->Fill(qcd_s->GetRandom());
468     }
469     //toy_data->Draw();
470     set_overflow_bins(toy_data);
471     return n_events;
472     //return _nsig;
473     }
474    
475     void toyMC::set_overflow_bins(TH1F * h){
476     Int_t _last_bin = h->GetNbinsX();
477     Double_t _overflow = h->GetBinContent(_last_bin+1);
478     Int_t n_entries = (Int_t)h->GetEntries();
479     //h->ResetBit(TH1::kCanRebin);
480     h->AddBinContent(1,h->GetBinContent(0));
481     h->AddBinContent(_last_bin,_overflow);
482     h->SetBinContent(0,0);
483     h->SetBinContent(_last_bin+1,0);
484     h->SetEntries(n_entries);
485     }
486    
487    
488     void toyMC::debug_hist(TH1F * h){
489     cout << "DEBUG: number of entries: " << h->GetEntries() << endl;
490     cout << "DEBUG: underflow: " << h->GetBinContent(0) << endl;
491     cout << "DEBUG: bin1: " << h->GetBinContent(1) << endl;
492     Int_t _last_bin = h->GetNbinsX();
493     cout << "DEBUG: last bin: " << h->GetBinContent(_last_bin) << endl;
494     cout << "DEBUG: overflow: " << h->GetBinContent(_last_bin+1) << endl;
495     }
496    
497    
498     int toyMC::fit_data(){
499     //load_templates();
500     //load_ejets_templates();
501     int _res = do_fit(data,0.0001);
502     int n_data = data->GetEntries();
503     TH1F * result = (TH1F*) fit->GetPlot();
504     result->SetFillColor(16);
505     //result->SetFillStyle(4050);
506     data->SetMinimum(0);
507     TCanvas * c = new TCanvas("canvas2","canvas2",800,600);
508     data->Draw("Ep");
509     result->Draw("same");
510     //
511     Double_t value,error;
512     fit->GetResult(0,value,error);
513     ttbar->Scale(n_data*value/ttbar->Integral());
514     ttbar->Draw("same");
515     //
516     fit->GetResult(1,value,error);
517     wzjets->Scale(n_data*value/wzjets->Integral());
518     wzjets->Draw("same");
519     //
520     fit->GetResult(2,value,error);
521     qcd->Scale(n_data*value/qcd->Integral());
522     qcd->Draw("same");
523     data->Draw("Ep:same");
524     //
525     c->SaveAs("fit.eps");
526     return _res;
527     }
528    
529    
530    
531     int toyMC::fit_data_stacked(){
532     //load_templates();
533     //load_ejets_templates();
534     int _res = do_fit(data,0.0001);
535     int n_data = data->GetEntries();
536     TH1F * result = (TH1F*) fit->GetPlot();
537     result->SetFillColor(16);
538     //result->SetFillStyle(4050);
539     data->SetMinimum(0);
540     TCanvas * c = new TCanvas("canvas2","canvas2",800,600);
541     data->Draw("Ep");
542     //result->Draw("same");
543     //
544     Double_t value,error;
545     fit->GetResult(0,value,error);
546     ttbar->Scale(n_data*value/ttbar->Integral());
547     //
548     fit->GetResult(1,value,error);
549     wzjets->Scale(n_data*value/wzjets->Integral());
550     //
551     fit->GetResult(2,value,error);
552     qcd->Scale(n_data*value/qcd->Integral());
553     //
554     wzjets->Add(qcd);
555     ttbar->Add(wzjets);
556     //
557     ttbar->Draw("same");
558     wzjets->Draw("same");
559     qcd->Draw("same");
560     data->Draw("Ep:same");
561     //
562     c->SaveAs("fit.eps");
563     return _res;
564     }
565    
566    
567    
568     std::pair<double,double> toyMC::do_toys(double n_sig, double n_bkg1, double n_bkg2, int n_exp){
569     pair<double, double> result;
570     v_mean.clear();
571     v_error.clear();
572     v_pull.clear();
573     double n_total = n_sig+n_bkg1+n_bkg2;
574    
575 msegala 1.2 TH1F * toy_mean = new TH1F("mean" ,"ttbar signal yield" ,50, n_sig-15.0*sqrt(n_sig), n_sig+15.0*sqrt(n_sig));
576     TH1F * toy_error = new TH1F("error" ,"ttbar signal yield error" ,50, 0, 5.0*sqrt(n_sig));
577 kukartse 1.1 TH1F * toy_pull = new TH1F("pull" ,"pull" ,50, -3, 3);
578     toy_mean->SetFillColor(44);
579     toy_error->SetFillColor(44);
580     toy_pull->SetFillColor(44);
581     for (int i=0;i<n_exp;i++){
582     double n_bkg2_smeared = n_bkg2;
583     /************
584     //_____ smear qcd rate due to xsec uncertainty
585     //
586     n_bkg2_smeared = -1.0;
587     while (n_bkg2_smeared < 0.0){
588     n_bkg2_smeared = _random.Gaus(n_bkg2,n_bkg2);
589     }
590     *************/
591     //generate_toy(n_sig,n_bkg1,n_bkg2_smeared);
592     generate_toy_from_syst(n_sig,n_bkg1,n_bkg2_smeared);
593     int _ttbar = n_gen_sig;
594     double n_gen_total = (double)(n_gen_sig + n_gen_bkg1 + n_gen_bkg2);
595     //
596     //_____ nominal
597     //
598     double qcd_rate = 9.5;
599     //
600     //_____ nominal
601     //
602     //double qcd_rate = 6.175;
603     //double qcd_rate = 16.625;
604     //
605     //double qcd_rate = 19.0;
606     //double qcd_rate = 0.0;
607     // ABCD uncertainty:
608     //double qcd_rate = 16.6;
609     //double qcd_rate = 2.4;
610     double qcd_rate_err = 6.7;
611     double qcd_rate_smeared = qcd_rate;
612     /*************
613     //_____ smearing of the ABCD prediction
614     //
615     double qcd_rate_smeared = -1.0;
616     while (qcd_rate_smeared < 0.0){
617     qcd_rate_smeared = _random.Gaus(qcd_rate,qcd_rate_err);
618     }
619     **************/
620     double qcd_frac = qcd_rate_smeared/(double)(n_gen_sig+n_gen_bkg1+n_gen_bkg2);
621     int status = do_fit(toy_data, qcd_frac);
622     if (status==0){
623     Double_t value,error;
624     fit->GetResult(0,value,error);
625     toy_mean->Fill(value*n_gen_total);
626     v_mean.push_back(value*n_gen_total);
627     v_error.push_back(error*n_gen_total);
628     toy_error->Fill(error*n_gen_total);
629     //toy_pull->Fill((value*n_gen_total-(Double_t)_ttbar)/n_gen_total/error);
630     toy_pull->Fill((value*n_gen_total-n_sig)/n_gen_total/error);
631     v_pull.push_back((value*n_gen_total-n_sig)/n_gen_total/error);
632     }
633     }
634     TCanvas * c = new TCanvas("canvas","canvas",800,600);
635     c->Divide(2,2);
636     TVirtualPad * pad = c->cd(1);
637     toy_mean->Fit("gaus");
638     TF1 * _fit = toy_mean->GetFunction("gaus");
639     result.first = _fit->GetParameter(1);
640     result.second = _fit->GetParError(1);
641     //result.second = _fit->GetParameter(2);
642     pad->SaveAs("mean.eps");
643     pad = c->cd(2);
644     toy_error->Draw();
645     pad->SaveAs("error.eps");
646     pad = c->cd(3);
647     toy_pull->Fit("gaus");
648     pad->SaveAs("pull.eps");
649    
650     return result;
651     }
652    
653    
654     // set frame styles
655     void toyMC::set_frame_style( TH1* frame, TVirtualPad * pad, Float_t scale )
656     {
657     frame->SetTitle("");
658     frame->SetLabelOffset( 0.012, "X" );// label offset on x axis
659     frame->SetLabelOffset( 0.012, "Y" );// label offset on x axis
660     frame->GetXaxis()->SetTitle( "" );
661     frame->GetYaxis()->SetTitle( "" );
662     frame->GetXaxis()->SetTitleOffset( 1.25 );
663     frame->GetYaxis()->SetTitleOffset( 1.22 );
664     frame->GetXaxis()->SetTitleSize( 0.045*scale );
665     frame->GetYaxis()->SetTitleSize( 0.045*scale );
666     Float_t labelSize = 0.06*scale;
667     frame->GetXaxis()->SetLabelSize( labelSize );
668     frame->GetYaxis()->SetLabelSize( labelSize );
669    
670     // global style settings
671     pad->SetTicks();
672     pad->SetLeftMargin ( 0.108*scale );
673     pad->SetRightMargin ( 0.050*scale );
674     pad->SetBottomMargin( 0.120*scale );
675     }
676    
677     double toyMC::print_chisq( TH1 * h1, TH1 * h2 ){
678     double result = -1.0;
679     int n_bins = h1->GetNbinsX();
680     double chisq = 0.0;
681     for (int i=0; i!=n_bins; i++){
682     double bin1 = h1->GetBinContent(i+1);
683     double bin2 = h2->GetBinContent(i+1);
684     //chisq+=(h1->GetBinContent(i+1)-h2->GetBinContent(i+1))*(h1->GetBinContent(i+1)-h2->GetBinContent(i+1));
685     double err1 = sqrt(bin1);
686     chisq+=(bin1-bin2)*(bin1-bin2)/err1/err1;
687     }
688     result = chisq;// (double)n_bins;
689     cout << result << endl;
690     return result;
691     }