ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/EdgeLimit.C
(Generate patch)

Comparing UserCode/cbrown/Development/Plotting/Modules/EdgeLimit.C (file contents):
Revision 1.3 by buchmann, Mon Jun 18 07:51:38 2012 UTC vs.
Revision 1.19 by buchmann, Fri Jun 14 05:36:10 2013 UTC

# Line 1 | Line 1
1   #include <iostream>
2  
3 + #include <TVirtualIndex.h>
4 +
5   #include <RooRealVar.h>
6   #include <RooArgSet.h>
7   #include <RooDataSet.h>
8 < #include <RooRealVar.h>
9 < #include <RooArgSet.h>
10 < #include <RooDataSet.h>
8 > #include <RooMCStudy.h>
9 > #include <RooCategory.h>
10 >
11 > #include <RooPlot.h>
12 > #include <RooSimultaneous.h>
13 > #include <RooAddPdf.h>
14 > #include <RooFitResult.h>
15 > #include <RooVoigtian.h>
16 > #include <RooMsgService.h>
17 >
18   #include <RooStats/ModelConfig.h>
19   #include "RooStats/ProfileLikelihoodCalculator.h"
20   #include "RooStats/LikelihoodInterval.h"
# Line 15 | Line 24
24   #include "RooStats/HybridCalculatorOriginal.h"
25   #include "RooStats/HypoTestInverterOriginal.h"
26  
27 + //#include "ParametrizedEdge.C"
28 + #include "EdgeModules/RooSUSYTPdf.cxx"
29 + #include "EdgeModules/RooSUSYBkgPdf.cxx"
30 +
31  
32   using namespace std;
33   using namespace PlottingSetup;
# Line 40 | Line 53 | namespace EdgeFitter {
53    
54    void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, float jzb_cut, int icut, int is_data, TCut cut, TTree*);
55    void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree*);
56 +  void getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp);
57    void InitializeVariables(float _mllmin, float _mllmax, float _jzbmax, TCut _cut);
58    void PrepareDatasets(int);
59 <  void DoFit();
59 >  void DrawDatasetContent(int);
60 >  void DoFit(int is_data, float jzb_cut);
61    string RandomStorageFile();
62    Yield Get_Z_estimate(float,int);
63    Yield Get_T_estimate(float,int);
64 <  float SetEdgeLimit(int , RooWorkspace *ws);
64 >  float calcExclusion(RooWorkspace *ws, RooDataSet data, bool calcExclusion);
65 >  vector<RooDataSet> generateToys(RooWorkspace *ws, int nToys);
66 >  void prepareLimits(RooWorkspace *ws, bool ComputeBands);
67 >  TGraph* prepareLM(float mass, float nEv);
68    
69    float jzbmax;
70    float mllmin;
# Line 54 | Line 72 | namespace EdgeFitter {
72    TCut cut;
73    
74    RooDataSet* AllData;
75 <  RooDataSet* eeSample;
76 <  RooDataSet* mmSample;
77 <  RooDataSet* emSample;
75 >  RooDataSet* SFSample;
76 >  RooDataSet* OFSample;
77 >  
78 >  bool MarcoDebug=true;
79 >  
80 >  float FixedMEdge=-1;
81 >  float FixedMEdgeChi2=-1;
82 >  
83 >  bool RejectPointIfNoConvergence=false;
84 >  
85 >  string Mode="UndefinedMode";
86 >  
87 > }
88 >
89 > TLatex* WriteAppleStudel() {
90 >  string sel="Apple Strudel Preliminary";
91 >  TLatex *sele = new TLatex(0.97,0.135,sel.c_str());
92 >  sele->SetNDC(true);
93 >  sele->SetTextColor(TColor::GetColor("#848484"));
94 >  sele->SetTextFont(42);
95 >  sele->SetTextAlign(32);
96 >  sele->SetTextSize(0.03);
97 >  sele->SetTextAngle(270);
98 >  return sele;
99 > }
100 >
101 > TGraph* EdgeFitter::prepareLM(float mass, float nEv) {
102 >  float massLM[1];
103 >  massLM[0]=mass;
104 >  float accEffLM[1];
105 >  accEffLM[0]=nEv/PlottingSetup::luminosity;
106 >  TGraph *lm = new TGraph(1, massLM, accEffLM);
107 >  lm->GetXaxis()->SetNoExponent(true);
108 >  lm->GetXaxis()->SetTitle("m_{cut} [GeV]");
109 >  lm->GetYaxis()->SetTitle("#sigma #times A [pb] 95% CL UL");
110 >  lm->GetXaxis()->SetLimits(0.,300.);
111 >  lm->GetYaxis()->SetRangeUser(0.,0.08);
112 >  lm->SetMarkerStyle(34);
113 >  lm->SetMarkerColor(kRed);
114 >  return lm;
115 > }
116 >
117 > vector<RooDataSet> EdgeFitter::generateToys(RooWorkspace *ws, int nToys) {
118 >  ws->ls();
119 >  ws->var("nSig")->setVal(0.);
120 >  ws->var("nSig")->setConstant(true);
121 >  RooFitResult* fit = ws->pdf("combModel")->fitTo(*ws->data("data_obs"),RooFit::Save());
122 >  vector<RooDataSet> theToys;
123 >  
124 >  RooMCStudy mcEE(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"EE"));
125 >  mcEE.generate(nToys,44,true);
126 >  RooMCStudy mcMM(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"MM"));
127 >  mcMM.generate(nToys,44,true);
128 >  RooMCStudy mcOSOF(*ws->pdf("combModel"),RooArgSet(*ws->var("inv")),RooFit::Slice(*ws->cat("cat"),"OSOF"));
129 >  mcOSOF.generate(nToys,44,true);
130    
131 <  bool MarcoDebug;
131 >  RooRealVar mll("m_{ll}","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
132 >  RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
133 >  RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
134 >  RooRealVar jzb("jzb","jzb",-jzbmax,jzbmax,"GeV/c");
135 >  RooRealVar weight("weight","weight",0,1000,"");
136 >  RooArgSet observables(mll,jzb,id1,id2,weight);
137 >
138 >  for(int i=0;i<nToys;i++) {
139 >    RooDataSet* toyEE    = (RooDataSet*)mcEE.genData(i);
140 >    RooDataSet* toyMM    = (RooDataSet*)mcMM.genData(i);
141 >    RooDataSet* toyOSOF  = (RooDataSet*)mcOSOF.genData(i);
142 >    stringstream toyname;
143 >    toyname << "theToy_" << i;
144 >    write_warning(__FUNCTION__,"Problem while adding toys");
145 >    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));
146 >    theToys.push_back(toyData);
147 >  }
148 >  ws->var("nSig")->setVal(17.0);
149 >  ws->var("nSig")->setConstant(false);
150 >  return theToys;
151   }
152  
153 < float EdgeFitter::SetEdgeLimit(int is_data, RooWorkspace *ws) {
153 > void EdgeFitter::getMedianLimit(RooWorkspace *ws,vector<RooDataSet> theToys,float &median,float &sigmaDown, float &sigmaUp, float &twoSigmaDown, float &twoSigmaUp) {
154 >  TH1F *gauLimit = new TH1F("gausLimit","gausLimit",60,0.,80./PlottingSetup::luminosity);
155 >  vector<float> theLimits;
156 >  for(int itoy=0;itoy<(int)theToys.size();itoy++) {
157 >    float theLimit = calcExclusion(ws,theToys[itoy],false);
158 >    if(theLimit > 0 ) gauLimit->Fill(theLimit);
159 >  }
160 >  const Int_t nQ = 4;
161 >  Double_t yQ[nQ] = {0.,0.,0.,0.};
162 >  Double_t xQ[nQ] = {0.022750132,0.1586552539,0.84134474609999998,0.977249868};
163 >  gauLimit->GetQuantiles(nQ,yQ,xQ);
164 >  median = gauLimit->GetMean();
165 > //  median = median1(gauLimit);
166 >  twoSigmaDown = abs(yQ[0]-median);
167 >  sigmaDown = abs(yQ[1]-median);
168 >  sigmaUp = abs(yQ[2]-median);
169 >  twoSigmaUp = abs(yQ[3]-median);
170 >  dout << median * PlottingSetup::luminosity << " " << sigmaUp * PlottingSetup::luminosity << endl;
171 > }
172 >
173 > void EdgeFitter::prepareLimits(RooWorkspace *ws, bool ComputeBands) {
174 >  if(ComputeBands) {
175 >    vector<RooDataSet> theToys = EdgeFitter::generateToys(ws,50);
176 >    vector<float> medVals;
177 >    vector<float> medLimits;
178 >    vector<float> sigmaLimitsDown;
179 >    vector<float> sigmaLimitsUp;
180 >    vector<float> twoSigmaLimitsDown;
181 >    vector<float> twoSigmaLimitsUp;
182 >    for(int i=20;i<=320;i+=40) {
183 >      ws->var("nSig")->setVal(10.0);
184 >      medVals.push_back((float)i);
185 >      ws->var("m0")->setVal((float)i);
186 >      ws->var("m0")->setConstant(true);
187 >      float Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp;
188 >      EdgeFitter::getMedianLimit(ws,theToys,Smedian,SsigmaDown,SsigmaUp,StwoSigmaDown,StwoSigmaUp);
189 >      medLimits.push_back(Smedian);
190 >      sigmaLimitsDown.push_back(SsigmaDown);
191 >      sigmaLimitsUp.push_back(SsigmaUp);
192 >      twoSigmaLimitsDown.push_back(StwoSigmaDown);
193 >      twoSigmaLimitsUp.push_back(StwoSigmaUp);
194 >    }
195 >    write_warning(__FUNCTION__,"Still need to store limits");
196 >  } else {
197 >    vector<float> theVals;
198 >    vector<float> theLimits;
199 >    for(int i=20;i<300;i+=5) {
200 >      ws->var("nSig")->setVal(0.0);
201 >      theVals.push_back((float)i);
202 >      ws->var("m0")->setVal((float)i);
203 >      ws->var("m0")->setConstant(true);
204 > //      theLimits.push_back(calcExclusion(ws,(RooDataSet)*ws->data("data_obs"),true));
205 >      write_error(__FUNCTION__,"Error while casting roo data set");
206 >    }
207 >    
208 >    for(int i=0;i<(int)theLimits.size();i++) {
209 >      if((theLimits[i]<2.0/PlottingSetup::luminosity)||(theLimits[i]>40.0/PlottingSetup::luminosity)) {
210 >        dout << i << " : " << theVals[i] << endl;
211 >        theLimits[i] = (theLimits[i+2]+theLimits[i-2])/2.0;
212 >        write_warning(__FUNCTION__,"Need to store limits");
213 >      }
214 >    write_warning(__FUNCTION__,"Need to store limits");
215 >    }
216 > }
217 > }
218 >  
219 >
220 > float EdgeFitter::calcExclusion(RooWorkspace *ws, RooDataSet data, bool LoadDataObs) {
221 >  int numberOfToys=50;
222    RooRealVar mu("mu","nSig",0,10000,"");
223    RooArgSet poi = RooArgSet(mu);
224    RooArgSet *nullParams = (RooArgSet*)poi.snapshot();
# Line 70 | Line 227 | float EdgeFitter::SetEdgeLimit(int is_da
227    model->SetWorkspace(*ws);
228    model->SetPdf("combModel");
229    model->SetParametersOfInterest(poi);
230 < RooAbsData* data = ws->data("data_obs");
230 > //  if(LoadDataObs) data = (RooDataSet)*ws->data("data_obs");
231  
232 <  RooStats::ProfileLikelihoodCalculator plc(*data, *model);
232 >  RooStats::ProfileLikelihoodCalculator plc(data, *model);
233    plc.SetNullParameters(*nullParams);
234    plc.SetTestSize(0.05);
235 +  
236    RooStats::LikelihoodInterval* interval = plc.GetInterval();
237    RooStats::HypoTestResult *htr = plc.GetHypoTest();
238    double theLimit = interval->UpperLimit( mu );
239 <  cout << "Significance " << htr->Significance() << endl;
239 > //  double significance = htr->Significance();
240    
241    ws->defineSet("obs","nB");
242    ws->defineSet("poi","nSig");
# Line 102 | Line 260 | RooAbsData* data = ws->data("data_obs");
260    slrts.SetAltParameters(*sb_model.GetSnapshot());
261    RooStats::ProfileLikelihoodTestStat profll = RooStats::ProfileLikelihoodTestStat(*b_model.GetPdf());
262    
263 <  RooStats::HybridCalculatorOriginal hc = RooStats::HybridCalculatorOriginal(*data, sb_model, b_model,0,0);
263 >  RooStats::HybridCalculatorOriginal hc = RooStats::HybridCalculatorOriginal(data, sb_model, b_model,0,0);
264    hc.SetTestStatistic(2);
265 <  hc.SetNumberOfToys(50);
108 <  
265 >  hc.SetNumberOfToys(numberOfToys);
266    
267    RooStats::HypoTestInverterOriginal hcInv =  RooStats::HypoTestInverterOriginal(hc,*ws->var("nSig"));
268    hcInv.SetTestSize(0.05);
# Line 114 | Line 271 | RooAbsData* data = ws->data("data_obs");
271    RooStats::HypoTestInverterResult* hcInt = hcInv.GetInterval();
272    float ulError = hcInt->UpperLimitEstimatedError();
273    theLimit = hcInt->UpperLimit();
274 <  cout << "Found upper limit : " << theLimit << "+/-" << ulError << endl;
274 >  dout << "Found upper limit : " << theLimit << "+/-" << ulError << endl;
275    
276    return theLimit/PlottingSetup::luminosity;
277    
278   }
279  
280   TTree* SkimTree(int isample) {
281 <  TTree* newTree = allsamples.collection[isample].events->CloneTree(0);
281 >  string TreeName = allsamples.collection[isample].filename;
282 >  TTree* newTree = new TTree("NanoTree",TreeName.c_str());
283 >  
284 >  float mll,edgeWeight;
285 >  int id1,id2;
286 >  
287 >  newTree->Branch("edgeWeight",&edgeWeight,"edgeWeight/F");
288 >  newTree->Branch("mll",&mll,"mll/F");
289 >  newTree->Branch("id1",&id1,"id1/I");
290 >  newTree->Branch("id2",&id2,"id2/I");
291 >  
292    float xsweight=1.0;
293    if(allsamples.collection[isample].is_data==false) xsweight=luminosity*allsamples.collection[isample].weight;
294    if(EdgeFitter::MarcoDebug) {
295 <    cout << "   Original tree contains " << allsamples.collection[isample].events->GetEntries() << endl;
296 <    cout << "   Going to reduce it with cut " << EdgeFitter::cut << endl;
295 >    dout << "   Original tree contains " << allsamples.collection[isample].events->GetEntries() << endl;
296 >    dout << "   Going to reduce it with cut " << EdgeFitter::cut << endl;
297    }
298 +  
299 +  float tmll;
300 +  int tid1,tid2;
301 +  allsamples.collection[isample].events->SetBranchAddress("mll",&tmll);
302 +  allsamples.collection[isample].events->SetBranchAddress("id1",&tid1);
303 +  allsamples.collection[isample].events->SetBranchAddress("id2",&tid2);
304 +  
305    TTreeFormula *select = new TTreeFormula("select", EdgeFitter::cut, allsamples.collection[isample].events);
306 +  TTreeFormula *Weight = new TTreeFormula("Weight", cutWeight, allsamples.collection[isample].events);
307 +  
308    float wgt=1.0;
133  allsamples.collection[isample].events->SetBranchAddress(cutWeight,&wgt);
309    for (Int_t entry = 0 ; entry < allsamples.collection[isample].events->GetEntries() ; entry++) {
310     allsamples.collection[isample].events->LoadTree(entry);
311     if (select->EvalInstance()) {
312       allsamples.collection[isample].events->GetEntry(entry);
313 <     wgt=wgt*xsweight;
313 >     mll=tmll;
314 >     id1=tid1;
315 >     id2=tid2;
316 >     wgt=Weight->EvalInstance();
317 >     edgeWeight=wgt*xsweight;
318       newTree->Fill();
319     }
320    }
321    
322 <  if(EdgeFitter::MarcoDebug) cout << "     Reduced tree contains " << newTree->GetEntries() << " entries " << endl;
322 >  if(EdgeFitter::MarcoDebug) dout << "     Reduced tree contains " << newTree->GetEntries() << " entries " << endl;
323    return newTree;
324   }
325  
# Line 150 | Line 329 | void EdgeFitter::InitializeVariables(flo
329    jzbmax=_jzbmax;
330    cut=_cut;
331   }
332 <  
332 >
333 > TTree* MergeTrees(vector<TTree*> trees) {
334 >  TTree * newtree = (TTree*)trees[0]->CloneTree(0);
335 >  trees[0]->GetListOfClones()->Remove(newtree);
336 >  trees[0]->ResetBranchAddresses();
337 >  newtree->ResetBranchAddresses();
338 >  
339 >  for(int itree=0;itree<trees.size();itree++) {
340 >    newtree->CopyAddresses(trees[itree]);
341 >    Long64_t nentries = trees[itree]->GetEntries();
342 >    for (Long64_t iEntry=0;iEntry<nentries;iEntry++) {
343 >      trees[itree]->GetEntry(iEntry);
344 >      newtree->Fill();
345 >    }
346 >    trees[itree]->ResetBranchAddresses(); // Disconnect from new tree
347 >    if (newtree->GetTreeIndex()) {
348 >      newtree->GetTreeIndex()->Append(trees[itree]->GetTreeIndex(),kTRUE);
349 >    }
350 >    if (newtree && newtree->GetTreeIndex()) {
351 >      newtree->GetTreeIndex()->Append(0,kFALSE); // Force the sorting
352 >    }
353 >  }
354 >  return newtree;
355 > }
356 >    
357 >      
358 >
359   void EdgeFitter::PrepareDatasets(int is_data) {
360 <  TTree *completetree;
361 <  bool hashit=0;
362 <  for(int isample=0;isample<allsamples.collection.size();isample++) {
360 >  write_warning(__FUNCTION__,"Need to make this function ready for scans as well (use signal from scan samples)");
361 >  TFile *tempout = new TFile("tempout.root","RECREATE");
362 >  vector<TTree*> SkimmedTrees;
363 >  TTree *SkimmedTree[(int)allsamples.collection.size()];
364 >  for(int isample=0;isample<(int)allsamples.collection.size();isample++) {
365      if(!allsamples.collection[isample].is_active) continue;
366      if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
367      if(is_data==1&&allsamples.collection[isample].is_data==false) continue;//kick all samples that aren't data if we're looking for data.
368      if(is_data!=1&&allsamples.collection[isample].is_data==true) continue;//kick all data samples when looking for MC
369      if(is_data!=2&&allsamples.collection[isample].is_signal==true) continue;//remove signal sample if we don't want it.
370 <    if(EdgeFitter::MarcoDebug) cout << "Considering : " << allsamples.collection[isample].samplename << endl;
371 <    if(!hashit) {
372 <      hashit=true;
373 <      completetree = SkimTree(isample)->CloneTree();
374 <    } else {
168 <      completetree->CopyEntries(SkimTree(isample));
169 <    }
170 <    if(EdgeFitter::MarcoDebug) cout << "Complete tree now contains " << completetree->GetEntries() << " entries " << endl;
370 >    if(EdgeFitter::MarcoDebug) dout << "Considering : " << allsamples.collection[isample].samplename << endl;
371 >    SkimmedTree[isample] = SkimTree(isample);
372 >    tempout->cd();
373 >    SkimmedTree[isample]->Write();
374 >    SkimmedTrees.push_back(SkimmedTree[isample]);
375    }
376    
377 <  RooRealVar mll("mll","mll",mllmin,mllmax,"GeV/c^{2}");
377 >  TTree *completetree  = MergeTrees(SkimmedTrees);
378 >  
379 >  if(EdgeFitter::MarcoDebug) dout << "Complete tree now contains " << completetree->GetEntries() << " entries " << endl;
380 >  
381 >  RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
382    RooRealVar id1("id1","id1",0,1,"GeV/c^{2}");
383    RooRealVar id2("id2","id2",0,1,"GeV/c^{2}");
384 <  RooRealVar jzb("jzb","jzb",-jzbmax,jzbmax,"GeV/c");
385 <  RooRealVar weight("weight","weight",0,1000,"");
178 <  RooArgSet observables(mll,jzb,id1,id2,weight);
384 >  RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
385 >  RooArgSet observables(mll,id1,id2,edgeWeight);
386    
387    string title="CMS Data";
388    if(is_data!=1) title="CMS SIMULATION";
389 <  RooDataSet LAllData("LAllData",title.c_str(),completetree,observables,"","weight");
389 >  RooDataSet LAllData("LAllData",title.c_str(),completetree,observables,"","edgeWeight");
390 >  tempout->cd();
391    completetree->Write();
392 < //  delete completetree;
392 >  tempout->Close();
393 >
394    
395 <  EdgeFitter::eeSample = (RooDataSet*)LAllData.reduce("id1==id2&&id1==0");
396 <  EdgeFitter::mmSample = (RooDataSet*)LAllData.reduce("id1==id2&&id1==1");
188 <  EdgeFitter::emSample = (RooDataSet*)LAllData.reduce("id1!=id2");
395 >  EdgeFitter::SFSample = (RooDataSet*)LAllData.reduce("id1==id2");
396 >  EdgeFitter::OFSample = (RooDataSet*)LAllData.reduce("id1!=id2");
397    EdgeFitter::AllData  = (RooDataSet*)LAllData.reduce("id1!=id2||id1==id2");
398    
399 <  eeSample->SetName("eeSample");
400 <  mmSample->SetName("mmSample");
193 <  emSample->SetName("emSample");
399 >  SFSample->SetName("SFSample");
400 >  OFSample->SetName("OFSample");
401    AllData->SetName("AllData");
402    
403    if(EdgeFitter::MarcoDebug) {
404 <    cout << "Number of events in data sample = " << AllData->numEntries() << endl;
405 <    cout << "Number of events in ee sample = " << eeSample->numEntries() << endl;
406 <    cout << "Number of events in mm sample = " << mmSample->numEntries() << endl;
200 <    cout << "Number of events in em sample = " << emSample->numEntries() << endl;
404 >    dout << "Number of events in data sample = " << AllData->sumEntries() << endl;
405 >    dout << "Number of events in eemm sample = " << SFSample->sumEntries() << endl;
406 >    dout << "Number of events in em sample = " << OFSample->sumEntries() << endl;
407    }
408 +  
409 + }
410 +
411 + void EdgeFitter::DrawDatasetContent(int is_data) {
412 +  RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
413 +  RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("SF sample")) ;
414 +  frame1->GetXaxis()->CenterTitle(1);
415 +  frame1->GetYaxis()->CenterTitle(1);
416 +  SFSample->plotOn(frame1,RooFit::Name("SFdata")) ;
417 +  
418 +  RooPlot* frame2 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
419 +  frame2->GetXaxis()->CenterTitle(1);
420 +  frame2->GetYaxis()->CenterTitle(1);
421 +  OFSample->plotOn(frame2,RooFit::Name("OFdata")) ;
422 +  
423 +  TCanvas* cSFdata = new TCanvas("cSFdata","cSFdata") ;
424 +  cSFdata->cd() ;
425 +  gPad->SetLeftMargin(0.15);
426 +  frame1->GetYaxis()->SetTitleOffset(1.4);
427 +  frame1->Draw();
428 +  if(is_data==data) DrawPrelim();
429 +  else DrawPrelim(PlottingSetup::luminosity,true);
430 +  CompleteSave(cSFdata,"Edge/SF_NoFit");
431 +  
432 +  TCanvas* cOFdata = new TCanvas("cOFdata","cOFdata") ;
433 +  cOFdata->cd() ;
434 +  gPad->SetLeftMargin(0.15);
435 +  frame2->SetMaximum(frame1->GetMaximum());
436 +  frame2->GetYaxis()->SetTitleOffset(1.4);
437 +  frame2->Draw();
438 +  if(is_data==data) DrawPrelim();
439 +  else DrawPrelim(PlottingSetup::luminosity,true);
440 +  CompleteSave(cOFdata,"Edge/OF_NoFit");
441 + }
442 +  
443 + string WriteWithError(float central, float error, int digits) {
444 +  float ref=central;
445 +  if(ref<0) ref=-central;
446 +  int HighestSigDigit = 0;
447 +  if(ref>1) HighestSigDigit = int(log(ref)/log(10))+1;
448 +  else HighestSigDigit = int(log(ref)/(log(10)));
449 +  
450 +  float divider=pow(10.0,(double(HighestSigDigit-digits)));
451 +  
452 +  stringstream result;
453 +  result << divider*int(central/divider+0.5) << " #pm " << divider*int(error/divider+0.5);
454 +  return result.str();
455   }
456  
457 +
458   string EdgeFitter::RandomStorageFile() {
459    TRandom3 *r = new TRandom3(0);
460    int rho = (int)r->Uniform(1,10000000);
# Line 213 | Line 467 | string EdgeFitter::RandomStorageFile() {
467   Yield EdgeFitter::Get_Z_estimate(float jzb_cut, int icut) {
468    if(MarcoDebug) write_error(__FUNCTION__,"Returning random Z yield");
469    Yield a(123,45,67); return a;
216  
470    return PlottingSetup::allresults.predictions[icut].Zbkg;
471   }
472  
# Line 223 | Line 476 | Yield EdgeFitter::Get_T_estimate(float j
476    return PlottingSetup::allresults.predictions[icut].Flavorsym;
477   }
478  
479 + void EdgeFitter::DoFit(int is_data, float jzb_cut) {
480 +  
481 +  stringstream prefix;
482 +  if(is_data==data) prefix << "data_";
483 +  if(is_data==mc) prefix << "mc_";
484 +  if(is_data==mcwithsignal) prefix << "mcwithS_";
485 +  
486 +  prefix << EdgeFitter::Mode << "_" << jzb_cut;
487 +  
488 +
489 +  
490 +  RooRealVar mll("mll","m_{ll}",mllmin,mllmax,"GeV/c^{2}");
491 +  RooRealVar edgeWeight("edgeWeight","edgeWeight",0,1000,"");
492 +  RooCategory sample("sample","sample") ;
493 +  sample.defineType("SF");
494 +  //sample.defineType("mm");
495 +  sample.defineType("OF");
496 +  
497 +  //RooDataSet combData("combData","combined data",mll,Index(sample),Import("SF",*SFSample),Import("mm",*mmSample),Import("OF",*OFSample));
498 +  RooDataSet combData("combData","combined data",RooArgSet(mll,edgeWeight),RooFit::Index(sample),RooFit::Import("SF",*SFSample),RooFit::Import("OF",*OFSample),RooFit::WeightVar(edgeWeight));
499 +  
500 +  
501 +  //First we make a fit to opposite flavor
502 +  RooRealVar fttbarOF("fttbarOF", "fttbarOF", 100, 0, 10000);
503 +  RooRealVar par1ttbarOF("par1ttbarOF", "par1ttbarOF", 1.6, 0.01, 4.0);
504 +  RooRealVar par2ttbarOF("par2ttbarOF", "par2ttbarOF", 1.0);
505 +  RooRealVar par3ttbarOF("par3ttbarOF", "par3ttbarOF", 0.028, 0.001, 1.0);
506 +  RooRealVar par4ttbarOF("par4ttbarOF", "par4ttbarOF", 2.0);
507 +  RooSUSYBkgPdf ttbarOF("ttbarOF","ttbarOF", mll , par1ttbarOF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
508 +  RooAddPdf model_OF("model_OF","model_OF", ttbarOF, fttbarOF);
509 +  RooSimultaneous simPdfOF("simPdfOF","simultaneous pdf", sample) ;
510 +  simPdfOF.addPdf(model_OF,"OF");
511 +  RooFitResult *resultOF = simPdfOF.fitTo(combData, RooFit::Save(),RooFit::Extended(),RooFit::Minos(true));
512 +  dout << "============================= < OF results > =============================" << endl;
513 +  resultOF->Print();
514 +  dout << "============================= < /OF results > =============================" << endl;
515 +  
516 +  
517 +  RooPlot* frameO = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample"));
518 +  frameO->GetXaxis()->CenterTitle(1);
519 +  frameO->GetYaxis()->CenterTitle(1);
520 +  combData.plotOn(frameO,RooFit::Name("OFdata"),RooFit::Cut("sample==sample::OF")) ;
521 +  simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("FullFit"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
522 +  simPdfOF.plotOn(frameO,RooFit::Slice(sample,"OF"),RooFit::Name("TTbarOFonly"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
523 +  
524 +  TCanvas* pof = new TCanvas("pof","pof") ;
525 +  pof->cd() ;
526 +  gPad->SetLeftMargin(0.15);
527 +  frameO->GetYaxis()->SetTitleOffset(1.4);
528 +  frameO->Draw();
529 +  if(is_data==data) DrawPrelim();
530 +  else DrawPrelim(PlottingSetup::luminosity,true);
531 +  if(EdgeFitter::FixedMEdge>=0) CompleteSave(pof,"Edge/OF__OFFitonly_"+prefix.str()+"__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
532 +  else CompleteSave(pof,"Edge/OF__OFFitonly_"+prefix.str(),false,false);
533 +  delete pof;
534 +    
535 +  if(resultOF->covQual()!=3) {
536 +    write_error(__FUNCTION__,"OF fit did not converge!!! Cannot continue!");
537 +    dout << "covQual is " << resultOF->covQual() << endl;
538 +    EdgeFitter::FixedMEdgeChi2=-1;
539 +    if(EdgeFitter::RejectPointIfNoConvergence) return;
540 +  } else {
541 +    write_info(__FUNCTION__,"OF fit converged");
542 +  }
543 +  
544 +  float StartingMedge=70;
545 +  if(EdgeFitter::FixedMEdge>0) StartingMedge=EdgeFitter::FixedMEdge;
546 +
547 +
548 +  RooDataSet *ZDataSet = (RooDataSet*)EdgeFitter::AllData->reduce("id1==id2 && abs(mll-91.2)<20");
549 +
550 +  float maxZ = ZDataSet->sumEntries();
551 +  dout << "maxZ was set to " << maxZ << endl;
552 +  delete ZDataSet;
553 +  
554 +  
555 +  // Now same flavor  
556 +  RooRealVar fzSF("fzSF", "fzSF", 39, 39, maxZ);
557 +  RooRealVar meanzSF("meanzSF", "meanzSF", 91.1876, 89, 95);
558 +  //RooRealVar sigmazSF("sigmazSF", "sigmazSF", 0.5);
559 +  RooRealVar sigmazSF("sigmazSF", "sigmazSF", 5, 0.5, 5);
560 +  RooRealVar widthzSF("widthzSF", "widthzSF", 2.94);
561 +  widthzSF.setConstant();
562 +  
563 +  RooRealVar fttbarSF("fttbarSF", "fttbarSF", 100, 0, 100000);
564 +  RooRealVar par1ttbarSF("par1ttbarSF", "par1ttbarSF", 1.02*par1ttbarOF.getVal(), (1.02-0.07)*par1ttbarOF.getVal(), (1.02+0.07)*par1ttbarOF.getVal());
565 +
566 +  RooRealVar fsignalSF("fsignalSF", "fsignalSF", 0, 0, 300);
567 +  RooRealVar par1signalSF("par1signalSF", "par1signalSF", 45, 20, 100);
568 +  RooRealVar par2signalSF("par2signalSF", "par2signalSF", 2, 1, 10);
569 +  RooRealVar par3signalSF("par3signalSF", "par3signalSF", StartingMedge, 0, 300);
570 +
571 +  RooVoigtian zSF("zSF", "zSF", mll, meanzSF, widthzSF, sigmazSF);
572 +
573 +  if(EdgeFitter::FixedMEdge>0) par3signalSF.setConstant();
574 +  
575 +  RooSUSYBkgPdf ttbarSF("ttbarSF","ttbarSF", mll , par1ttbarSF, par2ttbarOF, par3ttbarOF, par4ttbarOF);
576 +  RooSUSYTPdf signalSF("signalSF","signalSF", mll , par1signalSF, sigmazSF, par3signalSF);
577 +  
578 +  RooAddPdf model_SF("model_SF","model_SF", RooArgList(zSF, ttbarSF, signalSF), RooArgList(fzSF, fttbarSF, fsignalSF));
579 +  
580 +  RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
581 +  simPdf.addPdf(model_SF,"SF") ;
582 +  simPdf.addPdf(model_OF,"OF") ;
583 +
584 +  RooFitResult *result = simPdf.fitTo(combData, RooFit::Save(), RooFit::Extended(),RooFit::Minos(true));
585 +  
586 +  if(result->covQual()!=3) {
587 +    write_error(__FUNCTION__,"Full fit did not converge!!! Cannot continue!");
588 +    dout << "covQual is " << result->covQual() << endl;
589 +    EdgeFitter::FixedMEdgeChi2=-1;
590 +    if(EdgeFitter::RejectPointIfNoConvergence) return;
591 +  } else {
592 +    write_info(__FUNCTION__,"Full fit converged");
593 +  }
594 +
595 +  dout << "============================= < OF results > =============================" << endl;
596 +  result->Print();
597 +  dout << "============================= < /OF results > =============================" << endl;
598 +
599 +  
600 +  RooPlot* frame1 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("EE sample")) ;
601 +  frame1->GetXaxis()->CenterTitle(1);
602 +  frame1->GetYaxis()->CenterTitle(1);
603 +  combData.plotOn(frame1,RooFit::Name("SFdata"),RooFit::Cut("sample==sample::SF")) ;
604 +  simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("FullFit"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
605 +  simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("TTbarSFonly"),RooFit::Components("ttbarSF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
606 +  simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("DYSFonly"),RooFit::Components("zSF"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kRed));
607 +  simPdf.plotOn(frame1,RooFit::Slice(sample,"SF"),RooFit::Name("SignalSFonly"),RooFit::Components("signalSF"), RooFit::ProjWData(sample, combData), RooFit::LineStyle(kDashed), RooFit::LineColor(kGreen));
608 +  
609 +  EdgeFitter::FixedMEdgeChi2 = frame1->chiSquare("FullFit", "SFdata", 3);
610 +  
611 +  
612 +  dout << "Result   : " << endl;
613 +  dout << "f signal : " << fsignalSF.getVal() << " +/- " << fsignalSF.getError() << endl;
614 +  dout << "f ttbar  : " << fttbarSF.getVal() << " +/- " << fttbarSF.getError() << endl;
615 +  dout << "f tt OF  : " << fttbarOF.getVal() << " +/- " << fttbarOF.getError() << endl;
616 +  dout << "f z SF   : " << fzSF.getVal() << " +/- " << fzSF.getError() << endl;
617 +  dout << "#Chi^{2}/NDF     : " << EdgeFitter::FixedMEdgeChi2 << endl;
618 +  
619 +  // The same plot for the cointrol sample slice
620 +  RooPlot* frame3 = mll.frame(RooFit::Bins(int((mllmax-mllmin)/5.0)),RooFit::Title("OF sample")) ;
621 +  frame3->GetXaxis()->CenterTitle(1);
622 +  frame3->GetYaxis()->CenterTitle(1);
623 +  frame3->SetMaximum(frame1->GetMaximum());
624 +  combData.plotOn(frame3,RooFit::Cut("sample==sample::OF")) ;
625 +  simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::ProjWData(sample,combData), RooFit::LineColor(kBlack)) ;
626 +  simPdf.plotOn(frame3,RooFit::Slice(sample,"OF"),RooFit::Components("ttbarOF"),RooFit::ProjWData(sample,combData),RooFit::LineStyle(kDashed)) ;
627 +  
628 +  
629 +  TCanvas* c = new TCanvas("rf501_simultaneouspdf","rf403_simultaneouspdf") ;
630 +  c->cd() ;
631 +  gPad->SetLeftMargin(0.15);
632 +  frame1->GetYaxis()->SetTitleOffset(1.4);
633 +  frame1->Draw();
634 +  if(is_data==data) DrawPrelim();
635 +  else DrawPrelim(PlottingSetup::luminosity,true);
636 +  stringstream infotext;
637 +  infotext << "#splitline{Fit results (" << EdgeFitter::Mode << ">" << jzb_cut << "): }{#splitline{";
638 +  infotext << "N(Data) = " << EdgeFitter::SFSample->sumEntries() << "}{#splitline{";
639 +  infotext << "N(Z+Jets) = " << WriteWithError(fzSF.getVal(),fzSF.getError(),3) << "}{#splitline{";
640 +  infotext << "N(t#bar{t}) = " << WriteWithError(fttbarSF.getVal(),fttbarSF.getError(),3) << "}{#splitline{";
641 +  infotext << "N(signal) = " << WriteWithError(fsignalSF.getVal(),fsignalSF.getError(),3) << "}{";
642 +  infotext << "m_{edge} = " << WriteWithError(par3signalSF.getVal(),par3signalSF.getError(),3) << "}}}}}";  
643 +
644 +  TLatex *infobox = new TLatex(0.57,0.75,infotext.str().c_str());
645 +  infobox->SetNDC();
646 +  infobox->SetTextSize(0.03);
647 +  infobox->Draw();
648 +  if(EdgeFitter::FixedMEdge>=0) CompleteSave(c,"Edge/"+prefix.str()+"_SF__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
649 +  else CompleteSave(c,"Edge/"+prefix.str()+"_SF",false,false);
650 +  delete c;
651 +  
652 +  TCanvas* e = new TCanvas("rf501_simultaneouspdfem","rf403_simultaneouspdfem") ;
653 +  e->cd();
654 +  gPad->SetLeftMargin(0.15);
655 +  frame3->GetYaxis()->SetTitleOffset(1.4);
656 +  frame3->Draw();
657 +  if(is_data==data) DrawPrelim();
658 +  else DrawPrelim(PlottingSetup::luminosity,true);
659 +  if(EdgeFitter::FixedMEdge>=0) CompleteSave(e,"Edge/"+prefix.str()+"_OF__MEdgeFix_"+any2string(EdgeFitter::FixedMEdge),false,false);
660 +  else CompleteSave(e,"Edge/"+prefix.str()+"_OF",false,false);
661 +  delete e;
662 +  
663 +  
664 +  
665 +  
666 + /*  TCanvas* f = new TCanvas("rf501_simultaneouspdfem","rf403_simultaneouspdfem") ;
667 +  f->cd();
668 +  gPad->SetLeftMargin(0.15);
669 +  frame4->GetYaxis()->SetTitleOffset(1.4);
670 +  frame4->Draw();
671 +  if(is_data==data) DrawPrelim();
672 +  else DrawPrelim(PlottingSetup::luminosity,true);
673 +  CompleteSave(f,"Edge/"+prefix.str()+"_SF");
674 +  delete f;*/
675 +
676 +
677 + /*
678 +  float maxZ=200;
679 +  RooWorkspace* wspace = new RooWorkspace();
680 +  stringstream mllvar;
681 +  mllvar << "mll[" << (mllmax-mllmin)/2 << "," << mllmin << "," << mllmax << "]";
682 +  wspace->factory(mllvar.str().c_str());
683 +  wspace->var("mll")->setBins(30);
684 +  wspace->factory("nSig[1.,0.,100.]");
685 +  wspace->factory(("nZ[0.04.,0.,"+any2string(maxZ)+"]").c_str());
686 +  wspace->factory("rME[1.12,1.05,1.19]");
687 +  wspace->factory("effUncert[1.]");
688 +  EdgeFitter::prepareLimits(wspace, true);
689 + */
690 +
691 + write_warning(__FUNCTION__," A lot missing here to calculate limits");
692 +
693 + }
694 +
695   void EdgeFitter::DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, float jzb_cut, int icut, int is_data, TCut cut, TTree *signalevents=0) {
696    
697 <  string storagefile=EdgeFitter::RandomStorageFile();
698 <  TFile *f = new TFile(storagefile.c_str(),"RECREATE");
699 <  EdgeFitter::InitializeVariables(iMllLow,iMllHigh,jzbHigh,cut);
700 <  
701 <  Yield Zestimate=EdgeFitter::Get_Z_estimate(jzb_cut,icut);
233 <  Yield Testimate=EdgeFitter::Get_T_estimate(jzb_cut,icut);
234 <  cout << "Cut at JZB>" << jzb_cut << "; using estimates: " << endl;
235 <  cout << "       Z : " << Zestimate  << endl;
236 <  cout << "       T : " << Testimate  << endl;
697 >  TCut _cut(cut&&PlottingSetup::basiccut&&PlottingSetup::passtrig);
698 >  
699 >  TFile *f = new TFile("workingfile.root","RECREATE");
700 >
701 >  EdgeFitter::InitializeVariables(PlottingSetup::iMllLow,PlottingSetup::iMllHigh,PlottingSetup::jzbHigh,_cut);
702    
703    EdgeFitter::PrepareDatasets(is_data);
704 +  
705 +  EdgeFitter::DrawDatasetContent(is_data);
706 +  
707 +  RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
708 +  RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
709 +  
710 +  
711 +  bool ScanMassRange=false;
712 +  float ScanSteps=5.0;//GeV
713 +  
714 +  
715 +  
716 +  if(ScanMassRange) {
717 +    TFile *fscan = new TFile("fscan.root","UPDATE");
718 +    TGraph *gr = new TGraph();
719 +    stringstream GrName;
720 +    GrName << "ScanGraphFor_" << EdgeFitter::Mode << "_" << jzb_cut;
721 +    gr->SetName(GrName.str().c_str());
722  
723 <  EdgeFitter::eeSample->Write();
724 <  EdgeFitter::emSample->Write();
725 <  EdgeFitter::mmSample->Write();
726 <  EdgeFitter::AllData->Write();
727 <  f->Close();
723 >    int i=0;
724 >    for(float tempMedge=10;tempMedge<=300;tempMedge+=ScanSteps) {
725 >      write_info(__FUNCTION__,"Now testing Medge="+any2string(tempMedge)+" for "+EdgeFitter::Mode+">"+any2string(jzb_cut));
726 >      EdgeFitter::FixedMEdge=tempMedge;
727 >      EdgeFitter::DoFit(is_data, jzb_cut);
728 >      if(EdgeFitter::FixedMEdgeChi2<0) continue;
729 >      gr->SetPoint(i,tempMedge,EdgeFitter::FixedMEdgeChi2);
730 >      i++;
731 >    }
732 >    
733 >    TCanvas *ScanCan = new TCanvas("ScanCan","ScanCan",500,500);
734 >    gr->GetXaxis()->SetTitle("m_{edge}");
735 >    gr->GetXaxis()->CenterTitle();
736 >    gr->GetYaxis()->SetTitle("#Chi^{2} / NDF");
737 >    gr->GetYaxis()->CenterTitle();
738 >    gr->GetYaxis()->SetTitleOffset(0.95);
739 >    gr->GetXaxis()->SetTitleOffset(0.9);
740 >    gr->SetLineColor(kBlue);
741 >    gr->SetTitle("");
742 >    gr->Draw("AL");
743 >    stringstream ScanCanSave;
744 >    ScanCanSave << "Edge/MEdgeScan_"+EdgeFitter::Mode+"_" << jzb_cut;
745 >    if(is_data) DrawPrelim();
746 >    else DrawMCPrelim();
747 >    CompleteSave(ScanCan,ScanCanSave.str());
748 >    fscan->cd();
749 >    gr->Write();
750 >    delete ScanCan;
751 >    fscan->Close();
752 >  } else {
753 >    EdgeFitter::DoFit(is_data, jzb_cut);
754 >  }
755 >  
756 >      
757 >  
758 >  
759 >  EdgeFitter::DoFit(is_data, jzb_cut);
760 >  RooMsgService::instance().setGlobalKillBelow(msglevel);
761 >
762  
763 <  write_warning(__FUNCTION__,"Work continues here");
763 >  f->Close();
764  
248  if(EdgeFitter::MarcoDebug) write_warning(__FUNCTION__,"Need to uncomment the next line (remove the output file)");
249  //  gSystem->Exec(("rm "+storagefile).c_str());
765   }
766  
767   void DoEdgeFit(string mcjzb, string datajzb, float DataPeakError, float MCPeakError, vector<float> jzb_cut, int is_data, TCut cut, TTree *signalevents=0) {
768 <  for(int icut=0;icut<jzb_cut.size();icut++) {
768 >  
769 >  EdgeFitter::Mode="JZB";
770 >  if(mcjzb=="met[4]") EdgeFitter::Mode="MET";
771 >  
772 >  for(int icut=0;icut<(int)jzb_cut.size();icut++) {
773      stringstream addcut;
774      if(is_data==1) addcut << "(" << datajzb << ">" << jzb_cut[icut] << ")";
775      if(is_data!=1) addcut << "(" << mcjzb << ">" << jzb_cut[icut] << ")";
776      TCut jcut(addcut.str().c_str());
777      
778 +    
779      EdgeFitter::DoEdgeFit(mcjzb, datajzb, DataPeakError, MCPeakError, jzb_cut[icut], icut, is_data, jcut&&cut, signalevents);
780      
781    }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines