Changeset 22 in huonglan


Ignore:
Timestamp:
Sep 2, 2011, 3:07:15 PM (13 years ago)
Author:
huonglan
Message:

sPlots_NewRel improved

Location:
SPLOT/NEWREALEASE
Files:
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • SPLOT/NEWREALEASE/EFFICIENCY/EfficiencyDependOnCut.C

    r21 r22  
    1010#include <TLegend.h>
    1111#include <TROOT.h>
     12#include <TMinuit.h>
    1213
    1314#include <iostream>
     
    1516using namespace std;
    1617
     18//Set the cut values
     19double Cut1 = 0.7;
     20double Cut2 = 0.7;
     21double Cut3 = 48;
     22double Cut4 = 250;
     23double Cut5 = 620;
     24
     25//Define class Event
     26class EventC {
     27public:
     28  EventC() { m_w = 1; m_fs = 1; m_fb = 0; m_cat = 1; }
     29  EventC(double w0, double fs0, double fb0) { m_w = w0; m_fs = fs0; m_fb = fb0; }
     30  EventC(double w0, int cat0) { m_w = w0; m_cat = cat0; m_fs = 1; m_fb = 0; }
     31  double w() { return m_w; }
     32  double fs() { return m_fs; }
     33  double fb() { return m_fb; }
     34  int cat() { return m_cat; }
     35  void fs(double fs0) { m_fs = fs0; }
     36  void fb(double fb0) { m_fb = fb0; }
     37
     38private:
     39  double m_w, m_fs, m_fb;
     40  int m_cat;
     41};
     42
     43//global variables
     44std::vector<EventC> m_EventList;
     45double m_F1[3][31][6];
     46double m_F0[3][31][6];
     47double m_N1exp[3][31];
     48double m_N0exp[3][31];
     49
     50void fcnN0N1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) {
     51  double L = 0;
     52  for (unsigned int iev = 0; iev < m_EventList.size(); iev++) {
     53    EventC evt = m_EventList[iev];
     54    double smt = par[0]*evt.fb() + par[1]*evt.fs();
     55    if (smt > 0) L += evt.w()*log(smt);
     56  }
     57  f = par[0] + par[1] - L;
     58};
     59
     60vector<double> vFe(double cos1,double cos2,double dMLep, int i, int j)
     61{
     62  double f0e = 0;
     63  double f1e = 0;
     64 
     65  //cout << "cos1 = " << cos1 << " cos2 = " << cos2 << " dMLep = " << dMLep << endl;
     66 
     67  int it = -1;
     68  if (cos1<Cut1 && cos2<Cut2){
     69    if (dMLep<Cut3) it = 0;
     70    if (dMLep>Cut3) it = 1;
     71  }
     72  else if (cos1>Cut1 && cos2>Cut2){
     73    if (dMLep<Cut3) it = 2;
     74    if (dMLep>Cut3) it = 3;
     75  }
     76  else if ((cos1>Cut1 && cos2<Cut2) || (cos1<Cut1 && cos2>Cut2)){
     77    if (dMLep<Cut3) it = 4;
     78    if (dMLep>Cut3) it = 5;
     79  }
     80 
     81  f0e = m_F0[i][j][it];
     82  f1e = m_F1[i][j][it];
     83 
     84  //cout << "it = " << it << " f0e = " << f0e << " f1e = " << f1e << endl;
     85 
     86  vector<double> vfe;
     87  vfe.push_back(f0e);
     88  vfe.push_back(f1e);
     89  return vfe;
     90}
     91
     92//Make list of events in Data file
     93void MakeEventListf0f1(int i, int j){
     94  m_EventList.clear(); 
     95
     96  TFile *fD = TFile::Open("/data/atlas/huonglan/FROMLYON/DATA/resData.root");
     97  TTree *tD = (TTree*)fD->Get("T");
     98  int nD = tD->GetEntries();
     99  for (int iev = 0; iev < nD; iev++) {
     100    tD->GetEntry(iev);
     101   
     102    double mt    = tD->GetLeaf("mtophad")->GetValue();
     103    double HT    = tD->GetLeaf("HT")->GetValue();
     104    double x     = tD->GetLeaf("cos_tlepCMtpair")->GetValue();
     105    double y     = tD->GetLeaf("cosWqq")->GetValue();
     106    double dMLep = tD->GetLeaf("MinLep1")->GetValue();
     107
     108    int hasBJ = (int)tD->GetLeaf("hasBadJet")->GetValue();
     109    int RunN  = (int)tD->GetLeaf("RunNumber_ana")->GetValue();
     110   
     111    if (RunN >= 180614 && hasBJ==1) continue;
     112
     113    double cos1 = fabs(x);
     114    double cos2 = fabs(y);
     115   
     116    //sPlots total
     117    if (mt > Cut4 && HT > Cut5){
     118      vector<double> fsfb =  vFe(cos1,cos2,dMLep,i,j);
     119      double f0 = fsfb.at(0);
     120      double f1 = fsfb.at(1);
     121      //cout << "f0 = " << f0 << " f1 = " << f1 << endl;
     122      EventC eventC(1,f1,f0);
     123      m_EventList.push_back(eventC);
     124    }
     125  }
     126}
     127
     128//Fit the number N0 and N1 using the events from data files
     129std::pair<pair<double,double>,double> Fit(double N0_exp, double N1_exp, int i, int j){
     130  // Test
     131  MakeEventListf0f1(i,j);
     132  cout << "Number of events in the LH " << m_EventList.size() << endl;
     133
     134  int ifl = 0;
     135  double step = 0.01;
     136 
     137  double fitPar0 = N0_exp;
     138  double nmax0   = fitPar0 + 4*sqrt(N0_exp);
     139  double nmin0   = fitPar0 - 4*sqrt(N0_exp);
     140 
     141  double fitPar1 = N1_exp;
     142  double nmax1   = fitPar1 + 4*sqrt(N1_exp);
     143  double nmin1   =         - 4*sqrt(N1_exp);
     144 
     145  double n0best, err0best;
     146  double n1best, err1best;
     147 
     148  TMinuit* myMinuit = new TMinuit(2);
     149  //myMinuit->Command("SET PRI -1");
     150  myMinuit->Command("SET NOW");
     151  myMinuit->Command("SET ERR 0.5");
     152  myMinuit->Command("SET STR 2");   // try to improve mimimum search
     153  myMinuit->SetFCN(fcnN0N1);
     154  myMinuit->mnparm(0,"N0",fitPar0,step,nmin0,nmax0,ifl);
     155  myMinuit->mnparm(1,"N1",fitPar1,step,nmin1,nmax1,ifl);
     156  //myMinuit->FixParameter(0);
     157  myMinuit->Command("MIGRAD 500 1");
     158  myMinuit->GetParameter(0,n0best,err0best);
     159  myMinuit->GetParameter(1,n1best,err1best);
     160 
     161  std::cout << "Expected N0 = " << N0_exp << " fitted = " << n0best << " +- " << err0best << std::endl;
     162  std::cout << "Expected N1 = " << N1_exp << " fitted = " << n1best << " +- " << err1best << std::endl;
     163 
     164  double N0 = n0best;
     165  double N1 = n1best;
     166  double sig2 = err1best*err1best;
     167
     168  pair<double,double> N0N1 = make_pair<double,double> (N0,N1);
     169 
     170  return make_pair<pair<double,double>,double> (N0N1,sig2);
     171};
     172
     173
     174//##############################################################
     175//####################### MAIN PROGRAM #########################
     176//##############################################################
     177
    17178void EfficiencyDependOnCut(){
    18   TTree *t[3];
     179  TTree *t[4];
    19180
    20181  TFile *fB = TFile::Open("../MERGEFILES/TESTEFFICIENCY/res_AllBkg.root");
     
    24185  t[1] = (TTree*)fS->Get("T");
    25186
     187  TFile *fBS = TFile::Open("../MERGEFILES/TESTEFFICIENCY/res_FakeData.root");
     188  t[2] = (TTree*)fBS->Get("T");
     189
    26190  TFile *fD = TFile::Open("/exp/atlas/huonglan/DATA2011/resData.root");
    27   t[2] = (TTree*)fD->Get("T");
    28 
    29   double Cut1 = 0.7;
    30   double Cut2 = 0.7;
    31   double Cut3 = 48;
    32 
    33   double   e1[3][3][31];
    34   double   e2[3][3][31];   
    35   double   e3[3][3][31];   
    36   double   e4[3][3][31];   
    37   double   e5[3][3][31];   
    38   double   e6[3][3][31];   
    39   double iTot[3][3][31];
     191  t[3] = (TTree*)fD->Get("T");
     192
     193  double   e1[3][4][31];
     194  double   e2[3][4][31];   
     195  double   e3[3][4][31];   
     196  double   e4[3][4][31];   
     197  double   e5[3][4][31];   
     198  double   e6[3][4][31];   
     199  double iTot[3][4][31];
    40200
    41201  for (int k = 0; k < 3; k++){
    42     for (int i = 0; i < 3; i++){
     202    for (int i = 0; i < 4; i++){
    43203      for (int j = 0; j < 31; j++){
    44204        iTot[k][i][j] =0 ;
     
    56216  double mjjjErr[31]; for (int i = 0; i < 31; i++) { mjjjErr[i] = 0;}
    57217
    58   for (int i = 0; i < 3; i++){
     218  for (int i = 0; i < 4; i++){
    59219    int nevt = t[i]->GetEntries();
    60220    for (int iev = 0; iev < nevt; iev++) {
     
    64224      double wei = 1;
    65225      double cos1 = 1, cos2 = 1;
     226      int hfor = -1;
    66227
    67228      double mt   = t[i]->GetLeaf("mtophad")->GetValue();
    68229      double ht   = t[i]->GetLeaf("HT")->GetValue();
    69       if (i==0||i==1) {
     230      int hasBJ   = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     231      if (i==0||i==1||i==2) {
    70232        wei  = t[i]->GetLeaf("wei")->GetValue();
    71233        cos1 = fabs(t[i]->GetLeaf("cos1")->GetValue());
    72234        cos2 = fabs(t[i]->GetLeaf("cos2")->GetValue());
    73       } else if (i==2){
     235        hfor = (int)t[i]->GetLeaf("hfor")->GetValue();
     236      } else if (i==3){
    74237        cos1 = fabs(t[i]->GetLeaf("cos_tlepCMtpair")->GetValue());
    75238        cos2 = fabs(t[i]->GetLeaf("cosWqq")->GetValue());
    76239      }
    77240      double dM   = t[i]->GetLeaf("MinLep1")->GetValue();
     241
     242      if (hasBJ!=0 || hfor==4) continue;
    78243
    79244      int it = -1;
     
    110275  }
    111276
    112   double    eB[3][31][6],    eS[3][31][6],    eD[3][31][6];
    113   double eBErr[3][31][6], eSErr[3][31][6], eDErr[3][31][6];
     277  double    eB[3][31][6],    eS[3][31][6],    eBS[3][31][6],    eD[3][31][6];
     278  double eBErr[3][31][6], eSErr[3][31][6], eBSErr[3][31][6], eDErr[3][31][6];
    114279
    115280  for (int i = 0; i < 3; i++){
     
    136301      if (iTot[i][1][j]==0) { for (int k = 0; k < 6; k++) { eS[i][j][k] = 0; } }
    137302     
    138       eD[i][j][0] = e1[i][2][j]/iTot[i][2][j];
    139       eD[i][j][1] = e2[i][2][j]/iTot[i][2][j];
    140       eD[i][j][2] = e3[i][2][j]/iTot[i][2][j];
    141       eD[i][j][3] = e4[i][2][j]/iTot[i][2][j];
    142       eD[i][j][4] = e5[i][2][j]/iTot[i][2][j];
    143       eD[i][j][5] = e6[i][2][j]/iTot[i][2][j];
    144 
    145       if (iTot[i][2][j]==0) { for (int k = 0; k < 6; k++) { eD[i][j][k] = 0; } }
     303      eBS[i][j][0] = e1[i][2][j]/iTot[i][2][j];
     304      eBS[i][j][1] = e2[i][2][j]/iTot[i][2][j];
     305      eBS[i][j][2] = e3[i][2][j]/iTot[i][2][j];
     306      eBS[i][j][3] = e4[i][2][j]/iTot[i][2][j];
     307      eBS[i][j][4] = e5[i][2][j]/iTot[i][2][j];
     308      eBS[i][j][5] = e6[i][2][j]/iTot[i][2][j];
     309
     310      if (iTot[i][2][j]==0) { for (int k = 0; k < 6; k++) { eBS[i][j][k] = 0; } }
     311     
     312      eD[i][j][0] = e1[i][3][j]/iTot[i][3][j];
     313      eD[i][j][1] = e2[i][3][j]/iTot[i][3][j];
     314      eD[i][j][2] = e3[i][3][j]/iTot[i][3][j];
     315      eD[i][j][3] = e4[i][3][j]/iTot[i][3][j];
     316      eD[i][j][4] = e5[i][3][j]/iTot[i][3][j];
     317      eD[i][j][5] = e6[i][3][j]/iTot[i][3][j];
     318
     319      if (iTot[i][3][j]==0) { for (int k = 0; k < 6; k++) { eD[i][j][k] = 0; } }
    146320     
    147321      //cout << "e1S[" << i << "][" << j << "] = " << e1S[i][j];
     
    150324  }
    151325 
     326  //3 is the number of regions
     327  //31 is the number of the cut for mjjj
     328  //6 is the number of categories
    152329  for (int i = 0; i < 3; i++){
    153330    for (int j = 0; j < 31; j++){
    154331      for (int k = 0; k < 6; k++){
    155         eBErr[i][j][k] = sqrt(eB[i][j][k]*(1-eB[i][j][k])/iTot[i][0][j]);
    156         eSErr[i][j][k] = sqrt(eS[i][j][k]*(1-eS[i][j][k])/iTot[i][1][j]);
    157         eDErr[i][j][k] = sqrt(eD[i][j][k]*(1-eD[i][j][k])/iTot[i][2][j]);
    158       }
     332        eBErr[i][j][k]  = sqrt(eB[i][j][k]*(1-eB[i][j][k])/iTot[i][0][j]);
     333        eSErr[i][j][k]  = sqrt(eS[i][j][k]*(1-eS[i][j][k])/iTot[i][1][j]);
     334        eBSErr[i][j][k] = sqrt(eBS[i][j][k]*(1-eBS[i][j][k])/iTot[i][2][j]);
     335        eDErr[i][j][k]  = sqrt(eD[i][j][k]*(1-eD[i][j][k])/iTot[i][3][j]);
     336
     337        m_F1[i][j][k] = eS[i][j][k];
     338        m_F0[i][j][k] = eB[i][j][k];
     339        m_N1exp[i][j] = iTot[i][1][j];
     340        m_N0exp[i][j] = iTot[i][0][j];
     341
     342        cout << "region " << i << " cut mjjj " << mjjj[j] << " cate " << k+1 ;
     343        cout << " iTotB = " << iTot[i][0][j] ;
     344        cout << " iTotS = " << iTot[i][1][j] << endl;
     345      }
     346    }
     347  }
     348
     349  //3 is the number of regions
     350  //31 is the number of the cut for mjjj
     351  double N0fit[3][31], N1fit[3][31], N1err[3][31];
     352  for (int i = 0; i < 3; i++){
     353    for (int j = 0; j < 31; j++){
     354      pair<pair<double,double>,double> N0N1sig2 = Fit(m_N0exp[i][j],m_N1exp[i][j],i,j);
     355      pair<double,double> N0N1 = N0N1sig2.first;
     356      double N0   = N0N1.first;
     357      double N1   = N0N1.second;
     358      double sig2 = N0N1sig2.second;
     359      N0fit[i][j] = N0;
     360      N1fit[i][j] = N1;
     361      N1err[i][j] = sig2;
    159362    }
    160363  }
     
    175378  TString cat[6] = {"cat1","cat2","cat3","cat4","cat5","cat6"};
    176379
    177   TGraphErrors *heffB[3][6];
    178   TGraphErrors *heffS[3][6];
    179   TGraphErrors *heffD[3][6];
    180 
     380//   TGraphErrors *heffB[3][6];
     381//   TGraphErrors *heffS[3][6];
     382//   TGraphErrors *heffBS[3][6];
     383//   TGraphErrors *heffD[3][6];
     384
     385  TGraph *heffB[3][6];
     386  TGraph *heffS[3][6];
     387  TGraph *heffBS[3][6];
     388  TGraph *heffD[3][6];
     389
     390  //*************Define histograms on N1************
     391  TGraphErrors *hN1[3];
     392  for (int i = 0; i < 3; i++){
     393    double N1fittemp[31], N1errtemp[31];
     394    for (int j = 0; j < 31; j++){
     395      N1fittemp[j] = N1fit[i][j];
     396      N1errtemp[j] = N1err[i][j];
     397      cout << "mjjj = " << mjjj[j] << endl;
     398      cout << " m_N1exp[" << i << "][" << j << "]= " << m_N1exp[i][j] ;
     399      cout << " m_N0exp[" << i << "][" << j << "]= " << m_N0exp[i][j] ;
     400      cout << " N1fit[" << i << "][" << j << "]= " << N1fit[i][j] ;
     401      cout << " N0fit[" << i << "][" << j << "]= " << N0fit[i][j] ;
     402      cout << " N1err[" << i << "][" << j << "]= " << N1err[i][j] << endl;
     403    }
     404    hN1[i] = new TGraphErrors(31,mjjj,N1fittemp,mjjjErr,N1errtemp);
     405  }
     406
     407  //*************Define histograms on efficiencies****************
    181408  for (int i = 0; i < 3; i++){
    182409    for (int j = 0; j < 6; j++){
     
    189416        effErr[ic] = eBErr[i][ic][j];
    190417      }
    191       heffB[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
    192       heffB[i][j]->SetLineColor(4);
     418      //heffB[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     419      heffB[i][j] = new TGraph(31, mjjj, eff);
     420      heffB[i][j]->SetLineColor(8);
     421      heffB[i][j]->SetLineWidth(2);
    193422     
    194423      name = "heffS"; name += region[i]; name += cat[j];
     
    197426        effErr[ic] = eSErr[i][ic][j];
    198427      }
    199       heffS[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     428      //heffS[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     429      heffS[i][j] = new TGraph(31, mjjj, eff);
    200430      heffS[i][j]->SetLineColor(2);
     431      heffS[i][j]->SetLineWidth(2);
     432     
     433      name = "heffBS"; name += region[i]; name += cat[j];
     434      for (int ic = 0; ic < 31; ic++) {
     435        eff[ic]    = eBS[i][ic][j];
     436        effErr[ic] = eBSErr[i][ic][j];
     437      }
     438      //heffBS[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     439      heffBS[i][j] = new TGraph(31, mjjj, eff);
     440      heffBS[i][j]->SetLineColor(kMagenta);
     441      heffBS[i][j]->SetLineWidth(2);
    201442     
    202443      name = "heffD"; name += region[i]; name += cat[j];
     
    205446        effErr[ic] = eDErr[i][ic][j];
    206447      }
    207       heffD[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     448      //heffD[i][j] = new TGraphErrors(31, mjjj, eff, mjjjErr, effErr);
     449      heffD[i][j] = new TGraph(31, mjjj, eff);
     450      heffD[i][j]->SetLineWidth(2);
    208451    }
    209452  }
     
    211454  legend->AddEntry(heffB[0][1],"BkgMC");
    212455  legend->AddEntry(heffS[0][1],"SigMC");
     456  legend->AddEntry(heffBS[0][1],"Sig+BkgMC");
    213457  legend->AddEntry(heffD[0][1],"Data");
     458
     459  //****************** DRAWING *********************
     460
     461  TCanvas *cN1 = new TCanvas("cN1", "cN1", 1000, 400);
     462  cN1->Divide(3);
     463 
     464  cN1->cd(1);
     465  hN1[0]->Draw("ALP");
     466  cN1->cd(2);
     467  hN1[1]->Draw("ALP");
     468  cN1->cd(3);
     469  hN1[2]->Draw("ALP");
     470
     471  cN1->Print("N1fit.eps");
    214472
    215473  TCanvas *c[6];
     
    223481    frame1->SetTitle(name);
    224482    frame1->Draw();
    225     heffB[0][i]->Draw("LP");
    226     heffS[0][i]->Draw();
     483    heffS[0][i]->Draw("LP");
    227484    heffD[0][i]->Draw();
     485    heffBS[0][i]->Draw();
     486    heffB[0][i]->Draw();
    228487    legend->Draw("same");
    229488
     
    232491    frame2->SetTitle(name);
    233492    frame2->Draw();
    234     heffB[1][i]->Draw("LP");
    235     heffS[1][i]->Draw();
     493    heffS[1][i]->Draw("LP");
    236494    heffD[1][i]->Draw();
     495    heffBS[1][i]->Draw();
     496    heffB[1][i]->Draw();
    237497    legend->Draw("same");
    238498
     
    241501    frame3->SetTitle(name);
    242502    frame3->Draw();
    243     heffB[2][i]->Draw("LP");
    244     heffS[2][i]->Draw();
     503    heffS[2][i]->Draw("LP");
    245504    heffD[2][i]->Draw();
     505    heffBS[2][i]->Draw();
     506    heffB[2][i]->Draw();
    246507    legend->Draw("same");
    247508
     
    250511  }
    251512
    252   //********************************************
    253 
    254   //TCanvas *cN = new TCanvas("cN", "", 500, 400);
    255   //TGraph *hB = new TGraph(31, mjjj, N1);
    256   //hB->Draw("ALP");
    257513}
  • SPLOT/NEWREALEASE/MERGEFILES/MakeFakeData.C

    r21 r22  
    3939
    4040  TString s3 = "v3.root";
    41   TString ver = "ToTestEfficiency";
     41  TString ver = "v3";
    4242
    4343  //set the name of 78 files
     
    144144  //###################################
    145145
    146   TFile *fData    = new TFile("res_Data"+ver+".root","RECREATE");
     146  TFile *fData   = new TFile("res_FakeData"+ver+".root","RECREATE");
    147147  TTree *tWeiD   = new TTree("T","T");
    148   float b_weiD  = -100;
    149   float b_mtophad= -100;   
    150   float b_HT= -100;         
    151   float b_cos1= -100;       
    152   float b_cos2= -100;       
    153   float b_MinLep1= -100;   
    154   short b_hfor= -100;       
    155   short b_hasBadJet= -100; 
    156   short b_isEleEvent= -100;
    157   int b_RunNumber= -100; 
    158   tWeiD->Branch("wei",        &b_weiD,               "wei/F");
     148  float b_wei        = -100;
     149  float b_mtophad    = -100;   
     150  float b_HT         = -100;         
     151  float b_cos1       = -100;       
     152  float b_cos2       = -100;       
     153  float b_MinLep1    = -100;   
     154  short b_hfor       = -100;       
     155  short b_hasBadJet  = -100; 
     156  short b_isEleEvent = -100;
     157  int b_RunNumber    = -100; 
     158  tWeiD->Branch("wei",        &b_wei,                "wei/F");
    159159  tWeiD->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    160160  tWeiD->Branch("HT",         &b_HT,                  "HT/F");
     
    172172    countD++;
    173173    tData->GetEntry(ifi);
    174     b_mtophad = tSig->GetLeaf("mtophad")->GetValue();
    175     b_HT = tSig->GetLeaf("HT")->GetValue();
     174    b_mtophad = tData->GetLeaf("mtophad")->GetValue();
     175    b_HT      = tData->GetLeaf("HT")->GetValue();
    176176
    177177    //if (b_mtophad<250 || b_HT<620) continue;
    178     if (ifi%1000==0) cout << "Reading event " << ifi << " from Signal tree" << endl;
    179 
    180     //TString currentFile = tSig->GetTree()->GetCurrentFile()->GetName();
    181 
    182     b_weiS = Wei[0]*tSig->GetLeaf("WeiEvent")->GetValue();
    183     b_cos1 = tSig->GetLeaf("cos_tlepCMtpair")->GetValue();
    184     b_cos2 = tSig->GetLeaf("cosWqq")->GetValue();
    185     b_MinLep1 = tSig->GetLeaf("MinLep1")->GetValue();           
    186     b_hfor = (short)tSig->GetLeaf("hfor")->GetValue();
    187     b_isEleEvent = (short)tSig->GetLeaf("isEleEvent")->GetValue();
    188     b_hasBadJet = (short)tSig->GetLeaf("hasBadJet")->GetValue();
    189     b_RunNumber = (int)tSig->GetLeaf("RunNumber_ana")->GetValue();
    190 
    191     tWeiS->Fill();
    192   }
    193 
    194   fSig->cd();
    195   tWeiS->Write();
    196   fSig->Close();
    197 
     178    if (ifi%1000==0) cout << "Reading event " << ifi << " from Data tree" << endl;
     179
     180    TString currentFile = tData->GetTree()->GetCurrentFile()->GetName();
     181
     182    b_hfor       = (short)tData->GetLeaf("hfor")->GetValue();
     183    for (int i = 0; i < 78; i++){
     184      double kfac = 1, ksE = 1, ksM = 1;
     185      if (i>=2 && i<=32)  {kfac = 1.2; ksE = 0.906; ksM = 0.814;}//k factor for W+jets
     186      if (i>=40 && i<=69) {kfac = 1.25;}//Zjets
     187
     188      if (fname[i]==currentFile)
     189        b_wei = Wei[i]*(tData->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
     190    }
     191    b_cos1       = tData->GetLeaf("cos_tlepCMtpair")->GetValue();
     192    b_cos2       = tData->GetLeaf("cosWqq")->GetValue();
     193    b_MinLep1    = tData->GetLeaf("MinLep1")->GetValue();           
     194    b_hfor       = (short)tData->GetLeaf("hfor")->GetValue();
     195    b_isEleEvent = (short)tData->GetLeaf("isEleEvent")->GetValue();
     196    b_hasBadJet  = (short)tData->GetLeaf("hasBadJet")->GetValue();
     197    b_RunNumber  = (int)tData->GetLeaf("RunNumber_ana")->GetValue();
     198
     199    tWeiD->Fill();
     200  }
     201
     202  fData->cd();
     203  tWeiD->Write();
     204  fData->Close();
    198205
    199206  //###################################
     
    201208  //###################################
    202209
    203   TFile *fSig    = new TFile("res_Signal"+ver+".root","RECREATE");
     210  TFile *fSig    = new TFile("res_Signal"+s2[0]+ver+".root","RECREATE");
     211
    204212  TTree *tWeiS   = new TTree("T","T");
    205   float b_weiS  = -100;
    206   float b_mtophad= -100;   
    207   float b_HT= -100;         
    208   float b_cos1= -100;       
    209   float b_cos2= -100;       
    210   float b_MinLep1= -100;   
    211   short b_hfor= -100;       
    212   short b_hasBadJet= -100; 
    213   short b_isEleEvent= -100;
    214   int b_RunNumber= -100; 
    215   tWeiS->Branch("wei",        &b_weiS,               "wei/F");
     213  tWeiS->Branch("wei",        &b_wei,                "wei/F");
    216214  tWeiS->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    217215  tWeiS->Branch("HT",         &b_HT,                  "HT/F");
     
    230228    tSig->GetEntry(ifi);
    231229    b_mtophad = tSig->GetLeaf("mtophad")->GetValue();
    232     b_HT = tSig->GetLeaf("HT")->GetValue();
     230    b_HT      = tSig->GetLeaf("HT")->GetValue();
    233231
    234232    //if (b_mtophad<250 || b_HT<620) continue;
     
    237235    //TString currentFile = tSig->GetTree()->GetCurrentFile()->GetName();
    238236
    239     b_weiS = Wei[0]*tSig->GetLeaf("WeiEvent")->GetValue();
    240     b_cos1 = tSig->GetLeaf("cos_tlepCMtpair")->GetValue();
    241     b_cos2 = tSig->GetLeaf("cosWqq")->GetValue();
    242     b_MinLep1 = tSig->GetLeaf("MinLep1")->GetValue();           
    243     b_hfor = (short)tSig->GetLeaf("hfor")->GetValue();
     237    b_wei        = Wei[0]*tSig->GetLeaf("WeiEvent")->GetValue();
     238    b_cos1       = tSig->GetLeaf("cos_tlepCMtpair")->GetValue();
     239    b_cos2       = tSig->GetLeaf("cosWqq")->GetValue();
     240    b_MinLep1    = tSig->GetLeaf("MinLep1")->GetValue();           
     241    b_hfor       = (short)tSig->GetLeaf("hfor")->GetValue();
    244242    b_isEleEvent = (short)tSig->GetLeaf("isEleEvent")->GetValue();
    245     b_hasBadJet = (short)tSig->GetLeaf("hasBadJet")->GetValue();
    246     b_RunNumber = (int)tSig->GetLeaf("RunNumber_ana")->GetValue();
     243    b_hasBadJet  = (short)tSig->GetLeaf("hasBadJet")->GetValue();
     244    b_RunNumber  = (int)tSig->GetLeaf("RunNumber_ana")->GetValue();
    247245
    248246    tWeiS->Fill();
     
    271269
    272270  TTree *tWeiB   = new TTree("T","T");
    273   tWeiB->Branch("wei",        &b_weiS,               "wei/F");
     271  tWeiB->Branch("wei",        &b_wei,               "wei/F");
    274272  tWeiB->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    275273  tWeiB->Branch("HT",         &b_HT,                  "HT/F");
     
    303301
    304302      if (fname[i]==currentFile)
    305         b_weiS = Wei[i]*(tAllBkg->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
     303        b_wei = Wei[i]*(tAllBkg->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
    306304    }
    307305    b_cos1       = tAllBkg->GetLeaf("cos_tlepCMtpair")->GetValue();
     
    337335
    338336  TTree *tWeiT   = new TTree("T","T");
    339   tWeiT->Branch("wei",        &b_weiS,               "wei/F");
     337  tWeiT->Branch("wei",        &b_wei,               "wei/F");
    340338  tWeiT->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    341339  tWeiT->Branch("HT",         &b_HT,                  "HT/F");
     
    364362    for (int i = 0; i < 78; i++){
    365363      if (fname[i]==currentFile)
    366         b_weiS = Wei[i]*(tTop->GetLeaf("WeiEvent")->GetValue());
     364        b_wei = Wei[i]*(tTop->GetLeaf("WeiEvent")->GetValue());
    367365    }
    368366    b_cos1       = tTop->GetLeaf("cos_tlepCMtpair")->GetValue();
     
    386384
    387385  TTree *tWeiW   = new TTree("T","T");
    388   tWeiW->Branch("wei",        &b_weiS,               "wei/F");
     386  tWeiW->Branch("wei",        &b_wei,               "wei/F");
    389387  tWeiW->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    390388  tWeiW->Branch("HT",         &b_HT,                  "HT/F");
     
    417415
    418416      if (fname[i]==currentFile)
    419         b_weiS = Wei[i]*(tAllW->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
     417        b_wei = Wei[i]*(tAllW->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
    420418    }
    421419    b_cos1       = tAllW->GetLeaf("cos_tlepCMtpair")->GetValue();
     
    435433  //*********** File Single Top  *****************
    436434  //##############################################
    437   TFile *fSingT   = new TFile("res_SingleTop"+ver+".root","RECREATE");
     435  TFile *fSingT   = new TFile("res_SingT"+ver+".root","RECREATE");
    438436
    439437  TTree *tWeiST   = new TTree("T","T");
    440   tWeiST->Branch("wei",        &b_weiS,               "wei/F");
     438  tWeiST->Branch("wei",        &b_wei,               "wei/F");
    441439  tWeiST->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    442440  tWeiST->Branch("HT",         &b_HT,                  "HT/F");
     
    465463    for (int i = 0; i < 78; i++){
    466464      if (fname[i]==currentFile)
    467         b_weiS = Wei[i]*(tSingT->GetLeaf("WeiEvent")->GetValue());
     465        b_wei = Wei[i]*(tSingT->GetLeaf("WeiEvent")->GetValue());
    468466    }
    469467    b_hfor       = (short)tSingT->GetLeaf("hfor")->GetValue();
     
    487485
    488486  TTree *tWeiZ   = new TTree("T","T");
    489   tWeiZ->Branch("wei",        &b_weiS,               "wei/F");
     487  tWeiZ->Branch("wei",        &b_wei,               "wei/F");
    490488  tWeiZ->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    491489  tWeiZ->Branch("HT",         &b_HT,                  "HT/F");
     
    518516
    519517      if (fname[i]==currentFile)
    520         b_weiS = Wei[i]*(tAllZ->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
     518        b_wei = Wei[i]*(tAllZ->GetLeaf("WeiEvent")->GetValue())*WJet_SF(b_hfor)*kfac*ksE*ksM;
    521519    }
    522520    b_cos1       = tAllZ->GetLeaf("cos_tlepCMtpair")->GetValue();
     
    539537
    540538  TTree *tWeiQCD   = new TTree("T","T");
    541   tWeiQCD->Branch("wei",        &b_weiS,               "wei/F");
     539  tWeiQCD->Branch("wei",        &b_wei,               "wei/F");
    542540  tWeiQCD->Branch("mtophad",    &b_mtophad,        "mtophad/F");
    543541  tWeiQCD->Branch("HT",         &b_HT,                  "HT/F");
     
    566564    for (int i = 0; i < 78; i++){
    567565      if (fname[i]==currentFile)
    568         b_weiS = Wei[i]*(tQCD->GetLeaf("WeiEvent")->GetValue());
     566        b_wei = Wei[i]*(tQCD->GetLeaf("WeiEvent")->GetValue());
    569567    }
    570568    b_cos1       = tQCD->GetLeaf("cos_tlepCMtpair")->GetValue();
  • SPLOT/NEWREALEASE/sPlots_NewRel.C

    r19 r22  
    1212#include <vector>
    1313
     14#include <iostream>
     15
     16using namespace std;
     17
     18void sPlots_NewRel_N0known();
     19
    1420//Set the cut values
    1521double Cut1 = 0.7;
     
    1824double Cut4 = 250;
    1925double Cut5 = 620;
    20 double sigmaMin;
    21 int icount = 0;
    22 double iTot[2];
    23 double iCatS[6], iCatB[6];
    24 double F1[6];
    25 double F0[6];
    26 
    27 double N1 = 0., N0 = 0., sig2 = 0.;
     26
     27//global variables
     28int    m_icount = 0;
     29double m_iTot[2];              //for (int i = 0; i < 2; i++){ m_iTot[i] = 0;}
     30double m_iCatS[6], m_iCatB[6]; //for (int i = 0; i < 6; i++){ m_iCatS[i] = 0; m_iCatB[i] = 0;}
     31double m_F1[6];                //for (int i = 0; i < 6; i++){ m_F1[i] = 0; }
     32double m_F0[6];                //for (int i = 0; i < 6; i++){ m_F0[i] = 0; }
     33double m_N1 = 0., m_N0 = 0., m_sig2 = 0.;
     34
     35TH1F *h[7];
    2836
    2937//Define class Event
     
    4654
    4755std::vector<EventC> m_EventList;
    48 
    49 void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) {
     56bool m_ListAlreadyDone = false;
     57
     58void fcnN1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) {
    5059  double L = 0;
    51   for (int iev = 0; iev < m_EventList.size(); iev++) {
     60  for (unsigned int iev = 0; iev < m_EventList.size(); iev++) {
     61    EventC evt = m_EventList[iev];
     62    double smt = m_N0*evt.fb() + par[0]*evt.fs();
     63    if (smt > 0) L += evt.w()*log(smt);
     64  }
     65  f = par[0] - L;
     66};
     67
     68void fcnN0N1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) {
     69  double L = 0;
     70  for (unsigned int iev = 0; iev < m_EventList.size(); iev++) {
    5271    EventC evt = m_EventList[iev];
    5372    double smt = par[0]*evt.fb() + par[1]*evt.fs();
     
    5675  f = par[0] + par[1] - L;
    5776};
    58 
    5977
    6078vector<double> InvMatrix(double a, double b, double c, double d){
     
    7896  //######## COMPUTE the sWeights
    7997  TString filename = "";
    80   //TString type[2] = {"Top_MCAtNLO_PURewei","Signal"};
    81   TString type[2] = {"AllBkgTopMCAtNLO","Signal"};
     98  TString type[2] = {"AllBkgTopMCAtNLO","Signal115420"};
    8299  TFile *file[2];
    83100  TTree *t[2];
    84101 
    85102  for (int i = 0; i < 2; i++){
    86     filename = "./MERGEFILES/res_"; filename += type[i]; filename += "NEW.root";
     103    filename = "./MERGEFILES/res_"; filename += type[i]; filename += "v3.root";
    87104    file[i] = TFile::Open(filename);
    88105    t[i]    = (TTree*)file[i]->Get("T");
    89106  }
    90107
    91   for (int i = 0; i < 2; i++){
    92     iTot[i] = 0;
    93   }
    94   for (int i = 0; i < 6; i++){
    95     iCatS[i] = 0;
    96     iCatB[i] = 0;
    97   }
    98  
    99108  for (int i = 0; i < 2; i++){
    100109    int nevt = t[i]->GetEntries();
     
    111120      //cout << "WeiTot = " << WeiTot << endl;
    112121
    113       int hasBJ = t[i]->GetLeaf("hasBadJet")->GetValue();
    114       double hf = t[i]->GetLeaf("hfor")->GetValue();
     122      int hasBJ = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     123      int hf    = (int)t[i]->GetLeaf("hfor")->GetValue();
    115124
    116125      if (hf!=4 && hasBJ==0){
     
    120129     
    121130        if (mt > Cut4 && HT > Cut5) {
    122           icount++;
    123           iTot[i] += WeiTot;
     131          m_iTot[i] += WeiTot;
    124132          int it = -1;
    125133          if (cos1<Cut1 && cos2<Cut2){
     
    138146          }
    139147          if (i == 0)
    140             iCatB[it] += WeiTot;
     148            m_iCatB[it] += WeiTot;
    141149          else
    142             iCatS[it] += WeiTot;
     150            m_iCatS[it] += WeiTot;
    143151        }
    144152      }
     
    146154  }//End of reading 2 files
    147155
    148   cout << "iTot0 = " << iTot[0] << endl;
    149   cout << "iTot1 = " << iTot[1] << endl;
    150 
    151   N1 = iTot[1];
    152   N0 = iTot[0];
     156  m_N0 = m_iTot[0];
     157  m_N1 = m_iTot[1];
     158
     159  cout << "To calculate efficiencies using " << type[1] << endl;
     160  cout << "iTot0 = " << m_iTot[0] << endl;
     161  cout << "iTot1 = " << m_iTot[1] << endl;
    153162
    154163  double sigmaInv = 0;
    155164  double sumeffS = 0, sumeffB = 0;
    156165  for (int i = 0; i < 6; i++){
    157     F1[i] = iCatS[i]/iTot[1];
    158     F0[i] = iCatB[i]/iTot[0];
    159     sumeffS += F1[i];
    160     sumeffB += F0[i];
    161     cout << "F1[" << i << "] = " << F1[i] << endl;
    162     cout << "F0[" << i << "] = " << F0[i] << endl;
    163 
    164     double term = F1[i]*F1[i]/(N1*F1[i] + N0*F0[i]);
    165     if (F1[i]==0 && F0[i]==0)  term = 0;
     166    m_F1[i] = m_iCatS[i]/m_iTot[1];
     167    m_F0[i] = m_iCatB[i]/m_iTot[0];
     168    sumeffS += m_F1[i];
     169    sumeffB += m_F0[i];
     170    cout << "m_F1[" << i << "] = " << m_F1[i] << endl;
     171    cout << "m_F0[" << i << "] = " << m_F0[i] << endl;
     172
     173    double term = m_F1[i]*m_F1[i]/(m_N1*m_F1[i] + m_N0*m_F0[i]);
     174    if (m_F1[i]==0 && m_F0[i]==0)  term = 0;
    166175    sigmaInv += term;
    167176  }
    168 
    169   double sigma2 = 1/N1/N1/sigmaInv;
    170   sigmaMin = sqrt(sigma2);
    171  
    172   cout << "sumeffS = " << sumeffS << " sumeffB = " << sumeffB << endl;
    173   cout << "sigmaMin = " << sigmaMin << endl;
    174177}
    175178
     
    195198  }
    196199 
    197   f0e = F0[it];
    198   f1e = F1[it];
     200  f0e = m_F0[it];
     201  f1e = m_F1[it];
    199202 
    200203  //cout << "it = " << it << " f0e = " << f0e << " f1e = " << f1e << endl;
     
    207210
    208211void MakeEventListf0f1(){
     212  m_ListAlreadyDone = true;
    209213  //######## COMPUTE the sWeights
    210214  TString filename = "";
    211   //TString type[2] = {"Top_MCAtNLO_PURewei","Signal"};
    212   TString type[2] = {"AllBkgTopMCAtNLO","Signal"};
     215  TString type[2] = {"AllBkgTopMCAtNLO","Signal115426"};
    213216  TFile *file[2];
    214217  TTree *t[2];
    215218 
    216219  for (int i = 0; i < 2; i++){
    217     filename = "./MERGEFILES/res_"; filename += type[i]; filename += "NEW.root";
     220    filename = "./MERGEFILES/res_"; filename += type[i]; filename += "v3.root";
    218221    file[i] = TFile::Open(filename);
    219222    t[i]    = (TTree*)file[i]->Get("T");
     
    231234      double dMLep  = t[i]->GetLeaf("MinLep1")->GetValue();
    232235      double WeiTot = t[i]->GetLeaf("wei")->GetValue();
     236
     237      // Yes some events have 0 weight due to PU reweighting
     238      // (just to save time, remove them)
     239      if (fabs(WeiTot) < 1e-9) continue;
     240      //cout << "Reading event " << iev << " of type " << i << " w = " << WeiTot << endl;
    233241     
    234242      //cout << "WeiTot = " << WeiTot << endl;
    235243     
    236       int hasBJ  = t[i]->GetLeaf("hasBadJet")->GetValue();
    237       double hf = t[i]->GetLeaf("hfor")->GetValue();
     244      int hasBJ  = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     245      int hf     = (int)t[i]->GetLeaf("hfor")->GetValue();
    238246
    239247      if (hf!=4 && hasBJ==0){
     
    255263}
    256264
    257 Fit(){
     265void Fit(bool FitN0){
    258266  // Test
    259   MakeEventListf0f1();
    260 
    261   int ifl = 0;
    262   double step = 0.01;
    263   double fitPar0 = iTot[0];
    264   double fitPar1 = iTot[1];
    265   double nmax0   = fitPar0 + 4*sqrt(iTot[0]);
    266   double nmin0   = fitPar0 - 4*sqrt(iTot[0]);
    267   double nmax1   = fitPar1 + 4*sqrt(iTot[1]);
    268   double nmin1   = - 4*sqrt(iTot[1]);
    269   double n0best, err0best;
    270   double n1best, err1best;
    271   TMinuit* myMinuit = new TMinuit(2);
    272   //myMinuit->Command("SET PRI -1");
    273   myMinuit->Command("SET NOW");
    274   myMinuit->Command("SET ERR 0.5");
    275   myMinuit->Command("SET STR 2");   // try to improve mimimum search
    276   myMinuit->SetFCN(fcn);
    277   myMinuit->mnparm(0,"N0",fitPar0,step,nmin0,nmax0,ifl);
    278   myMinuit->mnparm(1,"N1",fitPar1,step,nmin1,nmax1,ifl);
    279   //myMinuit->FixParameter(0);
    280   myMinuit->Command("MIGRAD 500 1");
    281   myMinuit->GetParameter(0,n0best,err0best);
    282   myMinuit->GetParameter(1,n1best,err1best);
    283   std::cout << "Expected N0 = " << iTot[0] << " fitted = " << n0best << " +- " << err0best << std::endl;
    284   std::cout << "Expected N1 = " << iTot[1] << " fitted = " << n1best << " +- " << err1best << std::endl;
    285 
    286   N0 = n0best;
    287   N1 = n1best;
    288 
    289   cout << "N0 = " << N0 << " N1 = " << N1 << endl;
    290   sig2 = sigmaMin*sigmaMin*N1*N1;
     267  if (!m_ListAlreadyDone) MakeEventListf0f1();
     268  cout << "Number of events in the LH " << m_EventList.size() << endl;
     269
     270  if (FitN0){
     271
     272    double N0_exp = m_iTot[0];
     273    double N1_exp = m_iTot[1];
     274
     275    cout << "FitN0N1 " << " N0_exp " << N0_exp << " N1_exp " << N1_exp << endl;
     276
     277    int ifl = 0;
     278    double step = 0.01;
     279
     280    double fitPar0 = N0_exp;
     281    double nmax0   = fitPar0 + 4*sqrt(N0_exp);
     282    double nmin0   = fitPar0 - 4*sqrt(N0_exp);
     283
     284    double fitPar1 = N1_exp;
     285    double nmax1   = fitPar1 + 4*sqrt(N1_exp);
     286    double nmin1   =         - 4*sqrt(N1_exp);
     287
     288    double n0best, err0best;
     289    double n1best, err1best;
     290
     291    TMinuit* myMinuit = new TMinuit(2);
     292    //myMinuit->Command("SET PRI -1");
     293    myMinuit->Command("SET NOW");
     294    myMinuit->Command("SET ERR 0.5");
     295    myMinuit->Command("SET STR 2");   // try to improve mimimum search
     296    myMinuit->SetFCN(fcnN0N1);
     297    myMinuit->mnparm(0,"N0",fitPar0,step,nmin0,nmax0,ifl);
     298    myMinuit->mnparm(1,"N1",fitPar1,step,nmin1,nmax1,ifl);
     299    //myMinuit->FixParameter(0);
     300    myMinuit->Command("MIGRAD 500 1");
     301    myMinuit->GetParameter(0,n0best,err0best);
     302    myMinuit->GetParameter(1,n1best,err1best);
     303
     304    std::cout << "Expected N0 = " << N0_exp << " fitted = " << n0best << " +- " << err0best << std::endl;
     305    std::cout << "Expected N1 = " << N1_exp << " fitted = " << n1best << " +- " << err1best << std::endl;
     306   
     307    m_N0 = n0best;
     308    m_N1 = n1best;
     309  }
     310  else {
     311    double N0_exp = m_iTot[0];
     312    double N1_exp = m_iTot[1];
     313
     314    cout << "FitN1 " << " N0_exp " << N0_exp << " N1_exp " << N1_exp << endl;
     315
     316    int ifl = 0;
     317    double step = 0.01;
     318    double fitPar1 = N1_exp;
     319    double nmax1   = fitPar1 + 4*sqrt(N1_exp);
     320    double nmin1   = - 4*sqrt(N1_exp);
     321    double n1best, err1best;
     322    TMinuit* myMinuitN1 = new TMinuit(1);
     323    //myMinuitN1->Command("SET PRI -1");
     324    myMinuitN1->Command("SET NOW");
     325    myMinuitN1->Command("SET ERR 0.5");
     326    myMinuitN1->Command("SET STR 2");   // try to improve mimimum search
     327    myMinuitN1->SetFCN(fcnN1);
     328    myMinuitN1->mnparm(0,"N1",fitPar1,step,nmin1,nmax1,ifl);
     329    //myMinuitN1->FixParameter(0);
     330    myMinuitN1->Command("MIGRAD 500 1");
     331    myMinuitN1->GetParameter(0,n1best,err1best);
     332    std::cout << "Expected N0 = " << N0_exp << endl;
     333    std::cout << "Expected N1 = " << N1_exp << " fitted = " << n1best << " +- " << err1best << std::endl;
     334   
     335    m_N1 = n1best;
     336    m_sig2 = err1best*err1best;
     337  }
     338
     339  cout << "bool FitN0 " << FitN0 << " m_N0 = " << m_N0 << " m_N1 = " << m_N1 << endl;
     340  //cout << "m_N0 = " << m_N0 << " m_N1 = " << m_N1 << endl;
    291341}
    292342
     
    296346
    297347void sPlots_NewRel(){
    298   //gROOT->SetStyle("Plain");
    299   //Make the table of sP values first
    300 
    301348  cout << "Cut1 = " << Cut1 << endl;
    302349  cout << "Cut2 = " << Cut2 << endl;
     
    305352  cout << "Cut5 = " << Cut5 << endl;
    306353
     354  //Compute the efficiencies first
    307355  CalF0F1();
    308   cout << "icount = " << icount << endl;
     356
     357  //Reset m_iTot[0] and m_iTot[1]
     358  m_iTot[0] = 0.; m_iTot[1] = 0.;
     359  //double F0N[6]
     360
     361  //Now compute m_N0, m_N1
     362  TString filename = "";
     363  TString type[2] = {"AllBkgTopMCAtNLO","Signal115426"};
     364  TFile *file[2];
     365  TTree *t[2];
     366 
     367  for (int i = 0; i < 2; i++){
     368    filename = "./MERGEFILES/res_"; filename += type[i]; filename += "v3.root";
     369    file[i] = TFile::Open(filename);
     370    t[i]    = (TTree*)file[i]->Get("T");
     371  }
     372
     373  for (int i = 0; i < 2; i++){
     374    int nevt = t[i]->GetEntries();
     375    for (int iev = 0; iev < nevt; iev++) {
     376      t[i]->GetEntry(iev);
     377           
     378      double mt     = t[i]->GetLeaf("mtophad")->GetValue();
     379      double HT     = t[i]->GetLeaf("HT")->GetValue();
     380      double WeiTot = t[i]->GetLeaf("wei")->GetValue();
     381
     382      int hasBJ = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     383      int    hf = (int)t[i]->GetLeaf("hfor")->GetValue();
     384
     385      if (hf!=4 && hasBJ==0){
     386        if (mt > Cut4 && HT > Cut5) {
     387          m_icount++;
     388          m_iTot[i] += WeiTot;
     389        }
     390      }
     391    }//Read each file
     392  }//End of reading 2 files
     393
     394  cout << "In main macro" << endl;
     395  cout << "iTot0 = " << m_iTot[0] << endl;
     396  cout << "iTot1 = " << m_iTot[1] << endl;
     397
     398  m_N1 = m_iTot[1];
     399  m_N0 = m_iTot[0];
     400
     401  cout << "m_icount = " << m_icount << endl;
    309402
    310403  //sPlots N0 known
     
    314407  sPlots_NewRel_N0known();
    315408
    316   //Get right N0 and N1 for standard sPlot
    317409  cout << "***********************************************" << endl;
    318410  cout << "************* sPlot N0 unknown ****************" << endl;
    319411  cout << "***********************************************" << endl;
    320412
    321   Fit();
     413  //Get right N0 and N1 for standard sPlot
     414  Fit(true);
    322415 
    323   TString filename = "";
    324   //TString type[2] = {"Top_MCAtNLO_PURewei","Signal"};
    325   TString type[2] = {"AllBkgTopMCAtNLO","Signal"};
    326   TFile *file[2];
    327   TTree *t[2];
    328  
    329   for (int i = 0; i < 2; i++){
    330     filename = "./MERGEFILES/res_"; filename += type[i]; filename += "NEW.root";
    331     file[i] = TFile::Open(filename);
    332     t[i]    = (TTree*)file[i]->Get("T");
    333   }
    334 
    335416  double VI00 = 0, VI01 = 0, VI10 = 0, VI11 = 0;
    336417  //start reading 2 files
    337   //int icount = 0;
     418  //int m_icount = 0;
    338419  for (int i = 0; i < 2; i++){
    339420    int nevt = t[i]->GetEntries();
     
    351432      double cos2 = fabs(y);
    352433     
    353       int hasBJ  = t[i]->GetLeaf("hasBadJet")->GetValue();
    354       double hf = t[i]->GetLeaf("hfor")->GetValue();
     434      int hasBJ  = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     435      int     hf = (int)t[i]->GetLeaf("hfor")->GetValue();
    355436
    356437      if (hf!=4 && hasBJ==0){
     
    368449          double vi11;
    369450         
    370           vi00 = WeiTot*F0e*F0e/(N1*F1e + N0*F0e)/(N1*F1e + N0*F0e);
    371           vi01 = WeiTot*F0e*F1e/(N1*F1e + N0*F0e)/(N1*F1e + N0*F0e);
    372           vi10 = WeiTot*F1e*F0e/(N1*F1e + N0*F0e)/(N1*F1e + N0*F0e);
    373           vi11 = WeiTot*F1e*F1e/(N1*F1e + N0*F0e)/(N1*F1e + N0*F0e);
     451          vi00 = WeiTot*F0e*F0e/(m_N1*F1e + m_N0*F0e)/(m_N1*F1e + m_N0*F0e);
     452          vi01 = WeiTot*F0e*F1e/(m_N1*F1e + m_N0*F0e)/(m_N1*F1e + m_N0*F0e);
     453          vi10 = WeiTot*F1e*F0e/(m_N1*F1e + m_N0*F0e)/(m_N1*F1e + m_N0*F0e);
     454          vi11 = WeiTot*F1e*F1e/(m_N1*F1e + m_N0*F0e)/(m_N1*F1e + m_N0*F0e);
    374455
    375456          VI00 += vi00;
     
    382463  }//End of reading 2 files
    383464
    384   cout << "Main macro : N0 = " << N0 << " N1 = " << N1 << endl;
     465  cout << "Main macro : m_N0 = " << m_N0 << " m_N1 = " << m_N1 << endl;
    385466
    386467  cout << "VI00 = " << VI00 << endl;
     
    402483  cout << "sigmaN0 = " << sigmaN0 << " sigmaN1 = " << sigmaN1 << " rho = " << rho << endl;
    403484
    404   TH1F *hSigN  = new TH1F("hSigN",     "", 35, 250, 600);
    405   TH1F *hBkgN  = new TH1F("hBkgN",     "", 35, 250, 600);
    406   TH2F *h2DSig = new TH2F("h2DSig",    "", 35, 250, 600, 50, 500, 1500);
    407   TH2F *h2DBkg = new TH2F("h2DBkg",    "", 35, 250, 600, 50, 500, 1500);
     485  TH1F *hSigN  = new TH1F("hSigN",     "", 150, 0, 1500);
     486  TH1F *hBkgN  = new TH1F("hBkgN",     "", 150, 0, 1500);
     487  TH2F *h2DSig = new TH2F("h2DSig",    "", 150, 0, 1500, 50, 500, 1500);
     488  TH2F *h2DBkg = new TH2F("h2DBkg",    "", 150, 0, 1500, 50, 500, 1500);
    408489
    409490  //Try standard sPlots
    410491  double SumsP1 = 0.;
    411492  double SumsP0 = 0.;
    412   double *sPN = new double[icount];
    413   double *mtN = new double[icount];
     493  double *sPN = new double[m_icount];
     494  double *mtN = new double[m_icount];
    414495  int icc = 0;
    415496  for (int i = 0; i < 2; i++){
     
    424505      double dMLep = t[i]->GetLeaf("MinLep1")->GetValue();
    425506      double WeiTot = t[i]->GetLeaf("wei")->GetValue();
    426      
    427       int hasBJ  = t[i]->GetLeaf("hasBadJet")->GetValue();
    428       double hf = t[i]->GetLeaf("hfor")->GetValue();
     507      if (fabs(WeiTot) < 1e-9) continue;
     508     
     509      int hasBJ  = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     510      int     hf = (int)t[i]->GetLeaf("hfor")->GetValue();
    429511
    430512      if (hf!=4 && hasBJ==0){
     
    439521          double F1e = veff.at(1);
    440522         
    441           double sP1 = (V11*F1e + V10*F0e)/(N1*F1e + N0*F0e);
    442           double sP0 = (V00*F0e + V10*F1e)/(N1*F1e + N0*F0e);
     523          double sP1 = (V11*F1e + V10*F0e)/(m_N1*F1e + m_N0*F0e);
     524          double sP0 = (V00*F0e + V10*F1e)/(m_N1*F1e + m_N0*F0e);
    443525         
    444526          hSigN->Fill(mt,sP1*WeiTot);
     
    447529          h2DBkg->Fill(mt,HT,sP0*WeiTot);
    448530          mtN[icc] = mt;
    449           if (WeiTot==0) sPN[icc] = 0;
    450           else sPN[icc] = sP1*sP1*WeiTot;
     531          sPN[icc] = sP1*sP1*WeiTot;
    451532          icc++;
    452533         
     
    465546  cout << "hBkgN integral  " << hBkgN->Integral(0,1500)  << endl;
    466547 
    467   for (int i = 1; i < 36; i++){
    468     double vb1 = 250 + (i-1)*10;
    469     double vb2 = 250 + i*10;
     548  for (int i = 1; i < 151; i++){
     549    double vb1 = (i-1)*10;
     550    double vb2 = i*10;
    470551    double ErrBin = 0.;
    471     for (int j = 0; j < icount; j++){
     552    for (int j = 0; j < m_icount; j++){
    472553      double mt = mtN[j];
    473554      double sp = sPN[j];
     
    476557      }
    477558    }
    478     hSigN->SetBinError(i,sqrt(ErrBin));
    479   }
    480 
    481   TH2F *frame = new TH2F("frame", "", 35, 250, 600, 50, -10, 40);
    482 
    483   TCanvas *c = new TCanvas("c", "", 900, 400);
     559    if (ErrBin<0) cout << "ErrBin is negative in sPlot standard " << i << " " << ErrBin << endl;
     560    hSigN->SetBinError(i,sqrt(fabs(ErrBin)));
     561  }
     562
     563  //TH2F *frame = new TH2F("frame", "", 35, 250, 600, 50, -10, 40);
     564
     565  TCanvas *c = new TCanvas("c", "cStandard", 900, 400);
    484566  c->Divide(2);
    485567  c->cd(1);
    486568  //frame->Draw();
     569  double x1 = 250;
     570  double x2 = 600 - 0.01;
     571  TAxis *ax = hSigN->GetXaxis();
     572  int b1 = ax->FindBin(x1);
     573  int b2 = ax->FindBin(x2);
     574  ax->SetRange(b1,b2);
    487575  hSigN->SetLineColor(2);
    488576  hSigN->SetLineWidth(1);
    489577  hSigN->DrawCopy("E1");
    490578  hSigN->SetLineColor(1);
    491   hSigN->SetLineWidth(2.5);
     579  hSigN->SetLineWidth(2);
    492580  hSigN->DrawCopy("hist,same");
    493581  c->cd(2);
     582  TAxis *axB = hBkgN->GetXaxis();
     583  int b1B = axB->FindBin(x1);
     584  int b2B = axB->FindBin(x2);
     585  axB->SetRange(b1B,b2B);
    494586  hBkgN->SetLineColor(4);
    495   hBkgN->SetLineWidth(2.5);
     587  hBkgN->SetLineWidth(2);
    496588  hBkgN->Draw();
    497589
     
    510602
    511603void sPlots_NewRel_N0known(){
    512   cout << "in function N0 known " << icount << endl;
    513   N0 = iTot[0];
    514   N1 = iTot[1];
    515   sig2 = sigmaMin*sigmaMin*N1*N1;
    516   cout << "sigmaMin = " << sigmaMin << " sig2 = " << sig2 << endl;
    517   cout << "N0 = " << N0 << " N1 = " << N1 << endl;
    518 
    519   double k = (sig2 - N1)/(N0 - (sig2 - N1));
     604
     605  Fit(false);
     606
     607  cout << "in function N0 known " << m_icount << endl;
     608  cout << "m_sig2 = " << m_sig2 << endl;
     609  cout << "m_N0 = " << m_N0 << " m_N1 = " << m_N1 << endl;
     610
     611  double k = (m_sig2 - m_N1)/(m_N0 - (m_sig2 - m_N1));
    520612 
    521613  TH2F *twoDim_mt_HT_Sig     = new TH2F("mt_HT_Sig",     "", 75, 250, 1500, 50, 500, 2000);
    522614  TH2F *twoDim_mt_HT_Sig_esP = new TH2F("mt_HT_Sig_esP", "", 75, 250, 1500, 50, 500, 2000);
    523615 
    524   TH1F *h[7];
    525616  h[0]  = new TH1F("All1fb",       "",  150,  0,  1500);
    526617  h[1]  = new TH1F("Sig1fb",       "",  150,  0,  1500);
     
    531622  h[6]  = new TH1F("esPlotSig",    "",  150,  0,  1500);
    532623 
    533   double *mtV  = new double[icount];
    534   double *err  = new double[icount];
    535   double *errN = new double[icount];
     624  double *mtV  = new double[m_icount];
     625  double *err  = new double[m_icount];
     626  double *errN = new double[m_icount];
    536627  int icc = 0;
    537628 
    538629  TString filename = "";
    539   TString type[2] = {"AllBkgTopMCAtNLO","Signal"};
     630  TString type[2] = {"AllBkgTopMCAtNLO","Signal115426"};
    540631  TFile *file[2];
    541632  TTree *t[2];
    542633  for (int i = 0; i < 2; i++){
    543     filename = "./MERGEFILES/res_"; filename += type[i]; filename += "NEW.root";
     634    filename = "./MERGEFILES/res_"; filename += type[i]; filename += "v3.root";
    544635    file[i] = TFile::Open(filename);
    545636    t[i]    = (TTree*)file[i]->Get("T");
     
    558649      double WeiTot = t[i]->GetLeaf("wei")->GetValue();
    559650
    560       int hasBJ  = t[i]->GetLeaf("hasBadJet")->GetValue();
    561       double hf = t[i]->GetLeaf("hfor")->GetValue();
     651      int hasBJ  = (int)t[i]->GetLeaf("hasBadJet")->GetValue();
     652      int     hf = (int)t[i]->GetLeaf("hfor")->GetValue();
    562653     
    563654      if (hf!=4 && hasBJ==0){
     
    572663          double F1e = veff.at(1);
    573664         
    574           double sWei  = sig2*F1e*WeiTot/(N1*F1e + N0*F0e);
     665          double sWei  = m_sig2*F1e*WeiTot/(m_N1*F1e + m_N0*F0e);
    575666          double sWeiN = sWei*(k+1) - k*WeiTot;
     667
     668          //cout << "sWei  = " << sWei << endl;
     669          //cout << "sWeiN = " << sWeiN << endl;
    576670         
    577671          //For 1 fb-1
     
    586680          if (i==1){
    587681            h[1]->Fill(mt,WeiTot);
     682            //h[4]->Fill(mt,sWei);
    588683            twoDim_mt_HT_Sig->Fill(mt,HT,sWei);
    589684            twoDim_mt_HT_Sig_esP->Fill(mt,HT,sWeiN);   
     
    609704  for (int i = 1; i < 151; i++){
    610705    double sP = h[3]->GetBinContent(i);
    611     double B  = (sig2 - N1)*h[2]->GetBinContent(i);
    612706    double sB = h[5]->GetBinContent(i);
    613     double W  = sP - B;
    614707    double sW  = sP - sB;
    615     double Tot1fb = h[0]->GetBinContent(i);
    616     double BkgPure = Tot1fb - sP;
     708
     709    //double B  = (m_sig2 - m_N1)*h[2]->GetBinContent(i);
     710    //double W  = sP - B;
     711    //double Tot1fb = h[0]->GetBinContent(i);
     712    //double BkgPure = Tot1fb - sP;
    617713   
    618714    double vb1 = 10*(i-1);
     
    620716    double SumErrBin = 0;
    621717    double SumErrNBin = 0;
    622     for (int ic = 0; ic < icount; ic++){
     718    for (int ic = 0; ic < m_icount; ic++){
    623719      double mt  = mtV[ic];
    624720      double errV  = err[ic];
     
    630726    }
    631727    h[4]->SetBinContent(i,sW);
    632     h[4]->SetBinError(i,sqrt(SumErrBin));
    633     h[6]->SetBinError(i,sqrt(SumErrNBin));
     728    if (SumErrBin < 0) cout << "Warning negative error squared in bin " << i << " for sP" << endl;
     729    h[4]->SetBinError(i,sqrt(fabs(SumErrBin)));
     730    if (SumErrNBin < 0) cout << "Warning negative error squared in bin " << i << " for esP" << endl;
     731    h[6]->SetBinError(i,sqrt(fabs(SumErrNBin)));
    634732  }
    635733 
     
    642740  for (int i = 0; i < 7; i++) {
    643741    //h[i]->Rebin(2);
    644     h[i]->SetLineWidth(3);
     742    h[i]->SetLineWidth(2);
    645743    TAxis *ax = h[i]->GetXaxis();
    646744    int b1 = ax->FindBin(x1);
     
    650748
    651749  //plot for sPlot with M0 known
    652   TCanvas *csP = new TCanvas("csP", "", 800, 400);
     750  TCanvas *csP = new TCanvas("csP", "csP", 800, 400);
    653751  csP->Divide(2);
    654752  csP->cd(1);
     
    659757  h[1]->Draw("same");
    660758  h[4]->SetLineColor(1);
    661   h[4]->SetLineWidth(3);
     759  h[4]->SetLineWidth(2);
    662760  h[4]->DrawCopy("hist,same");
    663761  csP->cd(2);
     
    665763
    666764  //plot for sPlot with M0 UNknown
    667   TCanvas *cesP = new TCanvas("cesP", "", 800,400);
     765  TCanvas *cesP = new TCanvas("cesP", "cesP", 800,400);
    668766  cesP->Divide(2);
    669767  cesP->cd(1);
     
    672770  h[6]->DrawCopy("E1");
    673771  h[6]->SetLineColor(1);
    674   h[6]->SetLineWidth(3);
     772  h[6]->SetLineWidth(2);
    675773  h[6]->Draw("hist,same");
    676774  cesP->cd(2);
Note: See TracChangeset for help on using the changeset viewer.