//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void NormalizeHwwNtuple(const string InputFilename, const string datasetName, 
                        const string OutputFilename, const int nsel) {

  //TTree* HwwTree = getTreeFromFile(InputFilename.c_str(),nsel);
  //assert(HwwTree);

  SmurfTree sigEvent;
  sigEvent.LoadTree(InputFilename.c_str(),nsel);
  sigEvent.InitTree(0);
  sigEvent.tree_->SetName("tree");

  Double_t normalizationWeight = getNormalizationWeight(InputFilename, datasetName);

  //*************************************************************************************************
  //Create new normalized tree
  //*************************************************************************************************
  TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
  outputFile->cd();

  TTree *normalizedTree = sigEvent.tree_->CloneTree(0);
  
  cout << "Events in the ntuple: " << sigEvent.tree_->GetEntries() << endl;
  
  for (int n=0;n<sigEvent.tree_->GetEntries();n++) { 
    sigEvent.tree_->GetEntry(n);
    if     (normalizationWeight < 0){
      sigEvent.scale1fb_ = sigEvent.scale1fb_ * 1.0;
    }
    else if(nsel == 0){
      sigEvent.scale1fb_ = sigEvent.scale1fb_ * normalizationWeight *  1.0;
    }
    else if(nsel == 1){
      sigEvent.scale1fb_ = sigEvent.scale1fb_ * normalizationWeight *  1.0;
    }
    else {
      sigEvent.scale1fb_ = sigEvent.scale1fb_ * normalizationWeight *  1.0;
    }
    normalizedTree->Fill(); 
  }

  normalizedTree->Write();
  outputFile->Close();
}
//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void SkimGoodLumiList(const string InputFilename, const string OutputFilename, Int_t type,
                      const string GoodLumiFile) {

  mithep::RunLumiRangeMap rlrm;
  rlrm.AddJSONFile(GoodLumiFile.c_str()); 

  TTree* HwwTree = getTreeFromFile(InputFilename.c_str());
  assert(HwwTree);
  SmurfTree sigEvent;
  sigEvent.LoadTree(InputFilename.c_str(),type);
  sigEvent.InitTree(0);
  sigEvent.tree_->SetName("tree");

  //*************************************************************************************************
  //Create new normalized tree
  //*************************************************************************************************
  TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
  outputFile->cd();

  TTree *normalizedTree = sigEvent.tree_->CloneTree(0);
  
  cout << "Events in the ntuple: " << sigEvent.tree_->GetEntries() << endl;
  
  for (int n=0;n<sigEvent.tree_->GetEntries();n++) { 
    sigEvent.tree_->GetEntry(n);
    if (n%100000==0) cout << "Processed Event " << n << endl;    

    Bool_t passSkim = kFALSE;
    mithep::RunLumiRangeMap::RunLumiPairType rl(sigEvent.run_, sigEvent.lumi_);      
    if( sigEvent.dstype_ !=0 || rlrm.HasRunLumi(rl) ) passSkim = kTRUE;

    if (passSkim) {
      normalizedTree->Fill(); 
    }
  }
  cout << "Events in output ntuple: " << normalizedTree->GetEntries() << endl;
  normalizedTree->Write();
  outputFile->Close();
}
//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void ModifySmurfNtupleDatasetType(const string InputFilename, const string OutputFilename, 
                                  Int_t type, string TargetDatasetType ) {

  TTree* HwwTree = getTreeFromFile(InputFilename.c_str());
  assert(HwwTree);
  SmurfTree sigEvent;
  sigEvent.LoadTree(InputFilename.c_str(),type);
  sigEvent.InitTree(0);
  sigEvent.tree_->SetName("tree");

  //*************************************************************************************************
  //Create new normalized tree
  //*************************************************************************************************
  cout << "Output File : " << OutputFilename << endl;
  TFile *outputFile = new TFile(OutputFilename.c_str(), "RECREATE");
  outputFile->cd();
  TTree *normalizedTree = sigEvent.tree_->CloneTree(0);
  
  cout << "Events in the ntuple: " << sigEvent.tree_->GetEntries() << endl;
  
  for (int n=0;n<sigEvent.tree_->GetEntries();n++) { 
    sigEvent.tree_->GetEntry(n);
    if (n%100000==0) cout << "Processed Event " << n << endl;    

    if (TargetDatasetType == "qqww_MCAtNLO_ScaleUp") {
      sigEvent.dstype_ = SmurfTree::ggww;
    } 
    if (TargetDatasetType == "qqww_MCAtNLO_ScaleDown") {
      sigEvent.dstype_ = SmurfTree::ggzz;
    } 

    normalizedTree->Fill(); 
  }
  cout << "Events in output ntuple: " << normalizedTree->GetEntries() << endl;
  normalizedTree->Write();
  outputFile->Close();
}
Example #4
0
void wcuts(int nsel = 0) {
  if(nsel == 0 || nsel == 1){ // 0 == RECO vs. GEN, 1 == RECO1 vs. RECO2
    double sumRECO = 0.0;
    double sumGEN = 0.0;

    SmurfTree bgdEvent;
    bgdEvent.LoadTree("newfile_reco.root",0);
    bgdEvent.InitTree(0);
    for(int i0 = 0; i0 < bgdEvent.tree_->GetEntries(); i0++) {
      if(i0 % 10000 == 0) std::cout << "=== RECO Processed ===> " << i0 << std::endl;
      bgdEvent.tree_->GetEntry(i0);
      sumRECO = sumRECO + bgdEvent.scale1fb_;
    }

    TFile *iFilePDF = new TFile("newfile_gen.root");
    if     (nsel == 0){
      TTree *iTreePDF = (TTree*) iFilePDF->FindObjectAny("PDFTree");
      MitPDFNtupleEvent lMitPDFNtupleEvent(iTreePDF);
      for(int i0 = 0; i0 < iTreePDF->GetEntries(); i0++) {
	if(i0 % 10000 == 0) std::cout << "=== GEN Processed ===> " << i0 << std::endl;
	iTreePDF->GetEntry(i0);
	sumGEN = sumGEN + lMitPDFNtupleEvent.H_weight;
      }
    }
    else if(nsel == 1){
      SmurfTree genEvent;
      genEvent.LoadTree("newfile_gen.root",0);
      genEvent.InitTree(0);
      for(int i0 = 0; i0 < genEvent.tree_->GetEntries(); i0++) {
	if(i0 % 10000 == 0) std::cout << "=== GEN-RECO Processed ===> " << i0 << std::endl;
	genEvent.tree_->GetEntry(i0);
	sumGEN = sumGEN + genEvent.scale1fb_;
      }
    }

    ofstream  oFile("compare.txt");
    std::cout << setprecision (25) << sumRECO/sumGEN << " " << setprecision (25) << sumRECO << std::endl;
    oFile     << setprecision (25) << sumRECO/sumGEN << " " << setprecision (25) << sumRECO << std::endl;
    oFile.close();
  }
}
Example #5
0
void Content() {
  
  bool signal = false;
  TString bgdInputFile    = "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/backgroundA_3l.root";
  //TString bgdInputFile   =  "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/zhww125.root";

  
  //Load datasets
  SmurfTree background;
  background.LoadTree(bgdInputFile,-1);
  background.InitTree(0);
  
  char output[200];
  sprintf(output,"rootfiles/composition_study.root");     
  TFile* outFileNjets = new TFile(output,"recreate");
 
 
  TH1D* bck_cuts = new TH1D("bck_cuts", "cuts", 10, 0, 10);
  
  bck_cuts->Sumw2();
   double eventsPassBck = 0;
   double weight = 1;
     //Backgrounds
  double bckType[62] = {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.};

  double weiType[62] = {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,
			0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.};
  
  TString bckName[62] = {"null","null","null","null","null","null","null","null","null","null","null","null","null","null","null",
                         "null","null","null","null","null","null","null","null","null","null","null","null","null","null","null",
                         "null","null","null","null","null","null","null","null","null","null","null","null","null","null","null",
                         "null","null","null","null","null","null","null","null","null","null","null","null","null","null","null", "null", "fakes"};
  
  bckName[0] = "data";
  bckName[1] = "qqww";
  bckName[2] = "ggww";
  bckName[43] = "ttbar";
  bckName[44] = "tw";
  bckName[46] = "dymm";
  bckName[49] = "wz";
  bckName[50] = "zz";
  bckName[51] = "wgamma";
  bckName[59] = "www";
  bckName[60] = "dyttdd";   
  int nBck=background.tree_->GetEntries();
  for (int i=0; i<nBck; ++i) {
    
    
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nBck);
    background.tree_->GetEntry(i);
 
 
    if(signal && background.processId_ != 24) continue;

    weight = 1;
    if (background.dstype_ != SmurfTree::data) weight = lumi*background.scale1fb_*background.sfWeightPU_*background.sfWeightEff_*background.sfWeightTrig_;    

    int nsel = background.dstype_;
  
    //Three real leptons MC level
    bool isRealLepton = false;
    if((TMath::Abs(background.lep1McId_) == 11 || TMath::Abs(background.lep1McId_) == 13) &&
       (TMath::Abs(background.lep2McId_) == 11 || TMath::Abs(background.lep2McId_) == 13) &&
       (TMath::Abs(background.lep3McId_) == 11 || TMath::Abs(background.lep3McId_) == 13)) isRealLepton = true; 
    if (!isRealLepton && background.dstype_ != SmurfTree::data) continue;
     
    //Check for fakes
    int nFake = 0;
    if(((background.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (background.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (background.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (background.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (background.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (background.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (background.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
   
    if (nFake){ 
      nsel = 61;
      double factor = 1;
      weight*= background.sfWeightFR_*factor;
      //if (background.dstype_ != SmurfTree::data) weight *=-1;
    }
    
    bck_cuts->Fill(0., weight);
    if (signal & nFake) continue;
    
    
    
    //2 same flavor, oppposite sign leptons + extra one
    if (background.lid3_ == background.lid2_ && background.lid3_ == background.lid1_) continue;
    if (background.lid3_ == background.lid2_ && fabs(background.lid3_) != fabs(background.lid1_)) continue;
    if (background.lid3_ == background.lid1_ && fabs(background.lid3_) != fabs(background.lid2_)) continue;
    if (background.lid2_ == background.lid1_ && fabs(background.lid2_) != fabs(background.lid3_)) continue;
    
    //Select the different things: Z pair, extra lepton, Higgs system
    int idcat = 20; // 0 = eee, 1 = eemu, 2 = mumue, 3 = mumumu
    if (fabs(background.lid1_) == 11 && fabs(background.lid2_) == 11 && fabs(background.lid3_) == 11) idcat = 0;
    else if ((fabs(background.lid1_) == 11 && fabs(background.lid2_) == 11 && fabs(background.lid3_) == 13) 
          || (fabs(background.lid1_) == 11 && fabs(background.lid2_) == 13 && fabs(background.lid3_) == 11) 
	  || (fabs(background.lid1_) == 13 && fabs(background.lid2_) == 11 && fabs(background.lid3_) == 11)) idcat = 1;
    else if ((fabs(background.lid1_) == 11 && fabs(background.lid2_) == 13 && fabs(background.lid3_) == 13) 
          || (fabs(background.lid1_) == 13 && fabs(background.lid2_) == 13 && fabs(background.lid3_) == 11) 
	  || (fabs(background.lid1_) == 13 && fabs(background.lid2_) == 11 && fabs(background.lid3_) == 13)) idcat = 2;
    else  if (fabs(background.lid1_) == 13 && fabs(background.lid2_) == 13 && fabs(background.lid3_) == 13)  idcat = 3;
    
   
       
     bck_cuts->Fill(1., weight);
    
    //At least 2 jets
    if (background.njets_ < 2 ) continue; 
    bck_cuts->Fill(2., weight);
   
    
    //Make z-compatible pairs
    double m[3] = {0, 0, 0};
    LorentzVector pair1, pair2, pair3, trilep;
    if (fabs(background.lid1_) == fabs(background.lid2_) && background.lq1_*background.lq2_ < 0){
      pair1 = background.lep1_ + background.lep2_ ;
      m[0] = pair1.M();
    }
    if (fabs(background.lid2_) == fabs(background.lid3_) && background.lq2_*background.lq3_ < 0){
      pair2 = background.lep2_ + background.lep3_ ;
      m[1] = pair2.M();
    }
    if (fabs(background.lid1_) == fabs(background.lid3_) && background.lq1_*background.lq3_ < 0){
      pair3 = background.lep1_ + background.lep3_ ;
      m[2] = pair3.M();
    }
    trilep = background.lep1_ + background.lep2_ + background.lep3_ ;

    
    //Get the closest to the Z mass
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
    
    //Select the different things: Z pair, extra lepton, Higgs system
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
    double dR = 0;
    if (min == fabs(mz - m[0])) {  pair = pair1; mt =  background.mt3_; tlepton = background.lep3_; dR = fabs(ROOT::Math::VectorUtil::DeltaR(background.lep1_ ,background.lep2_));} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  background.mt1_; tlepton = background.lep1_; dR = fabs(ROOT::Math::VectorUtil::DeltaR(background.lep2_ ,background.lep3_));} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  background.mt2_; tlepton = background.lep2_; dR = fabs(ROOT::Math::VectorUtil::DeltaR(background.lep1_ ,background.lep3_));} 
    pairjet = background.jet1_+ background.jet2_;
    LorentzVector metvector(background.met_*cos(background.metPhi_), background.met_*sin(background.metPhi_), 0, 0);
    LorentzVector higgsSystem = tlepton + metvector + background.jet1_+ background.jet2_;
    LorentzVector lm = tlepton + metvector;
   
      
    double hp[5];
    hp[0] = tlepton.Px() + background.jet1_.Px()+ background.jet2_.Px()+ metvector.Px();
    hp[1] = tlepton.Py() + background.jet1_.Py()+ background.jet2_.Py()+ metvector.Py();
    hp[2] = tlepton.Pz() + background.jet1_.Pz()+ background.jet2_.Pz()+ metvector.Pz();
    
    //Calculate p of the neutrino using Maria's code
    double metp = 0;
    double otherSol = 0;
    double alpha=(mw*mw-mmu*mmu)/2/tlepton.P()+(tlepton.Px()*background.met_*cos(background.metPhi_)+tlepton.Py()*background.met_*sin(background.metPhi_))/tlepton.P();
    double A=tlepton.Pz()*tlepton.Pz()/tlepton.P()/tlepton.P()-1;
    double B=2*alpha*tlepton.Pz()/tlepton.P();
    double C=alpha*alpha-(background.met_*cos(background.metPhi_)*background.met_*cos(background.metPhi_) + background.met_*sin(background.metPhi_)*background.met_*sin(background.metPhi_));
    bool isComplex = false;
    double tmproot = B*B - 4.0*A*C;
      if (tmproot<0) { 
        isComplex= true;
        metp = - B/(2*A); 
	otherSol = metp;
      } else {
        isComplex = false;
	double tmpsol1 = (-B + TMath::Sqrt(tmproot))/(2.0*A);
	double tmpsol2 = (-B - TMath::Sqrt(tmproot))/(2.0*A);
	if (TMath::Abs(tmpsol1)<TMath::Abs(tmpsol2) ) {
	  metp = tmpsol1; otherSol = tmpsol2; 
	} else { metp = tmpsol2; otherSol = tmpsol1; }
     }
   
    
   // hp[3] = tlepton.P() + background.jet1_.P()+ background.jet2_.P()+ metvector.P(); //crappy solution
    hp[3] = tlepton.P() + background.jet1_.P()+ background.jet2_.P()+ metp;
    hp[4] = tlepton.Pt() + background.jet1_.Pt()+ background.jet2_.Pt()+ background.met_;
    
    double recomh  = hp[3]*hp[3]-hp[0]*hp[0]-hp[1]*hp[1]-hp[2]*hp[2]; if(recomh  > 0) recomh  = sqrt(recomh);else recomh   = 0.0;
    double recomth = hp[4]*hp[4]-hp[0]*hp[0]-hp[1]*hp[1]; if(recomth > 0) recomth = sqrt(recomth); else recomth  = 0.0;
    
 
    //Kinematic cuts
    if (pair.M() < (mz - separation)|| pair.M() > (mz + separation)) continue; 
    bck_cuts->Fill(3., weight); 
       
    
    if (background.met_ < metcut) continue;
    bck_cuts->Fill(4., weight);
       
     
    
    if (mt > mtcut) continue;
    bck_cuts->Fill(5., weight);
    
    
    if (pairjet.M() < (mw - separationjj) || pairjet.M() > (mw + separationjj)) continue;
    bck_cuts->Fill(6., weight);
  
   //double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),tlepton.Phi()));
    double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),lm.Phi()));
    if (deltaPhi > phicut) continue;
    bck_cuts->Fill(7., weight);
  
    eventsPassBck += weight;
    
	     
    bckType[(int)nsel] += weight;
    weiType[(int)nsel] += weight*weight;	
   

  }
  
  cout << endl;
  cout << eventsPassBck << " background events in " << lumi << " fb" << endl; 
  cout << endl;


  
  if (verboseLevel){ 
    cout << "------------------------------------------" << endl;
    cout << "[Backgrounds (All mixed):] " << endl;
    cout << "------------------------------------------" << endl;  
    for (int i = 1; i < 9; i++){
      if (i == 1) cout << " 3 lep:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 2) cout << " OSSF:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 3) cout << " 2 jet:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 4) cout << " mll:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 5) cout << " met:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 6) cout << " mt:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 7) cout << " mjj:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
      if (i == 8) cout << " phi:\t\t" <<  bck_cuts->GetBinContent(i) << " +/-  " <<  bck_cuts->GetBinError(i)  << endl;
    }
    cout << endl;
    cout << "[Breakdown:] " << endl;
    for(int i=0; i<62; i++){
      if(bckType[i] != 0 )
	cout << i <<"\t" << bckName[i] << ":\t\t" << bckType[i] << "+-" << sqrt(weiType[i]) <<endl;
    }
    cout << "------------------------------------------" << endl; 
  }
  
  outFileNjets->Write();
  outFileNjets->Close();
  
}
Example #6
0
void chain(int cem = 8, int nsel = 0, double mh = 125, int mode = 0){

  char plotName[300];
  sprintf(plotName,"test");
  bool isBackground = true;
  bool isData = false;
		  
  if (nsel == 0)                	{sprintf(plotName,"Data");	isBackground = false;	isData = true;}
  else if (nsel == 1)   		{sprintf(plotName,"ZH");	isBackground = false;}
  else if (nsel == 2)   		{sprintf(plotName,"WZ");}
  else if (nsel == 3)   		{sprintf(plotName,"ZZ");}
  else if (nsel == 4)   		{sprintf(plotName,"VVV");}
  else if (nsel == 5)			{sprintf(plotName,"Wjets");}
  else if (nsel == 6) 			{sprintf(plotName, "all");}
  else if (nsel == 7) 			{sprintf(plotName, "ZH_SM");	isBackground = false;} //no fakes allowed
				  
  char myRootFile[300];
  if (cem != 7 && cem !=8) cem = 8;
  double lumi = lumi8;
 if (cem == 8){
    if (nsel == 0) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/data_3l.root");
    else if (nsel == 7) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/zhww125.root");
    else if (nsel == 1 && (mh == 125 || mh == 124 || mh == 126 || mh == 125.7)) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/zhww125.root");
    else if (nsel == 1 && (mh == 118 || mh == 122)) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/hww120.root");
    else if (nsel == 1 &&  mh == 128) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/hww130.root");
    else if (nsel == 1) sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/hww%g.root", mh);
    else sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/backgroundA_3l.root");
  } else {
    lumi = lumi7;
    if (nsel == 0) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/data_3l.root");
    else if (nsel == 7) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/zhww125.root");
    else if (nsel == 1 && (mh == 125 || mh == 125.7)) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/zhww125.root");
    else if (nsel == 1 && (mh == 110 || mh == 115)) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/vtthww118.root");
    else if (nsel == 1 && mh == 145) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/vtthww140.root");
    else if (nsel == 1) sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/vtthww%g.root", mh);
    else sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/backgroundA_3l.root");
  }
  
  cout << "[Info:] "<< cem <<  "TeV, " << plotName << ", Higgs mass " << mh << ","  ;
  if (mode == 1) cout << " eee channel" << endl ;
  else if (mode == 2) cout << " eem channel" << endl ; 
  else if (mode == 3) cout << " emm channel" << endl ; 
  else if (mode == 4) cout << " mmm channel" << endl ; 
  else cout << " all final states" << endl;
 								    
  //Load datasets
  SmurfTree sample;
  cout << myRootFile << endl;
  sample.LoadTree(myRootFile,-1);
  sample.InitTree(0);

  // Prepare output file
  char rootFile[300];
  if (mode == 1) sprintf(rootFile,"%g/zh3l2j_input_shape_eee_%dTeV.root", mh, cem);
  else if (mode == 2) sprintf(rootFile,"%g/zh3l2j_input_shape_eem_%dTeV.root", mh, cem);
  else if (mode == 3) sprintf(rootFile,"%g/zh3l2j_input_shape_emm_%dTeV.root", mh, cem);
  else if (mode == 4) sprintf(rootFile,"%g/zh3l2j_input_shape_mmm_%dTeV.root", mh, cem);
  else sprintf(rootFile,"%g/zh3l2j_input_shape_%dTeV.root", mh, cem);
  
  TFile f_root(rootFile, "UPDATE");
											      
  // Prepare histograms
  char title[300];
												    
  sprintf(title,"histo_%s",plotName);
  TH1F* histo = new TH1F( title, " ", nbins, nbinlow, nbinhigh);
  histo->Sumw2();

  //Prepare useful things
  double weight = 1;
  double eventsPass = 0;
														  
  int nSample=sample.tree_->GetEntries();
  for (int i=0; i<nSample; ++i) {
														           
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nSample);
    sample.tree_->GetEntry(i);
    
    if(nsel == 1 && sample.processId_ != 24) continue;
    if(nsel == 7 && sample.processId_ != 24) continue;

    //Modes, 0 = all, 1 = eee, 2 = eem, 3 = emm, 4 = mmm
    if (mode == 1 && (abs(sample.lid1_)!= 11 || abs(sample.lid2_) != 11 || abs(sample.lid3_) != 11)) continue;
    if (mode == 2 && 
       ((abs(sample.lid1_)!= abs(sample.lid2_) && abs(sample.lid1_) != abs(sample.lid3_) && abs(sample.lid1_) == 11) ||
        (abs(sample.lid2_)!= abs(sample.lid1_) && abs(sample.lid2_) != abs(sample.lid3_) && abs(sample.lid2_) == 11) ||
	(abs(sample.lid3_)!= abs(sample.lid1_) && abs(sample.lid3_) != abs(sample.lid2_) && abs(sample.lid3_) == 11) ||
	(abs(sample.lid1_) == abs(sample.lid2_) && abs(sample.lid1_) == abs(sample.lid3_)))) continue;
    if (mode == 3 && 
       ((abs(sample.lid1_)!= abs(sample.lid2_) && abs(sample.lid1_) != abs(sample.lid3_) && abs(sample.lid1_) == 13) ||
        (abs(sample.lid2_)!= abs(sample.lid1_) && abs(sample.lid2_) != abs(sample.lid3_) && abs(sample.lid2_) == 13) ||
	(abs(sample.lid3_)!= abs(sample.lid1_) && abs(sample.lid3_) != abs(sample.lid2_) && abs(sample.lid3_) == 13) ||
	(abs(sample.lid1_) == abs(sample.lid2_) && abs(sample.lid1_) == abs(sample.lid3_)))) continue;
    if (mode == 4 && (abs(sample.lid1_)!= 13 || abs(sample.lid2_) != 13 || abs(sample.lid3_) != 13)) continue;
   
    				     
    weight = 1;
    if (!isData && sample.dstype_ != SmurfTree::data) weight = lumi*sample.scale1fb_*sample.sfWeightPU_*sample.sfWeightEff_*sample.sfWeightTrig_;    
      
    if (cem == 8 && nsel == 1 && mh == 118) weight *= ((0.472400*0.11800000)/(0.448300*0.14300000));  
    if (cem == 8 && nsel == 1 && mh == 122) weight *= ((0.425700*0.17000000)/(0.448300*0.14300000));  
    if (cem == 8 && nsel == 1 && mh == 124) weight *= ((0.404400*0.200000)/(0.394300*0.216000));    
    if (cem == 8 && nsel == 1 && mh == 126) weight *= ((0.384300*0.233000)/(0.394300*0.216000));   
    if (cem == 8 && nsel == 1 && mh == 128) weight *= ((0.365200*0.26800000)/(0.347300*0.30500000));  
    if (cem == 8 && nsel == 1 && mh == 127.5) weight *= ((0.387300*0.226200)/(0.394300*0.216000)); 
    
    if (cem == 7 && nsel == 1 && mh == 110) weight *= (0.02251917/0.0443547);
    if (cem == 7 && nsel == 1 && mh == 115) weight *= (0.03532622/0.0443547);
    if (cem == 7 && nsel == 1 && mh == 145) weight *= ((0.193000*0.600000)/(0.217200*0.501000));
    if (cem == 7 && nsel == 1 && mh == 125.7) weight *= ((0.215000*0.315800)/(0.226200*0.310100));
    
   
   //Three real leptons MC level
    if (!isData){
      bool isRealLepton = false;
      if((TMath::Abs(sample.lep1McId_) == 11 || TMath::Abs(sample.lep1McId_) == 13) &&
	 (TMath::Abs(sample.lep2McId_) == 11 || TMath::Abs(sample.lep2McId_) == 13) &&
	 (TMath::Abs(sample.lep3McId_) == 11 || TMath::Abs(sample.lep3McId_) == 13)) isRealLepton = true; 
      if (!isRealLepton && !isBackground) continue; //signal
      if (!isRealLepton && sample.dstype_ != SmurfTree::data) continue; //background
    }
																									         
    int ntype = sample.dstype_;
    
    //Check for fakes
    int nFake = 0;
    if(((sample.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (sample.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (sample.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (sample.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if (nFake !=0 && !isBackground) continue; 
    if (nFake !=0){ 
      ntype = 61;
      weight*= sample.sfWeightFR_*factor;
      //if (sample.dstype_ != SmurfTree::data) weight *=-1;
    }
    
    
    //2 same flavor, oppposite sign leptons + extra one
    if (sample.lid3_ == sample.lid2_ && sample.lid3_ == sample.lid1_) continue;
    if (sample.lid3_ == sample.lid2_ && fabs(sample.lid3_) != fabs(sample.lid1_)) continue;
    if (sample.lid3_ == sample.lid1_ && fabs(sample.lid3_) != fabs(sample.lid2_)) continue;
    if (sample.lid2_ == sample.lid1_ && fabs(sample.lid2_) != fabs(sample.lid3_)) continue;
    
    // At least 2 jets 
    if (sample.njets_ < 2) continue; 
    if (tau && (sample.jet1McId_ == 100 || sample.jet2McId_ == 100 || sample.jet3McId_ == 100 || sample.jet4McId_ == 100)) continue;

    //Make z-compatible pairs
    double m[3] = {-1, -1, -1};
    LorentzVector pair1, pair2, pair3;
    if (fabs(sample.lid1_) == fabs(sample.lid2_) && sample.lq1_*sample.lq2_ < 0){
      pair1 = sample.lep1_ + sample.lep2_ ;
      m[0] = pair1.M();
      if (m[0] < 12) continue;
    }
    if (fabs(sample.lid2_) == fabs(sample.lid3_) && sample.lq2_*sample.lq3_ < 0){
      pair2 = sample.lep2_ + sample.lep3_ ;
      m[1] = pair2.M();
      if (m[1] < 12) continue;
    }
    if (fabs(sample.lid1_) == fabs(sample.lid3_) && sample.lq1_*sample.lq3_ < 0){
      pair3 = sample.lep1_ + sample.lep3_ ;
      m[2] = pair3.M();
      if (m[2] < 12) continue;
    }
    if ( (m[0] > 0 && m[0] < 12) || (m[1] > 0 && m[1] < 12) || (m[2] > 0 && m[2] < 12)) continue;
    
    LorentzVector trelep = sample.lep1_ + sample.lep2_ + sample.lep3_;
    if (fabs(trelep.M() - mz) < 10) continue; 
																																																	         
    //Get the closest to the Z mass
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
    
    //Select the different things: Z pair, extra lepton, Higgs system
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
    // double dR = 0; //dR = fabs(ROOT::Math::VectorUtil::DeltaR(sample.lep1_ ,sample.lep2_)) etc
    if (min == fabs(mz - m[0])) {  pair = pair1; mt =  sample.mt3_; tlepton = sample.lep3_;} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  sample.mt1_; tlepton = sample.lep1_;} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  sample.mt2_; tlepton = sample.lep2_;} 
    pairjet = sample.jet1_+ sample.jet2_;
    LorentzVector metvector(sample.met_*cos(sample.metPhi_), sample.met_*sin(sample.metPhi_), 0, 0);
    LorentzVector higgsSystem = tlepton + metvector + sample.jet1_+ sample.jet2_;
    LorentzVector lm = tlepton + metvector;
    
    
    double hp[5];
    hp[0] = tlepton.Px() + sample.jet1_.Px()+ sample.jet2_.Px()+ metvector.Px();
    hp[1] = tlepton.Py() + sample.jet1_.Py()+ sample.jet2_.Py()+ metvector.Py();
    hp[2] = tlepton.Pz() + sample.jet1_.Pz()+ sample.jet2_.Pz()+ metvector.Pz();
    
    //Calculate p of the neutrino using Maria's code
    double metp = 0;
    // double otherSol = 0;
    double alpha=(mw*mw-mmu*mmu)/2/tlepton.P()+(tlepton.Px()*sample.met_*cos(sample.metPhi_)+tlepton.Py()*sample.met_*sin(sample.metPhi_))/tlepton.P();
    double A=tlepton.Pz()*tlepton.Pz()/tlepton.P()/tlepton.P()-1;
    double B=2*alpha*tlepton.Pz()/tlepton.P();
    double C=alpha*alpha-(sample.met_*cos(sample.metPhi_)*sample.met_*cos(sample.metPhi_) + sample.met_*sin(sample.metPhi_)*sample.met_*sin(sample.metPhi_));
    // bool isComplex = false;
    double tmproot = B*B - 4.0*A*C;
    if (tmproot<0) { 
      //isComplex= true;
      metp = - B/(2*A); 
      //otherSol = metp;
    } else {
      // isComplex = false;
      double tmpsol1 = (-B + TMath::Sqrt(tmproot))/(2.0*A);
      double tmpsol2 = (-B - TMath::Sqrt(tmproot))/(2.0*A);
      if (TMath::Abs(tmpsol1)<TMath::Abs(tmpsol2) ) {
	metp = tmpsol1; 
	//otherSol = tmpsol2; 
      } else { 
	metp = tmpsol2; 
	//otherSol = tmpsol1; 
      }
    }
    
    
    // hp[3] = tlepton.P() + sample.jet1_.P()+ sample.jet2_.P()+ metvector.P(); //crappy solution
    hp[3] = tlepton.P() + sample.jet1_.P()+ sample.jet2_.P()+ metp;
    hp[4] = tlepton.Pt() + sample.jet1_.Pt()+ sample.jet2_.Pt()+ sample.met_;
    
    double recomh  = hp[3]*hp[3]-hp[0]*hp[0]-hp[1]*hp[1]-hp[2]*hp[2]; if(recomh  > 0) recomh  = sqrt(recomh);else recomh   = 0.0;
    double recomth = hp[4]*hp[4]-hp[0]*hp[0]-hp[1]*hp[1]; if(recomth > 0) recomth = sqrt(recomth); else recomth  = 0.0;
    
    
    //Kinematic cuts
    if (pair.M() < (mz - separation)|| pair.M() > (mz + separation)) continue; 
    if (sample.met_ < metcut) continue;
    if (mt > mtcut) continue;
    if (pairjet.M() < (mw - separationjj) || pairjet.M() > (mw + separationjj)) continue;
    
    //double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),tlepton.Phi()));
    double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),lm.Phi()));
    if (deltaPhi > phicut) continue;
    
    
    if (nsel == 2 && ntype != 49) continue; //WZ
    if (nsel == 3 && ntype != 50) continue; //ZZ
    if (nsel == 4 && ntype != 59) continue; //VVV
    if (nsel == 5 && ntype != 61) continue; //fakes
    if (nsel == 0 && ntype != 0)  continue; //data
    
    
    
      histo->Fill(recomth, weight);
    //histo->Fill(higgsSystem.M(), weight);
    // histo->Fill(1, weight);
    eventsPass+= weight;
    
    
  }    
  
  cout << "[Info:] (" << plotName << ") " <<  eventsPass << " events pass, check " << histo->GetBinContent(1) << endl;
  
  
  f_root.Write();
  f_root.Close();
  
}
Example #7
0
void doWZ(int cem = 8, int mode = 0){

  char plotName[300];
  sprintf(plotName,"test");
  
  sprintf(plotName,"WZ");
  bool isBackground = true;
  bool isData = false;
  int nsel = 2;
  
  char myRootFile[300];
  if (cem != 7 && cem !=8) cem = 8;
  double lumi = lumi8;
  if (cem == 8){
    sprintf(myRootFile,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/backgroundA_3l.root");
  } else {
    lumi = lumi7;
    sprintf(myRootFile,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets/backgroundA_3l.root");
  }
  //Load datasets
  SmurfTree sample;
  sample.LoadTree(myRootFile,-1);
  sample.InitTree(0);

  // Prepare putput file
  char rootFile[300];
  if (cem == 8) sprintf(rootFile,"WZ8TeV.root");
  else sprintf(rootFile,"WZ7TeV.root");

  TFile f_root(rootFile, "RECREATE");
  
  // Prepare histograms
  char title[300];
  
  sprintf(title,"histogram");
  TH1F* histo = new TH1F( title, " ", nbins, nbinlow, nbinhigh);
  histo->Sumw2();

  //Prepare useful things
  double weight = 1;
  double eventsPass = 0;
  
  int nSample=sample.tree_->GetEntries();
   for (int i=0; i<nSample; ++i) {
    
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nSample);
    sample.tree_->GetEntry(i);
    
      //Modes, 0 = all, 1 = eee, 2 = eem, 3 = emm, 4 = mmm
    if (mode == 1 && (abs(sample.lid1_)!= 11 || abs(sample.lid2_) != 11 || abs(sample.lid3_) != 11)) continue;
    if (mode == 2 && 
       ((abs(sample.lid1_)!= abs(sample.lid2_) && abs(sample.lid1_) != abs(sample.lid3_) && abs(sample.lid1_) == 11) ||
        (abs(sample.lid2_)!= abs(sample.lid1_) && abs(sample.lid2_) != abs(sample.lid3_) && abs(sample.lid2_) == 11) ||
	(abs(sample.lid3_)!= abs(sample.lid1_) && abs(sample.lid3_) != abs(sample.lid2_) && abs(sample.lid3_) == 11) ||
	(abs(sample.lid1_) == abs(sample.lid2_) && abs(sample.lid1_) == abs(sample.lid3_)))) continue;
    if (mode == 3 && 
       ((abs(sample.lid1_)!= abs(sample.lid2_) && abs(sample.lid1_) != abs(sample.lid3_) && abs(sample.lid1_) == 13) ||
        (abs(sample.lid2_)!= abs(sample.lid1_) && abs(sample.lid2_) != abs(sample.lid3_) && abs(sample.lid2_) == 13) ||
	(abs(sample.lid3_)!= abs(sample.lid1_) && abs(sample.lid3_) != abs(sample.lid2_) && abs(sample.lid3_) == 13) ||
	(abs(sample.lid1_) == abs(sample.lid2_) && abs(sample.lid1_) == abs(sample.lid3_)))) continue;
    if (mode == 4 && (abs(sample.lid1_)!= 13 || abs(sample.lid2_) != 13 || abs(sample.lid3_) != 13)) continue;
   

    
    weight = 1;
    if (!isData && sample.dstype_ != SmurfTree::data) weight = lumi*sample.scale1fb_*sample.sfWeightPU_*sample.sfWeightEff_*sample.sfWeightTrig_;    
   
   //Three real leptons MC level
    if (!isData){
      bool isRealLepton = false;
      if((TMath::Abs(sample.lep1McId_) == 11 || TMath::Abs(sample.lep1McId_) == 13) &&
         (TMath::Abs(sample.lep2McId_) == 11 || TMath::Abs(sample.lep2McId_) == 13) &&
         (TMath::Abs(sample.lep3McId_) == 11 || TMath::Abs(sample.lep3McId_) == 13)) isRealLepton = true; 
      if (!isRealLepton && !isBackground) continue; //signal
      if (!isRealLepton && sample.dstype_ != SmurfTree::data) continue; //background
    }
    
    int ntype = sample.dstype_;
    
     //Check for fakes
    int nFake = 0;
    if(((sample.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (sample.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (sample.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (sample.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((sample.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (sample.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if (nFake !=0 && !isBackground) continue; 
    if (nFake !=0){ 
      ntype = 61;
      weight*= sample.sfWeightFR_*factor;
      //if (sample.dstype_ != SmurfTree::data) weight *=-1;
    }
    
    if (nsel == 2 && ntype != 49) continue; //WZ
    
    //2 same flavor, oppposite sign leptons + extra one
    if (sample.lid3_ == sample.lid2_ && sample.lid3_ == sample.lid1_) continue;
    if (sample.lid3_ == sample.lid2_ && fabs(sample.lid3_) != fabs(sample.lid1_)) continue;
    if (sample.lid3_ == sample.lid1_ && fabs(sample.lid3_) != fabs(sample.lid2_)) continue;
    if (sample.lid2_ == sample.lid1_ && fabs(sample.lid2_) != fabs(sample.lid3_)) continue;
    
    // At least 2 jets 
    if (sample.njets_ < 2) continue; 
    if (tau && (sample.jet1McId_ == 100 || sample.jet2McId_ == 100 || sample.jet3McId_ == 100 || sample.jet4McId_ == 100)) continue;

       //Make z-compatible pairs
    double m[3] = {-1, -1, -1};
    LorentzVector pair1, pair2, pair3;
    if (fabs(sample.lid1_) == fabs(sample.lid2_) && sample.lq1_*sample.lq2_ < 0){
      pair1 = sample.lep1_ + sample.lep2_ ;
      m[0] = pair1.M();
      if (m[0] < 12) continue;
    }
    if (fabs(sample.lid2_) == fabs(sample.lid3_) && sample.lq2_*sample.lq3_ < 0){
      pair2 = sample.lep2_ + sample.lep3_ ;
      m[1] = pair2.M();
      if (m[1] < 12) continue;
    }
    if (fabs(sample.lid1_) == fabs(sample.lid3_) && sample.lq1_*sample.lq3_ < 0){
      pair3 = sample.lep1_ + sample.lep3_ ;
      m[2] = pair3.M();
      if (m[2] < 12) continue;
    }
    if ( (m[0] > 0 && m[0] < 12) || (m[1] > 0 && m[1] < 12) || (m[2] > 0 && m[2] < 12)) continue;
    				
   LorentzVector trelep = sample.lep1_ + sample.lep2_ + sample.lep3_;
   if (fabs(trelep.M() - mz) < 10) continue; 
						
    //Get the closest to the Z mass
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
   
    //Select the different things: Z pair, extra lepton, Higgs system
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
   // double dR = 0; //dR = fabs(ROOT::Math::VectorUtil::DeltaR(sample.lep1_ ,sample.lep2_)) etc
    if (min == fabs(mz - m[0])) {  pair = pair1; mt =  sample.mt3_; tlepton = sample.lep3_;} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  sample.mt1_; tlepton = sample.lep1_;} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  sample.mt2_; tlepton = sample.lep2_;} 
    pairjet = sample.jet1_+ sample.jet2_;
    LorentzVector metvector(sample.met_*cos(sample.metPhi_), sample.met_*sin(sample.metPhi_), 0, 0);
    LorentzVector higgsSystem = tlepton + metvector + sample.jet1_+ sample.jet2_;
    LorentzVector lm = tlepton + metvector;
   
      
    double hp[5];
    hp[0] = tlepton.Px() + sample.jet1_.Px()+ sample.jet2_.Px()+ metvector.Px();
    hp[1] = tlepton.Py() + sample.jet1_.Py()+ sample.jet2_.Py()+ metvector.Py();
    hp[2] = tlepton.Pz() + sample.jet1_.Pz()+ sample.jet2_.Pz()+ metvector.Pz();
    
    //Calculate p of the neutrino using Maria's code
    double metp = 0;
   // double otherSol = 0;
    double alpha=(mw*mw-mmu*mmu)/2/tlepton.P()+(tlepton.Px()*sample.met_*cos(sample.metPhi_)+tlepton.Py()*sample.met_*sin(sample.metPhi_))/tlepton.P();
    double A=tlepton.Pz()*tlepton.Pz()/tlepton.P()/tlepton.P()-1;
    double B=2*alpha*tlepton.Pz()/tlepton.P();
    double C=alpha*alpha-(sample.met_*cos(sample.metPhi_)*sample.met_*cos(sample.metPhi_) + sample.met_*sin(sample.metPhi_)*sample.met_*sin(sample.metPhi_));
   // bool isComplex = false;
    double tmproot = B*B - 4.0*A*C;
      if (tmproot<0) { 
        //isComplex= true;
        metp = - B/(2*A); 
	//otherSol = metp;
      } else {
       // isComplex = false;
	double tmpsol1 = (-B + TMath::Sqrt(tmproot))/(2.0*A);
	double tmpsol2 = (-B - TMath::Sqrt(tmproot))/(2.0*A);
	if (TMath::Abs(tmpsol1)<TMath::Abs(tmpsol2) ) {
	  metp = tmpsol1; 
	  //otherSol = tmpsol2; 
	} else { 
	  metp = tmpsol2; 
	  //otherSol = tmpsol1; 
	}
     }
   
    
   // hp[3] = tlepton.P() + sample.jet1_.P()+ sample.jet2_.P()+ metvector.P(); //crappy solution
    hp[3] = tlepton.P() + sample.jet1_.P()+ sample.jet2_.P()+ metp;
    hp[4] = tlepton.Pt() + sample.jet1_.Pt()+ sample.jet2_.Pt()+ sample.met_;
    
    double recomh  = hp[3]*hp[3]-hp[0]*hp[0]-hp[1]*hp[1]-hp[2]*hp[2]; if(recomh  > 0) recomh  = sqrt(recomh);else recomh   = 0.0;
    double recomth = hp[4]*hp[4]-hp[0]*hp[0]-hp[1]*hp[1]; if(recomth > 0) recomth = sqrt(recomth); else recomth  = 0.0;
    
   
    //Kinematic cuts
    if (pair.M() < (mz - separation)|| pair.M() > (mz + separation)) continue; 
    if (sample.met_ < metcut) continue;
    if (mt > mtcut) continue;
    if (pairjet.M() < (mw - separationjj) || pairjet.M() > (mw + separationjj)) continue;
    
   //double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),tlepton.Phi()));
    double deltaPhi = fabs(DeltaPhi(pairjet.Phi(),lm.Phi()));
    if (deltaPhi > phicut) continue;
   
    
    histo->Fill(recomth, weight);
    //histo->Fill(higgsSystem.M(), weight);
    eventsPass+= weight;
     
  
  }    
  
   cout << "[Info:] (" << plotName << ") " <<  eventsPass << " events pass " << endl;
  
  
    f_root.Write();
    f_root.Close();
 
}
void NormalizeVGammaSample(TString  InputFilename0 = "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/wgamma.root",
                           TString  InputFilename1 = "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/wgammafo.root",
			   bool applyCorrection = true
  ) {

  Double_t eventCount0 = 0;

  TH1D *hDRatioPhotonElectron = new TH1D("hDRatioPhotonElectron","hDRatioPhotonElectron",10,0.0,2.5); hDRatioPhotonElectron->Sumw2();

  TH1D *hDLepSel[10];
  hDLepSel[0] = new TH1D("hDLepSel_0","hDLepSel_0",20,0.0,100.0);
  hDLepSel[1] = new TH1D("hDLepSel_1","hDLepSel_1",20,0.0,100.0);
  hDLepSel[2] = new TH1D("hDLepSel_2","hDLepSel_2",10,0.0,2.5);
  hDLepSel[3] = new TH1D("hDLepSel_3","hDLepSel_3",10,0.0,2.5);
  hDLepSel[4] = new TH1D("hDLepSel_4","hDLepSel_4",20,0.0,100.0);
  hDLepSel[5] = new TH1D("hDLepSel_5","hDLepSel_5",20,0.0,100.0);
  hDLepSel[6] = new TH1D("hDLepSel_6","hDLepSel_6",10,0.0,2.5);
  hDLepSel[7] = new TH1D("hDLepSel_7","hDLepSel_7",10,0.0,2.5);
  for(int i=0; i<8; i++) hDLepSel[i]->Sumw2();

  TH1D *hDpSel[20];
  hDpSel[0]  = new TH1D("hDpSel_0" ,"hDpSel_0" ,20,0.0,100.0);
  hDpSel[1]  = new TH1D("hDpSel_1" ,"hDpSel_1" ,20,0.0,100.0);
  hDpSel[2]  = new TH1D("hDpSel_2" ,"hDpSel_2" ,10,0.0,2.5);
  hDpSel[3]  = new TH1D("hDpSel_3" ,"hDpSel_3" ,10,0.0,2.5);
  hDpSel[4]  = new TH1D("hDpSel_4" ,"hDpSel_4" ,40,0.0,200.0);
  hDpSel[5]  = new TH1D("hDpSel_5" ,"hDpSel_5" ,40,0.0,200.0);
  hDpSel[6]  = new TH1D("hDpSel_6" ,"hDpSel_6" ,40,0.0,200.0);
  hDpSel[7]  = new TH1D("hDpSel_7" ,"hDpSel_7" ,36,0.0,160.0);
  hDpSel[8]  = new TH1D("hDpSel_8" ,"hDpSel_8" ,40,0.0,200.0);
  hDpSel[9]  = new TH1D("hDpSel_9" ,"hDpSel_9" ,40,0.0,200.0);
  hDpSel[10] = new TH1D("hDpSel_10","hDpSel_10",20,0.0,100.0);
  hDpSel[11] = new TH1D("hDpSel_11","hDpSel_11",20,0.0,100.0);
  hDpSel[12] = new TH1D("hDpSel_12","hDpSel_12",10,0.0,2.5);
  hDpSel[13] = new TH1D("hDpSel_13","hDpSel_13",10,0.0,2.5);
  hDpSel[14] = new TH1D("hDpSel_14","hDpSel_14",40,0.0,200.0);
  hDpSel[15] = new TH1D("hDpSel_15","hDpSel_15",40,0.0,200.0);
  hDpSel[16] = new TH1D("hDpSel_16","hDpSel_16",40,0.0,200.0);
  hDpSel[17] = new TH1D("hDpSel_17","hDpSel_17",36,0.0,160.0);
  hDpSel[18] = new TH1D("hDpSel_18","hDpSel_18",40,0.0,200.0);
  hDpSel[19] = new TH1D("hDpSel_19","hDpSel_19",40,0.0,200.0);
  for(int i=0; i<20; i++) hDpSel[i]->Sumw2();

  SmurfTree vgammaEvent;
  vgammaEvent.LoadTree(InputFilename0.Data());
  vgammaEvent.InitTree(0);
  vgammaEvent.tree_->SetName("tree");

  TFile *fRatioPhotonElectron = TFile::Open("/data/smurf/data/Run2012_Summer12_SmurfV9_53X/auxiliar/ratio_photon_electron.root");
  TH1D *fhDRatioPhotonElectron = (TH1D*)(fRatioPhotonElectron->Get("hDRatioPhotonElectron"));
  assert(fhDRatioPhotonElectron);
  fhDRatioPhotonElectron->SetDirectory(0);
  fRatioPhotonElectron->Close();
  delete fRatioPhotonElectron;

  for (int n=0;n<vgammaEvent.tree_->GetEntries();n++) { 
    vgammaEvent.tree_->GetEntry(n);

    bool lid = (vgammaEvent.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection 
           &&  (vgammaEvent.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection;
    if (!lid) continue;

    if( vgammaEvent.lep1_.pt() <= 20	    	     ) continue; // cut on leading lepton pt
    if( vgammaEvent.lep2_.pt() <= 10	    	     ) continue; // cut on trailing lepton pt
    if( vgammaEvent.dilep_.M() <= 12                 ) continue; // cut on minimum dilepton mass

    Double_t add = vgammaEvent.sfWeightPU_*vgammaEvent.sfWeightEff_*vgammaEvent.sfWeightTrig_;
    Double_t myWeight = vgammaEvent.scale1fb_*add;    

    if(!(TMath::Abs(vgammaEvent.lep1McId_) == 11 || TMath::Abs(vgammaEvent.lep1McId_) == 13)) hDLepSel[0]->Fill(TMath::Min(vgammaEvent.lep1_.pt(),99.999),myWeight);
    if(!(TMath::Abs(vgammaEvent.lep2McId_) == 11 || TMath::Abs(vgammaEvent.lep2McId_) == 13)) hDLepSel[0]->Fill(TMath::Min(vgammaEvent.lep2_.pt(),99.999),myWeight);
    if(!(TMath::Abs(vgammaEvent.lep1McId_) == 11 || TMath::Abs(vgammaEvent.lep1McId_) == 13)) hDLepSel[2]->Fill(TMath::Min(TMath::Abs(vgammaEvent.lep1_.eta()),2.499),myWeight);
    if(!(TMath::Abs(vgammaEvent.lep2McId_) == 11 || TMath::Abs(vgammaEvent.lep2McId_) == 13)) hDLepSel[2]->Fill(TMath::Min(TMath::Abs(vgammaEvent.lep2_.eta()),2.499),myWeight);
    if((TMath::Abs(vgammaEvent.lep1McId_) == 11 || TMath::Abs(vgammaEvent.lep1McId_) == 13)) hDLepSel[4]->Fill(TMath::Min(vgammaEvent.lep1_.pt(),99.999),myWeight);
    if((TMath::Abs(vgammaEvent.lep2McId_) == 11 || TMath::Abs(vgammaEvent.lep2McId_) == 13)) hDLepSel[4]->Fill(TMath::Min(vgammaEvent.lep2_.pt(),99.999),myWeight);
    if((TMath::Abs(vgammaEvent.lep1McId_) == 11 || TMath::Abs(vgammaEvent.lep1McId_) == 13)) hDLepSel[6]->Fill(TMath::Min(TMath::Abs(vgammaEvent.lep1_.eta()),2.499),myWeight);
    if((TMath::Abs(vgammaEvent.lep2McId_) == 11 || TMath::Abs(vgammaEvent.lep2McId_) == 13)) hDLepSel[6]->Fill(TMath::Min(TMath::Abs(vgammaEvent.lep2_.eta()),2.499),myWeight);

    eventCount0 += myWeight;

  }

  cout << "eventCount0 Event Count 1fb^-1 : " << eventCount0 << endl;
 
  Double_t eventCount1 = 0;

  SmurfTree vgammafoEvent;
  vgammafoEvent.LoadTree(InputFilename1.Data());
  vgammafoEvent.InitTree(0);
  vgammafoEvent.tree_->SetName("tree");

  for (int n=0;n<vgammafoEvent.tree_->GetEntries();n++) { 
    vgammafoEvent.tree_->GetEntry(n);

    bool lid = ((vgammafoEvent.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection 
            &&  (vgammafoEvent.cuts_ & SmurfTree::Lep2LooseEleV2) == SmurfTree::Lep2LooseEleV2) ||
	       ((vgammafoEvent.cuts_ & SmurfTree::Lep1LooseEleV2) == SmurfTree::Lep1LooseEleV2 
           &&   (vgammafoEvent.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection);
    if (!lid) continue;

    if( vgammafoEvent.lep1_.pt() <= 20	    	     ) continue; // cut on leading lepton pt
    if( vgammafoEvent.lep2_.pt() <= 10	    	     ) continue; // cut on trailing lepton pt
    if( TMath::Abs(vgammafoEvent.lep1_.eta()) >= 2.5 ) continue; // cut on leading lepton pt
    if( TMath::Abs(vgammafoEvent.lep2_.eta()) >= 2.5 ) continue; // cut on trailing lepton pt
    if( vgammafoEvent.dilep_.M() <= 12               ) continue; // cut on minimum dilepton mass

    Double_t add = vgammafoEvent.sfWeightPU_*vgammafoEvent.sfWeightEff_*vgammafoEvent.sfWeightTrig_;
    Double_t myWeight = vgammafoEvent.scale1fb_*add;    

    hDpSel[0]->Fill(TMath::Min(vgammafoEvent.lep1_.pt(),99.999),myWeight);
    hDpSel[1]->Fill(TMath::Min(vgammafoEvent.lep2_.pt(),99.999),myWeight);
    hDpSel[2]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep1_.eta()),2.499),myWeight);
    hDpSel[3]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep2_.eta()),2.499),myWeight);
    hDpSel[4]->Fill(TMath::Min((double)vgammafoEvent.mt_,199.999),myWeight);
    hDpSel[5]->Fill(TMath::Min((double)vgammafoEvent.dilep_.M(),199.999),myWeight);
    hDpSel[6]->Fill(TMath::Min((double)vgammafoEvent.met_,199.999),myWeight);
    hDpSel[7]->Fill(TMath::Min((double)vgammafoEvent.dPhi_*180/TMath::Pi(),179.999),myWeight);
    hDpSel[8]->Fill(TMath::Min((double)vgammafoEvent.mt1_,199.999),myWeight);
    hDpSel[9]->Fill(TMath::Min((double)vgammafoEvent.mt2_,199.999),myWeight);
    if(!(TMath::Abs(vgammafoEvent.lep1McId_) == 11 || TMath::Abs(vgammafoEvent.lep1McId_) == 13) && applyCorrection == true) myWeight = myWeight * ratioPhotonElectron(fhDRatioPhotonElectron,TMath::Abs(vgammafoEvent.lep1_.eta()));
    if(!(TMath::Abs(vgammafoEvent.lep2McId_) == 11 || TMath::Abs(vgammafoEvent.lep2McId_) == 13) && applyCorrection == true) myWeight = myWeight * ratioPhotonElectron(fhDRatioPhotonElectron,TMath::Abs(vgammafoEvent.lep2_.eta()));
    hDpSel[10]->Fill(TMath::Min(vgammafoEvent.lep1_.pt(),99.999),myWeight);
    hDpSel[11]->Fill(TMath::Min(vgammafoEvent.lep2_.pt(),99.999),myWeight);
    hDpSel[12]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep1_.eta()),2.499),myWeight);
    hDpSel[13]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep2_.eta()),2.499),myWeight);
    hDpSel[14]->Fill(TMath::Min((double)vgammafoEvent.mt_,199.999),myWeight);
    hDpSel[15]->Fill(TMath::Min((double)vgammafoEvent.dilep_.M(),199.999),myWeight);
    hDpSel[16]->Fill(TMath::Min((double)vgammafoEvent.met_,199.999),myWeight);
    hDpSel[17]->Fill(TMath::Min((double)vgammafoEvent.dPhi_*180/TMath::Pi(),179.999),myWeight);
    hDpSel[18]->Fill(TMath::Min((double)vgammafoEvent.mt1_,199.999),myWeight);
    hDpSel[19]->Fill(TMath::Min((double)vgammafoEvent.mt2_,199.999),myWeight);

    if(!(TMath::Abs(vgammafoEvent.lep1McId_) == 11 || TMath::Abs(vgammafoEvent.lep1McId_) == 13)) hDLepSel[1]->Fill(TMath::Min(vgammafoEvent.lep1_.pt(),99.999),myWeight);
    if(!(TMath::Abs(vgammafoEvent.lep2McId_) == 11 || TMath::Abs(vgammafoEvent.lep2McId_) == 13)) hDLepSel[1]->Fill(TMath::Min(vgammafoEvent.lep2_.pt(),99.999),myWeight);
    if(!(TMath::Abs(vgammafoEvent.lep1McId_) == 11 || TMath::Abs(vgammafoEvent.lep1McId_) == 13)) hDLepSel[3]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep1_.eta()),2.499),myWeight);
    if(!(TMath::Abs(vgammafoEvent.lep2McId_) == 11 || TMath::Abs(vgammafoEvent.lep2McId_) == 13)) hDLepSel[3]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep2_.eta()),2.499),myWeight);
    if((TMath::Abs(vgammafoEvent.lep1McId_) == 11 || TMath::Abs(vgammafoEvent.lep1McId_) == 13)) hDLepSel[5]->Fill(TMath::Min(vgammafoEvent.lep1_.pt(),99.999),myWeight);
    if((TMath::Abs(vgammafoEvent.lep2McId_) == 11 || TMath::Abs(vgammafoEvent.lep2McId_) == 13)) hDLepSel[5]->Fill(TMath::Min(vgammafoEvent.lep2_.pt(),99.999),myWeight);
    if((TMath::Abs(vgammafoEvent.lep1McId_) == 11 || TMath::Abs(vgammafoEvent.lep1McId_) == 13)) hDLepSel[7]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep1_.eta()),2.499),myWeight);
    if((TMath::Abs(vgammafoEvent.lep2McId_) == 11 || TMath::Abs(vgammafoEvent.lep2McId_) == 13)) hDLepSel[7]->Fill(TMath::Min(TMath::Abs(vgammafoEvent.lep2_.eta()),2.499),myWeight);

    eventCount1 += myWeight;

  }

  cout << "eventCount1 Event Count 1fb^-1 : " << eventCount1 << endl;
 
  double NormalizationWeight = eventCount0/eventCount1;
  cout << "Normalized  Event Count 1fb^-1 : " << NormalizationWeight << endl;

  hDLepSel[0]->Divide(hDLepSel[1]);
  hDLepSel[2]->Divide(hDLepSel[3]);
  hDLepSel[4]->Divide(hDLepSel[5]);
  hDLepSel[6]->Divide(hDLepSel[7]);

  hDRatioPhotonElectron->Add(hDLepSel[2]);
  TFile *weightPE = new TFile("ratio_photon_electron.root", "RECREATE");
  weightPE->cd();
  hDRatioPhotonElectron->Write();
  weightPE->Close();

  //*************************************************************************************************
  // Create new normalized tree
  //*************************************************************************************************
  SmurfTree newEvent;
  newEvent.LoadTree(InputFilename1.Data());
  newEvent.InitTree(0);
  newEvent.tree_->SetName("tree");

  TString OutputFilename = InputFilename1;
  OutputFilename.ReplaceAll(".root","_newweight.root");
  cout << "creating new root file: " << OutputFilename << endl;
  TFile *outputFile = new TFile(OutputFilename.Data(), "RECREATE");
  outputFile->cd();

  for(int i=0; i<8; i++) hDLepSel[i]->Write();
  for(int i=0; i<20; i++) hDpSel[i]->Scale(1./hDpSel[i]->GetSumOfWeights());
  for(int i=0; i<20; i++) hDpSel[i]->Write();

  TTree *normalizedTree = newEvent.tree_->CloneTree(0);  
  for (int n=0;n<newEvent.tree_->GetEntries();n++) { 
    newEvent.tree_->GetEntry(n);
    
    newEvent.scale1fb_ = newEvent.scale1fb_*NormalizationWeight;
    if((vgammafoEvent.cuts_ & SmurfTree::Lep1LooseEleV2) == SmurfTree::Lep1LooseEleV2) newEvent.cuts_ |= SmurfTree::Lep1FullSelection;
    if((vgammafoEvent.cuts_ & SmurfTree::Lep2LooseEleV2) == SmurfTree::Lep2LooseEleV2) newEvent.cuts_ |= SmurfTree::Lep2FullSelection;
    if((vgammafoEvent.cuts_ & SmurfTree::Lep3LooseEleV2) == SmurfTree::Lep3LooseEleV2) newEvent.cuts_ |= SmurfTree::Lep3FullSelection;
    normalizedTree->Fill(); 
  }

  normalizedTree->Write();
  outputFile->Close();

}
Example #9
0
void twohistos(int nsel = 1, int cem = 7, int var = 0, int jetbin =0){
  
   
  char plotName[300], Message[300], jetName[300], folderName[300];
  sprintf(plotName,"test");
  sprintf(Message,"test");
  sprintf(jetName,"0jets");
 // if (jetbin == 1) sprintf(jetName,"1jets");  
  
  sprintf(folderName,"/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets_mva");
  
  if (nsel == 1) {sprintf(plotName,"Higgs_SM"); sprintf(Message,"Standard Model JHU Gen");}
  else if (nsel == 2) {sprintf(plotName,"Higgs_0M"); sprintf(Message,"Pseudoscalar JHU Gen");}
  else if (nsel == 3) {sprintf(plotName,"Higgs_fa305"); sprintf(Message,"Mix fa3");}
  else if (nsel == 4) {sprintf(plotName,"Higgs_0PH"); sprintf(Message,"Scalar with Higher order corrections");}
  else if (nsel == 5) {sprintf(plotName,"Higgs_fa205"); sprintf(Message,"Mix fa2");}
  else if (nsel == 6) {sprintf(plotName,"Higgs_L1"); sprintf(Message,"Lambda 1");}
  else if (nsel == 7) {sprintf(plotName,"Higgs_mixL1"); sprintf(Message,"Lambda 1 Mix");}
  
  char myRootFile[300];
  double lumi = lumi8;
  if (cem == 8){
    if (nsel == 1) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0pm-v19.root", folderName,jetName);
    else if (nsel == 2) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0mt-v19.root", folderName, jetName);
    else if (nsel == 3) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0mf05ph0-v19.root", folderName, jetName);
    else if (nsel == 4) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0ph-v19.root", folderName, jetName);
    else if (nsel == 5) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0phf05ph0-v19.root", folderName, jetName); 
    else if (nsel == 6) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0l1-v19.root", folderName, jetName); 
    else if (nsel == 7) sprintf(myRootFile,"%s/ntuples2012_MultiClass_125train_%s_xww125p6_s12-x125ww4l-0l1f05ph180-v19.root", folderName, jetName);     
  } else {
    lumi = lumi7;
    sprintf(folderName,"/data/smurf/data/Run2011_Fall11_SmurfV9_42X/mitf-alljets_mva");
    if (nsel == 1) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0pm.root", folderName,jetName);
    else if (nsel == 2) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6-0m.root", folderName, jetName);
    else if (nsel == 3) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6-0mf05ph0.root", folderName, jetName);
    else if (nsel == 4) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0ph.root", folderName, jetName);
    else if (nsel == 5) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0phf05ph0.root", folderName, jetName); 
    else if (nsel == 6) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0phf05.root", folderName, jetName); 
    else if (nsel == 7) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0l1.root", folderName, jetName); 
    else if (nsel == 8) sprintf(myRootFile,"%s/ntuples_126train_%s_xww125p6_x125ww4l-0l1f05ph180.root", folderName, jetName); 
  }
  
  //Load datasets
  
  SmurfTree sample;
  cout << myRootFile << endl;
  cout << "You are running " << Message << endl;
  sample.LoadTree(myRootFile,-1);
  sample.InitTree(0);
  
  char rootFile[300];
  sprintf(rootFile,"rootfiles_var/test_mll_%dj.root", jetbin);
  if (var !=0) sprintf(rootFile,"rootfiles_var/test_mt_%dj.root", jetbin);
  
  TFile f_root(rootFile, "UPDATE");
  
  // Prepare histograms
  char title[300];
  
  sprintf(title,"%s",plotName);
  int n_bins = 25;
  int binfirst = 0;
  int binlast = 200;
  if (var !=0 ){
    n_bins = 30;
    binfirst = 60;
    binlast = 280;
  }
  TH1F* histo = new TH1F( title, "histos", n_bins, binfirst, binlast);
  histo->Sumw2();
  
   
  // 25, 0-200 mll called like the process, first one caps, title "histos"
  // 30, 60-280 mts
  
  //To business
  int nSample=sample.tree_->GetEntries();
  
  cout << nSample << endl;
  int events = 0;
  double events_norm = 0;
  
  for (int i=0; i<nSample; ++i) {
    //   for (int i=0; i<10; ++i) {
    sample.tree_->GetEntry(i); 
  
    if ((fabs(sample.lep1McId_) == fabs(sample.lep2McId_))) continue;
    if (sample.type_ != 1 && sample.type_ != 2 ) continue;
    if ((sample.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection || 
    (sample.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) continue;  
    events++;
    
    double weight = 1;
    if (sample.dstype_ != SmurfTree::data) weight = lumi*sample.scale1fb_*sample.sfWeightPU_*sample.sfWeightEff_*sample.sfWeightTrig_;
    
    //HWW slection mock-off
    int nJetsType = jetbin;
    if (sample.dilep_.M() <= 12 || sample.dilep_.M() >= 200) continue;
    if ((sample.cuts_ & SmurfTree::ExtraLeptonVeto) != SmurfTree::ExtraLeptonVeto) continue;
    if (sample.lq1_*sample.lq2_ > 0) continue;
    if (sample.njets_ != nJetsType) continue; 
    if (sample.lep1_.Pt() <= 20) continue;
    if (sample.lep2_.Pt() <= 10) continue;
    if (TMath::Min(sample.pmet_,sample.pTrackMet_) <= 20) continue;
    if (sample.dilep_.Pt() <= 30) continue;
    if ((sample.cuts_ & SmurfTree::TopVeto) != SmurfTree::TopVeto) continue; 
    if (sample.mt_ <=30 || sample.mt_ >=280) continue;
    if (sample.mt_ <= 60 || sample.mt_ >= 280 || sample.dilep_.M() >= 200) continue;
  
    
    events_norm +=weight;
    if (var ==0) histo->Fill(sample.dilep_.M(), weight);
    else histo->Fill(sample.mt_, weight);
  

  }
  
 cout << "Events used: " << events << endl;
 cout << "Final yield: " << events_norm << endl;
  
  f_root.Write();
  f_root.Close();
  
}
//*************************************************************************************************
//Main part of the macro
//*************************************************************************************************
void NormalizeZtautauEmbeddedSample(const string dyttMCFile    = "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/dyll.root",
                                    const string InputFilename = "/data/smurf/data/Run2012_Summer12_SmurfV9_53X/mitf-alljets/data_ztt.root"
  ) {

  const int NPoints = 11;
  TH1D* hDOpt0[NPoints];
  hDOpt0[0]  = new TH1D("hDOpt0_0", "electron pt", 100, 0, 100); 	hDOpt0[0] ->Sumw2();
  hDOpt0[1]  = new TH1D("hDOpt0_1", "muon pt", 100, 0, 100); 	        hDOpt0[1] ->Sumw2();
  hDOpt0[2]  = new TH1D("hDOpt0_2", "projected MET", 100, 0, 100); 	hDOpt0[2] ->Sumw2();
  hDOpt0[3]  = new TH1D("hDOpt0_3", "projected trackMET", 100, 0, 100); hDOpt0[3] ->Sumw2();
  hDOpt0[4]  = new TH1D("hDOpt0_4", "min(pMET,pTrackMET)", 100, 0, 100);hDOpt0[4] ->Sumw2();
  hDOpt0[5]  = new TH1D("hDOpt0_5", "MT", 100, 0, 100); 	        hDOpt0[5] ->Sumw2();
  hDOpt0[6]  = new TH1D("hDOpt0_6", "MLL", 100, 0, 100);        	hDOpt0[6] ->Sumw2();
  hDOpt0[7]  = new TH1D("hDOpt0_7", "PTLL", 100, 0, 100); 	        hDOpt0[7] ->Sumw2();
  hDOpt0[8]  = new TH1D("hDOpt0_8", "Njets",  10,-0.5, 9.5);            hDOpt0[8] ->Sumw2();
  hDOpt0[9]  = new TH1D("hDOpt0_9", "TopVeto",   2,-0.5, 1.5);          hDOpt0[9] ->Sumw2();
  hDOpt0[10] = new TH1D("hDOpt0_10","Nvtx", 60, 0,  60); 	        hDOpt0[10]->Sumw2();
  TH1D* hDOpt1[NPoints];
  hDOpt1[0]  = new TH1D("hDOpt1_0", "electron pt", 100, 0, 100);	hDOpt1[0] ->Sumw2();
  hDOpt1[1]  = new TH1D("hDOpt1_1", "muon pt", 100, 0, 100);		hDOpt1[1] ->Sumw2();
  hDOpt1[2]  = new TH1D("hDOpt1_2", "projected MET", 100, 0, 100);	hDOpt1[2] ->Sumw2();
  hDOpt1[3]  = new TH1D("hDOpt1_3", "projected trackMET", 100, 0, 100); hDOpt1[3] ->Sumw2();
  hDOpt1[4]  = new TH1D("hDOpt1_4", "min(pMET,pTrackMET)", 100, 0, 100);hDOpt1[4] ->Sumw2();
  hDOpt1[5]  = new TH1D("hDOpt1_5", "MT", 100, 0, 100); 		hDOpt1[5] ->Sumw2();
  hDOpt1[6]  = new TH1D("hDOpt1_6", "MLL", 100, 0, 100);		hDOpt1[6] ->Sumw2();
  hDOpt1[7]  = new TH1D("hDOpt1_7", "PTLL", 100, 0, 100);		hDOpt1[7] ->Sumw2();
  hDOpt1[8]  = new TH1D("hDOpt1_8", "Njets",  10,-0.5, 9.5);		hDOpt1[8] ->Sumw2();
  hDOpt1[9]  = new TH1D("hDOpt1_9", "TopVeto",   2,-0.5, 1.5);  	hDOpt1[9] ->Sumw2();
  hDOpt1[10] = new TH1D("hDOpt1_10","Nvtx", 60, 0,  60);		hDOpt1[10]->Sumw2();

  //*************************************************************************************************
  //Count dytt MC Normalization
  //*************************************************************************************************
  Double_t DYMCEventcount_emu = 0;

  SmurfTree dyttEvent;
  dyttEvent.LoadTree(dyttMCFile.c_str());
  dyttEvent.InitTree(0);
  dyttEvent.tree_->SetName("tree");

  for (int n=0;n<dyttEvent.tree_->GetEntries();n++) { 
    dyttEvent.tree_->GetEntry(n);

    if (!(((dyttEvent.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection) 
       && ((dyttEvent.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection))) continue;
    if( dyttEvent.lid3_ != 0              ) continue; // cut on third lepton
    if( dyttEvent.lq1_*dyttEvent.lq2_ > 0 ) continue; // cut on opposite-dytt leptons
    if( dyttEvent.dilep_.mass() <= 12.0   ) continue; // cut on low dilepton mass
    if( dyttEvent.lep1_.pt() <= 20	  ) continue; // cut on leading lepton pt
    if( dyttEvent.lep2_.pt() <= 10	  ) continue; // cut on trailing lepton pt

    //MC -> Data Corrections
    
    Double_t add = dyttEvent.sfWeightPU_*dyttEvent.sfWeightEff_*dyttEvent.sfWeightTrig_;
    Double_t myWeight = dyttEvent.scale1fb_*(1.0)*add;    

    if (dyttEvent.type_ == SmurfTree::em || dyttEvent.type_ == SmurfTree::me) {
      DYMCEventcount_emu += myWeight;
      if(dyttEvent.type_ == SmurfTree::em){
        hDOpt0[0]->Fill(TMath::Min((double)dyttEvent.lep1_.Pt(),99.999),myWeight);
        hDOpt0[1]->Fill(TMath::Min((double)dyttEvent.lep2_.Pt(),99.999),myWeight);
      } else {
        hDOpt0[0]->Fill(TMath::Min((double)dyttEvent.lep2_.Pt(),99.999),myWeight);
        hDOpt0[1]->Fill(TMath::Min((double)dyttEvent.lep1_.Pt(),99.999),myWeight);
      }
      hDOpt0[2]->Fill(TMath::Min((double)dyttEvent.pmet_,99.999),myWeight);
      hDOpt0[3]->Fill(TMath::Min((double)dyttEvent.pTrackMet_,99.999),myWeight);
      hDOpt0[4]->Fill(TMath::Min((double)dyttEvent.pmet_,(double)dyttEvent.pTrackMet_),myWeight);
      hDOpt0[5]->Fill(TMath::Min((double)dyttEvent.mt_,99.999),myWeight);
      hDOpt0[6]->Fill(TMath::Min((double)dyttEvent.dilep_.mass(),99.999),myWeight);
      hDOpt0[7]->Fill(TMath::Min((double)dyttEvent.dilep_.Pt(),99.999),myWeight);
      hDOpt0[8]->Fill(TMath::Min((double)dyttEvent.njets_,9.499),myWeight);
      hDOpt0[9]->Fill((double)(dyttEvent.cuts_ & SmurfTree::TopVeto) ==  SmurfTree::TopVeto,myWeight);
      hDOpt0[10]->Fill(TMath::Min((double)dyttEvent.nvtx_,59.999),myWeight);
    }

  }

  cout << "DYtt Event Count (e-mu) 1fb^-1 : " << DYMCEventcount_emu << endl;

  //*************************************************************************************************
  //Count embedded Normalization
  //*************************************************************************************************
  Double_t EmbeddedEventcount_emu = 0;

  SmurfTree embeddedEvent;
  embeddedEvent.LoadTree(InputFilename.c_str());
  embeddedEvent.InitTree(0);
  embeddedEvent.tree_->SetName("tree");

  for (int n=0;n<embeddedEvent.tree_->GetEntries();n++) { 
    embeddedEvent.tree_->GetEntry(n);

    if (!(((embeddedEvent.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection) 
       && ((embeddedEvent.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection))) continue;
    if( embeddedEvent.lid3_ != 0                  ) continue; // cut on third lepton
    if( embeddedEvent.lq1_*embeddedEvent.lq2_ > 0 ) continue; // cut on opposite-embedded leptons
    if( embeddedEvent.dilep_.mass() <= 12.0       ) continue; // cut on low dilepton mass
    if( embeddedEvent.lep1_.pt() <= 20	    	  ) continue; // cut on leading lepton pt
    if( embeddedEvent.lep2_.pt() <= 10	    	  ) continue; // cut on trailing lepton pt

    if (embeddedEvent.type_ == SmurfTree::em || embeddedEvent.type_ == SmurfTree::me) {
      
      Double_t myWeight = embeddedEvent.scale1fb_*embeddedEvent.sfWeightEff_*embeddedEvent.sfWeightTrig_;    
      EmbeddedEventcount_emu = EmbeddedEventcount_emu + myWeight;
      if(embeddedEvent.type_ == SmurfTree::em){
        hDOpt1[0]->Fill(TMath::Min((double)embeddedEvent.lep1_.Pt(),99.999),myWeight);
        hDOpt1[1]->Fill(TMath::Min((double)embeddedEvent.lep2_.Pt(),99.999),myWeight);
      } else {
        hDOpt1[0]->Fill(TMath::Min((double)embeddedEvent.lep2_.Pt(),99.999),myWeight);
        hDOpt1[1]->Fill(TMath::Min((double)embeddedEvent.lep1_.Pt(),99.999),myWeight);
      }
      hDOpt1[2]->Fill(TMath::Min((double)embeddedEvent.pmet_,99.999),myWeight);
      hDOpt1[3]->Fill(TMath::Min((double)embeddedEvent.pTrackMet_,99.999),myWeight);
      hDOpt1[4]->Fill(TMath::Min((double)embeddedEvent.pmet_,(double)embeddedEvent.pTrackMet_),myWeight);
      hDOpt1[5]->Fill(TMath::Min((double)embeddedEvent.mt_,99.999),myWeight);
      hDOpt1[6]->Fill(TMath::Min((double)embeddedEvent.dilep_.mass(),99.999),myWeight);
      hDOpt1[7]->Fill(TMath::Min((double)embeddedEvent.dilep_.Pt(),99.999),myWeight);
      hDOpt1[8]->Fill(TMath::Min((double)embeddedEvent.njets_,9.499),myWeight);
      hDOpt1[9]->Fill((double)(embeddedEvent.cuts_ & SmurfTree::TopVeto) ==  SmurfTree::TopVeto,myWeight);
      hDOpt1[10]->Fill(TMath::Min((double)embeddedEvent.nvtx_,59.999),myWeight);
    }

  }

  cout << "Embedded Event Count (e-mu) : " << EmbeddedEventcount_emu << endl;
  Double_t NormalizationWeight = DYMCEventcount_emu / EmbeddedEventcount_emu ;
  cout << "Normalization Weight = " << NormalizationWeight << endl;
  for(int i=0; i<NPoints; i++) hDOpt1[i]->Scale(NormalizationWeight);

  TString OutputFilename = "test.root";
  TFile *outputFile = new TFile(OutputFilename.Data(), "RECREATE");
  outputFile->cd();
  for(int i=0; i<NPoints; i++) hDOpt0[i]->Write();
  for(int i=0; i<NPoints; i++) hDOpt1[i]->Write();
  outputFile->Close();
}
Example #11
0
void Test() {
  
  TString bgdInputFile    = "samples/backgroundA_3l.root";
  TString dataInputFile   = "samples/data_3l.root";
  TString sigInputFile   =  "samples/hww125.root";
  
  SmurfTree background;
  background.LoadTree(bgdInputFile,-1);
  background.InitTree(0);
  
  SmurfTree data;
  data.LoadTree(dataInputFile,-1);
  data.InitTree(0);
  
  SmurfTree signal;
  signal.LoadTree(sigInputFile,-1);
  signal.InitTree(0);
  
  char output[200];
  sprintf(output,"histo_test.root");     
  TFile* outFileNjets = new TFile(output,"recreate");
  
  TH1F* types = new TH1F("types", "types", 80, -0.5 , 79.5);
  types->Sumw2();
  
  TH1D* bckg_met = new TH1D("bckg_met", "MET", 200, 0, 200);
  bckg_met->Sumw2();
  TH1D* bckg_mllz = new TH1D("bckg_mllz", "m_{ll}", 200, 0, 200);
  bckg_mllz->Sumw2();
  TH1D* bckg_mt = new TH1D("bckg_mt", "m_t", 200, 0, 200);
  bckg_mt->Sumw2();
  TH1D* bckg_ptjet = new TH1D("bckg_ptjet", "P_t of leading jet", 200, 0, 200);
  bckg_ptjet->Sumw2();
  TH1D* bckg_mH = new TH1D("bckg_mH", "m_H", 200, 0, 400);
  bckg_mH->Sumw2();
  TH1D* bckg_mjj = new TH1D("bckg_mjj", "m_jj", 200, 0, 400);
  bckg_mjj->Sumw2();
  TH1D* bckg_dphill = new TH1D("bckg_dphill", "#Delta#phi_{ll}", 200, 0, 3.5);
  bckg_dphill->Sumw2();
  
  TH2D* bckg_mll_mh  = new TH2D("bckg_mll_mh", " ", 100, 40, 120, 100, 0, 200);
  
  TH1D* sig_met = new TH1D("sig_met", "MET", 200, 0, 200);
  sig_met->Sumw2();
  TH1D* sig_mllz = new TH1D("sig_mllz", "m_{ll}", 200, 0, 200);
  sig_mllz->Sumw2();
  TH1D* sig_mt = new TH1D("sig_mt", "m_t", 200, 0, 200);
  sig_mt->Sumw2();
  TH1D* sig_ptjet = new TH1D("sig_ptjet", "P_t of leading jet", 200, 0, 200);
  sig_ptjet->Sumw2();
  TH1D* sig_mH = new TH1D("sig_mH", "m_H", 200, 0, 400);
  sig_mH->Sumw2();
  TH1D* sig_mjj = new TH1D("sig_mjj", "m_jj", 200, 0, 400);
  sig_mjj->Sumw2();
  TH1D* sig_dphill = new TH1D("sig_dphill", "#Delta#phi_{ll}", 200, 0, 3.5);
  sig_dphill->Sumw2();

  TH2D* sig_mll_mh  = new TH2D("sig_mll_mh", " ",100, 40, 120, 100, 0, 200);
  

  double lumi = 12.1;
  double weight = 1;
  double eventsPass = 0;
  int nBgd=background.tree_->GetEntries();
  for (int i=0; i<nBgd; ++i) {
    
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nBgd);
    background.tree_->GetEntry(i);
    if (background.njets_ <2 )continue;
    if (!((background.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection 
	  && (background.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection) ) continue;
  
    weight = 1;
    
    int nFake = 0;
    if(((background.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (background.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (background.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (background.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (background.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (background.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((background.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (background.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if (nFake !=0) continue; 
    
    if (nFake > 1) continue; 
    if (nFake == 1) weight = lumi*background.scale1fb_*background.sfWeightPU_*background.sfWeightEff_*background.sfWeightTrig_*background.sfWeightFR_ ;
    else weight = lumi*background.scale1fb_*background.sfWeightPU_*background.sfWeightEff_*background.sfWeightTrig_;
    //weight = lumi*background.scale1fb_*background.sfWeightPU_*background.sfWeightEff_*background.sfWeightTrig_;
  
    if (background.lid3_ == background.lid2_ && background.lid3_ == background.lid1_) continue;
    if (background.lid3_ == background.lid2_ && fabs(background.lid3_) != fabs(background.lid1_)) continue;
    if (background.lid3_ == background.lid1_ && fabs(background.lid3_) != fabs(background.lid2_)) continue;
    if (background.lid2_ == background.lid1_ && fabs(background.lid2_) != fabs(background.lid3_)) continue;
    
    
    double m[3] = {0, 0, 0};
    LorentzVector pair1, pair2, pair3;
    if (fabs(background.lid1_) == fabs(background.lid2_) && background.lq1_*background.lq2_ < 0){
      pair1 = background.lep1_ + background.lep2_ ;
      m[0] = pair1.M();
    }
    if (fabs(background.lid2_) == fabs(background.lid3_) && background.lq2_*background.lq3_ < 0){
      pair2 = background.lep2_ + background.lep3_ ;
      m[1] = pair2.M();
    }
    if (fabs(background.lid1_) == fabs(background.lid3_) && background.lq1_*background.lq3_ < 0){
      pair3 = background.lep1_ + background.lep3_ ;
      m[2] = pair3.M();
    }
    
    if ( (m[0] < 80 || m[0] > 100) &&  (m[1] < 80 || m[1] > 100) &&  (m[2] < 80 || m[2] > 100)) continue;
    //if ( (m[0] < 40 || m[0] > 120) &&  (m[1] < 40 || m[1] > 120) &&  (m[2] < 40 || m[2] > 120)) continue;
    
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
   
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
    if (min == fabs(mz - m[0])){  pair = pair1; mt =  background.mt3_; tlepton = background.lep3_;} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  background.mt1_; tlepton = background.lep1_;} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  background.mt2_; tlepton = background.lep2_;} 
    pairjet = background.jet1_+ background.jet2_;
    
    if (mt < 40 || background.met_ < 25) continue;
    //     if (mt < 40 ) continue;

    // if (pairjet.M() < 65 || pairjet.M() > 95) continue;
  
    types->Fill(background.dstype_);
    bckg_met->Fill(background.met_, weight);
    bckg_mllz->Fill(pair.M(), weight);
    bckg_mt->Fill(mt, weight);
    bckg_ptjet->Fill(background.jet1_.Pt(), weight);
    LorentzVector metvector(background.met_*cos(background.metPhi_), background.met_*sin(background.metPhi_), 0, 0);
    LorentzVector higgsSystem = tlepton + metvector + background.jet1_  + background.jet2_;
    bckg_mH->Fill(higgsSystem.M(), weight);
    bckg_mjj->Fill(pairjet.M(), weight);
    bckg_dphill->Fill(DeltaPhi(pairjet.Phi(),tlepton.Phi()), weight);
    bckg_mll_mh->Fill(pair.M(),higgsSystem.M(), weight); 
    eventsPass += weight;
 
  }
  cout << eventsPass << " background events in " << lumi << " fb" << endl; 
  
  
  int nSig=signal.tree_->GetEntries();
  int nTotal = 0;
  int nZH = 0;
  double eventsPassSig = 0;
  for (int i=0; i<nSig; ++i) {
    
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nSig);
    signal.tree_->GetEntry(i);
    
    nTotal++;
    if(signal.processId_==24)  nZH++;
    
    if (signal.njets_ < 2 )continue;

    if(!((signal.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection 
	 &&(signal.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection)) continue;
     
    
     
    weight = 1;
    
    int nFake = 0;
    if(((signal.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (signal.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((signal.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (signal.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((signal.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (signal.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((signal.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (signal.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((signal.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (signal.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((signal.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (signal.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;

    weight = lumi*signal.scale1fb_*signal.sfWeightPU_*signal.sfWeightEff_*signal.sfWeightTrig_;
    
    
    if (signal.lid3_ == signal.lid2_ && signal.lid3_ == signal.lid1_) continue;
    if (signal.lid3_ == signal.lid2_ && fabs(signal.lid3_) != fabs(signal.lid1_)) continue;
    if (signal.lid3_ == signal.lid1_ && fabs(signal.lid3_) != fabs(signal.lid2_)) continue;
    if (signal.lid2_ == signal.lid1_ && fabs(signal.lid2_) != fabs(signal.lid3_)) continue;
    
    double m[3] = {0, 0, 0};
    LorentzVector pair1, pair2, pair3;
    if (fabs(signal.lid1_) == fabs(signal.lid2_) && signal.lq1_*signal.lq2_ < 0){
      pair1 = signal.lep1_ + signal.lep2_ ;
      m[0] = pair1.M();
    }
    if (fabs(signal.lid2_) == fabs(signal.lid3_) && signal.lq2_*signal.lq3_ < 0){
      pair2 = signal.lep2_ + signal.lep3_ ;
      m[1] = pair2.M();
    }
    if (fabs(signal.lid1_) == fabs(signal.lid3_) && signal.lq1_*signal.lq3_ < 0){
      pair3 = signal.lep1_ + signal.lep3_ ;
      m[2] = pair3.M();
    }
    
    if ( (m[0] < 80 || m[0] > 100) &&  (m[1] < 80 || m[1] > 100) &&  (m[2] < 80 || m[2] > 100)) continue;
    //   if ( (m[0] < 40 || m[0] > 120) &&  (m[1] < 40 || m[1] > 120) &&  (m[2] < 40 || m[2] > 120)) continue;
        
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
    
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
    if (min == fabs(mz - m[0])){  pair = pair1; mt =  signal.mt3_; tlepton = signal.lep3_;} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  signal.mt1_; tlepton = signal.lep1_;} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  signal.mt2_; tlepton = signal.lep2_;} 
    pairjet = signal.jet1_+ signal.jet2_;
        
     
    if (mt < 40 || signal.met_ < 25) continue;
    // if (mt < 40 ) continue;
    //if (pairjet.M() < 65 || pairjet.M() > 95) continue;
    
    types->Fill(signal.dstype_);
    sig_met->Fill(signal.met_, weight);
    sig_mllz->Fill(pair.M(), weight);
    sig_mt->Fill(mt, weight);
    sig_ptjet->Fill(signal.jet1_.Pt(), weight);  
    LorentzVector metvector(signal.met_*cos(signal.metPhi_), signal.met_*sin(signal.metPhi_), 0, 0);
    LorentzVector higgsSystem = tlepton + metvector + signal.jet1_  + signal.jet2_;
    sig_mH->Fill(higgsSystem.M(), weight);
    sig_mjj->Fill(pairjet.M(), weight);
    sig_dphill->Fill(DeltaPhi(pairjet.Phi(),tlepton.Phi()), weight);
    sig_mll_mh->Fill(pair.M(),higgsSystem.M(), weight); 
    //    cout << signal.njets_ << " - " ;
    eventsPassSig += weight;
  }
  cout << endl;
  
  cout << eventsPassSig << " signal events in " << lumi << " fb" << endl; 
  cout << nTotal << "events, from which " << nZH << "are ZH" << endl;
  int nData=data.tree_->GetEntries();
  double eventsPassData = 0;
  for (int i=0; i<nData; ++i) {
    
    if (i%100000 == 0 && verboseLevel > 0)
      printf("--- reading event %5d of %5d\n",i,nData);
    data.tree_->GetEntry(i);
    if (data.njets_ < 2 )continue;

    if(!((data.cuts_ & SmurfTree::Lep1FullSelection) == SmurfTree::Lep1FullSelection 
	 && (data.cuts_ & SmurfTree::Lep2FullSelection) == SmurfTree::Lep2FullSelection)) continue;

    
    weight = 1;

    int nFake = 0;
    if(((data.cuts_ & SmurfTree::Lep1LooseMuV2)  == SmurfTree::Lep1LooseMuV2)  && (data.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((data.cuts_ & SmurfTree::Lep2LooseMuV2)  == SmurfTree::Lep2LooseMuV2)  && (data.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((data.cuts_ & SmurfTree::Lep3LooseMuV2)  == SmurfTree::Lep3LooseMuV2)  && (data.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if(((data.cuts_ & SmurfTree::Lep1LooseEleV4) == SmurfTree::Lep1LooseEleV4) && (data.cuts_ & SmurfTree::Lep1FullSelection) != SmurfTree::Lep1FullSelection) nFake++;
    if(((data.cuts_ & SmurfTree::Lep2LooseEleV4) == SmurfTree::Lep2LooseEleV4) && (data.cuts_ & SmurfTree::Lep2FullSelection) != SmurfTree::Lep2FullSelection) nFake++;
    if(((data.cuts_ & SmurfTree::Lep3LooseEleV4) == SmurfTree::Lep3LooseEleV4) && (data.cuts_ & SmurfTree::Lep3FullSelection) != SmurfTree::Lep3FullSelection) nFake++;
    if (nFake !=0) continue; 
 
    if (data.lid3_ == data.lid2_ && data.lid3_ == data.lid1_) continue;
    if (data.lid3_ == data.lid2_ && fabs(data.lid3_) != fabs(data.lid1_)) continue;
    if (data.lid3_ == data.lid1_ && fabs(data.lid3_) != fabs(data.lid2_)) continue;
    if (data.lid2_ == data.lid1_ && fabs(data.lid2_) != fabs(data.lid3_)) continue;
    
    double m[3] = {0, 0, 0};
    LorentzVector pair1, pair2, pair3;
    if (fabs(data.lid1_) == fabs(data.lid2_) && data.lq1_*data.lq2_ < 0){
      pair1 = data.lep1_ + data.lep2_ ;
      m[0] = pair1.M();
    }
    if (fabs(data.lid2_) == fabs(data.lid3_) && data.lq2_*data.lq3_ < 0){
      pair2 = data.lep2_ + data.lep3_ ;
      m[1] = pair2.M();
    }
    if (fabs(data.lid1_) == fabs(data.lid3_) && data.lq1_*data.lq3_ < 0){
      pair3 = data.lep1_ + data.lep3_ ;
      m[2] = pair3.M();
    }
    
    if ( (m[0] < 80 || m[0] > 100) &&  (m[1] < 80 || m[1] > 100) &&  (m[2] < 80 || m[2] > 100)) continue;
    //   if ( (m[0] < 40 || m[0] > 120) &&  (m[1] < 40 || m[1] > 120) &&  (m[2] < 40 || m[2] > 120)) continue;    
    double min = TMath::Min(TMath::Min(fabs(mz -m[0]), fabs(mz-m[1])), TMath::Min(fabs(mz -m[0]), fabs(mz-m[2])));
    
    LorentzVector pair, tlepton, pairjet;
    double mt = 0;
    if (min == fabs(mz - m[0])){  pair = pair1; mt =  data.mt3_; tlepton = data.lep3_;} 
    else if (min == fabs(mz - m[1])){  pair = pair2;  mt =  data.mt1_; tlepton = data.lep1_;} 
    else if (min == fabs(mz - m[2])){  pair = pair3;  mt =  data.mt2_; tlepton = data.lep2_;} 
    pairjet = data.jet1_+ data.jet2_;
        
    
    if (mt < 40 || data.met_ < 25) continue;
    //   if (mt < 40 ) continue;
    //  if (pairjet.M() < 65 || pairjet.M() > 95) continue;

    eventsPassData += weight;
  }
  
  cout << eventsPassData << " data events in " << lumi << " fb" << endl; 

  outFileNjets->Write();
  outFileNjets->Close();
  
  
}