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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines