ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/EdgeLimit.C
Revision: 1.31
Committed: Fri Jun 28 11:56:43 2013 UTC (11 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.30: +28 -8 lines
Log Message:
Switched ttbar model to polynomial

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