コード例 #1
0
ファイル: Ana_openAng.C プロジェクト: dannielliu/KKcross
int Ana(const char *filename, const char* outdir, TFile *fileout)
{
  //TFile *file = new TFile("mc_KPI_22324.root");
  TFile *file;
  if (filename==0) file= new TFile("KK_22324.root");
  else file = new TFile(filename);
  std::cout<<"File name is "<<file->GetName()<<std::endl;
  if (file==0) return -1;
  TTree *tree = (TTree*)file->Get("TwoProng");
  if (tree==0) return -2;

  double ene[22];
  double pcut[22];
  double epcut[22];
  double thecut[22];
  double m_pcut;
  double m_epcut;
  double m_thecut;
  ifstream cutin("cutpar");
  char line[1000];
  if (cutin.is_open()) cutin.getline(line,1000);
  int iene=0;
  // set E/p and p cut, mark: set cut
  while (!cutin.eof()){
    cutin.getline(line,1000);
    istringstream iss;
    iss.str(line);
    iss>>ene[iene]>>pcut[iene]>>epcut[iene];
    iene++;
    if (iene==22) break;
  }
  for (int i=0;i<22;i++) epcut[i] = 1.64295 - 0.629622*ene[i] + 0.104755 *pow(ene[i],2);
  //for (int i=0;i<21;i++) thecut[i] = 173.946 + 1.74736*ene[i];
  //for (int i=0;i<22;i++) thecut[i] = 179;

  double kappx,kappy,kappz,kampx,kampy,kampz;
  int nneu;
  int run;
  int idxmc;
  int pdgid[100];
  int motheridx[100];
  int emcstatusInt;
  short emcstatusShort;
  double emctrk1;
  double emctrk2;
  double epratio1;
  double epratio2;
  int ntof1;
  int ntof2;
  int tofl1[5];
  int tofl2[5];
  double tof1[5];
  double tof2[5];
  tree->SetBranchAddress("run",&run);
  tree->SetBranchAddress("indexmc",&idxmc);
  tree->SetBranchAddress("pdgid",pdgid);
  tree->SetBranchAddress("motheridx",motheridx);
  tree->SetBranchAddress("kappx",&kappx);
  tree->SetBranchAddress("kappy",&kappy);
  tree->SetBranchAddress("kappz",&kappz);
  tree->SetBranchAddress("kampx",&kampx);
  tree->SetBranchAddress("kampy",&kampy);
  tree->SetBranchAddress("kampz",&kampz);
  //tree->SetBranchAddress("costheta",&costheta);
  tree->SetBranchAddress("nneu",&nneu);
  if (strncmp(tree->GetBranch("emcstatus")->GetLeaf("emcstatus")->GetTypeName(),"Short_t",7)==0)
  	tree->SetBranchAddress("emcstatus",&emcstatusShort);
  else 
  	tree->SetBranchAddress("emcstatus",&emcstatusInt);
  tree->SetBranchAddress("epratio1",&epratio1);
  tree->SetBranchAddress("epratio2",&epratio2);
  tree->SetBranchAddress("emctrk1",&emctrk1);
  tree->SetBranchAddress("emctrk2",&emctrk2);
  tree->SetBranchAddress("ntof1",&ntof1);
  tree->SetBranchAddress("toflayer1",tofl1);
  tree->SetBranchAddress("tof1",tof1);
  tree->SetBranchAddress("ntof2",&ntof2);
  tree->SetBranchAddress("toflayer2",tofl2);
  tree->SetBranchAddress("tof2",tof2);
  double pi = TMath::Pi();
  double mka = 0.493677;
  //double mka = 0.13957;
  //double mka = 0.1057;
  

  tree->GetEntry(0);
  double Ebeam = GetEnergy(run);
  if (Ebeam<1.0) Ebeam = getEne(filename);
  //Ebeam = 3.08;
  for (int iene=0;iene<22;iene++){
    if (fabs(Ebeam-ene[iene])<1e-4) {
      m_pcut = pcut[iene];
      m_epcut = epcut[iene];
      m_thecut = thecut[iene];
      break;
    }
  }
  //m_epcut = m_epcut +0.05; // change E/p to determine uncertainty from this cut
  //m_thecut = 179+0.2; // uncertainty from theta cut
  //m_pcut = 0.956409;
  m_thecut = 179;
  m_epcut = 1.64295 - 0.629622*Ebeam + 0.104755 *pow(Ebeam,2);
  double m_tofcut = 3;
  //m_tofcut = 3-1;
  std::cout<<"pcut "<<m_pcut<<", E/p cut "<<m_epcut<<std::endl;


//std::cout<<"output file name is "<<name<<std::endl;
  //TFile *fileout = new TFile(name,"recreate");
  const char *pureName = getPureName2(filename);
  std::cout<<"Pure Name: "<< pureName <<std::endl;
  char name1[1000];
  //sprintf(name1,"output/%s.root",pureName);
//sprintf(name1,"output/%s.root",pureName);
//TFile *dir = new TFile(name1,"recreate");
//TTree *vars = new TTree("vars","vars");

  fileout->cd();
  TDirectory *dir = fileout->GetDirectory(pureName);
  if (dir==0) dir = fileout->mkdir(pureName);;
  dir->cd();
  TTree *vars = new TTree("vars","vars");

  TLorentzVector kap,kam;
  double mass;

  double totp, costheta1, costheta2;
  double costheta, theta;
  double p1,p2;
  double pt1,pt2;
  double tof11=-1000,tof21=-1000;
  int tofid;
  int isrtag=0;
  int tof1tag=0;
  int tof2tag=0;
  int emctag=0;
  double dtof;
  double eop1,eop2;
//vars->Branch("run",&run,"run/I");
//vars->Branch("indexmc",&idxmc,"indexmc/I");
//vars->Branch("pdgid",pdgid,"pdgid[indexmc]/I");
//vars->Branch("motheridx",motheridx,"motheridx[indexmc]/I");
//vars->Branch("costheta",&costheta,"costheta/D");
//vars->Branch("theta",&theta,"theta/D");
  vars->Branch("costheta1",&costheta1,"costheta1/D");
  vars->Branch("costheta2",&costheta2,"costheta2/D");
  vars->Branch("p1",&p1,"p1/D");
  vars->Branch("p2",&p2,"p2/D");
  vars->Branch("pt1",&pt1,"pt1/D");
  vars->Branch("pt2",&pt2,"pt2/D");
  vars->Branch("dtof",&dtof,"dtof/D");
  vars->Branch("eop1",&eop1,"eop1/D");
  vars->Branch("eop2",&eop2,"eop2/D");
  
  //fileout->cd();

  int nentries = tree->GetEntries();
  double pexp = sqrt(pow(Ebeam/2,2)-pow(mka,2));
  std::cout<<"Total entries is " << nentries << std::endl;
  std::cout<<"Beam energy is " << Ebeam << std::endl;
  std::cout<<"Expected K momentum is " << pexp << std::endl;
  int count0=0,count1=0,count2=0,count3=0;
  int count[10]={0};
  int tagmc=0;

  for (int ien=0;ien<nentries;ien++){
    tree->GetEntry(ien);
    
    isrtag = 0;
    for (int j=0;j<idxmc;j++){
      if (pdgid[j] == 22 && motheridx[j]==j) { tagmc++; isrtag=1;}
    }
    if (emcstatusShort!=0x3 && emcstatusInt!=0x3) continue;
    if (ntof1<1) continue;
    if (ntof2<1) continue;
    for (int i=0;i<ntof1;i++){
      if (tofl1[i]==1) tof11=tof1[i];
    }
    for (int i=0;i<ntof2;i++){
      if (tofl2[i]==1) tof21=tof2[i];
    }
 
    count0++;

    kap.SetVectMag(TVector3(kappx,kappy,kappz),mka);
    kam.SetVectMag(TVector3(kampx,kampy,kampz),mka);
    
    // angular information in lab coordinate
    costheta1 = kap.CosTheta();
    costheta2 = kam.CosTheta();
    
    // momentum information in cms coordinalte
    pt1 = kap.Perp();
    pt2 = kam.Perp();
    kap.Boost(-0.011,0,0);
    kam.Boost(-0.011,0,0);
    costheta = kap.Vect().Dot(kam.Vect())/(kap.Vect().Mag()*kam.Vect().Mag());
    theta = kap.Vect().Angle(kam.Vect())/TMath::Pi()*180.;
    mass = (kap+kam).M();
    totp = (kap+kam).Rho();
    p1 = kap.Rho();
    p2 = kam.Rho();
    dtof = tof11 - tof21;
    eop1 = epratio1;
    eop2 = epratio2;

    //vars->Fill();

    if (fabs(costheta1)<0.93 && costheta1 < 0.8 ) { count[4]++;
    if (fabs(costheta2)<0.93 && costheta2 > -0.8){ count[5]++;
    if (theta>m_thecut)
    { count[2]++;
    //if (epratio1<m_epcut)
    { count[0]++;
      //if (epratio2<m_epcut)
      { count[1]++;
        //if (fabs(tof11-tof21)<m_tofcut)
        if (p1<m_pcut && p2<m_pcut)
        {
          count3++;
          vars->Fill();
        //if (p2<m_pcut) {/*datasel2->Fill();*/ count2++; // p2 dis
        //  //datasel1->Fill();
        //  vars->Fill();
        //}
        }
      }
    }
    }
    
    }
    }
  }
  dir->Write();

  ofstream cutflow("cutflow2",std::ios::app);
  //ofstream cutflow("cutflow_cmp665andp01",std::ios::app);
  cutflow<<filename<<std::endl;
  //std::cout<<"data selction 2 size is "<<datasel2->GetEntries()<<" "<<count2<<std::endl;
  cutflow<<"Initial size      :"<<nentries<<std::endl;
  cutflow<<"Tagged ISR evt    :"<< tagmc  <<std::endl;
  cutflow<<"Tagged no ISR evt :"<< nentries - tagmc  <<std::endl;
  cutflow<<"Valid tof and emc :"<<count0<<std::endl;
  cutflow<<"After cos1<0.8    :"<<count[4]<<std::endl;
  cutflow<<"After cos2>-0.8   :"<<count[5]<<std::endl;
  cutflow<<"After theta cut   :"<<count[2]<<std::endl;
  cutflow<<"After ep1         :"<<count[0]<<std::endl;
  cutflow<<"After ep2         :"<<count[1]<<std::endl;
  //cutflow<<"After totp<0.05   :"<<count[3]<<std::endl;
  cutflow<<"After dtof<3      :"<<count3<<std::endl;
  //cutflow<<"p1-exp<0.08 for p1:"<<count1<<std::endl;
  cutflow<<"p1-exp<3 sigma    :"<<count2<<std::endl;

  return 0;
 
}
コード例 #2
0
ファイル: recoEff_histMaker.C プロジェクト: vlimant/usercode
//int ScanTree ( TTree* tree) {
int ScanTree ( TTree* tree, char *fileName) {

  // This reads in the tree.  As you might imagine.
  Init(tree);
  TFile *histFile = new TFile(fileName,"RECREATE");
  TDirectory *histDir = histFile->mkdir("eff_hist");
  TStopwatch timer;
  timer.Start();

  // Declare the usual constants
  double pi = 3.141592653;
  double ptCut = 5.0;
  vector<int> *l1UsedBySim = new vector<int>;
  vector<int> *l2UsedBySim = new vector<int>;
  vector<int> *l3UsedBySim = new vector<int>;

  histDir->cd();
  
  //  int nPtBins = 19;
  //  Double_t pt_Edges[20] = {0, 5, 7, 9, 11, 13, 15, 18, 21, 24, 27, 30, 34, 38, 42, 50, 60, 70, 80, 100};

  //  int nPtBins = 8;
  //  Double_t pt_Edges[9] = {0, 1, 2, 3, 4, 6, 8, 10, 20};

  int nPtBins = 11;
  Double_t pt_Edges[12] = {0, 3, 5, 7, 9, 11, 13, 15, 18, 21, 24, 30};

  TH1F *l3OverXEtaEffNum = new TH1F("l3OverXEtaEffNum","l3 associated sim #eta",24,-2.4,2.4);
  TH1F *l3OverXPtEffNum = new TH1F("l3OverXPtEffNum","l3 associated sim pt",nPtBins,pt_Edges);
  TH2F *l3OverXPtEtaEffNum = new TH2F("l3OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l2OverXEtaEffNum = new TH1F("l2OverXEtaEffNum","l2 associated sim #eta",24,-2.4,2.4);
  TH1F *l2OverXPtEffNum = new TH1F("l2OverXPtEffNum","l2 associated sim pt",nPtBins,pt_Edges);
  TH2F *l2OverXPtEtaEffNum = new TH2F("l2OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l1OverXEtaEffNum = new TH1F("l1OverXEtaEffNum","l1 associated sim #eta",24,-2.4,2.4);
  TH1F *l1OverXPtEffNum = new TH1F("l1OverXPtEffNum","l1 associated sim pt",nPtBins,pt_Edges);
  TH2F *l1OverXPtEtaEffNum = new TH2F("l1OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *lXOverSimEtaEffDenom = new TH1F("lXOverSimEtaEffDenom","sim #eta",24,-2.4,2.4);
  TH1F *lXOverSimPtEffDenom = new TH1F("lXOverSimPtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverSimPtEtaEffDenom = new TH2F("lXOverSimPtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);
  TH1F *lXOverL1EtaEffDenom = new TH1F("lXOverL1EtaEffDenom","l1 associated sim #eta",24,-2.4,2.4);
  TH1F *lXOverL1PtEffDenom = new TH1F("lXOverL1PtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverL1PtEtaEffDenom = new TH2F("lXOverL1PtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);
  TH1F *lXOverL2EtaEffDenom = new TH1F("lXOverL2EtaEffDenom","l2 associated sim #eta",24,-2.4,2.4);
  TH1F *lXOverL2PtEffDenom = new TH1F("lXOverL2PtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverL2PtEtaEffDenom = new TH2F("lXOverL2PtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverSimEtaEff = new TH1F("l3OverSimEtaEff","l3/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l3OverSimPtEff = new TH1F("l3OverSimPtEff","l3/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverSimPtEtaEff = new TH2F("l3OverSimPtEtaEff","l3/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverL1EtaEff = new TH1F("l3OverL1EtaEff","l3/l1 eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l3OverL1PtEff = new TH1F("l3OverL1PtEff","l3/l1 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverL1PtEtaEff = new TH2F("l3OverL1PtEtaEff","l3/l1 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverL2EtaEff = new TH1F("l3OverL2EtaEff","l3/l2 eff vs. assoc. sim #eta",24,-2.4,2.4);  
  TH1F *l3OverL2PtEff = new TH1F("l3OverL2PtEff","l3/l2 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverL2PtEtaEff = new TH2F("l3OverL2PtEtaEff","l3/l2 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);
  
  TH1F *l2OverSimEtaEff = new TH1F("l2OverSimEtaEff","l2/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l2OverSimPtEff = new TH1F("l2OverSimPtEff","l2/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l2OverSimPtEtaEff = new TH2F("l2OverSimPtEtaEff","l2/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l2OverL1EtaEff = new TH1F("l2OverL1EtaEff","l2/l1 eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l2OverL1PtEff = new TH1F("l2OverL1PtEff","l2/l1 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l2OverL1PtEtaEff = new TH2F("l2OverL1PtEtaEff","l2/l1 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);
 
  TH1F *l1OverSimEtaEff = new TH1F("l1OverSimEtaEff","l1/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l1OverSimPtEff = new TH1F("l1OverSimPtEff","l1/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l1OverSimPtEtaEff = new TH2F("l1OverSimPtEtaEff","l1/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);


  int nEntries = tree->GetEntries();
    
  //Event Loop
  for( int iEntry = 0; iEntry < nEntries; iEntry++) {
    if (iEntry%1000 == 0) cout << "Event " << iEntry << " time " << timer.RealTime() << " cpu " << timer.CpuTime() << endl;
    timer.Continue();
    tree->GetEntry(iEntry);
    
    double max_simPt = -999;

    try {
      //      cout << "trying passL1" << endl;
      bool passL1 = false;
      for (int iL1 = 0; iL1 < nL1; iL1++) {
	if ((*l1Pt).at(iL1) + 0.01 > 7 && (*l1Quality).at(iL1) >= 4) {
	  passL1 = true;
	}
      }

      //      cout << "trying passL2" << endl;
      bool passL2 = false;
      for (int iL2 = 0; iL2 < nL2; iL2++) {
	if (passL1) {
	  if ((*l2Pt).at(iL2) > 7 && (*l2Eta).at(iL2) > -2.5 && (*l2Eta).at(iL2) < 2.5) {
	    passL2 = true;
	  }
	}
      }
      bool passL3 = false;
      for (int iL3 = 0; iL3 < nL3; iL3++) {
	if (passL2) {
	  if ((*l3Pt).at(iL3) > 9 && (*l3Eta).at(iL3) > -2.5 && (*l3Eta).at(iL3) < 2.5) {
	    passL3 = true;
	  }
	}
      }

      for (int i = 0; i < nSimMuon; i++) {
	if ((*simMuonPt).at(i) > ptCut) {
	
	  lXOverSimEtaEffDenom->Fill((*simMuonEta).at(i));
	  lXOverSimPtEffDenom->Fill((*simMuonPt).at(i));
	  lXOverSimPtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	  //	cout << "trying to associate sim to L1 by DR" << endl;
	  int l1Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l1Eta,l1Phi,1.0,l1UsedBySim);
	  int l2Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l2Eta,l2Phi,0.2,l2UsedBySim);
	  int l3Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l3Eta,l3Phi,0.1,l3UsedBySim);
	  if (l1Index >= 0) {
	    l1OverXEtaEffNum->Fill((*simMuonEta).at(i));
	    l1OverXPtEffNum->Fill((*simMuonPt).at(i));
	    l1OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));
	    
	    lXOverL1EtaEffDenom->Fill((*simMuonEta).at(i));
	    lXOverL1PtEffDenom->Fill((*simMuonPt).at(i));
	    lXOverL1PtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	    if (l2Index >= 0) {
	      l2OverXEtaEffNum->Fill((*simMuonEta).at(i));
	      l2OverXPtEffNum->Fill((*simMuonPt).at(i));
	      l2OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));	      

	      lXOverL2EtaEffDenom->Fill((*simMuonEta).at(i));
	      lXOverL2PtEffDenom->Fill((*simMuonPt).at(i));
	      lXOverL2PtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	      if (l3Index >= 0) {
		l3OverXEtaEffNum->Fill((*simMuonEta).at(i));
		l3OverXPtEffNum->Fill((*simMuonPt).at(i));
		l3OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));	      
	      } // l3 associated
	    } // l2 associated
	  } // l1 associated
	} // cut on sim pT
      } // loop over sim
      (*l1UsedBySim).clear();
      (*l2UsedBySim).clear();
      (*l3UsedBySim).clear();
    } // try statement
    catch (...) {
      cout << "F**k it.  Pressing on" << endl;
    }
  } // event loop
  
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverSimEtaEffDenom,l3OverSimEtaEff);
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverL1EtaEffDenom,l3OverL1EtaEff);
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverL2EtaEffDenom,l3OverL2EtaEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverSimPtEffDenom,l3OverSimPtEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverL1PtEffDenom,l3OverL1PtEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverL2PtEffDenom,l3OverL2PtEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l3OverSimPtEtaEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverL1PtEtaEffDenom,l3OverL1PtEtaEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverL2PtEtaEffDenom,l3OverL2PtEtaEff);

  divide_histos_and_errors(l2OverXEtaEffNum,lXOverSimEtaEffDenom,l2OverSimEtaEff);
  divide_histos_and_errors(l2OverXEtaEffNum,lXOverL1EtaEffDenom,l2OverL1EtaEff);
  divide_histos_and_errors(l2OverXPtEffNum,lXOverSimPtEffDenom,l2OverSimPtEff);
  divide_histos_and_errors(l2OverXPtEffNum,lXOverL1PtEffDenom,l2OverL1PtEff);  
  divide_TH2_histos_and_errors(l2OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l2OverSimPtEtaEff);
  divide_TH2_histos_and_errors(l2OverXPtEtaEffNum,lXOverL1PtEtaEffDenom,l2OverL1PtEtaEff);

  divide_histos_and_errors(l1OverXEtaEffNum,lXOverSimEtaEffDenom,l1OverSimEtaEff);
  divide_histos_and_errors(l1OverXPtEffNum,lXOverSimPtEffDenom,l1OverSimPtEff);
  divide_TH2_histos_and_errors(l1OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l1OverSimPtEtaEff);
 
  histDir->Write("",TObject::kOverwrite);
  
  return 0;
}
コード例 #3
0
ファイル: rate_histMaker.C プロジェクト: vlimant/usercode
//int ScanTree ( TTree* tree) {
int ScanTree ( TTree* tree, char *fileName) {

  // This reads in the tree.  As you might imagine.
  Init(tree);
  TFile *histFile = new TFile(fileName,"RECREATE");
  TDirectory *histDir = histFile->mkdir("rate_hist");

  histDir->cd();

  // Time for some histograms and stacks

  double ptUpperLimit = 15;
  TH1F *l2PtRate_motherBin_1 = new TH1F("l2PtRate_motherBin_1"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_2 = new TH1F("l2PtRate_motherBin_2"," K",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_3 = new TH1F("l2PtRate_motherBin_3"," D",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_4 = new TH1F("l2PtRate_motherBin_4"," B",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_5 = new TH1F("l2PtRate_motherBin_5"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_6 = new TH1F("l2PtRate_motherBin_6"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_10 = new TH1F("l2PtRate_motherBin_10"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_12 = new TH1F("l2PtRate_motherBin_12"," other",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_13 = new TH1F("l2PtRate_motherBin_13"," non-associated",60,0,ptUpperLimit);

  TH1F *l2PtRate_motherBin_1_cd = new TH1F("l2PtRate_motherBin_1_cd"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_2_cd = new TH1F("l2PtRate_motherBin_2_cd"," K",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_3_cd = new TH1F("l2PtRate_motherBin_3_cd"," D",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_4_cd = new TH1F("l2PtRate_motherBin_4_cd"," B",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_5_cd = new TH1F("l2PtRate_motherBin_5_cd"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_6_cd = new TH1F("l2PtRate_motherBin_6_cd"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_10_cd = new TH1F("l2PtRate_motherBin_10_cd"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_12_cd = new TH1F("l2PtRate_motherBin_12_cd"," other",60,0,ptUpperLimit);
  TH1F *l2PtRate_motherBin_13_cd = new TH1F("l2PtRate_motherBin_13_cd"," non-associated",60,0,ptUpperLimit);

  TH1F *l2IsoPtRate_motherBin_1 = new TH1F("l2IsoPtRate_motherBin_1"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_2 = new TH1F("l2IsoPtRate_motherBin_2"," K",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_3 = new TH1F("l2IsoPtRate_motherBin_3"," D",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_4 = new TH1F("l2IsoPtRate_motherBin_4"," B",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_5 = new TH1F("l2IsoPtRate_motherBin_5"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_6 = new TH1F("l2IsoPtRate_motherBin_6"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_10 = new TH1F("l2IsoPtRate_motherBin_10"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_12 = new TH1F("l2IsoPtRate_motherBin_12"," other",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_13 = new TH1F("l2IsoPtRate_motherBin_13"," non-associated",60,0,ptUpperLimit);

  TH1F *l2IsoPtRate_motherBin_1_cd = new TH1F("l2IsoPtRate_motherBin_1_cd"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_2_cd = new TH1F("l2IsoPtRate_motherBin_2_cd"," K",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_3_cd = new TH1F("l2IsoPtRate_motherBin_3_cd"," D",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_4_cd = new TH1F("l2IsoPtRate_motherBin_4_cd"," B",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_5_cd = new TH1F("l2IsoPtRate_motherBin_5_cd"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_6_cd = new TH1F("l2IsoPtRate_motherBin_6_cd"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_10_cd = new TH1F("l2IsoPtRate_motherBin_10_cd"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_12_cd = new TH1F("l2IsoPtRate_motherBin_12_cd"," other",60,0,ptUpperLimit);
  TH1F *l2IsoPtRate_motherBin_13_cd = new TH1F("l2IsoPtRate_motherBin_13_cd"," non-associated",60,0,ptUpperLimit);

  THStack *l2PtRate = new THStack("l2PtRate","L2 rate as f(p_{T,L2})");
  THStack *l2PtRate_cd = new THStack("l2PtRate_cd","L2 rate as f(p_{T,L2})");
  THStack *l2IsoPtRate = new THStack("l2IsoPtRate","L2Iso rate as f(p_{T,L2})");
  THStack *l2IsoPtRate_cd = new THStack("l2IsoPtRate_cd","L2Iso rate as f(p_{T,L2})");

  TH1F *l3PtRate_motherBin_1 = new TH1F("l3PtRate_motherBin_1"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_2 = new TH1F("l3PtRate_motherBin_2"," K",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_3 = new TH1F("l3PtRate_motherBin_3"," D",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_4 = new TH1F("l3PtRate_motherBin_4"," B",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_5 = new TH1F("l3PtRate_motherBin_5"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_6 = new TH1F("l3PtRate_motherBin_6"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_10 = new TH1F("l3PtRate_motherBin_10"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_12 = new TH1F("l3PtRate_motherBin_12"," other",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_13 = new TH1F("l3PtRate_motherBin_13"," non-associated",60,0,ptUpperLimit);

  TH1F *l3PtRate_motherBin_1_cd = new TH1F("l3PtRate_motherBin_1_cd"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_2_cd = new TH1F("l3PtRate_motherBin_2_cd"," K",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_3_cd = new TH1F("l3PtRate_motherBin_3_cd"," D",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_4_cd = new TH1F("l3PtRate_motherBin_4_cd"," B",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_5_cd = new TH1F("l3PtRate_motherBin_5_cd"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_6_cd = new TH1F("l3PtRate_motherBin_6_cd"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_10_cd = new TH1F("l3PtRate_motherBin_10_cd"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_12_cd = new TH1F("l3PtRate_motherBin_12_cd"," other",60,0,ptUpperLimit);
  TH1F *l3PtRate_motherBin_13_cd = new TH1F("l3PtRate_motherBin_13_cd"," non-associated",60,0,ptUpperLimit);

  TH1F *l3PreIsoPtRate_motherBin_1 = new TH1F("l3PreIsoPtRate_motherBin_1"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_2 = new TH1F("l3PreIsoPtRate_motherBin_2"," K",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_3 = new TH1F("l3PreIsoPtRate_motherBin_3"," D",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_4 = new TH1F("l3PreIsoPtRate_motherBin_4"," B",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_5 = new TH1F("l3PreIsoPtRate_motherBin_5"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_6 = new TH1F("l3PreIsoPtRate_motherBin_6"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_10 = new TH1F("l3PreIsoPtRate_motherBin_10"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_12 = new TH1F("l3PreIsoPtRate_motherBin_12"," other",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_13 = new TH1F("l3PreIsoPtRate_motherBin_13"," non-associated",60,0,ptUpperLimit);

  TH1F *l3PreIsoPtRate_motherBin_1_cd = new TH1F("l3PreIsoPtRate_motherBin_1_cd"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_2_cd = new TH1F("l3PreIsoPtRate_motherBin_2_cd"," K",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_3_cd = new TH1F("l3PreIsoPtRate_motherBin_3_cd"," D",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_4_cd = new TH1F("l3PreIsoPtRate_motherBin_4_cd"," B",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_5_cd = new TH1F("l3PreIsoPtRate_motherBin_5_cd"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_6_cd = new TH1F("l3PreIsoPtRate_motherBin_6_cd"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_10_cd = new TH1F("l3PreIsoPtRate_motherBin_10_cd"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_12_cd = new TH1F("l3PreIsoPtRate_motherBin_12_cd"," other",60,0,ptUpperLimit);
  TH1F *l3PreIsoPtRate_motherBin_13_cd = new TH1F("l3PreIsoPtRate_motherBin_13_cd"," non-associated",60,0,ptUpperLimit);

  TH1F *l3IsoPtRate_motherBin_1 = new TH1F("l3IsoPtRate_motherBin_1"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_2 = new TH1F("l3IsoPtRate_motherBin_2"," K",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_3 = new TH1F("l3IsoPtRate_motherBin_3"," D",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_4 = new TH1F("l3IsoPtRate_motherBin_4"," B",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_5 = new TH1F("l3IsoPtRate_motherBin_5"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_6 = new TH1F("l3IsoPtRate_motherBin_6"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_10 = new TH1F("l3IsoPtRate_motherBin_10"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_12 = new TH1F("l3IsoPtRate_motherBin_12"," other",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_13 = new TH1F("l3IsoPtRate_motherBin_13"," non-associated",60,0,ptUpperLimit);

  TH1F *l3IsoPtRate_motherBin_1_cd = new TH1F("l3IsoPtRate_motherBin_1_cd"," #pi^{+/-}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_2_cd = new TH1F("l3IsoPtRate_motherBin_2_cd"," K",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_3_cd = new TH1F("l3IsoPtRate_motherBin_3_cd"," D",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_4_cd = new TH1F("l3IsoPtRate_motherBin_4_cd"," B",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_5_cd = new TH1F("l3IsoPtRate_motherBin_5_cd"," #Lambda_{b}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_6_cd = new TH1F("l3IsoPtRate_motherBin_6_cd"," J/#Psi",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_10_cd = new TH1F("l3IsoPtRate_motherBin_10_cd"," #tau^{+/-}",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_12_cd = new TH1F("l3IsoPtRate_motherBin_12_cd"," other",60,0,ptUpperLimit);
  TH1F *l3IsoPtRate_motherBin_13_cd = new TH1F("l3IsoPtRate_motherBin_13_cd"," non-associated",60,0,ptUpperLimit);

  THStack *l3PtRate = new THStack("l3PtRate","L3 rate as f(p_{T,L3})");
  THStack *l3PtRate_cd = new THStack("l3PtRate_cd","L3 rate as f(p_{T,L3})");
  THStack *l3PreIsoPtRate = new THStack("l3PreIsoPtRate","L3PreIso rate as f(p_{T,L3})");
  THStack *l3PreIsoPtRate_cd = new THStack("l3PreIsoPtRate_cd","L3PreIso rate as f(p_{T,L3})");
  THStack *l3IsoPtRate = new THStack("l3IsoPtRate","L3Iso rate as f(p_{T,L3})");
  THStack *l3IsoPtRate_cd = new THStack("l3IsoPtRate_cd","L3Iso rate as f(p_{T,L3})");

  // There are many comments to be made about ROOT.  Most of them Rated-R or Rated-X.
  // Here, I'll limit myself to saying this is necessary for the TStacks to go into the TDirectory.
  gDirectory->Append(l2PtRate);
  gDirectory->Append(l3PtRate);
  gDirectory->Append(l2IsoPtRate);
  gDirectory->Append(l3PreIsoPtRate);
  gDirectory->Append(l3IsoPtRate);
  gDirectory->Append(l2PtRate_cd);
  gDirectory->Append(l3PtRate_cd);
  gDirectory->Append(l2IsoPtRate_cd);
  gDirectory->Append(l3PreIsoPtRate_cd);
  gDirectory->Append(l3IsoPtRate_cd);

  int l2OverFlow[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  int l3OverFlow[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  int l2IsoOverFlow[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  int l3PreIsoOverFlow[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  int l3IsoOverFlow[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};

  int nEntries = tree->GetEntries();
    
  //Event Loop
  for( int iEntry = 0; iEntry < nEntries; iEntry++) {
    if (iEntry%1000 == 0) cout << "Event " << iEntry << endl;
    tree->GetEntry(iEntry);
    
    bool passL1 = false;
    for (int i = 0; i < nL1; i++) {
      //      if ((*l1Pt).at(i) + 0.01 > 7 && (*l1Quality).at(i) >= 4) {
      if ((*l1Pt).at(i) + 0.01 > 0 && (*l1Quality).at(i) >= 4) {
	passL1 = true;
	continue;
      }
    }
    bool passL2 = false;
    bool passL2Iso = false;
    if (passL1) {
      for (int i = 0; i < nL2; i++) {
        //      if ((*l2Pt).at(i) > 7 && abs((*l2Eta).at(i)) < 2.5) {
        if ((*l2Pt).at(i) > 0 && abs((*l2Eta).at(i)) < 2.5) {
          passL2 = true;
          if ((*l2CalIsoDeposit).at(i) < etaCalIsoDepositCut((*l2Eta).at(i))) {
            passL2Iso = true;
            continue;
          }
        }
      }
    }
    bool passL3 = false;
    bool passL3pre = false;
    bool passL3iso = false;
    if (passL2) {
      for (int i = 0; i < nL3; i++) {
        //        if ((*l3Pt).at(i) > 9 && fabs((*l3Eta).at(i)) < 2.5 && abs((*l3D0).at(i)) < 0.2) {
        if ((*l3Pt).at(i) > 0 && fabs((*l3Eta).at(i)) < 2.5 && abs((*l3D0).at(i)) < 0.2) {
          passL3 = true;
          continue;
        }
      }
    }
    if (passL2Iso) {
      for (int i = 0; i < nL3; i++) {
        //        if ((*l3Pt).at(i) > 7 && fabs((*l3Eta).at(i)) < 2.5 && abs((*l3D0).at(i)) < 0.2) {
        if ((*l3Pt).at(i) > 0 && fabs((*l3Eta).at(i)) < 2.5 && abs((*l3D0).at(i)) < 0.2) {
          passL3pre = true;
	  if ((*l3TrackIsoDeposit).at(i) < etaTrackIsoDepositCut((*l2Eta).at(i))) {
	    passL3iso = true;
	    continue;
	  }
        }
      }
    }

    if (passL2) {
      double pt_L2 = findMaxPt(l2Pt);
      int l2_index = findIndexOfMaxPt(l2Pt);
      if ((*l2IsAssociated).at(l2_index) == 1 && (*l2AssociationVar).at(l2_index) > -0.2) {
	switch ( (*l2MotherBinNumber).at(l2_index) ) {
	case 1 :
	  l2PtRate_motherBin_1->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[0] ++;
	  break;
	case 2 :
	  l2PtRate_motherBin_2->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[1] ++;
	  break;
	case 3 :
	  l2PtRate_motherBin_3->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[2] ++;
	  break;
	case 4 :
	  l2PtRate_motherBin_4->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[3] ++;
	  break;
	case 5 :
	  l2PtRate_motherBin_5->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[4] ++;
	  break;
	case 6 :
	  l2PtRate_motherBin_6->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[5] ++;
	  break;
	case 10 :
	  l2PtRate_motherBin_10->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[9] ++;
	  break;
	case 12 :
	  l2PtRate_motherBin_12->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[11] ++;
	  break;
	default :
	  //      cout << "this is interesting" << endl;
	  l2PtRate_motherBin_12->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2OverFlow[11] ++;
	}
      }
      else {
	l2PtRate_motherBin_13->Fill(pt_L2);
	if (pt_L2 > ptUpperLimit) l2OverFlow[12] ++;
      }
    }

    if (passL2Iso) {
      double pt_L2 = findMaxPt(l2Pt);
      int l2_index = findIndexOfMaxPt(l2Pt);
      if ((*l2IsAssociated).at(l2_index) == 1 && (*l2AssociationVar).at(l2_index) > -0.2) {
	switch ( (*l2MotherBinNumber).at(l2_index) ) {
	case 1 :
	  l2IsoPtRate_motherBin_1->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[0] ++;
	  break;
	case 2 :
	  l2IsoPtRate_motherBin_2->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[1] ++;
	  break;
	case 3 :
	  l2IsoPtRate_motherBin_3->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[2] ++;
	  break;
	case 4 :
	  l2IsoPtRate_motherBin_4->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[3] ++;
	  break;
	case 5 :
	  l2IsoPtRate_motherBin_5->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[4] ++;
	  break;
	case 6 :
	  l2IsoPtRate_motherBin_6->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[5] ++;
	  break;
	case 10 :
	  l2IsoPtRate_motherBin_10->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[9] ++;
	  break;
	case 12 :
	  l2IsoPtRate_motherBin_12->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[11] ++;
	  break;
	default :
	  //           cout << "this is interesting" << endl;
	  l2IsoPtRate_motherBin_12->Fill(pt_L2);
	  if (pt_L2 > ptUpperLimit) l2IsoOverFlow[11] ++;
	}
      }
      else {
	l2IsoPtRate_motherBin_13->Fill(pt_L2);
	if (pt_L2 > ptUpperLimit) l2IsoOverFlow[12] ++;
      }
    }

    if (passL3) {
      double pt_L3 = findMaxPt(l3Pt,l3Eta,l3D0);
      int l3_index = findIndexOfMaxPt(l3Pt,l3Eta,l3D0);
      if ((*l3IsAssociated).at(l3_index) == 1 && (*l3AssociationVar).at(l3_index) > -0.1) {
	switch ( (*l3MotherBinNumber).at(l3_index) ) {
	case 1 :
	  l3PtRate_motherBin_1->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[0] ++;
	  break;
	case 2 :
	  l3PtRate_motherBin_2->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[1] ++;
	  break;
	case 3 :
	  l3PtRate_motherBin_3->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[2] ++;
	  break;
	case 4 :
	  l3PtRate_motherBin_4->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[3] ++;
	  break;
	case 5 :
	  l3PtRate_motherBin_5->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[4] ++;
	  break;
	case 6 :
	  l3PtRate_motherBin_6->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[5] ++;
	  break;
	case 10 :
	  l3PtRate_motherBin_10->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[9] ++;
	  break;
	case 12 :
	  l3PtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[11] ++;
	  break;
	default :
	  //           cout << "oh, c**k, this is interesting" << endl;
	  l3PtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3OverFlow[11] ++;
	}
      }
      else {
	l3PtRate_motherBin_13->Fill(pt_L3);
	if (pt_L3 > ptUpperLimit) l3OverFlow[12] ++;
      }
    }
    
    if (passL3pre) {
      double pt_L3 = findMaxPt(l3Pt,l3Eta,l3D0);
      int l3_index = findIndexOfMaxPt(l3Pt,l3Eta,l3D0);
      if ((*l3IsAssociated).at(l3_index) == 1 && (*l3AssociationVar).at(l3_index) > -0.1) {
	switch ( (*l3MotherBinNumber).at(l3_index) ) {
	case 1 :
	  l3PreIsoPtRate_motherBin_1->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[0] ++;
	  break;
	case 2 :
	  l3PreIsoPtRate_motherBin_2->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[1] ++;
	  break;
	case 3 :
	  l3PreIsoPtRate_motherBin_3->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[2] ++;
	  break;
	case 4 :
	  l3PreIsoPtRate_motherBin_4->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[3] ++;
	  break;
	case 5 :
	  l3PreIsoPtRate_motherBin_5->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[4] ++;
	  break;
	case 6 :
	  l3PreIsoPtRate_motherBin_6->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[5] ++;
	  break;
	case 10 :
	  l3PreIsoPtRate_motherBin_10->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[9] ++;
	  break;
	case 12 :
	  l3PreIsoPtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[11] ++;
	  break;
	default :
	  //           cout << "oh, c**k, this is interesting" << endl;
	  l3PreIsoPtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[11] ++;
	}
      }
      else {
	l3PreIsoPtRate_motherBin_13->Fill(pt_L3);
	if (pt_L3 > ptUpperLimit) l3PreIsoOverFlow[12] ++;
      }
    }

    if (passL3iso) {
      double pt_L3 = findMaxPt(l3Pt,l3Eta,l3D0);
      int l3_index = findIndexOfMaxPt(l3Pt,l3Eta,l3D0);
      if ((*l3IsAssociated).at(l3_index) == 1 && (*l3AssociationVar).at(l3_index) > -0.1) {
	switch ( (*l3MotherBinNumber).at(l3_index) ) {
	case 1 :
	  l3IsoPtRate_motherBin_1->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[0] ++;
	  break;
	case 2 :
	  l3IsoPtRate_motherBin_2->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[1] ++;
	  break;
	case 3 :
	  l3IsoPtRate_motherBin_3->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[2] ++;
	  break;
	case 4 :
	  l3IsoPtRate_motherBin_4->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[3] ++;
	  break;
	case 5 :
	  l3IsoPtRate_motherBin_5->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[4] ++;
	  break;
	case 6 :
	  l3IsoPtRate_motherBin_6->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[5] ++;
	  break;
	case 10 :
	  l3IsoPtRate_motherBin_10->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[9] ++;
	  break;
	case 12 :
	  l3IsoPtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[11] ++;
	  break;
	default :
	  //           cout << "oh, c**k, this is interesting" << endl;
	  l3IsoPtRate_motherBin_12->Fill(pt_L3);
	  if (pt_L3 > ptUpperLimit) l3IsoOverFlow[11] ++;
	}
      }
      else {
	l3IsoPtRate_motherBin_13->Fill(pt_L3);
	if (pt_L3 > ptUpperLimit) l3IsoOverFlow[12] ++;
      }
    }
  }

  fill_overflow(l2PtRate_motherBin_1,l2OverFlow[0]);
  fill_overflow(l2PtRate_motherBin_2,l2OverFlow[1]);
  fill_overflow(l2PtRate_motherBin_3,l2OverFlow[2]);
  fill_overflow(l2PtRate_motherBin_4,l2OverFlow[3]);
  fill_overflow(l2PtRate_motherBin_5,l2OverFlow[4]);
  fill_overflow(l2PtRate_motherBin_6,l2OverFlow[5]);
  fill_overflow(l2PtRate_motherBin_10,l2OverFlow[9]);
  fill_overflow(l2PtRate_motherBin_12,l2OverFlow[11]);
  fill_overflow(l2PtRate_motherBin_13,l2OverFlow[12]);

  fill_overflow(l3PtRate_motherBin_1,l3OverFlow[0]);
  fill_overflow(l3PtRate_motherBin_2,l3OverFlow[1]);
  fill_overflow(l3PtRate_motherBin_3,l3OverFlow[2]);
  fill_overflow(l3PtRate_motherBin_4,l3OverFlow[3]);
  fill_overflow(l3PtRate_motherBin_5,l3OverFlow[4]);
  fill_overflow(l3PtRate_motherBin_6,l3OverFlow[5]);
  fill_overflow(l3PtRate_motherBin_10,l3OverFlow[9]);
  fill_overflow(l3PtRate_motherBin_12,l3OverFlow[11]);
  fill_overflow(l3PtRate_motherBin_13,l3OverFlow[12]);

  fill_overflow(l2IsoPtRate_motherBin_1,l2IsoOverFlow[0]);
  fill_overflow(l2IsoPtRate_motherBin_2,l2IsoOverFlow[1]);
  fill_overflow(l2IsoPtRate_motherBin_3,l2IsoOverFlow[2]);
  fill_overflow(l2IsoPtRate_motherBin_4,l2IsoOverFlow[3]);
  fill_overflow(l2IsoPtRate_motherBin_5,l2IsoOverFlow[4]);
  fill_overflow(l2IsoPtRate_motherBin_6,l2IsoOverFlow[5]);
  fill_overflow(l2IsoPtRate_motherBin_10,l2IsoOverFlow[9]);
  fill_overflow(l2IsoPtRate_motherBin_12,l2IsoOverFlow[11]);
  fill_overflow(l2IsoPtRate_motherBin_13,l2IsoOverFlow[12]);

  fill_overflow(l3PreIsoPtRate_motherBin_1,l3PreIsoOverFlow[0]);
  fill_overflow(l3PreIsoPtRate_motherBin_2,l3PreIsoOverFlow[1]);
  fill_overflow(l3PreIsoPtRate_motherBin_3,l3PreIsoOverFlow[2]);
  fill_overflow(l3PreIsoPtRate_motherBin_4,l3PreIsoOverFlow[3]);
  fill_overflow(l3PreIsoPtRate_motherBin_5,l3PreIsoOverFlow[4]);
  fill_overflow(l3PreIsoPtRate_motherBin_6,l3PreIsoOverFlow[5]);
  fill_overflow(l3PreIsoPtRate_motherBin_10,l3PreIsoOverFlow[9]);
  fill_overflow(l3PreIsoPtRate_motherBin_12,l3PreIsoOverFlow[11]);
  fill_overflow(l3PreIsoPtRate_motherBin_13,l3PreIsoOverFlow[12]);

  fill_overflow(l3IsoPtRate_motherBin_1,l3IsoOverFlow[0]);
  fill_overflow(l3IsoPtRate_motherBin_2,l3IsoOverFlow[1]);
  fill_overflow(l3IsoPtRate_motherBin_3,l3IsoOverFlow[2]);
  fill_overflow(l3IsoPtRate_motherBin_4,l3IsoOverFlow[3]);
  fill_overflow(l3IsoPtRate_motherBin_5,l3IsoOverFlow[4]);
  fill_overflow(l3IsoPtRate_motherBin_6,l3IsoOverFlow[5]);
  fill_overflow(l3IsoPtRate_motherBin_10,l3IsoOverFlow[9]);
  fill_overflow(l3IsoPtRate_motherBin_12,l3IsoOverFlow[11]);
  fill_overflow(l3IsoPtRate_motherBin_13,l3IsoOverFlow[12]);

  make_cd_from_histo(l2PtRate_motherBin_1,l2PtRate_motherBin_1_cd);
  make_cd_from_histo(l2PtRate_motherBin_2,l2PtRate_motherBin_2_cd);
  make_cd_from_histo(l2PtRate_motherBin_3,l2PtRate_motherBin_3_cd);
  make_cd_from_histo(l2PtRate_motherBin_4,l2PtRate_motherBin_4_cd);
  make_cd_from_histo(l2PtRate_motherBin_5,l2PtRate_motherBin_5_cd);
  make_cd_from_histo(l2PtRate_motherBin_6,l2PtRate_motherBin_6_cd);
  make_cd_from_histo(l2PtRate_motherBin_10,l2PtRate_motherBin_10_cd);
  make_cd_from_histo(l2PtRate_motherBin_12,l2PtRate_motherBin_12_cd);
  make_cd_from_histo(l2PtRate_motherBin_13,l2PtRate_motherBin_13_cd);

  make_cd_from_histo(l3PtRate_motherBin_1,l3PtRate_motherBin_1_cd);
  make_cd_from_histo(l3PtRate_motherBin_2,l3PtRate_motherBin_2_cd);
  make_cd_from_histo(l3PtRate_motherBin_3,l3PtRate_motherBin_3_cd);
  make_cd_from_histo(l3PtRate_motherBin_4,l3PtRate_motherBin_4_cd);
  make_cd_from_histo(l3PtRate_motherBin_5,l3PtRate_motherBin_5_cd);
  make_cd_from_histo(l3PtRate_motherBin_6,l3PtRate_motherBin_6_cd);
  make_cd_from_histo(l3PtRate_motherBin_10,l3PtRate_motherBin_10_cd);
  make_cd_from_histo(l3PtRate_motherBin_12,l3PtRate_motherBin_12_cd);
  make_cd_from_histo(l3PtRate_motherBin_13,l3PtRate_motherBin_13_cd);

  make_cd_from_histo(l2IsoPtRate_motherBin_1,l2IsoPtRate_motherBin_1_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_2,l2IsoPtRate_motherBin_2_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_3,l2IsoPtRate_motherBin_3_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_4,l2IsoPtRate_motherBin_4_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_5,l2IsoPtRate_motherBin_5_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_6,l2IsoPtRate_motherBin_6_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_10,l2IsoPtRate_motherBin_10_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_12,l2IsoPtRate_motherBin_12_cd);
  make_cd_from_histo(l2IsoPtRate_motherBin_13,l2IsoPtRate_motherBin_13_cd);

  make_cd_from_histo(l3PreIsoPtRate_motherBin_1,l3PreIsoPtRate_motherBin_1_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_2,l3PreIsoPtRate_motherBin_2_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_3,l3PreIsoPtRate_motherBin_3_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_4,l3PreIsoPtRate_motherBin_4_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_5,l3PreIsoPtRate_motherBin_5_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_6,l3PreIsoPtRate_motherBin_6_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_10,l3PreIsoPtRate_motherBin_10_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_12,l3PreIsoPtRate_motherBin_12_cd);
  make_cd_from_histo(l3PreIsoPtRate_motherBin_13,l3PreIsoPtRate_motherBin_13_cd);

  make_cd_from_histo(l3IsoPtRate_motherBin_1,l3IsoPtRate_motherBin_1_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_2,l3IsoPtRate_motherBin_2_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_3,l3IsoPtRate_motherBin_3_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_4,l3IsoPtRate_motherBin_4_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_5,l3IsoPtRate_motherBin_5_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_6,l3IsoPtRate_motherBin_6_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_10,l3IsoPtRate_motherBin_10_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_12,l3IsoPtRate_motherBin_12_cd);
  make_cd_from_histo(l3IsoPtRate_motherBin_13,l3IsoPtRate_motherBin_13_cd);

  l2PtRate->Add(l2PtRate_motherBin_1);
  l2PtRate->Add(l2PtRate_motherBin_2);
  l2PtRate->Add(l2PtRate_motherBin_3);
  l2PtRate->Add(l2PtRate_motherBin_4);
  l2PtRate->Add(l2PtRate_motherBin_5);
  l2PtRate->Add(l2PtRate_motherBin_6);
  l2PtRate->Add(l2PtRate_motherBin_10);
  l2PtRate->Add(l2PtRate_motherBin_12);
  l2PtRate->Add(l2PtRate_motherBin_13);

  l2PtRate_cd->Add(l2PtRate_motherBin_1_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_2_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_3_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_4_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_5_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_6_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_10_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_12_cd);
  l2PtRate_cd->Add(l2PtRate_motherBin_13_cd);

  l3PtRate->Add(l3PtRate_motherBin_1);
  l3PtRate->Add(l3PtRate_motherBin_2);
  l3PtRate->Add(l3PtRate_motherBin_3);
  l3PtRate->Add(l3PtRate_motherBin_4);
  l3PtRate->Add(l3PtRate_motherBin_5);
  l3PtRate->Add(l3PtRate_motherBin_6);
  l3PtRate->Add(l3PtRate_motherBin_10);
  l3PtRate->Add(l3PtRate_motherBin_12);
  l3PtRate->Add(l3PtRate_motherBin_13);

  l3PtRate_cd->Add(l3PtRate_motherBin_1_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_2_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_3_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_4_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_5_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_6_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_10_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_12_cd);
  l3PtRate_cd->Add(l3PtRate_motherBin_13_cd);

  l2IsoPtRate->Add(l2IsoPtRate_motherBin_1);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_2);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_3);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_4);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_5);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_6);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_10);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_12);
  l2IsoPtRate->Add(l2IsoPtRate_motherBin_13);

  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_1_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_2_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_3_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_4_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_5_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_6_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_10_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_12_cd);
  l2IsoPtRate_cd->Add(l2IsoPtRate_motherBin_13_cd);

  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_1);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_2);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_3);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_4);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_5);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_6);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_10);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_12);
  l3PreIsoPtRate->Add(l3PreIsoPtRate_motherBin_13);

  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_1_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_2_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_3_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_4_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_5_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_6_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_10_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_12_cd);
  l3PreIsoPtRate_cd->Add(l3PreIsoPtRate_motherBin_13_cd);

  l3IsoPtRate->Add(l3IsoPtRate_motherBin_1);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_2);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_3);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_4);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_5);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_6);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_10);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_12);
  l3IsoPtRate->Add(l3IsoPtRate_motherBin_13);

  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_1_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_2_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_3_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_4_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_5_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_6_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_10_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_12_cd);
  l3IsoPtRate_cd->Add(l3IsoPtRate_motherBin_13_cd);

  //  double crossSection_mb = 75.28; // MinBias
  //  double crossSection_mb = 51.6; // ppMuX
  double crossSection_mb = 48.44; // ppMuX @ 7 TeV
  //  double crossSection_mb = 0.000317 ; // ttbar
  //  double filterEff = 1.0; //MinBias
  //  double filterEff = 0.00289; // ppMuX
  double filterEff = 0.00176; // ppMuX @ 7 TeV
  //   double filterEff = 0.33;  // ttbar

  double eventNumberUnit= 1000000;
  //   double filterEff = inclusiveppMuX_filterEff;
  double numberOfEvents = nEntries /eventNumberUnit /filterEff ;
  cout << "numberOfEvents = " << numberOfEvents << endl;

  double crossSection=crossSection_mb ;
  double L_E32= 100000; //10^32 cm-2.s-1 = 10^5 mb-1.s-1
  double L_E31= 10000; //10^31 cm-2.s-1 = 10^4 mb-1.s-1
  double L_E30= 1000; //10^30 cm-2.s-1 = 10^3 mb-1.s-1
  double L=L_E30;

  double mbInvToHz=L/eventNumberUnit;
  double rateFactor=crossSection / numberOfEvents * mbInvToHz ;
  cout << "rateFactor = " << rateFactor << endl;

  histDir->cd();
  scaleToRate("*PtRate*", rateFactor);


  histDir->Write("",TObject::kOverwrite);
  
  return 0;
}