ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/EdgeLimit.C
Revision: 1.23
Committed: Thu Jun 20 13:16:43 2013 UTC (11 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.22: +38 -22 lines
Log Message:
Added edgeCache: storing passed trees for edge fit in a special cache according to the cuts used; this helps speeding up the process tremendously

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.8 #include "EdgeModules/RooSUSYTPdf.cxx"
33     #include "EdgeModules/RooSUSYBkgPdf.cxx"
34 buchmann 1.5
35 buchmann 1.23 #include "md5/md5.h"
36     #include "md5/md5.C"
37 buchmann 1.2
38 buchmann 1.1 using namespace std;
39     using namespace PlottingSetup;
40    
41    
42 buchmann 1.2
43    
44 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) {
45     write_error(__FUNCTION__,"Not implemented edge limits yet (returning crap)");
46     ShapeDroplet beta;beta.observed=-12345;beta.SignalIntegral=1;return beta;
47     }
48    
49    
50     void PrepareEdgeShapes(string mcjzb, string datajzb, vector<float> jzbbins, float jzbpeakerrordata) {
51     write_error(__FUNCTION__,"Not implemented edge shape storage yet");
52     }
53    
54    
55 buchmann 1.2 ///------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
56    
57    
58     namespace EdgeFitter {
59    
60     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, float jzb_cut, int icut, int is_data, TCut cut, TTree*);
61     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree*);
62 buchmann 1.7 void getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp);
63 buchmann 1.2 void InitializeVariables(float _mllmin, float _mllmax, float _jzbmax, TCut _cut);
64     void PrepareDatasets(int);
65 buchmann 1.15 void DrawDatasetContent(int);
66 buchmann 1.5 void DoFit(int is_data, float jzb_cut);
67 buchmann 1.2 string RandomStorageFile();
68     Yield Get_Z_estimate(float,int);
69     Yield Get_T_estimate(float,int);
70 buchmann 1.7 float calcExclusion(RooWorkspace *ws, RooDataSet data, bool calcExclusion);
71     vector<RooDataSet> generateToys(RooWorkspace *ws, int nToys);
72 buchmann 1.4 void prepareLimits(RooWorkspace *ws, bool ComputeBands);
73     TGraph* prepareLM(float mass, float nEv);
74 buchmann 1.2
75     float jzbmax;
76     float mllmin;
77     float mllmax;
78     TCut cut;
79    
80     RooDataSet* AllData;
81 buchmann 1.10 RooDataSet* SFSample;
82     RooDataSet* OFSample;
83 buchmann 1.2
84 buchmann 1.6 bool MarcoDebug=true;
85 buchmann 1.11
86     float FixedMEdge=-1;
87 buchmann 1.21 float FixedMEdgeChi2_H1=-1;
88     float FixedMEdgeChi2_H0=-1;
89 buchmann 1.11
90 buchmann 1.14 bool RejectPointIfNoConvergence=false;
91    
92     string Mode="UndefinedMode";
93    
94 buchmann 1.21 bool AllowTriangle=true;
95 buchmann 1.2 }
96    
97 buchmann 1.4 TGraph* EdgeFitter::prepareLM(float mass, float nEv) {
98     float massLM[1];
99     massLM[0]=mass;
100     float accEffLM[1];
101     accEffLM[0]=nEv/PlottingSetup::luminosity;
102     TGraph *lm = new TGraph(1, massLM, accEffLM);
103     lm->GetXaxis()->SetNoExponent(true);
104     lm->GetXaxis()->SetTitle("m_{cut} [GeV]");
105     lm->GetYaxis()->SetTitle("#sigma #times A [pb] 95% CL UL");
106     lm->GetXaxis()->SetLimits(0.,300.);
107     lm->GetYaxis()->SetRangeUser(0.,0.08);
108     lm->SetMarkerStyle(34);
109     lm->SetMarkerColor(kRed);
110     return lm;
111     }
112    
113 buchmann 1.7 vector<RooDataSet> EdgeFitter::generateToys(RooWorkspace *ws, int nToys) {
114 buchmann 1.10 ws->ls();
115 buchmann 1.4 ws->var("nSig")->setVal(0.);
116     ws->var("nSig")->setConstant(true);
117     RooFitResult* fit = ws->pdf("combModel")->fitTo(*ws->data("data_obs"),RooFit::Save());
118 buchmann 1.7 vector<RooDataSet> theToys;
119 buchmann 1.4
120     RooMCStudy mcEE(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"EE"));
121     mcEE.generate(nToys,44,true);
122     RooMCStudy mcMM(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"MM"));
123     mcMM.generate(nToys,44,true);
124     RooMCStudy mcOSOF(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"OSOF"));
125     mcOSOF.generate(nToys,44,true);
126    
127 buchmann 1.10 RooRealVar mll("m_{ll}","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
128 buchmann 1.4 RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
129     RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
130     RooRealVar jzb("jzb","jzb",-jzbmax,jzbmax,"GeV/c");
131     RooRealVar weight("weight","weight",0,1000,"");
132     RooArgSet observables(mll,jzb,id1,id2,weight);
133    
134     for(int i=0;i<nToys;i++) {
135     RooDataSet* toyEE = (RooDataSet*)mcEE.genData(i);
136     RooDataSet* toyMM = (RooDataSet*)mcMM.genData(i);
137     RooDataSet* toyOSOF = (RooDataSet*)mcOSOF.genData(i);
138     stringstream toyname;
139     toyname << "theToy_" << i;
140     write_warning(__FUNCTION__,"Problem while adding toys");
141 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));
142     theToys.push_back(toyData);
143 buchmann 1.4 }
144     ws->var("nSig")->setVal(17.0);
145     ws->var("nSig")->setConstant(false);
146     return theToys;
147     }
148    
149 buchmann 1.7 void EdgeFitter::getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp) {
150 buchmann 1.4 TH1F *gauLimit = new TH1F("gausLimit","gausLimit",60,0.,80./PlottingSetup::luminosity);
151     vector<float> theLimits;
152 buchmann 1.9 for(int itoy=0;itoy<(int)theToys.size();itoy++) {
153 buchmann 1.7 float theLimit = calcExclusion(ws,theToys[itoy],false);
154 buchmann 1.4 if(theLimit > 0 ) gauLimit->Fill(theLimit);
155     }
156     const Int_t nQ = 4;
157     Double_t yQ[nQ] = {0.,0.,0.,0.};
158     Double_t xQ[nQ] = {0.022750132,0.1586552539,0.84134474609999998,0.977249868};
159     gauLimit->GetQuantiles(nQ,yQ,xQ);
160     median = gauLimit->GetMean();
161     // median = median1(gauLimit);
162     twoSigmaDown = abs(yQ[0]-median);
163     sigmaDown = abs(yQ[1]-median);
164     sigmaUp = abs(yQ[2]-median);
165     twoSigmaUp = abs(yQ[3]-median);
166 buchmann 1.18 dout << median * PlottingSetup::luminosity << " " << sigmaUp * PlottingSetup::luminosity << endl;
167 buchmann 1.4 }
168    
169     void EdgeFitter::prepareLimits(RooWorkspace *ws, bool ComputeBands) {
170     if(ComputeBands) {
171 buchmann 1.7 vector<RooDataSet> theToys = EdgeFitter::generateToys(ws,50);
172 buchmann 1.4 vector<float> medVals;
173     vector<float> medLimits;
174     vector<float> sigmaLimitsDown;
175     vector<float> sigmaLimitsUp;
176     vector<float> twoSigmaLimitsDown;
177     vector<float> twoSigmaLimitsUp;
178     for(int i=20;i<=320;i+=40) {
179     ws->var("nSig")->setVal(10.0);
180     medVals.push_back((float)i);
181     ws->var("m0")->setVal((float)i);
182     ws->var("m0")->setConstant(true);
183     float Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp;
184     EdgeFitter::getMedianLimit(ws,theToys,Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp);
185     medLimits.push_back(Smedian);
186     sigmaLimitsDown.push_back(SsigmaDown);
187     sigmaLimitsUp.push_back(SsigmaUp);
188     twoSigmaLimitsDown.push_back(StwoSigmaDown);
189     twoSigmaLimitsUp.push_back(StwoSigmaUp);
190     }
191     write_warning(__FUNCTION__,"Still need to store limits");
192     } else {
193     vector<float> theVals;
194     vector<float> theLimits;
195     for(int i=20;i<300;i+=5) {
196     ws->var("nSig")->setVal(0.0);
197     theVals.push_back((float)i);
198     ws->var("m0")->setVal((float)i);
199     ws->var("m0")->setConstant(true);
200 buchmann 1.7 // theLimits.push_back(calcExclusion(ws,(RooDataSet)*ws->data("data_obs"),true));
201     write_error(__FUNCTION__,"Error while casting roo data set");
202 buchmann 1.4 }
203    
204 buchmann 1.7 for(int i=0;i<(int)theLimits.size();i++) {
205 buchmann 1.4 if((theLimits[i]<2.0/PlottingSetup::luminosity)||(theLimits[i]>40.0/PlottingSetup::luminosity)) {
206 buchmann 1.18 dout << i << " : " << theVals[i] << endl;
207 buchmann 1.4 theLimits[i] = (theLimits[i+2]+theLimits[i-2])/2.0;
208     write_warning(__FUNCTION__,"Need to store limits");
209     }
210     write_warning(__FUNCTION__,"Need to store limits");
211     }
212     }
213     }
214    
215    
216 buchmann 1.7 float EdgeFitter::calcExclusion(RooWorkspace *ws, RooDataSet data, bool LoadDataObs) {
217     int numberOfToys=50;
218 buchmann 1.3 RooRealVar mu("mu","nSig",0,10000,"");
219     RooArgSet poi = RooArgSet(mu);
220     RooArgSet *nullParams = (RooArgSet*)poi.snapshot();
221     nullParams->setRealValue("nSig",0);
222     RooStats::ModelConfig *model = new RooStats::ModelConfig();
223     model->SetWorkspace(*ws);
224     model->SetPdf("combModel");
225     model->SetParametersOfInterest(poi);
226 buchmann 1.7 // if(LoadDataObs) data = (RooDataSet)*ws->data("data_obs");
227 buchmann 1.3
228 buchmann 1.7 RooStats::ProfileLikelihoodCalculator plc(data, *model);
229 buchmann 1.3 plc.SetNullParameters(*nullParams);
230     plc.SetTestSize(0.05);
231 buchmann 1.7
232 buchmann 1.3 RooStats::LikelihoodInterval* interval = plc.GetInterval();
233     RooStats::HypoTestResult *htr = plc.GetHypoTest();
234     double theLimit = interval->UpperLimit( mu );
235 buchmann 1.7 // double significance = htr->Significance();
236 buchmann 1.3
237     ws->defineSet("obs","nB");
238     ws->defineSet("poi","nSig");
239    
240     RooStats::ModelConfig b_model = RooStats::ModelConfig("B_model", ws);
241     b_model.SetPdf(*ws->pdf("combModel"));
242     b_model.SetObservables(*ws->set("obs"));
243     b_model.SetParametersOfInterest(*ws->set("poi"));
244     ws->var("nSig")->setVal(0.0); //# important!
245     b_model.SetSnapshot(*ws->set("poi"));
246    
247     RooStats::ModelConfig sb_model = RooStats::ModelConfig("S+B_model", ws);
248     sb_model.SetPdf(*ws->pdf("combModel"));
249     sb_model.SetObservables(*ws->set("obs"));
250     sb_model.SetParametersOfInterest(*ws->set("poi"));
251     ws->var("nSig")->setVal(64.0); //# important!
252     sb_model.SetSnapshot(*ws->set("poi"));
253    
254     RooStats::SimpleLikelihoodRatioTestStat slrts = RooStats::SimpleLikelihoodRatioTestStat(*b_model.GetPdf(),*sb_model.GetPdf());
255     slrts.SetNullParameters(*b_model.GetSnapshot());
256     slrts.SetAltParameters(*sb_model.GetSnapshot());
257     RooStats::ProfileLikelihoodTestStat profll = RooStats::ProfileLikelihoodTestStat(*b_model.GetPdf());
258    
259 buchmann 1.7 RooStats::HybridCalculatorOriginal hc = RooStats::HybridCalculatorOriginal(data, sb_model, b_model,0,0);
260 buchmann 1.3 hc.SetTestStatistic(2);
261 buchmann 1.7 hc.SetNumberOfToys(numberOfToys);
262 buchmann 1.3
263     RooStats::HypoTestInverterOriginal hcInv = RooStats::HypoTestInverterOriginal(hc,*ws->var("nSig"));
264     hcInv.SetTestSize(0.05);
265     hcInv.UseCLs(true);
266     hcInv.RunFixedScan(5,theLimit-0.5,theLimit+0.5);;
267     RooStats::HypoTestInverterResult* hcInt = hcInv.GetInterval();
268     float ulError = hcInt->UpperLimitEstimatedError();
269     theLimit = hcInt->UpperLimit();
270 buchmann 1.18 dout << "Found upper limit : " << theLimit << "+/-" << ulError << endl;
271 buchmann 1.3
272     return theLimit/PlottingSetup::luminosity;
273    
274     }
275    
276 buchmann 1.23 string GetSkimName(int isample) {
277     return removefunnystring(allsamples.collection[isample].filename);
278     }
279    
280 buchmann 1.2 TTree* SkimTree(int isample) {
281 buchmann 1.23 string TreeName = GetSkimName(isample);
282 buchmann 1.20 cout << "About to skim " << TreeName << " for sample id " << isample << endl;
283 buchmann 1.23 TTree* newTree = new TTree(TreeName.c_str(),TreeName.c_str());
284 buchmann 1.15
285     float mll,edgeWeight;
286     int id1,id2;
287    
288     newTree->Branch("edgeWeight",&edgeWeight,"edgeWeight/F");
289     newTree->Branch("mll",&mll,"mll/F");
290     newTree->Branch("id1",&id1,"id1/I");
291     newTree->Branch("id2",&id2,"id2/I");
292 buchmann 1.20 newTree->Branch("isample",&isample,"isample/I");
293 buchmann 1.15
294 buchmann 1.2 float xsweight=1.0;
295     if(allsamples.collection[isample].is_data==false) xsweight=luminosity*allsamples.collection[isample].weight;
296     if(EdgeFitter::MarcoDebug) {
297 buchmann 1.20 dout << " Going to reduce it with cut " << EdgeFitter::cut << endl;
298 buchmann 1.18 dout << " Original tree contains " << allsamples.collection[isample].events->GetEntries() << endl;
299 buchmann 1.2 }
300 buchmann 1.15
301 buchmann 1.6 float tmll;
302 buchmann 1.15 int tid1,tid2;
303 buchmann 1.6 allsamples.collection[isample].events->SetBranchAddress("mll",&tmll);
304 buchmann 1.15 allsamples.collection[isample].events->SetBranchAddress("id1",&tid1);
305     allsamples.collection[isample].events->SetBranchAddress("id2",&tid2);
306 buchmann 1.6
307 buchmann 1.2 TTreeFormula *select = new TTreeFormula("select", EdgeFitter::cut, allsamples.collection[isample].events);
308 buchmann 1.10 TTreeFormula *Weight = new TTreeFormula("Weight", cutWeight, allsamples.collection[isample].events);
309 buchmann 1.15
310 buchmann 1.2 float wgt=1.0;
311     for (Int_t entry = 0 ; entry < allsamples.collection[isample].events->GetEntries() ; entry++) {
312     allsamples.collection[isample].events->LoadTree(entry);
313     if (select->EvalInstance()) {
314     allsamples.collection[isample].events->GetEntry(entry);
315 buchmann 1.15 mll=tmll;
316     id1=tid1;
317     id2=tid2;
318 buchmann 1.10 wgt=Weight->EvalInstance();
319 buchmann 1.6 edgeWeight=wgt*xsweight;
320 buchmann 1.2 newTree->Fill();
321     }
322     }
323    
324 buchmann 1.20 if(EdgeFitter::MarcoDebug) dout << " Reduced tree contains " << newTree->GetEntries() << " entries " << endl;
325 buchmann 1.2 return newTree;
326     }
327    
328     void EdgeFitter::InitializeVariables(float _mllmin, float _mllmax, float _jzbmax, TCut _cut) {
329     mllmin=_mllmin;
330     mllmax=_mllmax;
331     jzbmax=_jzbmax;
332     cut=_cut;
333     }
334 buchmann 1.10
335     TTree* MergeTrees(vector<TTree*> trees) {
336 buchmann 1.23 assert(trees.size()>0);
337 buchmann 1.15 TTree * newtree = (TTree*)trees[0]->CloneTree(0);
338 buchmann 1.20 newtree->SetTitle("FullTree");
339     newtree->SetName("FullTree");
340 buchmann 1.10 trees[0]->GetListOfClones()->Remove(newtree);
341     trees[0]->ResetBranchAddresses();
342     newtree->ResetBranchAddresses();
343    
344 buchmann 1.15 for(int itree=0;itree<trees.size();itree++) {
345 buchmann 1.10 newtree->CopyAddresses(trees[itree]);
346     Long64_t nentries = trees[itree]->GetEntries();
347     for (Long64_t iEntry=0;iEntry<nentries;iEntry++) {
348     trees[itree]->GetEntry(iEntry);
349     newtree->Fill();
350     }
351     trees[itree]->ResetBranchAddresses(); // Disconnect from new tree
352     if (newtree->GetTreeIndex()) {
353     newtree->GetTreeIndex()->Append(trees[itree]->GetTreeIndex(),kTRUE);
354     }
355     if (newtree && newtree->GetTreeIndex()) {
356     newtree->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
357     }
358     }
359     return newtree;
360     }
361    
362    
363    
364 buchmann 1.2 void EdgeFitter::PrepareDatasets(int is_data) {
365 buchmann 1.5 write_warning(__FUNCTION__,"Need to make this function ready for scans as well (use signal from scan samples)");
366 buchmann 1.23
367     ensure_directory_exists(PlottingSetup::cbafbasedir+"/EdgeCache/");
368    
369     stringstream FileName;
370     FileName << PlottingSetup::cbafbasedir << "/EdgeCache/" << md5((const char*)cut) << ".root";
371    
372     TFile *fEdgeCache = new TFile(FileName.str().c_str(),"UPDATE");
373     if(fEdgeCache->GetNkeys()==0) {
374     ofstream CacheOverview;
375     CacheOverview.open((PlottingSetup::cbafbasedir+"/EdgeCache/CacheOverview").c_str(),ios::app);
376     CacheOverview << md5((const char*)cut) << ";" << (const char*) cut << endl;
377     CacheOverview.close();
378     }
379    
380 buchmann 1.10 vector<TTree*> SkimmedTrees;
381     TTree *SkimmedTree[(int)allsamples.collection.size()];
382 buchmann 1.7 for(int isample=0;isample<(int)allsamples.collection.size();isample++) {
383 buchmann 1.2 if(!allsamples.collection[isample].is_active) continue;
384     if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
385     if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
386     if(is_data!=1&&allsamples.collection[isample].is_data==true) continue;//kick all data samples when looking for MC
387     if(is_data!=2&&allsamples.collection[isample].is_signal==true) continue;//remove signal sample if we don't want it.
388 buchmann 1.18 if(EdgeFitter::MarcoDebug) dout << "Considering : " << allsamples.collection[isample].samplename << endl;
389 buchmann 1.23
390     string SkimName = GetSkimName(isample);
391     SkimmedTree[isample] = (TTree*)fEdgeCache->Get(SkimName.c_str());
392     if(!SkimmedTree[isample]) {
393     dout << "Need to generate tree for " << allsamples.collection[isample].filename << endl;
394     SkimmedTree[isample] = SkimTree(isample);
395     fEdgeCache->cd();
396     SkimmedTree[isample]->Write();
397     } else {
398     dout << "Loaded tree for " << allsamples.collection[isample].filename << " from cache file " << endl;
399     }
400    
401 buchmann 1.15 SkimmedTrees.push_back(SkimmedTree[isample]);
402 buchmann 1.2 }
403 buchmann 1.10
404     TTree *completetree = MergeTrees(SkimmedTrees);
405    
406 buchmann 1.18 if(EdgeFitter::MarcoDebug) dout << "Complete tree now contains " << completetree->GetEntries() << " entries " << endl;
407 buchmann 1.2
408 buchmann 1.10 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
409 buchmann 1.2 RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
410     RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
411 buchmann 1.6 RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
412     RooArgSet observables(mll,id1,id2,edgeWeight);
413 buchmann 1.2
414     string title="CMS Data";
415     if(is_data!=1) title="CMS SIMULATION";
416 buchmann 1.6 RooDataSet LAllData("LAllData",title.c_str(),completetree,observables,"","edgeWeight");
417 buchmann 1.23 fEdgeCache->Close();
418     dout << "Edge cache closed." << endl;
419    
420 buchmann 1.10 EdgeFitter::SFSample = (RooDataSet*)LAllData.reduce("id1==id2");
421     EdgeFitter::OFSample = (RooDataSet*)LAllData.reduce("id1!=id2");
422 buchmann 1.2 EdgeFitter::AllData = (RooDataSet*)LAllData.reduce("id1!=id2||id1==id2");
423    
424 buchmann 1.10 SFSample->SetName("SFSample");
425     OFSample->SetName("OFSample");
426 buchmann 1.2 AllData->SetName("AllData");
427    
428     if(EdgeFitter::MarcoDebug) {
429 buchmann 1.20 dout << "Number of (weighted) events in full sample = " << AllData->sumEntries() << " (raw events : " << AllData->numEntries() << ")" << endl;
430     dout << "Number of (weighted) events in eemm sample = " << SFSample->sumEntries() << " (raw events : " << SFSample->numEntries() << ")" << endl;
431     dout << "Number of (weighted) events in em sample = " << OFSample->sumEntries() << " (raw events : " << OFSample->numEntries() << ")" << endl;
432 buchmann 1.2 }
433 buchmann 1.6
434 buchmann 1.2 }
435    
436 buchmann 1.15 void EdgeFitter::DrawDatasetContent(int is_data) {
437 buchmann 1.20 TCanvas* cSFdata = new TCanvas("cSFdata","cSFdata") ;
438    
439 buchmann 1.15 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
440     RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("SF sample")) ;
441     frame1->GetXaxis()->CenterTitle(1);
442     frame1->GetYaxis()->CenterTitle(1);
443     SFSample->plotOn(frame1,RooFit::Name("SFdata")) ;
444    
445     RooPlot* frame2 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
446     frame2->GetXaxis()->CenterTitle(1);
447     frame2->GetYaxis()->CenterTitle(1);
448     OFSample->plotOn(frame2,RooFit::Name("OFdata")) ;
449    
450 buchmann 1.20 // THStack sf_ref = allsamples.DrawStack("sf_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1==id2"),is_data, luminosity);
451     // THStack of_ref = allsamples.DrawStack("of_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1!=id2"),is_data, luminosity);
452    
453     TH1F *sf_ref = allsamples.Draw("sf_ref","mll",int((mllmax-mllmin)/5.0),mllmin,mllmax, "m_{ll} [GeV]", "events", cut&&TCut("id1==id2"),is_data, luminosity);
454     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);
455    
456     TH1F *sf_fit = (TH1F*)SFSample->createHistogram("sf_fit",mll,RooFit::Binning(int((mllmax-mllmin)/5)),RooFit::SumW2Error(true));
457     TH1F *of_fit = (TH1F*)OFSample->createHistogram("of_fit",mll,RooFit::Binning(int((mllmax-mllmin)/5)),RooFit::SumW2Error(true));
458    
459     TLegend *leg = allsamples.allbglegend();
460     leg->AddEntry(sf_fit,"RooDataSet content","p");
461     leg->SetX1(0.58);
462    
463 buchmann 1.15 cSFdata->cd() ;
464     gPad->SetLeftMargin(0.15);
465     frame1->GetYaxis()->SetTitleOffset(1.4);
466     frame1->Draw();
467 buchmann 1.20 sf_ref->Draw("histo,same");
468     sf_fit->Draw("e1,same");
469     leg->Draw("same");
470 buchmann 1.15 if(is_data==data) DrawPrelim();
471     else DrawPrelim(PlottingSetup::luminosity,true);
472     CompleteSave(cSFdata,"Edge/SF_NoFit");
473    
474     TCanvas* cOFdata = new TCanvas("cOFdata","cOFdata") ;
475     cOFdata->cd() ;
476     gPad->SetLeftMargin(0.15);
477 buchmann 1.16 frame2->SetMaximum(frame1->GetMaximum());
478 buchmann 1.15 frame2->GetYaxis()->SetTitleOffset(1.4);
479     frame2->Draw();
480 buchmann 1.20 of_ref->Draw("histo,same");
481     of_fit->Draw("e1,same");
482     leg->Draw("same");
483 buchmann 1.15 if(is_data==data) DrawPrelim();
484     else DrawPrelim(PlottingSetup::luminosity,true);
485     CompleteSave(cOFdata,"Edge/OF_NoFit");
486 buchmann 1.20
487     delete sf_fit;
488     delete of_fit;
489    
490     delete sf_ref;
491     delete of_ref;
492    
493 buchmann 1.15 }
494    
495 buchmann 1.10 string WriteWithError(float central, float error, int digits) {
496     float ref=central;
497     if(ref<0) ref=-central;
498     int HighestSigDigit = 0;
499     if(ref>1) HighestSigDigit = int(log(ref)/log(10))+1;
500     else HighestSigDigit = int(log(ref)/(log(10)));
501    
502     float divider=pow(10.0,(double(HighestSigDigit-digits)));
503    
504     stringstream result;
505     result << divider*int(central/divider+0.5) << " #pm " << divider*int(error/divider+0.5);
506     return result.str();
507     }
508    
509    
510 buchmann 1.2 string EdgeFitter::RandomStorageFile() {
511     TRandom3 *r = new TRandom3(0);
512     int rho = (int)r->Uniform(1,10000000);
513     stringstream RandomFile;
514     RandomFile << PlottingSetup::cbafbasedir << "/exchange/TempEdgeFile_" << rho << ".root";
515     delete r;
516     return RandomFile.str();
517     }
518    
519     Yield EdgeFitter::Get_Z_estimate(float jzb_cut, int icut) {
520     if(MarcoDebug) write_error(__FUNCTION__,"Returning random Z yield");
521     Yield a(123,45,67); return a;
522     return PlottingSetup::allresults.predictions[icut].Zbkg;
523     }
524    
525     Yield EdgeFitter::Get_T_estimate(float jzb_cut, int icut) {
526     if(MarcoDebug) write_error(__FUNCTION__,"Returning random TTbar yield");
527     Yield a(1234,56,78); return a;
528     return PlottingSetup::allresults.predictions[icut].Flavorsym;
529     }
530    
531 buchmann 1.5 void EdgeFitter::DoFit(int is_data, float jzb_cut) {
532 buchmann 1.16
533     stringstream prefix;
534     if(is_data==data) prefix << "data_";
535     if(is_data==mc) prefix << "mc_";
536     if(is_data==mcwithsignal) prefix << "mcwithS_";
537    
538     prefix << EdgeFitter::Mode << "_" << jzb_cut;
539    
540 buchmann 1.21 if(EdgeFitter::AllowTriangle) prefix << "__H1";
541     else prefix << "__H0";
542 buchmann 1.16
543 buchmann 1.10 RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
544 buchmann 1.6 RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
545 buchmann 1.5 RooCategory sample("sample","sample") ;
546 buchmann 1.10 sample.defineType("SF");
547 buchmann 1.5 //sample.defineType("mm");
548 buchmann 1.10 sample.defineType("OF");
549 buchmann 1.6
550 buchmann 1.10 //RooDataSet combData("combData","combined data",mll,Index(sample),Import("SF",*SFSample),Import("mm",*mmSample),Import("OF",*OFSample));
551     RooDataSet combData("combData","combined data",RooArgSet(mll,edgeWeight),RooFit::Index(sample),RooFit::Import("SF",*SFSample),RooFit::Import("OF",*OFSample),RooFit::WeightVar(edgeWeight));
552 buchmann 1.6
553 buchmann 1.5
554     //First we make a fit to opposite flavor
555 buchmann 1.21 RooRealVar fttbarOF("fttbarOF", "fttbarOF", 100, 0, 1.5*combData.sumEntries());
556 buchmann 1.10 RooRealVar par1ttbarOF("par1ttbarOF", "par1ttbarOF", 1.6, 0.01, 4.0);
557     RooRealVar par2ttbarOF("par2ttbarOF", "par2ttbarOF", 1.0);
558     RooRealVar par3ttbarOF("par3ttbarOF", "par3ttbarOF", 0.028, 0.001, 1.0);
559     RooRealVar par4ttbarOF("par4ttbarOF", "par4ttbarOF", 2.0);
560     RooSUSYBkgPdf ttbarOF("ttbarOF","ttbarOF", mll , par1ttbarOF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
561     RooAddPdf model_OF("model_OF","model_OF", ttbarOF, fttbarOF);
562 buchmann 1.5 RooSimultaneous simPdfOF("simPdfOF","simultaneous pdf", sample) ;
563 buchmann 1.10 simPdfOF.addPdf(model_OF,"OF");
564 buchmann 1.14 RooFitResult *resultOF = simPdfOF.fitTo(combData, RooFit::Save(),RooFit::Extended(),RooFit::Minos(true));
565 buchmann 1.18 dout << "============================= < OF results > =============================" << endl;
566 buchmann 1.17 resultOF->Print();
567 buchmann 1.18 dout << "============================= < /OF results > =============================" << endl;
568 buchmann 1.14
569 buchmann 1.16
570     RooPlot* frameO = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample"));
571     frameO->GetXaxis()->CenterTitle(1);
572     frameO->GetYaxis()->CenterTitle(1);
573     combData.plotOn(frameO,RooFit::Name("OFdata"),RooFit::Cut("sample==sample::OF")) ;
574     simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("FullFit"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
575     simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("TTbarOFonly"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
576    
577     TCanvas* pof = new TCanvas("pof","pof") ;
578     pof->cd() ;
579     gPad->SetLeftMargin(0.15);
580     frameO->GetYaxis()->SetTitleOffset(1.4);
581     frameO->Draw();
582     if(is_data==data) DrawPrelim();
583     else DrawPrelim(PlottingSetup::luminosity,true);
584 buchmann 1.17 if(EdgeFitter::FixedMEdge>=0) CompleteSave(pof,"Edge/OF__OFFitonly_"+prefix.str()+"__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
585     else CompleteSave(pof,"Edge/OF__OFFitonly_"+prefix.str(),false,false);
586 buchmann 1.16 delete pof;
587    
588 buchmann 1.14 if(resultOF->covQual()!=3) {
589     write_error(__FUNCTION__,"OF fit did not converge!!! Cannot continue!");
590 buchmann 1.18 dout << "covQual is " << resultOF->covQual() << endl;
591 buchmann 1.21 if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=-1;
592     else EdgeFitter::FixedMEdgeChi2_H0=-1;
593 buchmann 1.14 if(EdgeFitter::RejectPointIfNoConvergence) return;
594     } else {
595     write_info(__FUNCTION__,"OF fit converged");
596     }
597 buchmann 1.11
598     float StartingMedge=70;
599     if(EdgeFitter::FixedMEdge>0) StartingMedge=EdgeFitter::FixedMEdge;
600 buchmann 1.5
601    
602 buchmann 1.18 RooDataSet *ZDataSet = (RooDataSet*)EdgeFitter::AllData->reduce("id1==id2 && abs(mll-91.2)<20");
603    
604 buchmann 1.17 float maxZ = ZDataSet->sumEntries();
605 buchmann 1.18 dout << "maxZ was set to " << maxZ << endl;
606 buchmann 1.17 delete ZDataSet;
607    
608    
609 buchmann 1.5 // Now same flavor
610 buchmann 1.17 RooRealVar fzSF("fzSF", "fzSF", 39, 39, maxZ);
611 buchmann 1.10 RooRealVar meanzSF("meanzSF", "meanzSF", 91.1876, 89, 95);
612     //RooRealVar sigmazSF("sigmazSF", "sigmazSF", 0.5);
613     RooRealVar sigmazSF("sigmazSF", "sigmazSF", 5, 0.5, 5);
614     RooRealVar widthzSF("widthzSF", "widthzSF", 2.94);
615 buchmann 1.21 widthzSF.setConstant(1);
616    
617 buchmann 1.22 /*RooRealVar fttbarSFx("fttbarSFx","fttbarSFx",0.2,1.8);
618 buchmann 1.21 RooRealVar mRsfof("mRsfof","mRsfof",1.02);
619     RooRealVar wRsfof("wRsfof","wRsfof",0.07);
620    
621     RooGaussian RSFOF("RSFOF","RSFOF",fttbarSFx,mRsfof,wRsfof);
622 buchmann 1.22 RooProdPdf fttbarSF("fttbarSF","fttbarOF*RSFOF",RooArgList(RSFOF,fttbarOF));*/
623     RooRealVar fttbarSF("fttbarSF", "fttbarSF", fttbarOF.getVal(), 0.2*fttbarOF.getVal(), 1.5*fttbarOF.getVal());
624 buchmann 1.10
625 buchmann 1.17 RooRealVar par1ttbarSF("par1ttbarSF", "par1ttbarSF", 1.02*par1ttbarOF.getVal(), (1.02-0.07)*par1ttbarOF.getVal(), (1.02+0.07)*par1ttbarOF.getVal());
626 buchmann 1.5
627 buchmann 1.18 RooRealVar fsignalSF("fsignalSF", "fsignalSF", 0, 0, 300);
628 buchmann 1.10 RooRealVar par1signalSF("par1signalSF", "par1signalSF", 45, 20, 100);
629     RooRealVar par2signalSF("par2signalSF", "par2signalSF", 2, 1, 10);
630 buchmann 1.11 RooRealVar par3signalSF("par3signalSF", "par3signalSF", StartingMedge, 0, 300);
631 buchmann 1.5
632 buchmann 1.10 RooVoigtian zSF("zSF", "zSF", mll, meanzSF, widthzSF, sigmazSF);
633 buchmann 1.5
634 buchmann 1.11 if(EdgeFitter::FixedMEdge>0) par3signalSF.setConstant();
635 buchmann 1.5
636 buchmann 1.17 RooSUSYBkgPdf ttbarSF("ttbarSF","ttbarSF", mll , par1ttbarSF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
637 buchmann 1.10 RooSUSYTPdf signalSF("signalSF","signalSF", mll , par1signalSF, sigmazSF, par3signalSF);
638 buchmann 1.21
639     RooAddPdf model_SF("model_SF","model_SF", RooArgList(zSF, ttbarSF, signalSF), RooArgList(fzSF, fttbarSF, fsignalSF));
640    
641 buchmann 1.10
642 buchmann 1.21 if(!EdgeFitter::AllowTriangle) {
643     fsignalSF.setVal(0.0); // kill off the signal if we don't want the triangle
644     fsignalSF.setConstant(1);
645 buchmann 1.22 par1signalSF.setConstant(1);
646 buchmann 1.21 par2signalSF.setConstant(1);
647     par3signalSF.setConstant(1);
648     }
649 buchmann 1.5
650     RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
651 buchmann 1.10 simPdf.addPdf(model_SF,"SF") ;
652 buchmann 1.14 simPdf.addPdf(model_OF,"OF") ;
653 buchmann 1.5
654 buchmann 1.21
655 buchmann 1.14 RooFitResult *result = simPdf.fitTo(combData, RooFit::Save(), RooFit::Extended(),RooFit::Minos(true));
656    
657     if(result->covQual()!=3) {
658     write_error(__FUNCTION__,"Full fit did not converge!!! Cannot continue!");
659 buchmann 1.18 dout << "covQual is " << result->covQual() << endl;
660 buchmann 1.21 if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=-1;
661     else EdgeFitter::FixedMEdgeChi2_H0=-1;
662 buchmann 1.14 if(EdgeFitter::RejectPointIfNoConvergence) return;
663     } else {
664     write_info(__FUNCTION__,"Full fit converged");
665     }
666    
667 buchmann 1.21 dout << "============================= < Full results > =============================" << endl;
668 buchmann 1.17 result->Print();
669 buchmann 1.21 dout << "============================= < /Full results > =============================" << endl;
670 buchmann 1.17
671 buchmann 1.5
672 buchmann 1.10 RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("EE sample")) ;
673     frame1->GetXaxis()->CenterTitle(1);
674 buchmann 1.14 frame1->GetYaxis()->CenterTitle(1);
675 buchmann 1.11 combData.plotOn(frame1,RooFit::Name("SFdata"),RooFit::Cut("sample==sample::SF")) ;
676     simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("FullFit"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
677     simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("TTbarSFonly"),RooFit::Components("ttbarSF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
678     simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("DYSFonly"),RooFit::Components("zSF"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
679 buchmann 1.21 if(EdgeFitter::AllowTriangle) simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("SignalSFonly"),RooFit::Components("signalSF"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kGreen));
680 buchmann 1.5
681 buchmann 1.21 if(EdgeFitter::AllowTriangle) EdgeFitter::FixedMEdgeChi2_H1=frame1->chiSquare("FullFit", "SFdata", 3);
682     else EdgeFitter::FixedMEdgeChi2_H0=frame1->chiSquare("FullFit", "SFdata", 3);
683    
684 buchmann 1.18 dout << "Result : " << endl;
685 buchmann 1.21 if(EdgeFitter::AllowTriangle) dout << "f signal : " << fsignalSF.getVal() << " +/- " << fsignalSF.getError() << endl;
686     // dout << "f ttbar : " << fttbarSF.getVal() << " +/- " << fttbarSF.getError() << endl;
687     dout << "f ttbar : " << fttbarSF.getVal() << " +/- NO ERROR CUZ ITS A PDF "<< endl;
688 buchmann 1.18 dout << "f tt OF : " << fttbarOF.getVal() << " +/- " << fttbarOF.getError() << endl;
689     dout << "f z SF : " << fzSF.getVal() << " +/- " << fzSF.getError() << endl;
690 buchmann 1.21 if(EdgeFitter::AllowTriangle) dout << "#Chi^{2}/NDF : " << EdgeFitter::FixedMEdgeChi2_H1 << endl;
691     else dout << "#Chi^{2}/NDF : " << EdgeFitter::FixedMEdgeChi2_H0 << endl;
692 buchmann 1.5
693     // The same plot for the cointrol sample slice
694 buchmann 1.10 RooPlot* frame3 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
695     frame3->GetXaxis()->CenterTitle(1);
696 buchmann 1.14 frame3->GetYaxis()->CenterTitle(1);
697 buchmann 1.10 frame3->SetMaximum(frame1->GetMaximum());
698     combData.plotOn(frame3,RooFit::Cut("sample==sample::OF")) ;
699 buchmann 1.16 simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
700     simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
701 buchmann 1.5
702    
703     TCanvas* c = new TCanvas("rf501_simultaneouspdf","rf403_simultaneouspdf") ;
704     c->cd() ;
705     gPad->SetLeftMargin(0.15);
706     frame1->GetYaxis()->SetTitleOffset(1.4);
707     frame1->Draw();
708     if(is_data==data) DrawPrelim();
709     else DrawPrelim(PlottingSetup::luminosity,true);
710 buchmann 1.10 stringstream infotext;
711 buchmann 1.14 infotext << "#splitline{Fit results (" << EdgeFitter::Mode << ">" << jzb_cut << "): }{#splitline{";
712 buchmann 1.15 infotext << "N(Data) = " << EdgeFitter::SFSample->sumEntries() << "}{#splitline{";
713 buchmann 1.10 infotext << "N(Z+Jets) = " << WriteWithError(fzSF.getVal(),fzSF.getError(),3) << "}{#splitline{";
714 buchmann 1.21 //infotext << "N(t#bar{t}) = " << WriteWithError(fttbarSF.getVal(),fttbarSF.getError(),3) << "}{#splitline{";
715     infotext << "N(t#bar{t}) = " << WriteWithError(fttbarSF.getVal(),0,3) << "}{#splitline{";
716     write_warning(any2string(__LINE__),"Don't have the error yet, need to complete this");
717     if(EdgeFitter::AllowTriangle) {
718     infotext << "N(signal) = " << WriteWithError(fsignalSF.getVal(),fsignalSF.getError(),3) << "}{";
719     infotext << "m_{edge} = " << WriteWithError(par3signalSF.getVal(),par3signalSF.getError(),3) << "}}}}}";
720     } else infotext << "}{}}}}}";
721 buchmann 1.10
722     TLatex *infobox = new TLatex(0.57,0.75,infotext.str().c_str());
723     infobox->SetNDC();
724     infobox->SetTextSize(0.03);
725     infobox->Draw();
726 buchmann 1.14 if(EdgeFitter::FixedMEdge>=0) CompleteSave(c,"Edge/"+prefix.str()+"_SF__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
727     else CompleteSave(c,"Edge/"+prefix.str()+"_SF",false,false);
728 buchmann 1.5 delete c;
729    
730     TCanvas* e = new TCanvas("rf501_simultaneouspdfem","rf403_simultaneouspdfem") ;
731     e->cd();
732     gPad->SetLeftMargin(0.15);
733     frame3->GetYaxis()->SetTitleOffset(1.4);
734     frame3->Draw();
735     if(is_data==data) DrawPrelim();
736     else DrawPrelim(PlottingSetup::luminosity,true);
737 buchmann 1.14 if(EdgeFitter::FixedMEdge>=0) CompleteSave(e,"Edge/"+prefix.str()+"_OF__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
738     else CompleteSave(e,"Edge/"+prefix.str()+"_OF",false,false);
739 buchmann 1.5 delete e;
740    
741 buchmann 1.6
742    
743    
744 buchmann 1.5 /* TCanvas* f = new TCanvas("rf501_simultaneouspdfem","rf403_simultaneouspdfem") ;
745     f->cd();
746     gPad->SetLeftMargin(0.15);
747     frame4->GetYaxis()->SetTitleOffset(1.4);
748     frame4->Draw();
749     if(is_data==data) DrawPrelim();
750     else DrawPrelim(PlottingSetup::luminosity,true);
751 buchmann 1.10 CompleteSave(f,"Edge/"+prefix.str()+"_SF");
752 buchmann 1.5 delete f;*/
753 buchmann 1.7
754 buchmann 1.10
755     /*
756     float maxZ=200;
757     RooWorkspace* wspace = new RooWorkspace();
758     stringstream mllvar;
759     mllvar << "mll[" << (mllmax-mllmin)/2 << "," << mllmin << "," << mllmax << "]";
760     wspace->factory(mllvar.str().c_str());
761     wspace->var("mll")->setBins(30);
762     wspace->factory("nSig[1.,0.,100.]");
763     wspace->factory(("nZ[0.04.,0.,"+any2string(maxZ)+"]").c_str());
764     wspace->factory("rME[1.12,1.05,1.19]");
765     wspace->factory("effUncert[1.]");
766     EdgeFitter::prepareLimits(wspace, true);
767     */
768    
769     write_warning(__FUNCTION__," A lot missing here to calculate limits");
770    
771 buchmann 1.5 }
772    
773 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) {
774    
775 buchmann 1.5 TCut _cut(cut&&PlottingSetup::basiccut&&PlottingSetup::passtrig);
776    
777     TFile *f = new TFile("workingfile.root","RECREATE");
778    
779     EdgeFitter::InitializeVariables(PlottingSetup::iMllLow,PlottingSetup::iMllHigh,PlottingSetup::jzbHigh,_cut);
780 buchmann 1.2
781     EdgeFitter::PrepareDatasets(is_data);
782 buchmann 1.5
783 buchmann 1.15 EdgeFitter::DrawDatasetContent(is_data);
784    
785 buchmann 1.5 RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
786     RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
787 buchmann 1.11
788    
789 buchmann 1.21
790     EdgeFitter::AllowTriangle=false;
791     EdgeFitter::DoFit(is_data, jzb_cut);
792    
793     write_info(__FUNCTION__,"TAKING SHORTCUT");return;
794    
795     EdgeFitter::AllowTriangle=true;
796    
797 buchmann 1.18 bool ScanMassRange=false;
798 buchmann 1.17 float ScanSteps=5.0;//GeV
799 buchmann 1.12
800 buchmann 1.11
801     if(ScanMassRange) {
802 buchmann 1.12 TFile *fscan = new TFile("fscan.root","UPDATE");
803     TGraph *gr = new TGraph();
804 buchmann 1.21 TGraph *Rgr = new TGraph();
805 buchmann 1.12 stringstream GrName;
806 buchmann 1.14 GrName << "ScanGraphFor_" << EdgeFitter::Mode << "_" << jzb_cut;
807 buchmann 1.21 stringstream RGrName;
808     RGrName << "ScanRatioGraphFor_" << EdgeFitter::Mode << "_" << jzb_cut;
809 buchmann 1.12 gr->SetName(GrName.str().c_str());
810 buchmann 1.21 Rgr->SetName(RGrName.str().c_str());
811 buchmann 1.12
812 buchmann 1.11 int i=0;
813 buchmann 1.17 for(float tempMedge=10;tempMedge<=300;tempMedge+=ScanSteps) {
814 buchmann 1.14 write_info(__FUNCTION__,"Now testing Medge="+any2string(tempMedge)+" for "+EdgeFitter::Mode+">"+any2string(jzb_cut));
815 buchmann 1.11 EdgeFitter::FixedMEdge=tempMedge;
816     EdgeFitter::DoFit(is_data, jzb_cut);
817 buchmann 1.21 if(EdgeFitter::FixedMEdgeChi2_H1<0) continue;
818     gr->SetPoint(i,tempMedge,EdgeFitter::FixedMEdgeChi2_H1);
819     Rgr->SetPoint(i,tempMedge,EdgeFitter::FixedMEdgeChi2_H1/EdgeFitter::FixedMEdgeChi2_H0);
820 buchmann 1.12 i++;
821 buchmann 1.11 }
822    
823     TCanvas *ScanCan = new TCanvas("ScanCan","ScanCan",500,500);
824     gr->GetXaxis()->SetTitle("m_{edge}");
825     gr->GetXaxis()->CenterTitle();
826 buchmann 1.12 gr->GetYaxis()->SetTitle("#Chi^{2} / NDF");
827 buchmann 1.11 gr->GetYaxis()->CenterTitle();
828 buchmann 1.13 gr->GetYaxis()->SetTitleOffset(0.95);
829     gr->GetXaxis()->SetTitleOffset(0.9);
830     gr->SetLineColor(kBlue);
831     gr->SetTitle("");
832     gr->Draw("AL");
833 buchmann 1.11 stringstream ScanCanSave;
834 buchmann 1.14 ScanCanSave << "Edge/MEdgeScan_"+EdgeFitter::Mode+"_" << jzb_cut;
835 buchmann 1.12 if(is_data) DrawPrelim();
836     else DrawMCPrelim();
837 buchmann 1.13 CompleteSave(ScanCan,ScanCanSave.str());
838 buchmann 1.21
839     Rgr->GetXaxis()->SetTitle("m_{edge}");
840     Rgr->GetXaxis()->CenterTitle();
841     Rgr->GetYaxis()->SetTitle("#Chi^{2} / NDF");
842     Rgr->GetYaxis()->CenterTitle();
843     Rgr->GetYaxis()->SetTitleOffset(0.95);
844     Rgr->GetXaxis()->SetTitleOffset(0.9);
845     Rgr->SetLineColor(kBlue);
846     Rgr->SetTitle("");
847     Rgr->Draw("AL");
848     ScanCanSave.str("");
849     ScanCanSave << "Edge/MEdgeScan_Ratio_"+EdgeFitter::Mode+"_" << jzb_cut;
850     if(is_data) DrawPrelim();
851     else DrawMCPrelim();
852     CompleteSave(ScanCan,ScanCanSave.str());
853 buchmann 1.13 fscan->cd();
854     gr->Write();
855 buchmann 1.11 delete ScanCan;
856 buchmann 1.12 fscan->Close();
857 buchmann 1.11 } else {
858     EdgeFitter::DoFit(is_data, jzb_cut);
859 buchmann 1.21 dout << "Chi^2 (H0) = " << EdgeFitter::FixedMEdgeChi2_H0 << endl;
860     dout << "Chi^2 (H1) = " << EdgeFitter::FixedMEdgeChi2_H1 << endl;
861 buchmann 1.11 }
862    
863    
864 buchmann 1.5 RooMsgService::instance().setGlobalKillBelow(msglevel);
865    
866 buchmann 1.2 f->Close();
867    
868     }
869    
870     void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree *signalevents=0) {
871 buchmann 1.14
872     EdgeFitter::Mode="JZB";
873     if(mcjzb=="met[4]") EdgeFitter::Mode="MET";
874    
875 buchmann 1.7 for(int icut=0;icut<(int)jzb_cut.size();icut++) {
876 buchmann 1.2 stringstream addcut;
877     if(is_data==1) addcut << "(" << datajzb << ">" << jzb_cut[icut] << ")";
878     if(is_data!=1) addcut << "(" << mcjzb << ">" << jzb_cut[icut] << ")";
879     TCut jcut(addcut.str().c_str());
880    
881 buchmann 1.5
882 buchmann 1.2 EdgeFitter::DoEdgeFit(mcjzb, datajzb, DataPeakError, MCPeakError, jzb_cut[icut], icut, is_data, jcut&&cut, signalevents);
883    
884     }
885     }