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.25 by buchmann, Fri Jun 21 07:31:08 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines