ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/EdgeLimit.C
Revision: 1.30
Committed: Thu Jun 27 06:02:20 2013 UTC (11 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.29: +2 -2 lines
Log Message:
Fixed axis labels for scan plots

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2    
3 buchmann 1.21
4    
5 buchmann 1.10 #include <TVirtualIndex.h>
6    
7 buchmann 1.2 #include <RooRealVar.h>
8     #include <RooArgSet.h>
9     #include <RooDataSet.h>
10 buchmann 1.4 #include <RooMCStudy.h>
11     #include <RooCategory.h>
12    
13 buchmann 1.5 #include <RooPlot.h>
14 buchmann 1.21 #include <RooGaussian.h>
15     #include <RooConstVar.h>
16 buchmann 1.5 #include <RooSimultaneous.h>
17     #include <RooAddPdf.h>
18     #include <RooFitResult.h>
19     #include <RooVoigtian.h>
20     #include <RooMsgService.h>
21    
22 buchmann 1.3 #include <RooStats/ModelConfig.h>
23     #include "RooStats/ProfileLikelihoodCalculator.h"
24     #include "RooStats/LikelihoodInterval.h"
25     #include "RooStats/HypoTestResult.h"
26     #include "RooStats/SimpleLikelihoodRatioTestStat.h"
27     #include "RooStats/ProfileLikelihoodTestStat.h"
28     #include "RooStats/HybridCalculatorOriginal.h"
29     #include "RooStats/HypoTestInverterOriginal.h"
30    
31 buchmann 1.5 //#include "ParametrizedEdge.C"
32 buchmann 1.27 #include "ExtendedMath.C"
33 buchmann 1.8 #include "EdgeModules/RooSUSYTPdf.cxx"
34     #include "EdgeModules/RooSUSYBkgPdf.cxx"
35 buchmann 1.27 #include "EdgeModules/RooSUSYCompleteBkgPdf.cxx"
36    
37 buchmann 1.5
38 buchmann 1.23 #include "md5/md5.h"
39     #include "md5/md5.C"
40 buchmann 1.2
41 buchmann 1.1 using namespace std;
42     using namespace PlottingSetup;
43    
44    
45 buchmann 1.2
46    
47 buchmann 1.1 ShapeDroplet LimitsFromEdge(float low_fullCLs, float high_CLs, TTree *events, string addcut, string name, string mcjzb, string datajzb, vector<float> jzbbins, float jzbpeakerrormc, float jzbpeakerrordata) {
48     write_error(__FUNCTION__,"Not implemented edge limits yet (returning crap)");
49     ShapeDroplet beta;beta.observed=-12345;beta.SignalIntegral=1;return beta;
50     }
51    
52    
53     void PrepareEdgeShapes(string mcjzb, string datajzb, vector<float> jzbbins, float jzbpeakerrordata) {
54     write_error(__FUNCTION__,"Not implemented edge shape storage yet");
55     }
56    
57    
58 buchmann 1.2 ///------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
59    
60    
61     namespace EdgeFitter {
62    
63     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, float jzb_cut, int icut, int is_data, TCut cut, TTree*);
64     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree*);
65 buchmann 1.7 void getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp);
66 buchmann 1.2 void InitializeVariables(float _mllmin, float _mllmax, float _jzbmax, TCut _cut);
67     void PrepareDatasets(int);
68 buchmann 1.15 void DrawDatasetContent(int);
69 buchmann 1.5 void DoFit(int is_data, float jzb_cut);
70 buchmann 1.2 string RandomStorageFile();
71     Yield Get_Z_estimate(float,int);
72     Yield Get_T_estimate(float,int);
73 buchmann 1.7 float calcExclusion(RooWorkspace *ws, RooDataSet data, bool calcExclusion);
74     vector<RooDataSet> generateToys(RooWorkspace *ws, int nToys);
75 buchmann 1.4 void prepareLimits(RooWorkspace *ws, bool ComputeBands);
76     TGraph* prepareLM(float mass, float nEv);
77 buchmann 1.2
78     float jzbmax;
79     float mllmin;
80     float mllmax;
81     TCut cut;
82    
83     RooDataSet* AllData;
84 buchmann 1.24 RooDataSet* eeSample;
85     RooDataSet* mmSample;
86 buchmann 1.27 RooDataSet* OFSample;
87     RooDataSet* SFSample;
88 buchmann 1.2
89 buchmann 1.6 bool MarcoDebug=true;
90 buchmann 1.11
91     float FixedMEdge=-1;
92 buchmann 1.21 float FixedMEdgeChi2_H1=-1;
93     float FixedMEdgeChi2_H0=-1;
94 buchmann 1.11
95 buchmann 1.14 bool RejectPointIfNoConvergence=false;
96    
97     string Mode="UndefinedMode";
98    
99 buchmann 1.21 bool AllowTriangle=true;
100 buchmann 1.2 }
101    
102 buchmann 1.4 TGraph* EdgeFitter::prepareLM(float mass, float nEv) {
103     float massLM[1];
104     massLM[0]=mass;
105     float accEffLM[1];
106     accEffLM[0]=nEv/PlottingSetup::luminosity;
107     TGraph *lm = new TGraph(1, massLM, accEffLM);
108     lm->GetXaxis()->SetNoExponent(true);
109     lm->GetXaxis()->SetTitle("m_{cut} [GeV]");
110     lm->GetYaxis()->SetTitle("#sigma #times A [pb] 95% CL UL");
111     lm->GetXaxis()->SetLimits(0.,300.);
112     lm->GetYaxis()->SetRangeUser(0.,0.08);
113     lm->SetMarkerStyle(34);
114     lm->SetMarkerColor(kRed);
115     return lm;
116     }
117    
118 buchmann 1.7 vector<RooDataSet> EdgeFitter::generateToys(RooWorkspace *ws, int nToys) {
119 buchmann 1.10 ws->ls();
120 buchmann 1.4 ws->var("nSig")->setVal(0.);
121     ws->var("nSig")->setConstant(true);
122     RooFitResult* fit = ws->pdf("combModel")->fitTo(*ws->data("data_obs"),RooFit::Save());
123 buchmann 1.7 vector<RooDataSet> theToys;
124 buchmann 1.4
125     RooMCStudy mcEE(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"EE"));
126     mcEE.generate(nToys,44,true);
127     RooMCStudy mcMM(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"MM"));
128     mcMM.generate(nToys,44,true);
129     RooMCStudy mcOSOF(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"OSOF"));
130     mcOSOF.generate(nToys,44,true);
131    
132 buchmann 1.10 RooRealVar mll("m_{ll}","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
133 buchmann 1.4 RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
134     RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
135     RooRealVar jzb("jzb","jzb",-jzbmax,jzbmax,"GeV/c");
136     RooRealVar weight("weight","weight",0,1000,"");
137     RooArgSet observables(mll,jzb,id1,id2,weight);
138    
139     for(int i=0;i<nToys;i++) {
140     RooDataSet* toyEE = (RooDataSet*)mcEE.genData(i);
141     RooDataSet* toyMM = (RooDataSet*)mcMM.genData(i);
142     RooDataSet* toyOSOF = (RooDataSet*)mcOSOF.genData(i);
143     stringstream toyname;
144     toyname << "theToy_" << i;
145     write_warning(__FUNCTION__,"Problem while adding toys");
146 buchmann 1.7 RooDataSet toyData = RooDataSet(toyname.str().c_str(),toyname.str().c_str(),observables,RooFit::Index(const_cast<RooCategory&>(*ws->cat("cat"))),RooFit::Import("OSOF",*toyOSOF),RooFit::Import("EE",*toyEE),RooFit::Import("MM",*toyMM));
147     theToys.push_back(toyData);
148 buchmann 1.4 }
149     ws->var("nSig")->setVal(17.0);
150     ws->var("nSig")->setConstant(false);
151     return theToys;
152     }
153    
154 buchmann 1.7 void EdgeFitter::getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp) {
155 buchmann 1.4 TH1F *gauLimit = new TH1F("gausLimit","gausLimit",60,0.,80./PlottingSetup::luminosity);
156     vector<float> theLimits;
157 buchmann 1.9 for(int itoy=0;itoy<(int)theToys.size();itoy++) {
158 buchmann 1.7 float theLimit = calcExclusion(ws,theToys[itoy],false);
159 buchmann 1.4 if(theLimit > 0 ) gauLimit->Fill(theLimit);
160     }
161     const Int_t nQ = 4;
162     Double_t yQ[nQ] = {0.,0.,0.,0.};
163     Double_t xQ[nQ] = {0.022750132,0.1586552539,0.84134474609999998,0.977249868};
164     gauLimit->GetQuantiles(nQ,yQ,xQ);
165     median = gauLimit->GetMean();
166     // median = median1(gauLimit);
167     twoSigmaDown = abs(yQ[0]-median);
168     sigmaDown = abs(yQ[1]-median);
169     sigmaUp = abs(yQ[2]-median);
170     twoSigmaUp = abs(yQ[3]-median);
171 buchmann 1.18 dout << median * PlottingSetup::luminosity << " " << sigmaUp * PlottingSetup::luminosity << endl;
172 buchmann 1.4 }
173    
174     void EdgeFitter::prepareLimits(RooWorkspace *ws, bool ComputeBands) {
175     if(ComputeBands) {
176 buchmann 1.7 vector<RooDataSet> theToys = EdgeFitter::generateToys(ws,50);
177 buchmann 1.4 vector<float> medVals;
178     vector<float> medLimits;
179     vector<float> sigmaLimitsDown;
180     vector<float> sigmaLimitsUp;
181     vector<float> twoSigmaLimitsDown;
182     vector<float> twoSigmaLimitsUp;
183     for(int i=20;i<=320;i+=40) {
184     ws->var("nSig")->setVal(10.0);
185     medVals.push_back((float)i);
186     ws->var("m0")->setVal((float)i);
187     ws->var("m0")->setConstant(true);
188     float Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp;
189     EdgeFitter::getMedianLimit(ws,theToys,Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp);
190     medLimits.push_back(Smedian);
191     sigmaLimitsDown.push_back(SsigmaDown);
192     sigmaLimitsUp.push_back(SsigmaUp);
193     twoSigmaLimitsDown.push_back(StwoSigmaDown);
194     twoSigmaLimitsUp.push_back(StwoSigmaUp);
195     }
196     write_warning(__FUNCTION__,"Still need to store limits");
197     } else {
198     vector<float> theVals;
199     vector<float> theLimits;
200     for(int i=20;i<300;i+=5) {
201     ws->var("nSig")->setVal(0.0);
202     theVals.push_back((float)i);
203     ws->var("m0")->setVal((float)i);
204     ws->var("m0")->setConstant(true);
205 buchmann 1.7 // theLimits.push_back(calcExclusion(ws,(RooDataSet)*ws->data("data_obs"),true));
206     write_error(__FUNCTION__,"Error while casting roo data set");
207 buchmann 1.4 }
208    
209 buchmann 1.7 for(int i=0;i<(int)theLimits.size();i++) {
210 buchmann 1.4 if((theLimits[i]<2.0/PlottingSetup::luminosity)||(theLimits[i]>40.0/PlottingSetup::luminosity)) {
211 buchmann 1.18 dout << i << " : " << theVals[i] << endl;
212 buchmann 1.4 theLimits[i] = (theLimits[i+2]+theLimits[i-2])/2.0;
213     write_warning(__FUNCTION__,"Need to store limits");
214     }
215     write_warning(__FUNCTION__,"Need to store limits");
216     }
217     }
218     }
219    
220    
221 buchmann 1.7 float EdgeFitter::calcExclusion(RooWorkspace *ws, RooDataSet data, bool LoadDataObs) {
222     int numberOfToys=50;
223 buchmann 1.3 RooRealVar mu("mu","nSig",0,10000,"");
224     RooArgSet poi = RooArgSet(mu);
225     RooArgSet *nullParams = (RooArgSet*)poi.snapshot();
226     nullParams->setRealValue("nSig",0);
227     RooStats::ModelConfig *model = new RooStats::ModelConfig();
228     model->SetWorkspace(*ws);
229     model->SetPdf("combModel");
230     model->SetParametersOfInterest(poi);
231 buchmann 1.7 // if(LoadDataObs) data = (RooDataSet)*ws->data("data_obs");
232 buchmann 1.3
233 buchmann 1.7 RooStats::ProfileLikelihoodCalculator plc(data, *model);
234 buchmann 1.3 plc.SetNullParameters(*nullParams);
235     plc.SetTestSize(0.05);
236 buchmann 1.7
237 buchmann 1.3 RooStats::LikelihoodInterval* interval = plc.GetInterval();
238     RooStats::HypoTestResult *htr = plc.GetHypoTest();
239     double theLimit = interval->UpperLimit( mu );
240 buchmann 1.7 // double significance = htr->Significance();
241 buchmann 1.3
242     ws->defineSet("obs","nB");
243     ws->defineSet("poi","nSig");
244    
245     RooStats::ModelConfig b_model = RooStats::ModelConfig("B_model", ws);
246     b_model.SetPdf(*ws->pdf("combModel"));
247     b_model.SetObservables(*ws->set("obs"));
248     b_model.SetParametersOfInterest(*ws->set("poi"));
249     ws->var("nSig")->setVal(0.0); //# important!
250     b_model.SetSnapshot(*ws->set("poi"));
251    
252     RooStats::ModelConfig sb_model = RooStats::ModelConfig("S+B_model", ws);
253     sb_model.SetPdf(*ws->pdf("combModel"));
254     sb_model.SetObservables(*ws->set("obs"));
255     sb_model.SetParametersOfInterest(*ws->set("poi"));
256     ws->var("nSig")->setVal(64.0); //# important!
257     sb_model.SetSnapshot(*ws->set("poi"));
258    
259     RooStats::SimpleLikelihoodRatioTestStat slrts = RooStats::SimpleLikelihoodRatioTestStat(*b_model.GetPdf(),*sb_model.GetPdf());
260     slrts.SetNullParameters(*b_model.GetSnapshot());
261     slrts.SetAltParameters(*sb_model.GetSnapshot());
262     RooStats::ProfileLikelihoodTestStat profll = RooStats::ProfileLikelihoodTestStat(*b_model.GetPdf());
263    
264 buchmann 1.7 RooStats::HybridCalculatorOriginal hc = RooStats::HybridCalculatorOriginal(data, sb_model, b_model,0,0);
265 buchmann 1.3 hc.SetTestStatistic(2);
266 buchmann 1.7 hc.SetNumberOfToys(numberOfToys);
267 buchmann 1.3
268     RooStats::HypoTestInverterOriginal hcInv = RooStats::HypoTestInverterOriginal(hc,*ws->var("nSig"));
269     hcInv.SetTestSize(0.05);
270     hcInv.UseCLs(true);
271     hcInv.RunFixedScan(5,theLimit-0.5,theLimit+0.5);;
272     RooStats::HypoTestInverterResult* hcInt = hcInv.GetInterval();
273     float ulError = hcInt->UpperLimitEstimatedError();
274     theLimit = hcInt->UpperLimit();
275 buchmann 1.18 dout << "Found upper limit : " << theLimit << "+/-" << ulError << endl;
276 buchmann 1.3
277     return theLimit/PlottingSetup::luminosity;
278    
279     }
280    
281 buchmann 1.23 string GetSkimName(int isample) {
282     return removefunnystring(allsamples.collection[isample].filename);
283     }
284    
285 buchmann 1.2 TTree* SkimTree(int isample) {
286 buchmann 1.23 string TreeName = GetSkimName(isample);
287 buchmann 1.20 cout << "About to skim " << TreeName << " for sample id " << isample << endl;
288 buchmann 1.23 TTree* newTree = new TTree(TreeName.c_str(),TreeName.c_str());
289 buchmann 1.15
290     float mll,edgeWeight;
291     int id1,id2;
292    
293     newTree->Branch("edgeWeight",&edgeWeight,"edgeWeight/F");
294     newTree->Branch("mll",&mll,"mll/F");
295     newTree->Branch("id1",&id1,"id1/I");
296     newTree->Branch("id2",&id2,"id2/I");
297 buchmann 1.20 newTree->Branch("isample",&isample,"isample/I");
298 buchmann 1.15
299 buchmann 1.2 float xsweight=1.0;
300     if(allsamples.collection[isample].is_data==false) xsweight=luminosity*allsamples.collection[isample].weight;
301     if(EdgeFitter::MarcoDebug) {
302 buchmann 1.20 dout << " Going to reduce it with cut " << EdgeFitter::cut << endl;
303 buchmann 1.18 dout << " Original tree contains " << allsamples.collection[isample].events->GetEntries() << endl;
304 buchmann 1.2 }
305 buchmann 1.15
306 buchmann 1.6 float tmll;
307 buchmann 1.15 int tid1,tid2;
308 buchmann 1.6 allsamples.collection[isample].events->SetBranchAddress("mll",&tmll);
309 buchmann 1.15 allsamples.collection[isample].events->SetBranchAddress("id1",&tid1);
310     allsamples.collection[isample].events->SetBranchAddress("id2",&tid2);
311 buchmann 1.6
312 buchmann 1.2 TTreeFormula *select = new TTreeFormula("select", EdgeFitter::cut, allsamples.collection[isample].events);
313 buchmann 1.10 TTreeFormula *Weight = new TTreeFormula("Weight", cutWeight, allsamples.collection[isample].events);
314 buchmann 1.15
315 buchmann 1.2 float wgt=1.0;
316     for (Int_t entry = 0 ; entry < allsamples.collection[isample].events->GetEntries() ; entry++) {
317     allsamples.collection[isample].events->LoadTree(entry);
318     if (select->EvalInstance()) {
319     allsamples.collection[isample].events->GetEntry(entry);
320 buchmann 1.15 mll=tmll;
321     id1=tid1;
322     id2=tid2;
323 buchmann 1.10 wgt=Weight->EvalInstance();
324 buchmann 1.6 edgeWeight=wgt*xsweight;
325 buchmann 1.2 newTree->Fill();
326     }
327     }
328    
329 buchmann 1.20 if(EdgeFitter::MarcoDebug) dout << " Reduced tree contains " << newTree->GetEntries() << " entries " << endl;
330 buchmann 1.2 return newTree;
331     }
332    
333     void EdgeFitter::InitializeVariables(float _mllmin, float _mllmax, float _jzbmax, TCut _cut) {
334     mllmin=_mllmin;
335     mllmax=_mllmax;
336     jzbmax=_jzbmax;
337     cut=_cut;
338     }
339 buchmann 1.10
340     TTree* MergeTrees(vector<TTree*> trees) {
341 buchmann 1.23 assert(trees.size()>0);
342 buchmann 1.15 TTree * newtree = (TTree*)trees[0]->CloneTree(0);
343 buchmann 1.20 newtree->SetTitle("FullTree");
344     newtree->SetName("FullTree");
345 buchmann 1.10 trees[0]->GetListOfClones()->Remove(newtree);
346     trees[0]->ResetBranchAddresses();
347     newtree->ResetBranchAddresses();
348    
349 buchmann 1.15 for(int itree=0;itree<trees.size();itree++) {
350 buchmann 1.10 newtree->CopyAddresses(trees[itree]);
351     Long64_t nentries = trees[itree]->GetEntries();
352     for (Long64_t iEntry=0;iEntry<nentries;iEntry++) {
353     trees[itree]->GetEntry(iEntry);
354     newtree->Fill();
355     }
356     trees[itree]->ResetBranchAddresses(); // Disconnect from new tree
357     if (newtree->GetTreeIndex()) {
358     newtree->GetTreeIndex()->Append(trees[itree]->GetTreeIndex(),kTRUE);
359     }
360     if (newtree && newtree->GetTreeIndex()) {
361     newtree->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
362     }
363     }
364     return newtree;
365     }
366    
367    
368    
369 buchmann 1.2 void EdgeFitter::PrepareDatasets(int is_data) {
370 buchmann 1.5 write_warning(__FUNCTION__,"Need to make this function ready for scans as well (use signal from scan samples)");
371 buchmann 1.23
372     ensure_directory_exists(PlottingSetup::cbafbasedir+"/EdgeCache/");
373    
374     stringstream FileName;
375     FileName << PlottingSetup::cbafbasedir << "/EdgeCache/" << md5((const char*)cut) << ".root";
376    
377 buchmann 1.25
378     TFile *fEdgeCache;
379     if(PlottingSetup::do_CleanCache) fEdgeCache = new TFile(FileName.str().c_str(),"RECREATE");
380     else fEdgeCache = new TFile(FileName.str().c_str(),"UPDATE");
381    
382 buchmann 1.23 if(fEdgeCache->GetNkeys()==0) {
383     ofstream CacheOverview;
384     CacheOverview.open((PlottingSetup::cbafbasedir+"/EdgeCache/CacheOverview").c_str(),ios::app);
385     CacheOverview << md5((const char*)cut) << ";" << (const char*) cut << endl;
386     CacheOverview.close();
387     }
388    
389 buchmann 1.10 vector<TTree*> SkimmedTrees;
390     TTree *SkimmedTree[(int)allsamples.collection.size()];
391 buchmann 1.7 for(int isample=0;isample<(int)allsamples.collection.size();isample++) {
392 buchmann 1.2 if(!allsamples.collection[isample].is_active) continue;
393     if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
394     if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
395     if(is_data!=1&&allsamples.collection[isample].is_data==true) continue;//kick all data samples when looking for MC
396     if(is_data!=2&&allsamples.collection[isample].is_signal==true) continue;//remove signal sample if we don't want it.
397 buchmann 1.18 if(EdgeFitter::MarcoDebug) dout << "Considering : " << allsamples.collection[isample].samplename << endl;
398 buchmann 1.23
399     string SkimName = GetSkimName(isample);
400     SkimmedTree[isample] = (TTree*)fEdgeCache->Get(SkimName.c_str());
401     if(!SkimmedTree[isample]) {
402     dout << "Need to generate tree for " << allsamples.collection[isample].filename << endl;
403     SkimmedTree[isample] = SkimTree(isample);
404     fEdgeCache->cd();
405     SkimmedTree[isample]->Write();
406     } else {
407     dout << "Loaded tree for " << allsamples.collection[isample].filename << " from cache file " << endl;
408     }
409    
410 buchmann 1.15 SkimmedTrees.push_back(SkimmedTree[isample]);
411 buchmann 1.2 }
412 buchmann 1.10
413     TTree *completetree = MergeTrees(SkimmedTrees);
414    
415 buchmann 1.18 if(EdgeFitter::MarcoDebug) dout << "Complete tree now contains " << completetree->GetEntries() << " entries " << endl;
416 buchmann 1.2
417 buchmann 1.10 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
418 buchmann 1.2 RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
419     RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
420 buchmann 1.6 RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
421     RooArgSet observables(mll,id1,id2,edgeWeight);
422 buchmann 1.2
423     string title="CMS Data";
424     if(is_data!=1) title="CMS SIMULATION";
425 buchmann 1.6 RooDataSet LAllData("LAllData",title.c_str(),completetree,observables,"","edgeWeight");
426 buchmann 1.23 fEdgeCache->Close();
427     dout << "Edge cache closed." << endl;
428    
429 buchmann 1.24 EdgeFitter::eeSample = (RooDataSet*)LAllData.reduce("id1==id2&&id1==0");
430     EdgeFitter::mmSample = (RooDataSet*)LAllData.reduce("id1==id2&&id1==1");
431 buchmann 1.10 EdgeFitter::OFSample = (RooDataSet*)LAllData.reduce("id1!=id2");
432 buchmann 1.27 EdgeFitter::SFSample = (RooDataSet*)LAllData.reduce("id1==id2");
433 buchmann 1.2 EdgeFitter::AllData = (RooDataSet*)LAllData.reduce("id1!=id2||id1==id2");
434    
435 buchmann 1.27
436 buchmann 1.24 eeSample->SetName("eeSample");
437     mmSample->SetName("mmSample");
438 buchmann 1.10 OFSample->SetName("OFSample");
439 buchmann 1.27 SFSample->SetName("SFSample");
440 buchmann 1.2 AllData->SetName("AllData");
441    
442 buchmann 1.27 eeSample->SetTitle("eeSample");
443     mmSample->SetTitle("mmSample");
444     OFSample->SetTitle("OFSample");
445     SFSample->SetTitle("SFSample");
446     AllData->SetTitle("AllData");
447    
448 buchmann 1.2 if(EdgeFitter::MarcoDebug) {
449 buchmann 1.20 dout << "Number of (weighted) events in full sample = " << AllData->sumEntries() << " (raw events : " << AllData->numEntries() << ")" << endl;
450 buchmann 1.24 dout << "Number of (weighted) events in ee sample = " << eeSample->sumEntries() << " (raw events : " << eeSample->numEntries() << ")" << endl;
451     dout << "Number of (weighted) events in mm sample = " << mmSample->sumEntries() << " (raw events : " << mmSample->numEntries() << ")" << endl;
452 buchmann 1.27 dout << "Number of (weighted) events in SF sample = " << SFSample->sumEntries() << " (raw events : " << SFSample->numEntries() << ")" << endl;
453 buchmann 1.20 dout << "Number of (weighted) events in em sample = " << OFSample->sumEntries() << " (raw events : " << OFSample->numEntries() << ")" << endl;
454 buchmann 1.2 }
455 buchmann 1.6
456 buchmann 1.2 }
457    
458 buchmann 1.15 void EdgeFitter::DrawDatasetContent(int is_data) {
459 buchmann 1.24 TCanvas* ceedata = new TCanvas("ceedata","ceedata") ;
460 buchmann 1.20
461 buchmann 1.15 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
462 buchmann 1.26 RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("ee sample")) ;
463 buchmann 1.15 frame1->GetXaxis()->CenterTitle(1);
464     frame1->GetYaxis()->CenterTitle(1);
465 buchmann 1.27 eeSample->plotOn(frame1,RooFit::Name("eedata"));
466 buchmann 1.15
467 buchmann 1.26 RooPlot* frame2 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("mm sample")) ;
468 buchmann 1.15 frame2->GetXaxis()->CenterTitle(1);
469     frame2->GetYaxis()->CenterTitle(1);
470 buchmann 1.27 mmSample->plotOn(frame2,RooFit::Name("mmdata"));
471 buchmann 1.25
472     RooPlot* frame3 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
473     frame3->GetXaxis()->CenterTitle(1);
474     frame3->GetYaxis()->CenterTitle(1);
475 buchmann 1.27 OFSample->plotOn(frame3,RooFit::Name("OFdata"));
476 buchmann 1.15
477 buchmann 1.24 TH1F *ee_ref = allsamples.Draw("ee_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1==id2&&id1==0"),is_data, luminosity);
478     TH1F *mm_ref = allsamples.Draw("mm_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1==id2&&id1==1"),is_data, luminosity);
479 buchmann 1.20 TH1F *of_ref = allsamples.Draw("of_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1!=id2"),is_data, luminosity);
480    
481 buchmann 1.25 ee_ref->SetFillColor(TColor::GetColor("#3ADF00"));
482     mm_ref->SetFillColor(TColor::GetColor("#3ADF00"));
483     of_ref->SetFillColor(TColor::GetColor("#3ADF00"));
484    
485 buchmann 1.24 TH1F *ee_fit = (TH1F*)eeSample->createHistogram("ee_fit",mll,RooFit::Binning(int((mllmax-mllmin)/5)),RooFit::SumW2Error(true));
486 buchmann 1.25 TH1F *mm_fit = (TH1F*)mmSample->createHistogram("mm_fit",mll,RooFit::Binning(int((mllmax-mllmin)/5)),RooFit::SumW2Error(true));
487 buchmann 1.20 TH1F *of_fit = (TH1F*)OFSample->createHistogram("of_fit",mll,RooFit::Binning(int((mllmax-mllmin)/5)),RooFit::SumW2Error(true));
488    
489 buchmann 1.25 TLegend *leg = make_legend();
490 buchmann 1.24 leg->AddEntry(ee_fit,"RooDataSet content","p");
491 buchmann 1.25 leg->AddEntry(ee_ref,"CBAF cross-check","f");
492     leg->SetX1(0.5);
493     leg->SetY1(0.75);
494 buchmann 1.20
495 buchmann 1.24 ceedata->cd() ;
496     gPad->SetLeftMargin(0.15);
497 buchmann 1.25 frame1->SetMaximum(1.2*frame1->GetMaximum());
498 buchmann 1.24 frame1->GetYaxis()->SetTitleOffset(1.4);
499     frame1->Draw();
500     ee_ref->Draw("histo,same");
501     ee_fit->Draw("e1,same");
502     leg->Draw("same");
503     if(is_data==data) DrawPrelim();
504     else DrawPrelim(PlottingSetup::luminosity,true);
505 buchmann 1.25 CompleteSave(ceedata,"Edge/PreFit_ee");
506 buchmann 1.24
507     ceedata->cd() ;
508 buchmann 1.15 gPad->SetLeftMargin(0.15);
509 buchmann 1.25 frame2->SetMaximum(1.2*frame2->GetMaximum());
510     frame2->GetYaxis()->SetTitleOffset(1.4);
511     frame2->Draw();
512 buchmann 1.24 mm_ref->Draw("histo,same");
513     mm_fit->Draw("e1,same");
514 buchmann 1.20 leg->Draw("same");
515 buchmann 1.15 if(is_data==data) DrawPrelim();
516     else DrawPrelim(PlottingSetup::luminosity,true);
517 buchmann 1.25 CompleteSave(ceedata,"Edge/PreFit_mm");
518 buchmann 1.15
519     TCanvas* cOFdata = new TCanvas("cOFdata","cOFdata") ;
520     cOFdata->cd() ;
521     gPad->SetLeftMargin(0.15);
522 buchmann 1.25 frame3->SetMaximum(1.2*frame3->GetMaximum());
523     frame3->GetYaxis()->SetTitleOffset(1.4);
524     frame3->Draw();
525 buchmann 1.20 of_ref->Draw("histo,same");
526     of_fit->Draw("e1,same");
527     leg->Draw("same");
528 buchmann 1.15 if(is_data==data) DrawPrelim();
529     else DrawPrelim(PlottingSetup::luminosity,true);
530 buchmann 1.25 CompleteSave(cOFdata,"Edge/PreFit_OF");
531 buchmann 1.20
532 buchmann 1.24 delete ee_fit;
533     delete mm_fit;
534 buchmann 1.20 delete of_fit;
535    
536 buchmann 1.24 delete ee_ref;
537     delete mm_ref;
538 buchmann 1.20 delete of_ref;
539 buchmann 1.15 }
540    
541 buchmann 1.10 string WriteWithError(float central, float error, int digits) {
542     float ref=central;
543     if(ref<0) ref=-central;
544     int HighestSigDigit = 0;
545     if(ref>1) HighestSigDigit = int(log(ref)/log(10))+1;
546     else HighestSigDigit = int(log(ref)/(log(10)));
547    
548     float divider=pow(10.0,(double(HighestSigDigit-digits)));
549    
550     stringstream result;
551     result << divider*int(central/divider+0.5) << " #pm " << divider*int(error/divider+0.5);
552     return result.str();
553     }
554    
555    
556 buchmann 1.2 string EdgeFitter::RandomStorageFile() {
557     TRandom3 *r = new TRandom3(0);
558     int rho = (int)r->Uniform(1,10000000);
559     stringstream RandomFile;
560     RandomFile << PlottingSetup::cbafbasedir << "/exchange/TempEdgeFile_" << rho << ".root";
561     delete r;
562     return RandomFile.str();
563     }
564    
565     Yield EdgeFitter::Get_Z_estimate(float jzb_cut, int icut) {
566     if(MarcoDebug) write_error(__FUNCTION__,"Returning random Z yield");
567     Yield a(123,45,67); return a;
568     return PlottingSetup::allresults.predictions[icut].Zbkg;
569     }
570    
571     Yield EdgeFitter::Get_T_estimate(float jzb_cut, int icut) {
572     if(MarcoDebug) write_error(__FUNCTION__,"Returning random TTbar yield");
573     Yield a(1234,56,78); return a;
574     return PlottingSetup::allresults.predictions[icut].Flavorsym;
575     }
576    
577 buchmann 1.5 void EdgeFitter::DoFit(int is_data, float jzb_cut) {
578 buchmann 1.16
579     stringstream prefix;
580     if(is_data==data) prefix << "data_";
581     if(is_data==mc) prefix << "mc_";
582     if(is_data==mcwithsignal) prefix << "mcwithS_";
583    
584     prefix << EdgeFitter::Mode << "_" << jzb_cut;
585    
586 buchmann 1.27 if(!EdgeFitter::AllowTriangle && EdgeFitter::FixedMEdge>=0) prefix << "__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge);
587    
588 buchmann 1.21 if(EdgeFitter::AllowTriangle) prefix << "__H1";
589     else prefix << "__H0";
590 buchmann 1.16
591 buchmann 1.10 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
592 buchmann 1.6 RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
593 buchmann 1.5 RooCategory sample("sample","sample") ;
594 buchmann 1.24 sample.defineType("ee");
595     sample.defineType("mm");
596 buchmann 1.10 sample.defineType("OF");
597 buchmann 1.27 sample.defineType("SF");
598 buchmann 1.6
599 buchmann 1.27 RooDataSet combData("combData","combined data",RooArgSet(mll,edgeWeight),RooFit::Index(sample),RooFit::Import("ee",*eeSample),RooFit::Import("mm",*mmSample),RooFit::Import("OF",*OFSample),RooFit::Import("SF",*SFSample),RooFit::WeightVar(edgeWeight));
600 buchmann 1.6
601 buchmann 1.25 //------------------------------------------------------------------------------------------------------------------------------------
602     // _____ _ __
603     // / ____| | /_ |
604     // | (___ | |_ ___ _ __ | |
605     // \___ \| __/ _ \ '_ \ | |
606     // ____) | || __/ |_) | | |
607     // |_____/ \__\___| .__/ |_|
608     // | |
609     // |_|
610     //
611     // Fit OF to get good initial values for flavor-symmetric background
612    
613    
614 buchmann 1.5
615     //First we make a fit to opposite flavor
616 buchmann 1.28 RooRealVar fttbarOF("fttbarOF", "fttbarOF", 0.8*OFSample->sumEntries(), 0, 1.2*OFSample->sumEntries());
617 buchmann 1.25 RooRealVar par1ttbarOF("par1ttbarOF", "par1ttbarOF", 1.77, 0.01, 4.0);
618 buchmann 1.10 RooRealVar par2ttbarOF("par2ttbarOF", "par2ttbarOF", 1.0);
619 buchmann 1.25 RooRealVar par3ttbarOF("par3ttbarOF", "par3ttbarOF", 0.0272, 0.001, 1.0);
620 buchmann 1.10 RooRealVar par4ttbarOF("par4ttbarOF", "par4ttbarOF", 2.0);
621     RooSUSYBkgPdf ttbarOF("ttbarOF","ttbarOF", mll , par1ttbarOF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
622     RooAddPdf model_OF("model_OF","model_OF", ttbarOF, fttbarOF);
623 buchmann 1.5 RooSimultaneous simPdfOF("simPdfOF","simultaneous pdf", sample) ;
624 buchmann 1.10 simPdfOF.addPdf(model_OF,"OF");
625 buchmann 1.14 RooFitResult *resultOF = simPdfOF.fitTo(combData, RooFit::Save(),RooFit::Extended(),RooFit::Minos(true));
626 buchmann 1.18 dout << "============================= < OF results > =============================" << endl;
627 buchmann 1.17 resultOF->Print();
628 buchmann 1.18 dout << "============================= < /OF results > =============================" << endl;
629 buchmann 1.14
630 buchmann 1.16
631     RooPlot* frameO = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample"));
632     frameO->GetXaxis()->CenterTitle(1);
633     frameO->GetYaxis()->CenterTitle(1);
634     combData.plotOn(frameO,RooFit::Name("OFdata"),RooFit::Cut("sample==sample::OF")) ;
635     simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("FullFit"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
636     simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("TTbarOFonly"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
637    
638     TCanvas* pof = new TCanvas("pof","pof") ;
639     pof->cd() ;
640     gPad->SetLeftMargin(0.15);
641     frameO->GetYaxis()->SetTitleOffset(1.4);
642     frameO->Draw();
643     if(is_data==data) DrawPrelim();
644     else DrawPrelim(PlottingSetup::luminosity,true);
645 buchmann 1.27 CompleteSave(pof,"Edge/OF__OFFitonly_"+prefix.str(),false,false);
646 buchmann 1.16 delete pof;
647 buchmann 1.25
648     TCanvas* pof2 = new TCanvas("pof2","pof2") ;
649     pof2->cd();
650     gPad->SetLeftMargin(0.15);
651     frameO->GetYaxis()->SetTitleOffset(1.4);
652     frameO->Draw();
653     if(is_data==data) DrawPrelim();
654     else DrawPrelim(PlottingSetup::luminosity,true);
655     stringstream OFFitParameterSummary;
656     OFFitParameterSummary << "#splitline{ftbbarOF = " << WriteWithError(fttbarOF.getVal(),fttbarOF.getError(),3) << "}{";
657     OFFitParameterSummary << "#splitline{par1ttbarOF = " << WriteWithError(par1ttbarOF.getVal(),par1ttbarOF.getError(),3) << "}{";
658     OFFitParameterSummary << "#splitline{par2ttbarOF = " << WriteWithError(par2ttbarOF.getVal(),par2ttbarOF.getError(),3) << "}{";
659     OFFitParameterSummary << "#splitline{par3ttbarOF = " << WriteWithError(par3ttbarOF.getVal(),par3ttbarOF.getError(),3) << "}{";
660     OFFitParameterSummary << "par4ttbarOF = " << WriteWithError(par4ttbarOF.getVal(),par4ttbarOF.getError(),3);
661     OFFitParameterSummary << "}}}}";
662     TLatex *OFFitinfobox = new TLatex(0.57,0.75,OFFitParameterSummary.str().c_str());
663     OFFitinfobox->SetNDC();
664     OFFitinfobox->SetTextSize(0.03);
665     OFFitinfobox->Draw();
666    
667    
668 buchmann 1.27 CompleteSave(pof2,"Edge/OF__OFFitonly_"+prefix.str()+"__INFO",false,false);
669 buchmann 1.25
670     delete pof2;
671 buchmann 1.16
672 buchmann 1.14 if(resultOF->covQual()!=3) {
673     write_error(__FUNCTION__,"OF fit did not converge!!! Cannot continue!");
674 buchmann 1.18 dout << "covQual is " << resultOF->covQual() << endl;
675 buchmann 1.21 if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=-1;
676     else EdgeFitter::FixedMEdgeChi2_H0=-1;
677 buchmann 1.14 if(EdgeFitter::RejectPointIfNoConvergence) return;
678     } else {
679     write_info(__FUNCTION__,"OF fit converged");
680     }
681 buchmann 1.11
682 buchmann 1.26
683     // _____ _ ___
684     // / ____| | |__ \
685     // | (___ | |_ ___ _ __ ) |
686     // \___ \| __/ _ \ '_ \ / /
687     // ____) | || __/ |_) | / /_
688     // |_____/ \__\___| .__/ |____|
689     // | |
690     // |_|
691     //
692 buchmann 1.27 // Set up all the models
693 buchmann 1.25
694    
695 buchmann 1.26 // Step 2a: set up edge position (if fixed), and maximum Z yield
696 buchmann 1.11 float StartingMedge=70;
697     if(EdgeFitter::FixedMEdge>0) StartingMedge=EdgeFitter::FixedMEdge;
698 buchmann 1.5
699 buchmann 1.18 RooDataSet *ZDataSet = (RooDataSet*)EdgeFitter::AllData->reduce("id1==id2 && abs(mll-91.2)<20");
700    
701 buchmann 1.17 float maxZ = ZDataSet->sumEntries();
702 buchmann 1.18 dout << "maxZ was set to " << maxZ << endl;
703 buchmann 1.17 delete ZDataSet;
704    
705 buchmann 1.27 RooRealVar JustOne("JustOne","Just One",1) ;
706    
707 buchmann 1.17
708 buchmann 1.26 // Step 2b: set up the models!
709 buchmann 1.27 RooRealVar eefrac("eefrac","eefrac",eeSample->sumEntries()/(eeSample->sumEntries()+mmSample->sumEntries()));
710 buchmann 1.26 RooFormulaVar mmfrac("mmfrac","1-eefrac",RooArgSet(eefrac));
711    
712     RooRealVar rOneOverEMFrac("rOneOverEMFrac","rOneOverEMFrac",1.02,1.02-0.07,1.02+0.07);
713     RooRealVar rmean("rmean","rmean",1.02);
714     RooRealVar rwidth("rwidth","rwidth",0.07);
715     RooGaussian OneOverEMFrac("OneOverEMFrac","OneOverEMFrac",rOneOverEMFrac,rmean,rwidth);
716    
717    
718     // FIRST: Drell-Yan: different parameters for resolution of DY (and of course yield), but same mean and so on
719 buchmann 1.25 RooRealVar widthz("widthz", "widthz", 2.94);
720 buchmann 1.26 widthz.setConstant(1);
721 buchmann 1.25 RooRealVar meanz("meanz", "meanz", 91.1876, 89, 93);
722 buchmann 1.26 RooRealVar fzSF("fzSF","fzSF",39,0,maxZ);
723     RooFormulaVar fzee("fzee","eefrac*fzSF",RooArgSet(eefrac,fzSF));
724     RooFormulaVar fzmm("fzmm","mmfrac*fzSF",RooArgSet(mmfrac,fzSF));
725 buchmann 1.24 RooRealVar sigmazee("sigmazee", "sigmazee", 5, 0.5, 5);
726     RooRealVar sigmazmm("sigmazmm", "sigmazmm", 5, 0.5, 5);
727    
728 buchmann 1.26 RooVoigtian zee("zee", "zee", mll, meanz, widthz, sigmazee);
729     RooVoigtian zmm("zmm", "zmm", mll, meanz, widthz, sigmazmm);
730 buchmann 1.28
731 buchmann 1.10
732 buchmann 1.24
733 buchmann 1.26 //SECOND: Flavor-Symmetry contribution. Only relative fraction is different, all other parameters are the same;
734     RooFormulaVar fttbaree("fttbaree","eefrac*fttbarOF*OneOverEMFrac",RooArgSet(eefrac,fttbarOF,OneOverEMFrac));
735     RooFormulaVar fttbarmm("fttbarmm","mmfrac*fttbarOF*OneOverEMFrac",RooArgSet(mmfrac,fttbarOF,OneOverEMFrac));
736 buchmann 1.5
737 buchmann 1.25 RooSUSYBkgPdf ttbaree("ttbaree","ttbaree", mll , par1ttbarOF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
738     RooSUSYBkgPdf ttbarmm("ttbarmm","ttbarmm", mll , par1ttbarOF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
739 buchmann 1.26 RooAddPdf ttbarSF("ttbarSF","ttbarSF", RooArgList(ttbaree,ttbarmm), RooArgList(fttbaree,fttbarmm));
740 buchmann 1.25
741 buchmann 1.21
742 buchmann 1.26 //for signal only the resolution and absolute fraction are different for ee / mm
743 buchmann 1.28 RooRealVar fsignal("fsignal", "fsignal", 0, 0, (eeSample->sumEntries()+mmSample->sumEntries()-OFSample->sumEntries()));
744 buchmann 1.26 RooFormulaVar fsignalee("fsignalee","eefrac*fsignal",RooArgSet(eefrac,fsignal));
745     RooFormulaVar fsignalmm("fsignalmm","mmfrac*fsignal",RooArgSet(mmfrac,fsignal));
746    
747     RooRealVar par1signal("par1signal", "par1signal", 45, 20, 100);
748 buchmann 1.29 par1signal.setConstant(1); // doesn't have any effect.
749     RooRealVar par3signal("par3signal", "par3signal", StartingMedge, 0, 300); // this is the edge position
750 buchmann 1.26 if(EdgeFitter::FixedMEdge>0) par3signal.setConstant();
751    
752     RooSUSYTPdf signalee("signalee","signalee", mll , par1signal, sigmazee, par3signal);
753     RooSUSYTPdf signalmm("signalmm","signalmm", mll , par1signal, sigmazmm, par3signal);
754     RooAddPdf signalSF("signalSF","signalSF",RooArgList(signalee,signalmm),RooArgList(fsignalee,fsignalmm));
755 buchmann 1.10
756 buchmann 1.21 if(!EdgeFitter::AllowTriangle) {
757 buchmann 1.26 fsignal.setVal(0.0); // kill off the signal if we don't want the triangle
758     fsignal.setConstant(1);
759     par1signal.setConstant(1);
760     par3signal.setConstant(1);
761 buchmann 1.21 }
762 buchmann 1.5
763 buchmann 1.26
764 buchmann 1.27
765 buchmann 1.26 //COMPLETE MODEL:
766     RooAddPdf model_ee("model_ee","model_ee", RooArgList(zee, ttbaree, signalee), RooArgList(fzee, fttbaree, fsignalee));
767     RooAddPdf model_mm("model_mm","model_mm", RooArgList(zmm, ttbarmm, signalmm), RooArgList(fzmm, fttbarmm, fsignalmm));
768 buchmann 1.27 RooAddPdf model_SF("model_SF","model_SF",RooArgList(model_ee,model_mm),RooArgList(JustOne,JustOne));
769    
770    
771     // _____ _ ____
772     // / ____| | |___ \
773     // | (___ | |_ ___ _ __ __) |
774     // \___ \| __/ _ \ '_ \ |__ <
775     // ____) | || __/ |_) | ___) |
776     // |_____/ \__\___| .__/ |____/
777     // | |
778     // |_|
779     //
780     // Fitting
781 buchmann 1.26
782 buchmann 1.5 RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
783 buchmann 1.24 simPdf.addPdf(model_ee,"ee") ;
784     simPdf.addPdf(model_mm,"mm") ;
785 buchmann 1.14 simPdf.addPdf(model_OF,"OF") ;
786 buchmann 1.5
787 buchmann 1.29 //RooFitResult *result = simPdf.fitTo(combData, RooFit::Save(), RooFit::Extended(),RooFit::Minos(true),RooFit::ExternalConstraints(OneOverEMFrac));
788     RooFitResult *result = simPdf.fitTo(combData, RooFit::Save(), RooFit::Extended(),RooFit::Minos(true));
789 buchmann 1.14
790     if(result->covQual()!=3) {
791     write_error(__FUNCTION__,"Full fit did not converge!!! Cannot continue!");
792 buchmann 1.18 dout << "covQual is " << result->covQual() << endl;
793 buchmann 1.21 if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=-1;
794     else EdgeFitter::FixedMEdgeChi2_H0=-1;
795 buchmann 1.14 if(EdgeFitter::RejectPointIfNoConvergence) return;
796     } else {
797     write_info(__FUNCTION__,"Full fit converged");
798     }
799    
800 buchmann 1.21 dout << "============================= < Full results > =============================" << endl;
801 buchmann 1.17 result->Print();
802 buchmann 1.21 dout << "============================= < /Full results > =============================" << endl;
803 buchmann 1.17
804 buchmann 1.27 // _____ _ _ _
805     // / ____| | | || |
806     // | (___ | |_ ___ _ __ | || |_
807     // \___ \| __/ _ \ '_ \ |__ _|
808     // ____) | || __/ |_) | | |
809     // |_____/ \__\___| .__/ |_|
810     // | |
811     // |_|
812     //
813     // Present results
814 buchmann 1.5
815 buchmann 1.10 RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("EE sample")) ;
816     frame1->GetXaxis()->CenterTitle(1);
817 buchmann 1.14 frame1->GetYaxis()->CenterTitle(1);
818 buchmann 1.24 combData.plotOn(frame1,RooFit::Name("eedata"),RooFit::Cut("sample==sample::ee")) ;
819 buchmann 1.26 simPdf.plotOn(frame1,RooFit::Slice(sample,"ee"),RooFit::Name("FullFitEE"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
820     simPdf.plotOn(frame1,RooFit::Slice(sample,"ee"),RooFit::Name("TTbarEEonly"),RooFit::Components("ttbaree"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
821     simPdf.plotOn(frame1,RooFit::Slice(sample,"ee"),RooFit::Name("DYEEonly"),RooFit::Components("zee"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
822 buchmann 1.24 if(EdgeFitter::AllowTriangle) simPdf.plotOn(frame1,RooFit::Slice(sample,"ee"),RooFit::Name("SignalEEonly"),RooFit::Components("signalee"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kGreen));
823 buchmann 1.5
824    
825 buchmann 1.26 RooPlot* frame2 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("mm sample")) ;
826     frame2->GetXaxis()->CenterTitle(1);
827     frame2->GetYaxis()->CenterTitle(1);
828     combData.plotOn(frame2,RooFit::Name("mmdata"),RooFit::Cut("sample==sample::mm")) ;
829     simPdf.plotOn(frame2,RooFit::Slice(sample,"mm"),RooFit::Name("FullFitMM"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
830     simPdf.plotOn(frame2,RooFit::Slice(sample,"mm"),RooFit::Name("TTbarMMonly"),RooFit::Components("ttbarmm"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
831     simPdf.plotOn(frame2,RooFit::Slice(sample,"mm"),RooFit::Name("DYMMonly"),RooFit::Components("zmm"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
832     if(EdgeFitter::AllowTriangle) simPdf.plotOn(frame2,RooFit::Slice(sample,"mm"),RooFit::Name("SignalMMonly"),RooFit::Components("signalmm"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kGreen));
833    
834    
835 buchmann 1.10 RooPlot* frame3 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
836     frame3->GetXaxis()->CenterTitle(1);
837 buchmann 1.14 frame3->GetYaxis()->CenterTitle(1);
838 buchmann 1.26 combData.plotOn(frame3,RooFit::Name("OFdata"),RooFit::Cut("sample==sample::OF")) ;
839     simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::Name("FullFitOF"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
840     simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::Name("TTbarOFonly"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
841     simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::Name("DYOFonly"),RooFit::Components("zOF"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
842    
843 buchmann 1.27 RooAbsPdf *ee_result = simPdf.getPdf("ee");
844     RooAbsPdf *mm_result = simPdf.getPdf("mm");
845     RooAddPdf SF_result("SF_result","SF_result",RooArgList(*ee_result,*mm_result));
846    
847    
848     TCanvas* g = new TCanvas("g","g") ;
849 buchmann 1.26
850     RooPlot* frame4 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("SF sample")) ;
851     frame4->GetXaxis()->CenterTitle(1);
852     frame4->GetYaxis()->CenterTitle(1);
853 buchmann 1.27 EdgeFitter::SFSample->plotOn(frame4,RooFit::Name("SFdata")) ;
854     SF_result.plotOn(frame4,RooFit::Name("FullFitSF"),RooFit::ProjWData(*EdgeFitter::SFSample),RooFit::LineColor(kBlack));
855     SF_result.plotOn(frame4,RooFit::Name("TTbarSFonly"),RooFit::ProjWData(*EdgeFitter::SFSample),RooFit::Components("ttbaree,ttbarmm"),RooFit::LineColor(kBlue),RooFit::LineStyle(kDashed));
856     SF_result.plotOn(frame4,RooFit::Name("DYSFonly"),RooFit::ProjWData(*EdgeFitter::SFSample),RooFit::Components("zmm,zee"), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
857 buchmann 1.29 if(EdgeFitter::AllowTriangle) SF_result.plotOn(frame4,RooFit::Name("SignalSFonly"),RooFit::Components("signalee,signalmm"), RooFit::ProjWData(*EdgeFitter::SFSample), RooFit::LineStyle(kDashed), RooFit::LineColor(kGreen));
858 buchmann 1.5
859 buchmann 1.26 /// ********************************************************************************************************************************
860 buchmann 1.5 TCanvas* c = new TCanvas("rf501_simultaneouspdf","rf403_simultaneouspdf") ;
861     c->cd() ;
862     gPad->SetLeftMargin(0.15);
863     frame1->GetYaxis()->SetTitleOffset(1.4);
864     frame1->Draw();
865     if(is_data==data) DrawPrelim();
866     else DrawPrelim(PlottingSetup::luminosity,true);
867 buchmann 1.27 CompleteSave(c,"Edge/"+prefix.str()+"_ee",false,false);
868 buchmann 1.28 stringstream eeFitParameterSummary;
869     eeFitParameterSummary << "#splitline{t#bar{t} : " << WriteWithError((OneOverEMFrac.getVal()*fttbarOF.getVal()*eefrac.getVal()),(OneOverEMFrac.getVal()*fttbarOF.getVal()*eefrac.getVal())*sqrt(pow(fttbarOF.getError()/fttbarOF.getVal(),2)+pow(eefrac.getError()/eefrac.getVal(),2)),3) << "}{";//no taking uncertainty on R(SF/OF) into consideration!
870     eeFitParameterSummary << "#splitline{DY : " << WriteWithError((fzSF.getVal()*eefrac.getVal()),(fzSF.getVal()*eefrac.getVal())*sqrt(pow(fzSF.getError()/fzSF.getVal(),2)+pow(eefrac.getError()/eefrac.getVal(),2)),3) << "}{";
871     eeFitParameterSummary << "#splitline{signal : " << WriteWithError((fsignal.getVal()*eefrac.getVal()),(fsignal.getVal()*eefrac.getVal())*sqrt(pow(fsignal.getError()/fsignal.getVal(),2)+pow(eefrac.getError()/eefrac.getVal(),2)),3) << "}{";
872 buchmann 1.29 eeFitParameterSummary << "}}}";
873 buchmann 1.28 TLatex *eeFitinfobox = new TLatex(0.57,0.75,eeFitParameterSummary.str().c_str());
874     eeFitinfobox->SetNDC();
875     eeFitinfobox->SetTextSize(0.03);
876     eeFitinfobox->Draw();
877     CompleteSave(c,"Edge/"+prefix.str()+"_ee__INFO",false,false);
878 buchmann 1.26
879     c->cd() ;
880     gPad->SetLeftMargin(0.15);
881     frame2->GetYaxis()->SetTitleOffset(1.4);
882     frame2->Draw();
883     if(is_data==data) DrawPrelim();
884     else DrawPrelim(PlottingSetup::luminosity,true);
885 buchmann 1.27 CompleteSave(c,"Edge/"+prefix.str()+"_mm",false,false);
886 buchmann 1.28 stringstream mmFitParameterSummary;
887     mmFitParameterSummary << "#splitline{t#bar{t} : " << WriteWithError((OneOverEMFrac.getVal()*fttbarOF.getVal()*(1-eefrac.getVal())),(OneOverEMFrac.getVal()*fttbarOF.getVal()*(1-eefrac.getVal()))*sqrt(pow(fttbarOF.getError()/fttbarOF.getVal(),2)+pow(eefrac.getError()/(1-eefrac.getVal()),2)),3) << "}{";//no taking uncertainty on R(SF/OF) into consideration!
888     mmFitParameterSummary << "#splitline{DY : " << WriteWithError((fzSF.getVal()*(1-eefrac.getVal())),(fzSF.getVal()*(1-eefrac.getVal()))*sqrt(pow(fzSF.getError()/fzSF.getVal(),2)+pow(eefrac.getError()/(1-eefrac.getVal()),2)),3) << "}{";
889     mmFitParameterSummary << "#splitline{signal : " << WriteWithError((fsignal.getVal()*(1-eefrac.getVal())),(fsignal.getVal()*(1-eefrac.getVal()))*sqrt(pow(fsignal.getError()/fsignal.getVal(),2)+pow(eefrac.getError()/(1-eefrac.getVal()),2)),3) << "}{";
890 buchmann 1.29 mmFitParameterSummary << "}}}";
891 buchmann 1.28 TLatex *mmFitinfobox = new TLatex(0.57,0.75,mmFitParameterSummary.str().c_str());
892     mmFitinfobox->SetNDC();
893     mmFitinfobox->SetTextSize(0.03);
894     mmFitinfobox->Draw();
895     CompleteSave(c,"Edge/"+prefix.str()+"_mm__INFO",false,false);
896 buchmann 1.5
897 buchmann 1.26 c->cd() ;
898 buchmann 1.5 gPad->SetLeftMargin(0.15);
899     frame3->GetYaxis()->SetTitleOffset(1.4);
900     frame3->Draw();
901     if(is_data==data) DrawPrelim();
902     else DrawPrelim(PlottingSetup::luminosity,true);
903 buchmann 1.27 CompleteSave(c,"Edge/"+prefix.str()+"_OF",false,false);
904 buchmann 1.26
905     c->cd() ;
906 buchmann 1.5 gPad->SetLeftMargin(0.15);
907     frame4->GetYaxis()->SetTitleOffset(1.4);
908     frame4->Draw();
909     if(is_data==data) DrawPrelim();
910     else DrawPrelim(PlottingSetup::luminosity,true);
911 buchmann 1.27 CompleteSave(c,"Edge/"+prefix.str()+"_SF",false,false);
912 buchmann 1.29 stringstream SFFitParameterSummary;
913     SFFitParameterSummary << "#splitline{t#bar{t} : " << WriteWithError((OneOverEMFrac.getVal()*fttbarOF.getVal()),(OneOverEMFrac.getVal()*fttbarOF.getVal())*sqrt(pow(fttbarOF.getError()/fttbarOF.getVal(),2)),3) << "}{";//no taking uncertainty on R(SF/OF) into consideration!
914     SFFitParameterSummary << "#splitline{DY : " << WriteWithError((fzSF.getVal()),(fzSF.getVal())*sqrt(pow(fzSF.getError()/fzSF.getVal(),2)),3) << "}{";
915     SFFitParameterSummary << "#splitline{signal : " << WriteWithError(fsignal.getVal(),(fsignal.getVal())*sqrt(pow(fsignal.getError()/fsignal.getVal(),2)),3) << "}{";
916     SFFitParameterSummary << "}}}";
917     TLatex *SFFitinfobox = new TLatex(0.57,0.75,SFFitParameterSummary.str().c_str());
918     SFFitinfobox->SetNDC();
919     SFFitinfobox->SetTextSize(0.03);
920     SFFitinfobox->Draw();
921     CompleteSave(c,"Edge/"+prefix.str()+"_SF__INFO",false,false);
922 buchmann 1.26
923     delete c;
924     /// ********************************************************************************************************************************
925 buchmann 1.10
926 buchmann 1.29 if(result->covQual()==3) {
927     if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=frame4->chiSquare("FullFitSF", "SFdata", 3);
928     else EdgeFitter::FixedMEdgeChi2_H0=frame4->chiSquare("FullFitSF", "SFdata", 3);
929     }
930 buchmann 1.10
931 buchmann 1.5 }
932    
933 buchmann 1.2 void EdgeFitter::DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, float jzb_cut, int icut, int is_data, TCut cut, TTree *signalevents=0) {
934    
935 buchmann 1.5 TCut _cut(cut&&PlottingSetup::basiccut&&PlottingSetup::passtrig);
936    
937     TFile *f = new TFile("workingfile.root","RECREATE");
938    
939     EdgeFitter::InitializeVariables(PlottingSetup::iMllLow,PlottingSetup::iMllHigh,PlottingSetup::jzbHigh,_cut);
940 buchmann 1.2
941     EdgeFitter::PrepareDatasets(is_data);
942 buchmann 1.5
943 buchmann 1.15 EdgeFitter::DrawDatasetContent(is_data);
944    
945 buchmann 1.5 RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
946     RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
947 buchmann 1.11
948    
949 buchmann 1.21 EdgeFitter::AllowTriangle=false;
950     EdgeFitter::DoFit(is_data, jzb_cut);
951    
952     EdgeFitter::AllowTriangle=true;
953    
954 buchmann 1.29 bool ScanMassRange=true;
955 buchmann 1.17 float ScanSteps=5.0;//GeV
956 buchmann 1.12
957 buchmann 1.11
958     if(ScanMassRange) {
959 buchmann 1.12 TFile *fscan = new TFile("fscan.root","UPDATE");
960     TGraph *gr = new TGraph();
961 buchmann 1.21 TGraph *Rgr = new TGraph();
962 buchmann 1.12 stringstream GrName;
963 buchmann 1.14 GrName << "ScanGraphFor_" << EdgeFitter::Mode << "_" << jzb_cut;
964 buchmann 1.21 stringstream RGrName;
965     RGrName << "ScanRatioGraphFor_" << EdgeFitter::Mode << "_" << jzb_cut;
966 buchmann 1.12 gr->SetName(GrName.str().c_str());
967 buchmann 1.21 Rgr->SetName(RGrName.str().c_str());
968 buchmann 1.12
969 buchmann 1.11 int i=0;
970 buchmann 1.17 for(float tempMedge=10;tempMedge<=300;tempMedge+=ScanSteps) {
971 buchmann 1.14 write_info(__FUNCTION__,"Now testing Medge="+any2string(tempMedge)+" for "+EdgeFitter::Mode+">"+any2string(jzb_cut));
972 buchmann 1.11 EdgeFitter::FixedMEdge=tempMedge;
973     EdgeFitter::DoFit(is_data, jzb_cut);
974 buchmann 1.21 if(EdgeFitter::FixedMEdgeChi2_H1<0) continue;
975     gr->SetPoint(i,tempMedge,EdgeFitter::FixedMEdgeChi2_H1);
976     Rgr->SetPoint(i,tempMedge,EdgeFitter::FixedMEdgeChi2_H1/EdgeFitter::FixedMEdgeChi2_H0);
977 buchmann 1.12 i++;
978 buchmann 1.11 }
979    
980     TCanvas *ScanCan = new TCanvas("ScanCan","ScanCan",500,500);
981     gr->GetXaxis()->SetTitle("m_{edge}");
982     gr->GetXaxis()->CenterTitle();
983 buchmann 1.30 gr->GetYaxis()->SetTitle("#chi^{2}");
984 buchmann 1.11 gr->GetYaxis()->CenterTitle();
985 buchmann 1.13 gr->GetYaxis()->SetTitleOffset(0.95);
986     gr->GetXaxis()->SetTitleOffset(0.9);
987     gr->SetLineColor(kBlue);
988     gr->SetTitle("");
989     gr->Draw("AL");
990 buchmann 1.11 stringstream ScanCanSave;
991 buchmann 1.14 ScanCanSave << "Edge/MEdgeScan_"+EdgeFitter::Mode+"_" << jzb_cut;
992 buchmann 1.12 if(is_data) DrawPrelim();
993     else DrawMCPrelim();
994 buchmann 1.13 CompleteSave(ScanCan,ScanCanSave.str());
995 buchmann 1.21
996     Rgr->GetXaxis()->SetTitle("m_{edge}");
997     Rgr->GetXaxis()->CenterTitle();
998 buchmann 1.30 Rgr->GetYaxis()->SetTitle("#frac{#chi^{2}(H_{1})}{#chi^{2}(H_{0})}");
999 buchmann 1.21 Rgr->GetYaxis()->CenterTitle();
1000     Rgr->GetYaxis()->SetTitleOffset(0.95);
1001     Rgr->GetXaxis()->SetTitleOffset(0.9);
1002     Rgr->SetLineColor(kBlue);
1003     Rgr->SetTitle("");
1004     Rgr->Draw("AL");
1005     ScanCanSave.str("");
1006     ScanCanSave << "Edge/MEdgeScan_Ratio_"+EdgeFitter::Mode+"_" << jzb_cut;
1007     if(is_data) DrawPrelim();
1008     else DrawMCPrelim();
1009     CompleteSave(ScanCan,ScanCanSave.str());
1010 buchmann 1.13 fscan->cd();
1011     gr->Write();
1012 buchmann 1.11 delete ScanCan;
1013 buchmann 1.12 fscan->Close();
1014 buchmann 1.11 } else {
1015     EdgeFitter::DoFit(is_data, jzb_cut);
1016 buchmann 1.21 dout << "Chi^2 (H0) = " << EdgeFitter::FixedMEdgeChi2_H0 << endl;
1017     dout << "Chi^2 (H1) = " << EdgeFitter::FixedMEdgeChi2_H1 << endl;
1018 buchmann 1.11 }
1019    
1020    
1021 buchmann 1.5 RooMsgService::instance().setGlobalKillBelow(msglevel);
1022    
1023 buchmann 1.2 f->Close();
1024    
1025     }
1026    
1027     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree *signalevents=0) {
1028 buchmann 1.14
1029     EdgeFitter::Mode="JZB";
1030     if(mcjzb=="met[4]") EdgeFitter::Mode="MET";
1031    
1032 buchmann 1.7 for(int icut=0;icut<(int)jzb_cut.size();icut++) {
1033 buchmann 1.2 stringstream addcut;
1034     if(is_data==1) addcut << "(" << datajzb << ">" << jzb_cut[icut] << ")";
1035     if(is_data!=1) addcut << "(" << mcjzb << ">" << jzb_cut[icut] << ")";
1036     TCut jcut(addcut.str().c_str());
1037    
1038 buchmann 1.5
1039 buchmann 1.2 EdgeFitter::DoEdgeFit(mcjzb, datajzb, DataPeakError, MCPeakError, jzb_cut[icut], icut, is_data, jcut&&cut, signalevents);
1040    
1041     }
1042     }