示例#1
0
bool CLogicLikeCal::CheckAndFormat(string strExp /* = "" */){
	//if(strExp.empty()!=0) 对于string对象,""不等于empty
	if(strExp!="") 
		m_strExp = strExp;
	if(m_strExp.empty() == true) return false;
	
	string::iterator iter, begin,end;
	begin = m_strExp.begin();
	end = m_strExp.end();
	
	//check if every element in the string is fit the expression
	for(iter = begin;iter!=end;iter++){
		if(!IsAccept(*iter)) return false;
	}

	////////////////////////
	//cout<<endl<<strtmp;
	//////////////////////////
	
	
	if(CheckRegulation()==false){
		//cout<<endl<<"wrong regulation!";
		return false;
	}
	
	return true;
}
示例#2
0
void amyDropCanvas::dragEnterEvent(QDragEnterEvent *event)
{
	VTYPE type=amyShuttle::GetInstance()->GetType();
	if(IsAccept(type))
	{
		 event->acceptProposedAction();
	}
}
void pbpbEffJpsiSFSysSTA__idx_(double minPt = 6.5, double maxPt = 30.0)   
{
  int Prompt =1; int PutWeight = 1;

  int JpsiCat_ = 1; // 1 : prompt, 2 : non-prompt

  double minRap = 1.6; double maxRap = 2.4;
  minPt = 3.0; maxPt = 30.0;

  bool bDefault = true; // true : default, false : sailor or cowboy

  char cCd[512];
  if(bDefault) sprintf(cCd, "default");

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(0);
  gStyle->SetCanvasColor(0);
  gStyle->SetTitleFillColor(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadBorderSize(0);
  gStyle->SetCanvasBorderSize(0);
  gStyle->SetOptTitle(0); // at least most of the time
  gStyle->SetOptStat(0); // most of the time, sometimes "nemriou" might be useful to display name, 
  //number of entries, mean, rms, integral, overflow and underflow
  gStyle->SetOptFit(0); // set to 1 only if you want to display fit results
  //==================================== Define Histograms====================================================

  //==============================================Define Acc Eff Stuff here===========================================
  // Pt bin sizes
  // 0-1.5, 1.5-3, 3-4.5, 4.5-6, 6-7.5...

  const char *cTrg[5] = {"Bit1", "L1DM0HighQ", "L2Mu3NHitQ", "L3Mu3", "L3DMOpen"};
  //int iTrg = 0;
  TFile *outfile;
  char tmp_output[512];
  if(JpsiCat_ == 1) sprintf(tmp_output,"pbpbPrJpsi_y_%0.1f_%0.1f_pT_%0.1f_%0.1f_HighQ_tnpWgt_pr2.root", minRap, maxRap, minPt, maxPt);
  if(JpsiCat_ == 2) sprintf(tmp_output,"pbpbPrJpsi_y_%0.1f_%0.1f_pT_%0.1f_%0.1f_HighQ_tnpWgt_npr2.root", minRap, maxRap, minPt, maxPt);
  outfile =new TFile(tmp_output, "Recreate");
  TH1F *hGenCent = new TH1F("hGenCent",";Centrality (%);Weighted Counts",40,0,40);
  TH1F *hRecCent = new TH1F("hRecCent",";Centrality (%);Weighted Counts",40,0,40);
  hGenCent->Sumw2();
  hRecCent->Sumw2();

  for(int iSpec = 1; iSpec < 2; iSpec++){

    ////const int nCentBins = 11; // Non-prompt 
    ////const int nCentBins = 14; // Prompt
    const int nCentBins = 10; // Non-prompt (60-100)
    //const int nCentBins = 13; // Prompt (60-100)
    const int nPtBins = 3;
    const int nRapBins = 6;
    const int ndPhiBins = 8;
    //const int ndPhiBins = 4;
    const int ndPhi2Bins = 8;
    const int nPhiBins = 8;
    const int nFiles = 6;
    //if(JpsiCat_ == 2) nCentBins = 11;
    // more bins 0-10,10-20,20-30,30-40,40-50,50-70,70-100
    // cent bins : 0-5, 5-10, 10-15, 15-20, 20-25, 25-30, 30-35, 35-40, 40-45, 45-50, 50-55, 55-60, 60-70, 70-100
    ////double ct_bound[nCentBins+1] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 40};
    //double ct_bound[nCentBins+1] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 40}; // Prompt (60-100)
    double ct_bound[nCentBins+1] = {0, 2, 4, 6, 8, 10, 12, 16, 20, 24, 40}; // NonPrompt (60-100)
    double ct_bound2[nCentBins+1] = {0.0};
    for(int ict = 0; ict < nCentBins+1; ict++){
      ct_bound2[ict] = ct_bound[ict]*2.5;
    }
    double xct_bound[nCentBins] = {0.0};
    //double pt_bound[nPtBins+1] = {0.0, 3.0, 6.5};
    // pT bins : 6.5-7.5, 7.5-8.5, 8.5-9.5, 9.5-10.5, 10.5-11.5, 11.5-12.5, 12.5-14.5, 14.5-16.5, 16.5-20.0, 20.0-30.0
    double pt_bound[nPtBins+1] = {3.0, 4.5, 5.5, 6.5};
    double xpt_bound[nPtBins] = {0.0};
    // rap bins : 0.0-0.3, 0.3-0.6, 0.6-0.9, 0.9-1.2, 1.2-1.5, 1.5-1.8, 1.8-2.1, 2.1-2.4
    double rap_bound[nRapBins+1] = {0.0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4};
    double xrap_bound[nRapBins] = {0.0};
    double dphi_bound[ndPhiBins+1] = {0.0, TMath::Pi()/16, 2*TMath::Pi()/16, 3*TMath::Pi()/16, 4*TMath::Pi()/16, 5*TMath::Pi()/16, 6*TMath::Pi()/16, 7*TMath::Pi()/16, 8*TMath::Pi()/16};
    double xdphi_bound[ndPhiBins] = {0.0};
    double phi_bound[nPhiBins+1] = {-4*TMath::Pi()/4, -3*TMath::Pi()/4, -2*TMath::Pi()/4, -1*TMath::Pi()/4,0.0, TMath::Pi()/4, 2*TMath::Pi()/4, 3*TMath::Pi()/4, TMath::Pi()};
    double xphi_bound[nPhiBins] = {0.0};
    double dphi2_bound[ndPhi2Bins+1] = {0.0, TMath::Pi()/16, 2*TMath::Pi()/16, 3*TMath::Pi()/16, 4*TMath::Pi()/16, 5*TMath::Pi()/16, 6*TMath::Pi()/16, 7*TMath::Pi()/16, 8*TMath::Pi()/16};
    double xdphi2_bound[ndPhi2Bins] = {0.0};

    const char *cSp[6] = {"Cents","Pts","Raps","Phi","dPhi","gdPhi"};
    char OutTextFile[100];
    if(JpsiCat_ == 1) sprintf(OutTextFile,"eff_HighPt_%s_%s_%s_y_%0.1f_%0.1f_pT_%0.1f_%0.1f_pr.tex", cSp[iSpec], cCd, cTrg[0], minRap, maxRap, minPt, maxPt);
    if(JpsiCat_ == 2) sprintf(OutTextFile,"eff_HighPt_%s_%s_%s_y_%0.1f_%0.1f_pT_%0.1f_%0.1f_npr.tex", cSp[iSpec], cCd, cTrg[0], minRap, maxRap, minPt, maxPt);
    //sprintf(OutTextFile,"eff_HighPt_%s_%s_%s.tex", cSp[iSpec], cCd, cTrg[iCat]);
    ofstream dataFile(Form(OutTextFile));

    char tmp_start[512];
    sprintf(tmp_start,"%%%% Getting Efficiency starts, Category : %s !!!!! %%%%%", cCd);  
    cout<< tmp_start << endl;
    //dataFile<< tmp_start << endl;

    // x, y, z - axis 
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of Cent"<<endl;
    for(int i = 0; i < nCentBins; i++){
      if(i == (nCentBins-1)){
        xct_bound[i] = ct_bound[i-4] + (ct_bound[i-1]-ct_bound[i-4])/2;
        //cout<<"xct_bound["<<i<<"] : "<<xct_bound[i]<<endl;
        //dataFile<<"xct_bound["<<i<<"] : "<<xct_bound[i]<<endl;
      }else{
        xct_bound[i] = ct_bound[i] + (ct_bound[i+1]-ct_bound[i])/2;
        //cout<<"xct_bound["<<i<<"] : "<<xct_bound[i]<<endl;
        //dataFile<<"xct_bound["<<i<<"] : "<<xct_bound[i]<<endl;
      }
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of pT"<<endl;
    for(int i = 0; i < nPtBins; i++){
      xpt_bound[i] = pt_bound[i] + (pt_bound[i+1]-pt_bound[i])/2;
      //cout<<"xpt_bound["<<i<<"] : "<<xpt_bound[i]<<endl;
      //dataFile<<"xpt_bound["<<i<<"] : "<<xpt_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of rap"<<endl;
    for(int i = 0; i < nRapBins; i++){
      xrap_bound[i] = rap_bound[i] + (rap_bound[i+1]-rap_bound[i])/2;
      //cout<<"xrap_bound["<<i<<"] : "<<xrap_bound[i]<<endl;
      //dataFile<<"xrap_bound["<<i<<"] : "<<xrap_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of dphi"<<endl;
    for(int i = 0; i < ndPhiBins; i++){
      xdphi_bound[i] = dphi_bound[i] + (dphi_bound[i+1]-dphi_bound[i])/2;
      //cout<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
      //dataFile<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of phi"<<endl;
    for(int i = 0; i < nPhiBins; i++){
      xphi_bound[i] = phi_bound[i] + (phi_bound[i+1]-phi_bound[i])/2;
      //cout<<"xphi_bound["<<i<<"] : "<<xphi_bound[i]<<endl;
      //dataFile<<"xphi_bound["<<i<<"] : "<<xphi_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of dphi2"<<endl;
    for(int i = 0; i < ndPhi2Bins; i++){
      xdphi2_bound[i] = dphi2_bound[i] + (dphi2_bound[i+1]-dphi2_bound[i])/2;
      //cout<<"xdphi2_bound["<<i<<"] : "<<xdphi2_bound[i]<<endl;
      //dataFile<<"xdphi2_bound["<<i<<"] : "<<xdphi2_bound[i]<<endl;
    }

    int nBins_tmp = 0;
    if(iSpec == 0) { nBins_tmp = nCentBins; }
    if(iSpec == 1) { nBins_tmp = nPtBins; }
    if(iSpec == 2) { nBins_tmp = nRapBins; }
    if(iSpec == 3) { nBins_tmp = nPhiBins; }
    if(iSpec == 4) { nBins_tmp = ndPhiBins; }
    if(iSpec == 5) { nBins_tmp = ndPhi2Bins; }
    const int nBins = nBins_tmp;

    TH1F *hTempMass = new TH1F("hTempMass","",100, 2.0, 4.0);
    TH1F *hGenDiMuonf[nFiles][nBins];
    TH1F *hRecoDiMuonf[nFiles][nBins];
    TH1F *hGenDiMuon[nBins];
    TH1F *hRecoDiMuon[nBins];
    double genNo[nBins];
    double genErr[nBins];
    double recoNo[nBins];
    double recoErr[nBins];
    double eff[nBins];
    double effErr[nBins];
    for(int fl = 0; fl < nFiles; fl++){
      for(int i = 0; i < nBins; i++){
        hGenDiMuonf[fl][i] = (TH1F*)hTempMass->Clone();
        hRecoDiMuonf[fl][i] = (TH1F*)hTempMass->Clone();
        hGenDiMuon[i] = (TH1F*)hTempMass->Clone();
        hRecoDiMuon[i] = (TH1F*)hTempMass->Clone();
        hGenDiMuonf[fl][i]->Sumw2();
        hRecoDiMuonf[fl][i]->Sumw2();
        hGenDiMuon[i]->Sumw2();
        hRecoDiMuon[i]->Sumw2();
      }
    }

    char fileName[10][512];
    //scales for different pT bins
    double scale[6]={1.758e-07, 1.46074e-07, 2.58269e-08, 5.4948e-09, 1.63265e-09, 7.70795e-10};
    if(PutWeight==0){scale[0]=(1);scale[1]=(1);scale[2]=(1);scale[3]=(1);scale[4]=(1);scale[5]=(1);}

    // loop for pT
    cout<<"================================="<<endl;
    sprintf(fileName[0],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_0003.root");
    sprintf(fileName[1],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_0306.root");
    sprintf(fileName[2],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_0609.root");
    sprintf(fileName[3],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_0912.root");
    sprintf(fileName[4],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_1215.root");
    sprintf(fileName[5],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_Regit_Pro2/HiDiMuonAna_Prompt_RegIt_MC_20131006_Pt_1530.root");

    TFile *infile;
    TTree *tree;
    TTree *gentree;

    for(int ifile = 0; ifile < nFiles; ifile++){
      infile=new TFile(fileName[ifile],"R");
      tree=(TTree*)infile->Get("SingleMuonTree");
      gentree=(TTree*)infile->Get("SingleGenMuonTree");
      //Event variables
      int eventNb,runNb,lumiBlock, gbin, rbin;
      int hbit1;
      double zVtx;
      //Jpsi Variables
      Double_t JpsiMass,JpsiPt,JpsiRap, JpsiCharge;
      Double_t JpsiVprob;
      Double_t JpsiPhi;
      Double_t JpsiEta;
      Double_t JpsiPsi[38];
      Double_t JpsiGenPsi;
      //2.) muon variables RECO                                                                       
      double muPosPx, muPosPy, muPosPz,  muPosEta, muPosPt, muPosP, muPosPhi;
      double muNegPx, muNegPy, muNegPz,  muNegEta, muNegPt, muNegP, muNegPhi;
      //(1).Positive Muon                                     
      double muPos_nchi2In, muPos_dxy, muPos_dz, muPos_nchi2Gl;
      int muPos_found, muPos_pixeLayers, muPos_nValidMuHits,muPos_arbitrated,muPos_TMOneST,muPos_trkLayMeas;
      bool muPos_matches,muPos_tracker;
      int muPos_Trigger2, muPos_Trigger10;
      int muPos_Trigger4, muPos_Trigger12, muPos_Trigger13;
      int muPos_Trigger21, muPos_Trigger22;
      //(2).Negative Muon                                     
      double muNeg_nchi2In, muNeg_dxy, muNeg_dz, muNeg_nchi2Gl;
      int muNeg_found, muNeg_pixeLayers, muNeg_nValidMuHits,muNeg_arbitrated,muNeg_TMOneST,muNeg_trkLayMeas;
      bool muNeg_matches,muNeg_tracker;
      int muNeg_Trigger2, muNeg_Trigger10;
      int muNeg_Trigger4, muNeg_Trigger12, muNeg_Trigger13;
      int muNeg_Trigger21, muNeg_Trigger22;

      //Gen Level variables
      //Gen PrJpsi Variables
      double GenJpsiMass, GenJpsiPt, GenJpsiRap;
      double GenJpsiPx, GenJpsiPy, GenJpsiPz;
      double GenJpsiPhi;
      double GenJpsiEta;
      double GenJpsiPsi;
      //2.) Gen muon variables 
      double GenmuPosPx, GenmuPosPy, GenmuPosPz,  GenmuPosEta, GenmuPosPt, GenmuPosPhi;
      double GenmuNegPx, GenmuNegPy, GenmuNegPz,  GenmuNegEta, GenmuNegPt, GenmuNegPhi;

      //Event variables
      tree->SetBranchAddress("eventNb",&eventNb);
      tree->SetBranchAddress("runNb",&runNb);
      tree->SetBranchAddress("lumiBlock",&lumiBlock);
      tree->SetBranchAddress("hbit1",&hbit1);
      tree->SetBranchAddress("zVtx",&zVtx);

      //Jpsi Variables
      tree->SetBranchAddress("JpsiCharge",&JpsiCharge);
      tree->SetBranchAddress("JpsiMass",&JpsiMass);
      tree->SetBranchAddress("JpsiPt",&JpsiPt);
      tree->SetBranchAddress("JpsiPhi",&JpsiPhi);
      tree->SetBranchAddress("JpsiEta",&JpsiEta);
      tree->SetBranchAddress("JpsiPsi",&JpsiPsi);
      tree->SetBranchAddress("JpsiGenPsi",&JpsiGenPsi);
      tree->SetBranchAddress("JpsiRap",&JpsiRap);
      tree->SetBranchAddress("JpsiVprob",&JpsiVprob);
      tree->SetBranchAddress("rbin",&rbin);

      //muon variable
      tree->SetBranchAddress("muPosPx",&muPosPx);
      tree->SetBranchAddress("muPosPy",&muPosPy);
      tree->SetBranchAddress("muPosPz",&muPosPz);
      tree->SetBranchAddress("muPosEta",&muPosEta);
      tree->SetBranchAddress("muPosPhi",&muPosPhi);
      tree->SetBranchAddress("muNegPx", &muNegPx);
      tree->SetBranchAddress("muNegPy", &muNegPy);
      tree->SetBranchAddress("muNegPz", &muNegPz);
      tree->SetBranchAddress("muNegEta", &muNegEta);
      tree->SetBranchAddress("muNegPhi", &muNegPhi);


      //1). Positive Muon
      tree->SetBranchAddress("muPos_nchi2In", &muPos_nchi2In);
      tree->SetBranchAddress("muPos_dxy", &muPos_dxy);
      tree->SetBranchAddress("muPos_dz", &muPos_dz);
      tree->SetBranchAddress("muPos_nchi2Gl", &muPos_nchi2Gl);
      tree->SetBranchAddress("muPos_found", &muPos_found);
      tree->SetBranchAddress("muPos_pixeLayers", &muPos_pixeLayers);
      tree->SetBranchAddress("muPos_nValidMuHits", &muPos_nValidMuHits);
      tree->SetBranchAddress("muPos_matches", &muPos_matches);
      tree->SetBranchAddress("muPos_tracker", &muPos_tracker);
      tree->SetBranchAddress("muPos_arbitrated", &muPos_arbitrated);
      tree->SetBranchAddress("muPos_TMOneST", &muPos_TMOneST);
      tree->SetBranchAddress("muPos_trkLayMeas", &muPos_trkLayMeas);
      tree->SetBranchAddress("muPos_Trigger2", &muPos_Trigger2);
      tree->SetBranchAddress("muPos_Trigger21", &muPos_Trigger21);
      tree->SetBranchAddress("muPos_Trigger22", &muPos_Trigger22);
      tree->SetBranchAddress("muPos_Trigger4", &muPos_Trigger4);
      tree->SetBranchAddress("muPos_Trigger10", &muPos_Trigger10);
      tree->SetBranchAddress("muPos_Trigger12", &muPos_Trigger12);
      tree->SetBranchAddress("muPos_Trigger13", &muPos_Trigger13);

      //2). Negative Muon                                                                            
      tree->SetBranchAddress("muNeg_nchi2In", &muNeg_nchi2In);
      tree->SetBranchAddress("muNeg_dxy", &muNeg_dxy);
      tree->SetBranchAddress("muNeg_dz", &muNeg_dz);
      tree->SetBranchAddress("muNeg_nchi2Gl", &muNeg_nchi2Gl);
      tree->SetBranchAddress("muNeg_found", &muNeg_found);
      tree->SetBranchAddress("muNeg_pixeLayers", &muNeg_pixeLayers);
      tree->SetBranchAddress("muNeg_nValidMuHits", &muNeg_nValidMuHits);
      tree->SetBranchAddress("muNeg_matches", &muNeg_matches);
      tree->SetBranchAddress("muNeg_tracker", &muNeg_tracker);
      tree->SetBranchAddress("muNeg_arbitrated", &muNeg_arbitrated);
      tree->SetBranchAddress("muNeg_TMOneST", &muNeg_TMOneST);
      tree->SetBranchAddress("muNeg_trkLayMeas", &muNeg_trkLayMeas);
      tree->SetBranchAddress("muNeg_Trigger2", &muNeg_Trigger2);
      tree->SetBranchAddress("muNeg_Trigger21", &muNeg_Trigger21);
      tree->SetBranchAddress("muNeg_Trigger22", &muNeg_Trigger22);
      tree->SetBranchAddress("muNeg_Trigger4", &muNeg_Trigger4);
      tree->SetBranchAddress("muNeg_Trigger10", &muNeg_Trigger10);
      tree->SetBranchAddress("muNeg_Trigger12", &muNeg_Trigger12);
      tree->SetBranchAddress("muNeg_Trigger13", &muNeg_Trigger13);
      //====================================Gen Variables=========================================================
      //Gen Jpsi Variables
      gentree->SetBranchAddress("GenJpsiMass",   &GenJpsiMass);
      gentree->SetBranchAddress("GenJpsiPt",     &GenJpsiPt);
      gentree->SetBranchAddress("GenJpsiPhi",    &GenJpsiPhi);
      gentree->SetBranchAddress("GenJpsiPsi",    &GenJpsiPsi);
      gentree->SetBranchAddress("GenJpsiRap",    &GenJpsiRap);
      gentree->SetBranchAddress("GenJpsiEta",    &GenJpsiEta);
      gentree->SetBranchAddress("GenJpsiPx",     &GenJpsiPx);
      gentree->SetBranchAddress("GenJpsiPy",     &GenJpsiPy);
      gentree->SetBranchAddress("GenJpsiPz",     &GenJpsiPz);
      gentree->SetBranchAddress("gbin",          &gbin);
      //muon variable
      gentree->SetBranchAddress("GenmuPosPx",    &GenmuPosPx);
      gentree->SetBranchAddress("GenmuPosPy",    &GenmuPosPy);
      gentree->SetBranchAddress("GenmuPosPz",    &GenmuPosPz);
      gentree->SetBranchAddress("GenmuPosEta",   &GenmuPosEta);
      gentree->SetBranchAddress("GenmuPosPhi",   &GenmuPosPhi);
      gentree->SetBranchAddress("GenmuNegPx",    &GenmuNegPx);
      gentree->SetBranchAddress("GenmuNegPy",    &GenmuNegPy);
      gentree->SetBranchAddress("GenmuNegPz",    &GenmuNegPz);
      gentree->SetBranchAddress("GenmuNegEta",   &GenmuNegEta);
      gentree->SetBranchAddress("GenmuNegPhi",   &GenmuNegPhi);

      //====================================================== Gen tree loop ================================================
      int NAccep=0;
      int nGenEntries=gentree->GetEntries();
      cout<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ==============="<<endl;

      for(int iGen=0; iGen< nGenEntries; iGen++)  {        
        //cout<<"i : "<<i<<endl;
        if(!(gentree->GetEntry(iGen))) continue;
        //cout<<" gentree ("<<i<<")"<<endl;
        //Only printing 
        if(iGen%10000==0){
          //cout<<" processing record "<<iGen<<"/"<<nGenEntries<<endl;
          //cout<<" Mass "<< GenJpsiMass<< " pT "<< GenJpsiPt << " Y " <<GenJpsiRap<<endl;
        }

        bool GenPosIn=0, GenNegIn=0;
        GenmuPosPt= TMath::Sqrt(GenmuPosPx*GenmuPosPx + GenmuPosPy*GenmuPosPy); 
        GenmuNegPt= TMath::Sqrt(GenmuNegPx*GenmuNegPx + GenmuNegPy*GenmuNegPy); 

        if(IsAccept(GenmuPosPt, GenmuPosEta)) {GenPosIn=1;}
        if(IsAccept(GenmuNegPt, GenmuNegEta)) {GenNegIn=1;}

        double GenCenWeight =0, GenWeight =0;
        GenCenWeight=FindCenWeight(gbin);   
        GenWeight=GenCenWeight*scale[ifile];

        hGenCent->Fill(gbin,GenWeight);

        int AccJpsi = 0;
        //if(GenJpsiPt < 6.5) continue;
        if((GenJpsiPt >= minPt && GenJpsiPt <= maxPt && fabs(GenJpsiRap) >= minRap && fabs(GenJpsiRap) <= maxRap && 
              GenPosIn == 1 && GenNegIn == 1 && GenJpsiMass > 2.0 && GenJpsiMass < 4.0)) {AccJpsi = 1;}

        if((GenPosIn ==1 && GenNegIn==1)) NAccep++;

        if(PutWeight==0) GenWeight=1; 

        //adding pT of all pt bins to see diss is cont

        //cout<<"1. GenJpsiPt : "<<GenJpsiPt<<", GenJpsiEta : "<<GenJpsiEta<<", GenJpsiRap : "<<GenJpsiRap<<", |GenJpsiPsi| : "<<TMath::Abs(GenJpsiPsi)<<endl;
        //cout<<"1. GenPosIn : "<<GenPosIn<<", GenNegIn : "<<GenNegIn<<endl;
        double vars = 0.0, bin1 = 0.0, bin2 = 0.0;
        if(iSpec == 0) vars = gbin;
        if(iSpec == 1) vars = GenJpsiPt;
        if(iSpec == 2) vars = fabs(GenJpsiRap);
        if(iSpec == 3) vars = GenJpsiPhi;
        if(iSpec == 4) vars = fabs(GenJpsiPsi);
        if(iSpec == 5) vars = fabs(GenJpsiPsi);
        for(int i = 0; i < nBins; i++){
          if(iSpec == 0){
            bin1 = ct_bound[i]; bin2 = ct_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && GenJpsiPt >= minPt && GenJpsiPt <= maxPt && fabs(GenJpsiRap) >= minRap && fabs(GenJpsiRap) < maxRap ) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
          if(iSpec == 1){
            bin1 = pt_bound[i]; bin2 = pt_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && gbin >= 0 && gbin <= 40 && fabs(GenJpsiRap) >= minRap && fabs(GenJpsiRap) < maxRap ) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
          if(iSpec == 2){
            bin1 = rap_bound[i]; bin2 = rap_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && gbin >= 0 && gbin <= 40 && GenJpsiPt >= minPt && GenJpsiPt < maxPt) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
          if(iSpec == 3){
            bin1 = phi_bound[i]; bin2 = phi_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && gbin >= 0 && gbin <= 40 && GenJpsiPt >= minPt && GenJpsiPt < maxPt && fabs(GenJpsiRap) < maxRap) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
          if(iSpec == 4){
            bin1 = dphi_bound[i]; bin2 = dphi_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && gbin >= 0 && gbin <= 40 && GenJpsiPt >= minPt && GenJpsiPt < maxPt && fabs(GenJpsiRap) < maxRap) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
          if(iSpec == 5){
            bin1 = dphi2_bound[i]; bin2 = dphi2_bound[i+1];
            if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && gbin >= 0 && gbin <= 40 && GenJpsiPt >= minPt && GenJpsiPt < maxPt && fabs(GenJpsiRap) < maxRap) {
              hGenDiMuonf[ifile][i]->Fill(GenJpsiMass,GenWeight);
            }
          }
        }
      }//gen loop end

      //cout<<" accepted no "<< NAccep<<endl;

      //=============== Rec Tree Loop ==============================================================================

      int nRecEntries=tree->GetEntries();
      cout<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<< "====="<<endl;
      for(int iRec=0; iRec<nRecEntries; iRec++)  {     
        tree->GetEntry(iRec);
        //Only printing 
        if(iRec%10000==0){
          //cout<<" processing record "<<iRec<<"/"<<nRecEntries<<endl;
          //cout<<" processing Run  " <<runNb <<" event "<<eventNb<<" lum block "<<lumiBlock<<endl;    
          //cout<<" Mass "<< JpsiMass<< " pT "<< JpsiPt << " Y " <<JpsiRap<<"  "<<JpsiVprob<<" charge "<<JpsiCharge<<" rbin "<<rbin<<endl; 
        }
        bool PosPass=0, NegPass=0, AllCut=0 ,PosIn=0, NegIn=0;
        muPosPt= TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy); 
        muPosP = TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy+ muPosPz*muPosPz); 
        muNegPt= TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy); 
        muNegP = TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy +muNegPz*muNegPz); 


        double tnpWgt1 = 0.0, tnpWgt2 = 0.0;
        double staWgt1 = 0.0, staWgt2 = 0.0;

        double x = 0.0;
        if(fabs(muPosEta) < 0.9){
          x = muPosPt;
          tnpWgt1 = tnpSF1->Eval(x);
        }else if(fabs(muPosEta) < 1.6){
          x = muPosPt;
          tnpWgt1 = tnpSF2->Eval(x);
        }else if(fabs(muPosEta) < 2.1){
          x = muPosPt;
          tnpWgt1 = tnpSF3->Eval(x);
        }else{
          x = muPosPt;
          tnpWgt1 = tnpSF4->Eval(x);
        }

        if(fabs(muNegEta) < 0.9){
          x = muNegPt;
          tnpWgt2 = tnpSF1->Eval(x);
        }else if(fabs(muNegEta) < 1.6){
          x = muNegPt;
          tnpWgt2 = tnpSF2->Eval(x);
        }else if(fabs(muNegEta) < 2.1){
          x = muNegPt;
          tnpWgt2 = tnpSF3->Eval(x);
        }else{
          x = muNegPt;
          tnpWgt2 = tnpSF4->Eval(x);
        }

        double xta = 0.0;
        if(fabs(muPosEta) < 1.6){
          xta = muPosPt;
          staWgt1 = fun1->Eval(xta);
        }else{
          xta = muPosPt;
          staWgt1 = fun2->Eval(xta);
        }

        if(fabs(muNegEta) < 1.6){
          xta = muNegPt;
          staWgt2 = fun1->Eval(xta);
        }else{
          xta = muNegPt;
          staWgt2 = fun2->Eval(xta);
        }

        double RecCenWeight=0,RecWeight=0;
        RecCenWeight=FindCenWeight(rbin);   
        RecWeight=RecCenWeight*scale[ifile]*tnpWgt1*tnpWgt2*staWgt1*staWgt2;


        hRecCent->Fill(gbin,RecWeight);

        if(IsAccept(muPosPt, muPosEta)){PosIn=1;}
        if(IsAccept(muNegPt, muNegEta)){NegIn=1;}

        int AccJpsi = 0;
        if((JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) >= minRap && fabs(JpsiRap) <= maxRap && PosIn == 1 && NegIn == 1)) {AccJpsi = 1;}

        /*
           cout<<"muPos_pixeLayers : "<<muPos_pixeLayers<<" muPos_nchi2In : "<<muPos_nchi2In
           <<" TMath::Abs(muPos_dxy) : "<<TMath::Abs(muPos_dxy)<<" TMath::Abs(muPos_dz) : "<<TMath::Abs(muPos_dz)
           <<" muPos_trkLayMeas : "<<muPos_trkLayMeas
           <<" muPos_arbitrated : "<<muPos_arbitrated<<" muPos_TMOneST : "<<muPos_TMOneST<<" muPos_tracker : "<<muPos_tracker<<endl;
           cout<<"muNeg_pixeLayers : "<<muNeg_pixeLayers<<" muNeg_nchi2In : "<<muNeg_nchi2In
           <<" TMath::Abs(muNeg_dxy) : "<<TMath::Abs(muNeg_dxy)<<" TMath::Abs(muNeg_dz) : "<<TMath::Abs(muNeg_dz)
           <<" muNeg_trkLayMeas : "<<muNeg_trkLayMeas
           <<" muNeg_arbitrated : "<<muNeg_arbitrated<<" muNeg_TMOneST : "<<muNeg_TMOneST<<" muNeg_tracker : "<<muNeg_tracker<<endl;
           */
        bool mu_Global = ((muPos_nchi2Gl >=0) && (muNeg_nchi2Gl >=0));
        bool mu_Tracker = ((muPos_tracker==1) && (muNeg_tracker==1));

        if(muPos_found > 10 &&
            muPos_pixeLayers > 0 &&
            muPos_nchi2In < 4.0 &&
            TMath::Abs(muPos_dxy) < 3 &&
            TMath::Abs(muPos_dz) < 15 && muPos_nchi2Gl < 20 &&
            muPos_arbitrated==1 &&
            muPos_tracker==1){
          PosPass=1;
        }

        if(muNeg_found > 10 &&
            muNeg_pixeLayers > 0 &&
            muNeg_nchi2In < 4.0 &&
            TMath::Abs(muNeg_dxy) < 3 &&
            TMath::Abs(muNeg_dz) < 15 &&
            muNeg_nchi2Gl < 20 &&
            muNeg_arbitrated==1 &&
            muNeg_tracker==1){
          NegPass=1;
        }

        if(hbit1 == 1 && (muPos_Trigger22==1 && muNeg_Trigger22==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1) && mu_Global && mu_Tracker ){AllCut=1;}

        if(PutWeight==0)RecWeight=1;

        double JpsidPhi = JpsiGenPsi;

        //Eff loop for reco
        double vars = 0.0, bin1 = 0.0, bin2 = 0.0;
        if(iSpec == 0) vars = rbin;
        if(iSpec == 1) vars = JpsiPt;
        if(iSpec == 2) vars = fabs(JpsiRap);
        if(iSpec == 3) vars = JpsiPhi;
        if(iSpec == 4) vars = fabs(JpsidPhi);
        if(iSpec == 5) vars = fabs(JpsiGenPsi);
        if((JpsiCharge == 0) && (JpsiVprob > 0.01) && (JpsiMass >= 2.95 && JpsiMass < 3.25)) {     
          for(int i = 0; i < nBins; i++){
            if(iSpec == 0){
              bin1 = ct_bound[i]; bin2 = ct_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) >= minRap && fabs(JpsiRap) <= maxRap ) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
            if(iSpec == 1){
              bin1 = pt_bound[i]; bin2 = pt_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && rbin >= 0 && rbin <= 40 && fabs(JpsiRap) >= minRap && fabs(JpsiRap) <= maxRap ) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
            if(iSpec == 2){ 
              bin1 = rap_bound[i]; bin2 = rap_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && rbin >= 0 && rbin <= 40 && JpsiPt >= minPt && JpsiPt <= maxPt) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
            if(iSpec == 3){ 
              bin1 = phi_bound[i]; bin2 = phi_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && rbin >= 0 && rbin <= 40 && JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) <=maxRap) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
            if(iSpec == 4){ 
              bin1 = dphi_bound[i]; bin2 = dphi_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && rbin >= 0 && rbin <= 40 && JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) <=maxRap) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
            if(iSpec == 5){ 
              bin1 = dphi2_bound[i]; bin2 = dphi2_bound[i+1];
              if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && rbin >= 0 && rbin <= 40 && JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) <=maxRap) {
                hRecoDiMuonf[ifile][i]->Fill(JpsiMass,RecWeight);
              }
            }
          }
        }
      } //rec tree loop ends
    }  // file loop ends

    //======================  File loop Starts ============================

    ///////////////////////////////////////////////////////////////////
    cout<< " adding "<<endl;
    TCanvas *c1 = new TCanvas();
    char gtmp[512], gtmp1[512];
    char rtmp[512], rtmp1[512];

    for(int i = 0; i < nBins; i++){
      for(int ifile = 0; ifile < nFiles; ifile++){
        hGenDiMuon[i]->Add(hGenDiMuonf[ifile][i],1);
        hRecoDiMuon[i]->Add(hRecoDiMuonf[ifile][i],1);
      }
      if(iSpec == 0) sprintf(gtmp,"hGenDiMuon_Cent_%1.f_%1.f",ct_bound[i],ct_bound[i+1]);
      if(iSpec == 1) sprintf(gtmp,"hGenDiMuon_Pt_%0.1f_%0.1f",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(gtmp,"hGenDiMuon_Rap_%0.1f_%0.1f",rap_bound[i],rap_bound[i+1]);
      if(iSpec == 0) sprintf(gtmp1,"plots/Gen/hGenDiMuon_Cent_%1.f_%1.f_%s.png",ct_bound[i],ct_bound[i+1],cCd);
      if(iSpec == 1) sprintf(gtmp1,"plots/Gen/hGenDiMuon_Pt_%0.1f_%0.1f_%s.png",pt_bound[i],pt_bound[i+1],cCd);
      if(iSpec == 2) sprintf(gtmp1,"plots/Gen/hGenDiMuon_Rap_%0.1f_%0.1f_%s.png",rap_bound[i],rap_bound[i+1],cCd);
      if(iSpec == 0) sprintf(rtmp,"hRecDiMuon_Cent_%1.f_%1.f",ct_bound[i],ct_bound[i+1]);
      if(iSpec == 1) sprintf(rtmp,"hRecDiMuon_Pt_%0.1f_%0.1f",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(rtmp,"hRecDiMuon_Rap_%0.1f_%0.1f",rap_bound[i],rap_bound[i+1]);
      if(iSpec == 0) sprintf(rtmp1,"plots/Rec/hRecDiMuon_Cent_%1.f_%1.f_%s.png",ct_bound[i],ct_bound[i+1],cCd);
      if(iSpec == 1) sprintf(rtmp1,"plots/Rec/hRecDiMuon_Pt_%0.1f_%0.1f_%s.png",pt_bound[i],pt_bound[i+1],cCd);
      if(iSpec == 2) sprintf(rtmp1,"plots/Rec/hRecDiMuon_Rap_%0.1f_%0.1f_%s.png",rap_bound[i],rap_bound[i+1],cCd);
      hGenDiMuon[i]->SetName(gtmp);
      //hGenDiMuon[i]->Write();
      //hGenDiMuon[ipt][irap][idphi]->Draw();
      //c1->SaveAs(gtmp1);
      hRecoDiMuon[i]->SetName(rtmp);
      //hRecoDiMuon[i]->Write();
      //hRecoDiMuon[ipt][irap][idphi]->Draw();
      //c1->SaveAs(rtmp1);
    }

    TH1F *hReco = new TH1F();
    TH1F *hGen = new TH1F();
    hReco->Sumw2();
    hGen->Sumw2();
    int nRecoBins;
    int nGenBins;
    if(iSpec == 0) {hReco = new TH1F("hReco","hReco;Centrality (%);Weighted Yields",nCentBins,ct_bound2); nRecoBins = nCentBins;}
    if(iSpec == 1) {hReco = new TH1F("hReco","hReco;p_{T} GeV/c;Weighted Yields",nPtBins,pt_bound); nRecoBins = nPtBins;}
    if(iSpec == 2) {hReco = new TH1F("hReco","hReco;y;Weighted Yields",nRapBins,rap_bound); nRecoBins = nRapBins;}
    if(iSpec == 3) {hReco = new TH1F("hReco","hReco;#phi;Weighted Yields",nPhiBins,phi_bound); nRecoBins = nPhiBins;}
    if(iSpec == 4) {hReco = new TH1F("hReco","hReco;d#phi;Weighted Yields",ndPhiBins,dphi_bound); nRecoBins = ndPhiBins;}
    if(iSpec == 5) {hReco = new TH1F("hReco","hReco;d#phi;Weighted Yields",ndPhi2Bins,dphi2_bound); nRecoBins = ndPhi2Bins;}

    if(iSpec == 0) {hGen = new TH1F("hGen","hGen;Centrality (%);Weighted Yields",nCentBins,ct_bound2); nGenBins = nCentBins;}
    if(iSpec == 1) {hGen = new TH1F("hGen","hGen;p_{T} GeV/c;Weighted Yields",nPtBins,pt_bound); nGenBins = nPtBins;}
    if(iSpec == 2) {hGen = new TH1F("hGen","hGen;y;Weighted Yields",nRapBins,rap_bound); nGenBins = nRapBins;}
    if(iSpec == 3) {hGen = new TH1F("hGen","hGen;#phi;Weighted Yields",nPhiBins,phi_bound); nGenBins = nPhiBins;}
    if(iSpec == 4) {hGen = new TH1F("hGen","hGen;d#phi;Weighted Yields",ndPhiBins,dphi_bound); nGenBins = ndPhiBins;}
    if(iSpec == 5) {hGen = new TH1F("hGen","hGen;d#phi;Weighted Yields",ndPhi2Bins,dphi2_bound); nGenBins = ndPhi2Bins;}

    cout<<"Starts to calculate efficiency"<<endl;
    //dataFile<<""<<endl;
    //=====================Loop for eff========================================================================================//
    //define stuff here for error on weighted samples
    for(int i = 0; i < nBins; i++){
      int Gbinlow =hGenDiMuon[i]->GetXaxis()->FindBin(2.0);//2.95
      int Gbinhi=hGenDiMuon[i]->GetXaxis()->FindBin(4.0);//2.95
      //int Gbinlow =hGenDiMuon[i]->GetXaxis()->FindBin(2.95);//2.95
      //int Gbinhi=hGenDiMuon[i]->GetXaxis()->FindBin(3.25);//2.95

      int Rbinlow =hRecoDiMuon[i]->GetXaxis()->FindBin(2.95);//2.95
      int Rbinhi=hRecoDiMuon[i]->GetXaxis()->FindBin(3.25);//2.95

      cout<<"Gbinlow : "<<Gbinlow<<", Gbinhi : "<<Gbinhi<<endl;
      cout<<"Rbinlow : "<<Rbinlow<<", Rbinhi : "<<Rbinhi<<endl;

      genNo[i] = hGenDiMuon[i]->IntegralAndError(Gbinlow, Gbinhi, genErr[i]);
      recoNo[i] = hRecoDiMuon[i]->IntegralAndError(Rbinlow, Rbinhi, recoErr[i]);

      //calculate Eff         
      if(genNo[i] == 0 || recoNo[i] == 0) {
        cout<<"No Gen or Reco, # of Gen : "<<genNo[i] <<", # of Reco : "<<recoNo[i]<<endl;
        eff[i] = 0;
        effErr[i] = 0;
      }else{
        eff[i] = recoNo[i]/genNo[i]; 

        double tmpGenNo = genNo[i];
        double tmpGenErr = genErr[i];
        double tmpRecNo = recoNo[i];
        double tmpRecErr = recoErr[i];
        double tmpEff = eff[i];
        double tmpEffErr = 0.0;

        hReco->SetBinContent(i+1,tmpRecNo);
        hReco->SetBinError(i+1,tmpRecErr);
        hGen->SetBinContent(i+1,tmpGenNo);
        hGen->SetBinError(i+1,tmpGenErr);

        //error    
        double tmp_err_s1_1 = (tmpEff * tmpEff)/(tmpGenNo * tmpGenNo);
        double tmp_err_s1_2 = (tmpRecErr * tmpRecErr);
        double tmp_err_cat_s1 = tmp_err_s1_1 * tmp_err_s1_2;


        double tmp_err_s2_1 = ( (1 - tmpEff)*(1 - tmpEff) ) / (tmpGenNo * tmpGenNo);
        double tmp_err_s2_2 = TMath::Abs(( tmpGenErr*tmpGenErr ) - ( tmpRecErr * tmpRecErr));
        double tmp_err_cat_s2 = tmp_err_s2_1 * tmp_err_s2_2;
        tmpEffErr = sqrt( tmp_err_cat_s1 + tmp_err_cat_s2 );

        effErr[i] = tmpEffErr;

        //error without weight
        //dataFile<<" Bin ["<<i<<"] - "<< " Reco Jpsi : "<< tmpRecNo  <<", Gen Jpsi : "<< tmpGenNo <<endl;
        //dataFile<<" Eff ["<<i<<"] - "<< tmpEff <<" Error "<< tmpEffErr <<endl;
        cout<<" Bin ["<<i<<"] - "<< " Reco Jpsi : "<< tmpRecNo  <<", Gen Jpsi : "<< tmpGenNo <<endl;
        cout<<" Eff ["<<i<<"] - "<< tmpEff <<" Error "<< tmpEffErr <<endl;
      }
    }
    TH1F *hEff = new TH1F();
    hEff->Sumw2();
    int nEffBins = 0;
    if(iSpec == 0) {hEff = new TH1F("hEff","hEff;Centrality (%);Efficiency",nCentBins,ct_bound2); nEffBins = nCentBins;}
    if(iSpec == 1) {hEff = new TH1F("hEff","hEff;p_{T} GeV/c;Efficiency",nPtBins,pt_bound); nEffBins = nPtBins;}
    if(iSpec == 2) {hEff = new TH1F("hEff","hEff;y;Efficiency",nRapBins,rap_bound); nEffBins = nRapBins;}
    if(iSpec == 3) {hEff = new TH1F("hEff","hEff;#phi;Efficiency",nPhiBins,phi_bound); nEffBins = nPhiBins;}
    if(iSpec == 4) {hEff = new TH1F("hEff","hEff;d#phi;Efficiency",ndPhiBins,dphi_bound); nEffBins = ndPhiBins;}
    if(iSpec == 5) {hEff = new TH1F("hEff","hEff;d#phi;Efficiency",ndPhi2Bins,dphi2_bound); nEffBins = ndPhi2Bins;}

    for(int i = 0; i < nEffBins; i++){
      hEff->SetBinContent(i+1,eff[i]);
      hEff->SetBinError(i+1,effErr[i]);
      cout<<"Trying to measure Reconstruction eff vs "<<cSp[iSpec]<<endl;
      cout<<"Eff : "<<eff[i]<<", err : "<<effErr[i]<<endl;
      dataFile<<eff[i]<<endl;//", err : "<<effErr[i]<<endl;
      //dataFile<<"Eff : "<<eff[i]<<", err : "<<effErr[i]<<endl;
    }

    outfile->cd();
    hEff->Draw();
    char tmp_histo[512];
    sprintf(tmp_histo,"hEff_%s",cSp[iSpec]);
    char tmp_histo_reco[512];
    sprintf(tmp_histo_reco,"hReco_%s",cSp[iSpec]);
    char tmp_histo_gen[512];
    sprintf(tmp_histo_gen,"hGen_%s",cSp[iSpec]);
    hEff->SetName(tmp_histo);
    hEff->Write();
    hReco->SetName(tmp_histo_reco);
    hReco->Write();
    hGen->SetName(tmp_histo_gen);
    hGen->Write();
    //c1->SaveAs("eff_3D_default_etHFm.png");
    //outfile->Close();
    dataFile<<""<<endl;
    dataFile.close();
  }
  hGenCent->Write();
  hRecCent->Write();
  outfile->Write();
}
示例#4
0
void UpsilonMassFit_All(int iSpec = 3, int PutWeight=1)
{
  //See pT Cut
  double PtCut= 4.0;
  
  //minbias integrated, |y|<1.2 and |y|\in[1.2,2.4], centrality [0,10][10,20][20,100]%,  pt [0,6.5], [6.5, 10] [10,20]

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(0);
  gStyle->SetCanvasColor(0);
  gStyle->SetTitleFillColor(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadBorderSize(0);
  gStyle->SetCanvasBorderSize(0);
  gStyle->SetOptTitle(1); // at least most of the time
  gStyle->SetOptStat(1); // most of the time, sometimes "nemriou" might be useful to display name, 
  //number of entries, mean, rms, integral, overflow and underflow
  gStyle->SetOptFit(1); // set to 1 only if you want to display fit results
  
  //==================================== Define Histograms====================================================
  ofstream dataFile(Form("Eff_Upsilon.txt"));
  
  TH1D *diMuonsInvMass_Gen = new TH1D("diMuonsInvMass_Gen","diMuonsInvMass_Gen", 100,8.0,12.0);
  TH1D *diMuonsPt_Gen = new TH1D("diMuonsPt_Gen","diMuonsPt_Gen", 100,0,30);
  //Rapidity Gen
  TH1D *diMuonsRap_Gen0 = new TH1D("diMuonsRap_Gen0","diMuonsRap_Gen0", 100,-5,5);
  TH1D *diMuonsRap_Gen1 = new TH1D("diMuonsRap_Gen1","diMuonsRap_Gen1", 100,-5,5);
  TH1D *diMuonsRap_Gen2 = new TH1D("diMuonsRap_Gen2","diMuonsRap_Gen2", 100,-5,5);
  TH1D *diMuonsRap_Gen3 = new TH1D("diMuonsRap_Gen3","diMuonsRap_Gen3", 100,-5,5);
  TH1D *diMuonsRap_Gen4 = new TH1D("diMuonsRap_Gen4","diMuonsRap_Gen4", 100,-5,5);
  TH1D *diMuonsRap_Gen5 = new TH1D("diMuonsRap_Gen5","diMuonsRap_Gen5", 100,-5,5);
  ////Rapidity Reco
  TH1D *diMuonsRap_Rec0 = new TH1D("diMuonsRap_Rec0","diMuonsRap_Rec0", 100,-5,5);
  diMuonsRap_Rec0->SetLineColor(2);
  TH1D *diMuonsRap_Rec1 = new TH1D("diMuonsRap_Rec1","diMuonsRap_Rec1", 100,-5,5);
  diMuonsRap_Rec1->SetLineColor(2);
  TH1D *diMuonsRap_Rec2 = new TH1D("diMuonsRap_Rec2","diMuonsRap_Rec2", 100,-5,5);
  diMuonsRap_Rec2->SetLineColor(2);
  TH1D *diMuonsRap_Rec3 = new TH1D("diMuonsRap_Rec3","diMuonsRap_Rec3", 100,-5,5);
  diMuonsRap_Rec3->SetLineColor(2);
  TH1D *diMuonsRap_Rec4 = new TH1D("diMuonsRap_Rec4","diMuonsRap_Rec4", 100,-5,5);
  diMuonsRap_Rec4->SetLineColor(2);
  TH1D *diMuonsRap_Rec5 = new TH1D("diMuonsRap_Rec5","diMuonsRap_Rec5", 100,-5,5);
  diMuonsRap_Rec5->SetLineColor(2);
  TH1D *Bin_Gen = new TH1D("Bin_Gen","Bin_Gen", 40,0,40);
  
  //==============================================Define AccEff Stuff here===========================================
  // Pt bin sizes
  int Nptbin=1;
  double pt_bound[100] = {0};
  if(iSpec == 1) { 
    Nptbin = 8;
    
   
    //for plots
    pt_bound[0] = 0.0;
    pt_bound[1] = 2.0;
    pt_bound[2] = 4.0;
    pt_bound[3] = 6.0;
    pt_bound[4] = 8.0;
    pt_bound[5] = 10.0;
    pt_bound[6] = 14.0;
    pt_bound[7] = 20.0;
     pt_bound[8] = 30.0;


    //pt_bound[0] = 0;
    //pt_bound[1] = 20.0;
    //pt_bound[2] = 0.0;
    //pt_bound[3] = 6.5;
    //pt_bound[4] = 10.0;
    //pt_bound[5] = 20.0;
    
    
   
  }
  
  if(iSpec == 2) { 
    Nptbin = 8;
    
    pt_bound[0] = -2.4; 
    pt_bound[1] = -1.6; 
    pt_bound[2] = -1.2; 
    pt_bound[3] = -0.8; 
    pt_bound[4] = 0.0; 
    pt_bound[5] = 0.8;
    pt_bound[6] = 1.2; 
    pt_bound[7] = 1.6; 
    pt_bound[8] = 2.4; 


    //pt_bound[0] = 0.0; 
    //pt_bound[1] = 1.2; 
    //pt_bound[2] = 2.4; 
    
   
    
  }
  
  
  if(iSpec == 3) {
    Nptbin = 8;
   
    // pt_bound[0] = 0.0;//0
    //pt_bound[1] = 8.0;//20
    //pt_bound[2] = 24.0;//60
    //pt_bound[3] = 40.0;//100



    //for plots
    pt_bound[0] = 0.0;//0
    pt_bound[1] = 4.0;//10
    pt_bound[2] = 8.0;//20
    pt_bound[3] = 12.0;//25
    pt_bound[4] = 16.0;//50
    pt_bound[5] = 20.0;//100
    pt_bound[6] = 24.0;
    pt_bound[7] = 32.0;
    pt_bound[8] = 40.0;
    
  }
  //X Axis error on Eff graph 
  double PT[100], DelPT[100], mom_err[100];
  for (Int_t ih = 0; ih < Nptbin; ih++) {
    PT[ih] = (pt_bound[ih] + pt_bound[ih+1])/2.0;
    DelPT[ih] = pt_bound[ih+1] - pt_bound[ih];
    mom_err[ih] = DelPT[ih]/2.0;
  }
  
  double genError, recError;
  double gen_pt[100]={0}, gen_ptError[100]={0}; 
  double rec_pt[100]={0}, rec_ptError[100]={0}; 
  double Eff_cat_1[100]={0},Err_Eff_cat_1[100]={0};  
  
  // Histogram arrays
  TH1D *diMuonsInvMass_GenA[10][1000];
  TH1D *diMuonsInvMass_RecA[10][1000];
  TH1D *diMuonsPt_GenA[10][1000];
  TH1D *diMuonsPt_RecA[10][1000];
  char nameGen[10][500], nameRec[10][500], nameGenPt[10][500], nameRecPt[10][500];
 
  
  for (int ifile = 0; ifile <= 5; ifile++) {
    for (Int_t ih = 0; ih < Nptbin; ih++) {
      sprintf(nameGen[ifile],"DiMuonMassGen_pt_%d_%d",ih,ifile);
      sprintf(nameRec[ifile],"DiMuonMassRec_pt_%d_%d",ih,ifile);
      
      sprintf(nameGenPt[ifile],"DiMuonPtGen_pt_%d_%d",ih,ifile);
      sprintf(nameRecPt[ifile],"DiMuonPtRec_pt_%d_%d",ih,ifile);
      
      diMuonsInvMass_GenA[ifile][ih]= new TH1D(nameGen[ifile],nameGen[ifile],  100,8.0,12.0); //for eff Gen;
      diMuonsInvMass_GenA[ifile][ih]->Sumw2();
      diMuonsInvMass_GenA[ifile][ih]->SetMarkerStyle(7);
      diMuonsInvMass_GenA[ifile][ih]->SetMarkerColor(4);
      diMuonsInvMass_GenA[ifile][ih]->SetLineColor(4);
      
      diMuonsInvMass_RecA[ifile][ih] = new TH1D(nameRec[ifile],nameRec[ifile], 100,8.0,12.0); //for eff Rec;
      diMuonsInvMass_RecA[ifile][ih]->Sumw2();
      diMuonsInvMass_RecA[ifile][ih]->SetMarkerStyle(8);
      diMuonsInvMass_RecA[ifile][ih]->SetMarkerColor(4);
      diMuonsInvMass_RecA[ifile][ih]->SetLineColor(4);
      
      
      diMuonsPt_GenA[ifile][ih]= new TH1D(nameGenPt[ifile],nameGenPt[ifile],  100,0,40); //for eff Gen;
      diMuonsPt_RecA[ifile][ih]= new TH1D(nameRecPt[ifile],nameRecPt[ifile],  100,0,40); //for eff Rec;
    }
  }
  
  //===========================================Input Root File============================================================
  char fileName[10][500];
  //0.0380228 	0.0480769 	0.0293255 	0.0125156 	0.00336587 	0.00276319*2/5 = 0.001105276
  //Scales
  double scale[10]={0};  
  
  scale[0]=(6.8802); // pT [0-3]
  scale[1]=(8.6995); // pT [3-6]
  scale[2]=(5.3065); // pT [6-9]
  scale[3]=(2.2647); // pT [9-12] 
  scale[4]=(3.0453); // pT [12-15] 
  scale[5]=(1.0000); // pT [15-30]
  

  
  sprintf(fileName[0],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt03_N.root");
  sprintf(fileName[1],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt36_N.root");
  sprintf(fileName[2],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt69_N.root");
  sprintf(fileName[3],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt912_N.root");
  sprintf(fileName[4],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1215_N.root");
  sprintf(fileName[5],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1530_N.root");
  
  TFile *infile;
  TTree *tree;
  TTree *gentree;
  
  //===========File loop ======================
  
  for(int ifile =0; ifile<=5; ifile++){
    
    infile=new TFile(fileName[ifile],"R");
    tree=(TTree*)infile->Get("SingleMuonTree");
    gentree=(TTree*)infile->Get("SingleGenMuonTree");
    
    //Event variables
    int eventNb,runNb,lumiBlock, gbin, rbin;
    //Jpsi Variables
    Double_t JpsiMass,JpsiPt,JpsiRap, JpsiCharge;
    Double_t JpsiVprob;
    //2.) muon variables RECO                                                                       
    double muPosPx, muPosPy, muPosPz,  muPosEta, muPosPt,muPosP;
    double muNegPx, muNegPy, muNegPz,  muNegEta, muNegPt,muNegP;
    //(1).Positive Muon                                     
    double muPos_nchi2In, muPos_dxy, muPos_dz, muPos_nchi2Gl;
    int muPos_found, muPos_pixeLayers, muPos_nValidMuHits,muPos_arbitrated;
    bool muPos_matches,muPos_tracker;
    //(2).Negative Muon                                     
    double muNeg_nchi2In, muNeg_dxy, muNeg_dz, muNeg_nchi2Gl;
    int muNeg_found, muNeg_pixeLayers, muNeg_nValidMuHits,muNeg_arbitrated;
    bool muNeg_matches,muNeg_tracker;
    //Gen Level variables
    //Gen JPsi Variables
    double GenJpsiMass, GenJpsiPt, GenJpsiRap;
    double GenJpsiPx, GenJpsiPy, GenJpsiPz;
    
    //2.) Gen muon variables 
    double GenmuPosPx, GenmuPosPy, GenmuPosPz,  GenmuPosEta, GenmuPosPt;
    double GenmuNegPx, GenmuNegPy, GenmuNegPz,  GenmuNegEta, GenmuNegPt;

    //Event variables
    tree->SetBranchAddress("eventNb",&eventNb);
    tree->SetBranchAddress("runNb",&runNb);
    tree->SetBranchAddress("lumiBlock",&lumiBlock);
    
    //Jpsi Variables
    tree->SetBranchAddress("JpsiCharge",&JpsiCharge);
    tree->SetBranchAddress("JpsiMass",&JpsiMass);
    tree->SetBranchAddress("JpsiPt",&JpsiPt);
    tree->SetBranchAddress("JpsiRap",&JpsiRap);
    tree->SetBranchAddress("JpsiVprob",&JpsiVprob);
    tree->SetBranchAddress("rbin",&rbin);
    
    //muon variable
    tree->SetBranchAddress("muPosPx",&muPosPx);
    tree->SetBranchAddress("muPosPy",&muPosPy);
    tree->SetBranchAddress("muPosPz",&muPosPz);
    tree->SetBranchAddress("muPosEta",&muPosEta);
    tree->SetBranchAddress("muNegPx", &muNegPx);
    tree->SetBranchAddress("muNegPy",    &muNegPy);
    tree->SetBranchAddress("muNegPz",    &muNegPz);
    tree->SetBranchAddress("muNegEta",    &muNegEta);
    //1). Positive Muon
    tree->SetBranchAddress("muPos_nchi2In", &muPos_nchi2In);
    tree->SetBranchAddress("muPos_dxy", &muPos_dxy);
    tree->SetBranchAddress("muPos_dz", &muPos_dz);
    tree->SetBranchAddress("muPos_nchi2Gl", &muPos_nchi2Gl);
    tree->SetBranchAddress("muPos_found", &muPos_found);
    tree->SetBranchAddress("muPos_pixeLayers", &muPos_pixeLayers);
    tree->SetBranchAddress("muPos_nValidMuHits", &muPos_nValidMuHits);
    tree->SetBranchAddress("muPos_matches", &muPos_matches);
    tree->SetBranchAddress("muPos_tracker", &muPos_tracker);
    tree->SetBranchAddress("muPos_arbitrated", &muPos_arbitrated);
    
    //2). Negative Muon                                                                            
    tree->SetBranchAddress("muNeg_nchi2In", &muNeg_nchi2In);
    tree->SetBranchAddress("muNeg_dxy", &muNeg_dxy);
    tree->SetBranchAddress("muNeg_dz", &muNeg_dz);
    tree->SetBranchAddress("muNeg_nchi2Gl", &muNeg_nchi2Gl);
    tree->SetBranchAddress("muNeg_found", &muNeg_found);
    tree->SetBranchAddress("muNeg_pixeLayers", &muNeg_pixeLayers);
    tree->SetBranchAddress("muNeg_nValidMuHits", &muNeg_nValidMuHits);
    tree->SetBranchAddress("muNeg_matches", &muNeg_matches);
    tree->SetBranchAddress("muNeg_tracker", &muNeg_tracker);
    tree->SetBranchAddress("muNeg_arbitrated", &muNeg_arbitrated);
    
    //====================================Gen Variables=========================================================
    //Gen Jpsi Variables
    gentree->SetBranchAddress("GenJpsiMass",   &GenJpsiMass);
    gentree->SetBranchAddress("GenJpsiPt",     &GenJpsiPt);
    gentree->SetBranchAddress("GenJpsiRap",    &GenJpsiRap);
    gentree->SetBranchAddress("GenJpsiPx",     &GenJpsiPx);
    gentree->SetBranchAddress("GenJpsiPy",     &GenJpsiPy);
    gentree->SetBranchAddress("GenJpsiPz",     &GenJpsiPz);
    gentree->SetBranchAddress("gbin",&gbin);
    //muon variable
    gentree->SetBranchAddress("GenmuPosPx",    &GenmuPosPx);
    gentree->SetBranchAddress("GenmuPosPy",    &GenmuPosPy);
    gentree->SetBranchAddress("GenmuPosPz",    &GenmuPosPz);
    gentree->SetBranchAddress("GenmuPosEta",    &GenmuPosEta);
    gentree->SetBranchAddress("GenmuNegPx",    &GenmuNegPx);
    gentree->SetBranchAddress("GenmuNegPy",    &GenmuNegPy);
    gentree->SetBranchAddress("GenmuNegPz",    &GenmuNegPz);
    gentree->SetBranchAddress("GenmuNegEta",    &GenmuNegEta);
    

    //====================================================== Gen tree loop ================================================
    int NAccep=0;
    int nGenEntries=gentree->GetEntries();
    cout<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ========="<<endl;
    //dataFile<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ====="<<endl;
    
    
    for(int i=0; i< nGenEntries; i++)  {	    
      gentree->GetEntry(i);
      
      if(i%1000==0){
	cout<<" processing record "<<i<<endl;
	cout<<" Mass "<< GenJpsiMass<< " pT "<< GenJpsiPt << " Y " <<GenJpsiRap<<endl; 
      }
      
      bool GenPosIn=0, GenNegIn=0,GenPosPass=0,GenNegPass=0;
      
      GenmuPosPt= TMath::Sqrt(GenmuPosPx*GenmuPosPx + GenmuPosPy*GenmuPosPy); 
      GenmuNegPt= TMath::Sqrt(GenmuNegPx*GenmuNegPx + GenmuNegPy*GenmuNegPy); 
            
      diMuonsInvMass_Gen->Fill(GenJpsiMass);
      diMuonsPt_Gen->Fill(GenJpsiPt);
 
      if(IsAccept(GenmuPosPt, GenmuPosEta)) {GenPosIn=1;}
      if(IsAccept(GenmuNegPt, GenmuNegEta)) {GenNegIn=1;}
      
     
      if(GenPosIn && GenNegIn ) NAccep++;
      
      if(GenPosIn==1 && GenmuPosPt> PtCut) {GenPosPass=1;}
      if(GenNegIn==1 && GenmuNegPt> PtCut) {GenNegPass=1;}

      double GenCenWeight=0,GenWeight=0;
      GenCenWeight=FindCenWeight(gbin);
      Bin_Gen->Fill(gbin);
      GenWeight=GenCenWeight*scale[ifile];
      if(PutWeight==0){GenWeight=1;}
      
      if(GenPosIn && GenNegIn){
	if(ifile==0){diMuonsRap_Gen0->Fill(GenJpsiRap);}
	if(ifile==1){diMuonsRap_Gen1->Fill(GenJpsiRap);}
	if(ifile==2){diMuonsRap_Gen2->Fill(GenJpsiRap);}
	if(ifile==3){diMuonsRap_Gen3->Fill(GenJpsiRap);}
	if(ifile==4){diMuonsRap_Gen4->Fill(GenJpsiRap);}
	if(ifile==5){diMuonsRap_Gen5->Fill(GenJpsiRap);}
      }
      
      
      for (Int_t ih = 0; ih < Nptbin; ih++) {
	//adding pT of all pt bins to see diss is cont
	if(iSpec == 1)  if( (GenPosPass==1 && GenNegPass==1) && (TMath::Abs(GenJpsiRap)<2.4 ) && (GenJpsiPt>pt_bound[ih] && GenJpsiPt<=pt_bound[ih+1])){diMuonsPt_GenA[ifile][ih]->Fill(GenJpsiPt,GenWeight);}
	
	if(iSpec == 1)  if( (GenPosPass==1 && GenNegPass==1) && (TMath::Abs(GenJpsiRap)<2.4 )&&(GenJpsiPt>pt_bound[ih] && GenJpsiPt<=pt_bound[ih+1])){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight);}
	//if(iSpec == 2)  if((GenPosPass==1 && GenNegPass==1) &&  (GenJpsiPt<20.0) && (TMath::Abs(GenJpsiRap) > pt_bound[ih] && TMath::Abs(GenJpsiRap) <=pt_bound[ih+1] )){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight);}
	

	//for non symetric plots
	if(iSpec == 2)  if((GenPosPass==1 && GenNegPass==1) &&  (GenJpsiPt<20.0) && ((GenJpsiRap) > pt_bound[ih] && (GenJpsiRap) <=pt_bound[ih+1] )){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight);}

	if(iSpec == 3)  if( (GenPosPass==1 && GenNegPass==1) && (GenJpsiPt < 20.0) &&  (TMath::Abs(GenJpsiRap)<2.4 ) && (gbin>=pt_bound[ih] && gbin<pt_bound[ih+1])){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight);}
	
      }
      
    }//gen loop end
    
    cout<<" accepted no "<< NAccep<<endl;
    //dataFile<<" accepted no "<< NAccep<<endl;
    
    //   new TCanvas;
    //diMuonsInvMass_Gen->Draw();
    //gPad->Print("plots/diMuonsInvMass_Gen.png");
    
    //new TCanvas;
    //diMuonsPt_Gen->Draw();
    //gPad->Print("plots/diMuonsPt_Gen.png");
    
    //new TCanvas;
    //diMuonsRap_Gen0->Draw();
    //sprintf(PlotName,"plots/diMuonsRap_Gen_%d.pdf",ifile);   
    //gPad->Print(PlotName);
    
    //new TCanvas;
    //Bin_Gen->Draw();
    //gPad->Print("plots/Bin_Gen.png");
    
    
    
    //=============== Rec Tree Loop ==============================================================================
    
    int nRecEntries=tree->GetEntries();
    cout<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<< "====="<<endl;
    //dataFile<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<<endl;
    
    for(int i=0; i<nRecEntries; i++)  {	    
      tree->GetEntry(i);
      if(i%100000==0){
	cout<<" processing record "<<i<<endl;
	cout<<" processing Run  " <<runNb <<" event "<<eventNb<<" lum block "<<lumiBlock<<endl;    
	cout<<" Mass "<< JpsiMass<< " pT "<< JpsiPt << " Y " <<JpsiRap<<"  "<<JpsiVprob<<" charge "<<JpsiCharge<<endl; 
      }
      
      bool PosPass=0, NegPass=0, AllCut=0 ,PosIn=0, NegIn=0;
      muPosPt= TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy); 
      muPosP = TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy+ muPosPz*muPosPz); 
      muNegPt= TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy); 
      muNegP = TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy +muNegPz*muNegPz); 
    
      if(IsAccept(muPosPt, muPosEta)){PosIn=1;}
      if(IsAccept(muNegPt, muNegEta)){NegIn=1;}
      
      if(muPos_found > 10 && muPos_pixeLayers > 0 && muPos_nchi2In < 4.0 && muPos_dxy < 3 && muPos_dz < 15 && muPos_nchi2Gl < 20 &&  muPos_arbitrated==1 && muPos_tracker==1 ){PosPass=1;}	  
	 
	 if((muNeg_found >10 && muNeg_pixeLayers >0 && muNeg_nchi2In <4.0 && muNeg_dxy < 3 && muNeg_dz < 15 && muNeg_nchi2Gl < 20 && muNeg_arbitrated==1 && muNeg_tracker==1)){NegPass=1;}

      

	     // cout<<muPos_matches<<"  "<<muNeg_matches<<endl;

	  if((muPosPt > PtCut && muNegPt > PtCut) && (muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)){AllCut=1;}
	 
	 //without muon ID cuts
	 //if((muPosPt > PtCut && muNegPt > PtCut) && (muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1)){AllCut=1;}
	 
	 //without trigger
	 //if(  (muPosPt > PtCut && muNegPt > PtCut) && (PosIn==1 && NegIn==1 )  &&   (PosPass==1 && NegPass==1)){AllCut=1;}
	 
	 
	 double RecCenWeight=0,RecWeight=0;
	 RecCenWeight=FindCenWeight(rbin);
	 RecWeight=RecCenWeight*scale[ifile];
	 if(PutWeight==0){RecWeight=1;}
	 
	 if(i%100000==0){
	   cout<<" eff loop for reco "<<endl;
	 }
      
	 if(AllCut==1){
	   if(ifile==0){diMuonsRap_Rec0->Fill(JpsiRap);}
	   if(ifile==1){diMuonsRap_Rec1->Fill(JpsiRap);}
	   if(ifile==2){diMuonsRap_Rec2->Fill(JpsiRap);}
	   if(ifile==3){diMuonsRap_Rec3->Fill(JpsiRap);}
	   if(ifile==4){diMuonsRap_Rec4->Fill(JpsiRap);}
	   if(ifile==5){diMuonsRap_Rec5->Fill(JpsiRap);}
	 }

	 //Eff loop for reco
	 if((JpsiCharge == 0) && (JpsiVprob > 0.01)) {
	   for (Int_t ih = 0; ih < Nptbin; ih++) {
	     //to see cont reco pT

	     if(iSpec == 1)if((AllCut==1) && (TMath::Abs(JpsiRap) < 2.4) && (JpsiPt>pt_bound[ih] && JpsiPt<=pt_bound[ih+1])) {diMuonsPt_RecA[ifile][ih]->Fill(JpsiPt,RecWeight);}
	     if(iSpec == 1)if((AllCut==1) && (TMath::Abs(JpsiRap)<2.4 ) && (JpsiPt > pt_bound[ih] && JpsiPt <=pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass, RecWeight);}
	    

	     //if(iSpec == 2) if( (AllCut==1) &&  (JpsiPt<20.0) && (TMath::Abs(JpsiRap) > pt_bound[ih] && TMath::Abs(JpsiRap) <=pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass,RecWeight);}
	     
	     //for non symetric plots
	     if(iSpec == 2) if( (AllCut==1) &&  (JpsiPt<20.0) && ((JpsiRap) > pt_bound[ih] && (JpsiRap) <=pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass,RecWeight);}
	     

	     if(iSpec == 3) if((AllCut==1) &&  (JpsiPt<20.0) && (TMath::Abs(JpsiRap) < 2.4) && (rbin>=pt_bound[ih] &&  rbin < pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass,RecWeight);}
	     
	   }
	 }
 }
  
    /*
      new TCanvas;
      if(ifile==0){diMuonsRap_Gen0->Draw();new TCanvas; diMuonsRap_Rec0->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen0.png");}
      if(ifile==1){diMuonsRap_Gen1->Draw();new TCanvas; diMuonsRap_Rec1->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen1.png");}
      if(ifile==2){diMuonsRap_Gen2->Draw();new TCanvas; diMuonsRap_Rec2->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen2.png");}
      if(ifile==3){diMuonsRap_Gen3->Draw();new TCanvas; diMuonsRap_Rec3->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen3.png");}
      if(ifile==4){diMuonsRap_Gen4->Draw();new TCanvas; diMuonsRap_Rec4->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen4.png");}
      if(ifile==5){diMuonsRap_Gen5->Draw();new TCanvas; diMuonsRap_Rec5->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen5.png");}
    */
    }  // file loop ends 

  ///////////////////////////////////////////////////////////////////

  cout<< " adding "<<endl;
  TH1D *diMuonsInvMass_RecA1[100];
  TH1D *diMuonsInvMass_GenA1[100];
  TH1D *diMuonsPt_GenA1[100];
  TH1D *diMuonsPt_RecA1[100];
  TF1 *backfun_1;
  char namePt_1B[500];//for bkg func
 
  for(Int_t ih = 0; ih < Nptbin; ih++){
    diMuonsInvMass_RecA1[ih] = diMuonsInvMass_RecA[0][ih];
    diMuonsInvMass_GenA1[ih] = diMuonsInvMass_GenA[0][ih];
    diMuonsPt_GenA1[ih] = diMuonsPt_GenA[0][ih];
    diMuonsPt_RecA1[ih] = diMuonsPt_RecA[0][ih];
    
    for (int ifile = 1; ifile <= 5; ifile++) {
      diMuonsInvMass_RecA1[ih]->Add(diMuonsInvMass_RecA[ifile][ih]);
      diMuonsInvMass_GenA1[ih]->Add(diMuonsInvMass_GenA[ifile][ih]);     
      
      diMuonsPt_GenA1[ih]->Add(diMuonsPt_GenA[ifile][ih]); 
      diMuonsPt_RecA1[ih]->Add(diMuonsPt_RecA[ifile][ih]); 
    }
  }
  
  //===========================Fitting===================================================================//
  // Fit ranges
  double mass_low, mass_high;
  double MassUpsilon, WeidthUpsilon;
  
  // Low mass range upsilon width 54 KeV
  MassUpsilon = 9.46; WeidthUpsilon = 0.060;
  mass_low = 8.8; mass_high = 10.0;  // Fit ranges
  
  
  // Fit Function crystall ball
  // TF1 *GAUSPOL = new TF1("GAUSPOL",CrystalBall,8.0,12.0,5);
  //GAUSPOL->SetParNames("#alpha","n","Mean","#sigma","N");
  //GAUSPOL->SetLineWidth(2.0);
  //GAUSPOL->SetLineColor(2);
  //GAUSPOL->SetParameter(0, 1.756);
  //GAUSPOL->SetParameter(1, 2.636);
  //GAUSPOL->SetParameter(2, MassUpsilon);
  //GAUSPOL->SetParameter(3, WeidthUpsilon);
  //GAUSPOL->SetParLimits(2, 0.1*MassUpsilon,2.0*MassUpsilon);
  //GAUSPOL->SetParLimits(3, 0.1*WeidthUpsilon,2.0*WeidthUpsilon);


 // Fit Function crystall ball
  TF1 *GAUSPOL = new TF1("GAUSPOL",CrystalBall,8.0,12.0,6);
  GAUSPOL->SetParNames("Yield (#Upsilon)","BinWidth","Mean","Sigma","#alpha","n");
  GAUSPOL->SetParameter(2, MassUpsilon);
  GAUSPOL->SetParameter(3, WeidthUpsilon);
  GAUSPOL->SetParLimits(3, 0.1*WeidthUpsilon,2.0*WeidthUpsilon);
  GAUSPOL->SetParameter(4, 1.0);
  GAUSPOL->SetParameter(5, 20.0);
  GAUSPOL->SetLineWidth(2.0);
  GAUSPOL->SetLineColor(2);
  


  //=====================Loop for eff===========================================================
  double GenNo[100]={0};
  double Eff[100]={0};
  double GenError[100]={0};
  double RecError[100]={0};
  double errEff_cat_S1[100]={0};
  double errEff_cat_S2[100]={0};
  double errEff_cat_S1_1[100]={0},errEff_cat_S1_2[100]={0};
  double errEff_cat_S2_1[100]={0},errEff_cat_S2_2[100]={0};
  char PlotName[500],PlotName1[500], PlotName2[500]; 
  char GPlotName[500],GPlotName1[500], GPlotName2[500];

  for (Int_t ih = 0; ih < Nptbin; ih++) {
   
    cout<<" no of gen dimuons from diMuons Pt histo    "<<diMuonsPt_GenA1[ih]->Integral(1,100)<<endl;
    cout<<" no of gen dimuons from diMuons Mass histo  "<<diMuonsInvMass_GenA1[ih]->Integral(1,100)<<endl;
    

    //from pT histogram
    //gen_pt[ih] =diMuonsPt_GenA1[ih]->IntegralAndError(1,100,genError);
   
    gen_pt[ih] = diMuonsInvMass_GenA1[ih]->IntegralAndError(1,100,genError);
    gen_ptError[ih]= genError;
    
    if(iSpec==1) sprintf(PlotName,"plots/DiMuonMass_PtBin_%d.png",ih);
    if(iSpec==2) sprintf(PlotName,"plots/DiMuonMass_RapBin_%d.png",ih);
    if(iSpec==3) sprintf(PlotName,"plots/DiMuonMass_CentBin_%d.png",ih);
    
    if(iSpec==1) sprintf(PlotName1,"plots/DiMuonMass_PtBin_%d.pdf",ih);
    if(iSpec==2) sprintf(PlotName1,"plots/DiMuonMass_RapBin_%d.pdf",ih);
    if(iSpec==3) sprintf(PlotName1,"plots/DiMuonMass_CentBin_%d.pdf",ih);
    
    if(iSpec==1) sprintf(PlotName2,"plots/DiMuonMass_PtBin_%d.eps",ih);
    if(iSpec==2) sprintf(PlotName2,"plots/DiMuonMass_RapBin_%d.eps",ih);
    if(iSpec==3) sprintf(PlotName2,"plots/DiMuonMass_CentBin_%d.eps",ih);
 
    //giving inetial value for crystall ball fourth parameter 
    diMuonsInvMass_RecA1[ih]->Rebin(2);
    //GAUSPOL->SetParameter(0, diMuonsInvMass_RecA1[ih]->GetMaximum());
    GAUSPOL->SetParameter(0, diMuonsInvMass_RecA1[ih]->Integral(0,50));
    GAUSPOL->FixParameter(1, diMuonsInvMass_RecA1[ih]->GetBinWidth(1));
    
    new TCanvas; 
    diMuonsInvMass_RecA1[ih]->Fit("GAUSPOL","LLMERQ", "", mass_low, mass_high);
   
    double UpsilonMass = GAUSPOL->GetParameter(2);
    double UpsilonWidth = GAUSPOL->GetParameter(3);
   
    double UpsilonYield = GAUSPOL->GetParameter(0);
    double UpsilonYieldError = GAUSPOL->GetParError(0);

    double par[20];
    GAUSPOL->GetParameters(par);
    sprintf(namePt_1B,"pt_1B_%d",ih);
    backfun_1 = new TF1(namePt_1B, Pol2, mass_low, mass_high, 3);
    backfun_1->SetParameters(&par[4]);
   

    double MassLow=(UpsilonMass-3*UpsilonWidth);
    double MassHigh=(UpsilonMass+3*UpsilonWidth);
    
    int binlow =diMuonsInvMass_RecA1[ih]->GetXaxis()->FindBin(MassLow);
    int binhi =diMuonsInvMass_RecA1[ih]->GetXaxis()->FindBin(MassHigh);
    double binwidth=diMuonsInvMass_RecA1[ih]->GetBinWidth(1);
        
    //yield by function 
    //rec_pt[ih] = UpsilonYield;
    //rec_ptError[ih]=UpsilonYieldError;

    cout<<"Rec diMuons from Pt histo "<<diMuonsPt_RecA1[ih]->Integral(1,100)<<endl;  
    cout<<"Rec dimuons from mass "<<diMuonsInvMass_RecA1[ih]->Integral(1,100)<<endl; 
    

      
    //from pT histo
    //rec_pt[ih]=diMuonsPt_RecA1[ih]->IntegralAndError(1, 100,recError);

    //yield by histogram integral
    rec_pt[ih] = diMuonsInvMass_RecA1[ih]->IntegralAndError(binlow, binhi,recError);
    rec_ptError[ih]= recError;
  
    //Cal eff 
    Eff_cat_1[ih] = rec_pt[ih]/gen_pt[ih]; 
  
    //calculate error on eff
    GenNo[ih]=gen_pt[ih];
    Eff[ih]= Eff_cat_1[ih];
    GenError[ih]=gen_ptError[ih];
    RecError[ih]=rec_ptError[ih];
    //error    
    errEff_cat_S1_1[ih]= ( (Eff[ih] * Eff[ih]) /(GenNo[ih] * GenNo[ih]) );
    errEff_cat_S1_2[ih]= (RecError[ih] * RecError[ih]);
    errEff_cat_S1[ih]= (errEff_cat_S1_1[ih] * errEff_cat_S1_2[ih]);
   

    errEff_cat_S2_1[ih]= ( (1 - Eff[ih])* (1 - Eff[ih]) ) / ( GenNo[ih] * GenNo[ih]);
    errEff_cat_S2_2[ih]= (GenError[ih] * GenError[ih] ) - ( RecError[ih] * RecError[ih] );


    errEff_cat_S2[ih]=errEff_cat_S2_1[ih]*errEff_cat_S2_2[ih];
    Err_Eff_cat_1[ih]=sqrt(errEff_cat_S1[ih] + errEff_cat_S2[ih]);
   
    //error for no weights
    //Err_Eff_cat_1[ih]= Eff_cat_1[ih]*TMath::Sqrt(gen_ptError[ih]*gen_ptError[ih]/(gen_pt[ih]*gen_pt[ih]) + rec_ptError[ih]*rec_ptError[ih]/(rec_pt[ih]* rec_pt[ih]));

    cout<<"Upsilon Yield by integral of histo:  "<< diMuonsInvMass_RecA1[ih]->IntegralAndError(binlow, binhi,recError) <<"  error "<< rec_ptError[ih]<<endl; 
    cout<<"UpsilonYield by CB    yield det:     "<< UpsilonYield << " UpsilonWidth "<< UpsilonWidth<<" UpsilonMass "<<UpsilonMass <<endl;
    cout<<"Upsilon Yield by Function integral:  "<< GAUSPOL->Integral(MassLow,MassHigh)/binwidth <<endl;
    cout<<" rec_pt[ih]  "<<  rec_pt[ih] <<" gen_pt[ih] "<<gen_pt[ih]<<endl;
    //dataFile<<" rec_pt[ih]  "<<  rec_pt[ih] <<" gen_pt[ih] "<<gen_pt[ih]<<endl;
    cout<<" eff "<< Eff_cat_1[ih]<<" error "<<Err_Eff_cat_1[ih]<<endl;
    
    dataFile<<"ih " <<ih<<" eff "<< Eff_cat_1[ih]<<" error "<<Err_Eff_cat_1[ih]<<endl;
    
    if(iSpec==1) sprintf(GPlotName,"plots/GenDiMuonMass_PtBin_%d.png",ih);
    if(iSpec==2) sprintf(GPlotName,"plots/GenDiMuonMass_RapBin_%d.png",ih);
    if(iSpec==3) sprintf(GPlotName,"plots/GenDiMuonMass_CentBin_%d.png",ih);
    
    if(iSpec==1) sprintf(GPlotName1,"plots/GenDiMuonMass_PtBin_%d.pdf",ih);
    if(iSpec==2) sprintf(GPlotName1,"plots/GenDiMuonMass_RapBin_%d.pdf",ih);
    if(iSpec==3) sprintf(GPlotName1,"plots/GenDiMuonMass_CentBin_%d.pdf",ih);
    
    if(iSpec==1) sprintf(GPlotName2,"plots/GenDiMuonMass_PtBin_%d.eps",ih);
    if(iSpec==2) sprintf(GPlotName2,"plots/GenDiMuonMass_RapBin_%d.eps",ih);
    if(iSpec==3) sprintf(GPlotName2,"plots/GenDiMuonMass_CentBin_%d.eps",ih);
       
    backfun_1->SetLineColor(4);
    backfun_1->SetLineWidth(1);
    //backfun_1->Draw("same");
    gPad->Print(PlotName);
    gPad->Print(PlotName1);
    gPad->Print(PlotName2);

    new TCanvas;
    diMuonsInvMass_GenA1[ih]->Draw();
    gPad->Print(GPlotName);
    gPad->Print(GPlotName1);
    gPad->Print(GPlotName2);


    // new TCanvas;
    //diMuonsPt_GenA1[ih]->Draw();
    //new TCanvas;
    //diMuonsPt_RecA1[ih]->Draw();
 
}
  dataFile.close();

   TFile *outfile;
  if(iSpec==1)outfile =new TFile("EffUpsilon_Pt.root","Recreate");
  if(iSpec==2)outfile =new TFile("EffUpsilon_Rap.root","Recreate");
  if(iSpec==3)outfile =new TFile("EffUpsilon_Cent.root","Recreate");

  TGraphErrors *Eff_Upsilon = new TGraphErrors(Nptbin, PT, Eff_cat_1, mom_err,Err_Eff_cat_1);
  Eff_Upsilon->SetMarkerStyle(21);
  Eff_Upsilon->SetMarkerColor(2);
  Eff_Upsilon->GetYaxis()->SetTitle("Reco Eff");
 
  if(iSpec==1) Eff_Upsilon->GetXaxis()->SetTitle("#Upsilon pT (GeV/c^{2})");
  if(iSpec==2) Eff_Upsilon->GetXaxis()->SetTitle("#Upsilon rapidity");
  if(iSpec==3) Eff_Upsilon->GetXaxis()->SetTitle("bin");
   Eff_Upsilon->GetYaxis()->SetRangeUser(0,1.0);

  TLegend *legend_GP = new TLegend( 0.50,0.79,0.80,0.89);
  legend_GP->SetBorderSize(0);
  legend_GP->SetFillStyle(0);
  legend_GP->SetFillColor(0);
  legend_GP->SetTextSize(0.032);
  legend_GP->AddEntry(Eff_Upsilon,"PythiaEvtGen + HydjetBass", "P");
  
  new TCanvas;
  Eff_Upsilon->Draw("AP");
  legend_GP->Draw("Same");
    
 Eff_Upsilon->Write();

  if(iSpec==1){ gPad->Print("plots/Eff_Upsilon_Pt.pdf");gPad->Print("plots/Eff_Upsilon_Pt.png");gPad->Print("plots/Eff_Upsilon_Pt.eps");}
  if(iSpec==2){ gPad->Print("plots/Eff_Upsilon_Rap.pdf");gPad->Print("plots/Eff_Upsilon_Rap.png");  gPad->Print("plots/Eff_Upsilon_Rap.eps");}
  if(iSpec==3){ gPad->Print("plots/Eff_Upsilon_Cent.pdf");gPad->Print("plots/Eff_Upsilon_Cent.png"); gPad->Print("plots/Eff_Upsilon_Cent.eps"); }
 
  outfile->Write();
  outfile->Close();



}
void ppEffJpsiSysSFsSTA__idx_()   
{
  double minRap = 0.0;
  double maxRap = 2.4;
  double minPt = 6.5;
  double maxPt = 30.0;

  // if you want to measure low pT, you can set up minRap = 1.6 and minPt = 3.0, maxPt = 6.5;

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(0);
  gStyle->SetCanvasColor(0);
  gStyle->SetTitleFillColor(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadBorderSize(0);
  gStyle->SetCanvasBorderSize(0);
  gStyle->SetOptTitle(0); // at least most of the time
  gStyle->SetOptStat(0); // most of the time, sometimes "nemriou" might be useful to display name, 
  //number of entries, mean, rms, integral, overflow and underflow
  gStyle->SetOptFit(0); // set to 1 only if you want to display fit results
  //==================================== Define Histograms====================================================

  //==============================================Define Acc Eff Stuff here===========================================
  // Pt bin sizes
  // 0-1.5, 1.5-3, 3-4.5, 4.5-6, 6-7.5...

  TFile *outfile;
  char tmp_output[512];
  sprintf(tmp_output,"ppPrJpsi_pT_%0.1f_%0.1f_y_%0.1f_%0.1f_HighQ_tnpWgt.root", minPt, maxPt, minRap, maxRap);
  outfile =new TFile(tmp_output, "Recreate");

  for(int iSpec = 0; iSpec < 2; iSpec++){

    const int nPtBins = 7;
    const int nRapBins = 6; // 6
    const int ndPhiBins = 4;
    double pt_bound[nPtBins+1] = {6.5, 7.5, 8.5, 9.5, 11.0, 13.0, 16.0, 30.0};
    double xpt_bound[nPtBins] = {0.0};
    //double rap_bound[nRapBins+1] = {0.0, 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4};
    double rap_bound[nRapBins+1] = {0.0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4};
    double xrap_bound[nRapBins] = {0.0};
    double dphi_bound[ndPhiBins+1] = {0.0, TMath::Pi()/8, TMath::Pi()/4, 3*TMath::Pi()/8, TMath::Pi()/2};
    double xdphi_bound[ndPhiBins] = {0.0};

    const char *cSp[2] = {"Pts","Raps"};
    char OutTextFile[100];
    sprintf(OutTextFile,"ppPromp_eff_%s_pT_%0.1f_%0.1f_y_%0.1f_%0.1f_HighQ.tex", cSp[iSpec], minPt, maxPt, minRap, maxRap);
    ofstream dataFile(OutTextFile);
    //ofstream dataFile(Form(OutTextFile));

    cout<< "%%%% Getting Efficiency starts : %s !!!!! %%%%%" << endl;
    //dataFile<< "%%%% Getting Efficiency starts : %s !!!!! %%%%%" << endl;

    // x, y, z - axis 
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of pT"<<endl;
    for(int i = 0; i < nPtBins; i++){
      xpt_bound[i] = pt_bound[i] + (pt_bound[i+1]-pt_bound[i])/2;
      cout<<"xpt_bound["<<i<<"] : "<<xpt_bound[i]<<endl;
      //dataFile<<"xpt_bound["<<i<<"] : "<<xpt_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of rap"<<endl;
    for(int i = 0; i < nRapBins; i++){
      xrap_bound[i] = rap_bound[i] + (rap_bound[i+1]-rap_bound[i])/2;
      cout<<"xrap_bound["<<i<<"] : "<<xrap_bound[i]<<endl;
      //dataFile<<"xrap_bound["<<i<<"] : "<<xrap_bound[i]<<endl;
    }
    //dataFile<<""<<endl;
    //dataFile<<"xaxis of dphi"<<endl;
    for(int i = 0; i < ndPhiBins; i++){
      xdphi_bound[i] = dphi_bound[i] + (dphi_bound[i+1]-dphi_bound[i])/2;
      cout<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
      //dataFile<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
    }

    int nBins_tmp = 0;
    if(iSpec == 0) { nBins_tmp = nPtBins; }
    if(iSpec == 1) { nBins_tmp = nRapBins; }
    const int nBins = nBins_tmp;

    TH1F *hTempMass = new TH1F("hTempMass","",100, 2.0, 4.0);
    TH1F *hGenDiMuon[nBins];
    TH1F *hRecoDiMuon[nBins];
    double genNo[nBins];
    double genErr[nBins];
    double recoNo[nBins];
    double recoErr[nBins];
    double eff[nBins];
    double effErr[nBins];
    for(int i = 0; i < nBins; i++){
      hGenDiMuon[i] = (TH1F*)hTempMass->Clone();
      hRecoDiMuon[i] = (TH1F*)hTempMass->Clone();
      hGenDiMuon[i]->Sumw2();
      hRecoDiMuon[i]->Sumw2();
    }

    char fileName[10][512];

    // loop for pT
    cout<<"==================Prompt PrJpsi================================================"<<endl;

    sprintf(fileName[0],"/Users/dmoon/Dropbox/Analysis/HiMC/EffStudy/gRpAngRootFiles_538_pp/HiDiMuonAna_538_pp_NonPromptJpsi_20140317_Trg21.root");


    TFile *infile;
    TTree *tree;
    TTree *gentree;
    TTree *evttree;

    infile=new TFile(fileName[0],"R");
    tree=(TTree*)infile->Get("SingleMuonTree");
    gentree=(TTree*)infile->Get("SingleGenMuonTree");
    evttree=(TTree*)infile->Get("EventTree");
    //Event variables
    int eventNb,runNb,lumiBlock;
    int hbit1;
    //double vertexZ;
    //double GenvertexZ;
    //Jpsi Variables
    Double_t JpsiMass,JpsiPt,JpsiRap, JpsiCharge;
    Double_t JpsiVprob;
    Double_t JpsiPhi;
    Double_t JpsiEta;
    //2.) muon variables RECO                                                                       
    double muPosPx, muPosPy, muPosPz,  muPosEta, muPosPt, muPosP, muPosPhi;
    double muNegPx, muNegPy, muNegPz,  muNegEta, muNegPt, muNegP, muNegPhi;
    //(1).Positive Muon                                     
    double muPos_nchi2In, muPos_dxy, muPos_dz, muPos_nchi2Gl;
    int muPos_found, muPos_pixeLayers, muPos_nValidMuHits,muPos_arbitrated;
    bool muPos_matches,muPos_tracker;
    int muPos_Trigger10, muPos_Trigger2, muPos_Trigger21, muPos_Trigger22;
    //(2).Negative Muon                                     
    double muNeg_nchi2In, muNeg_dxy, muNeg_dz, muNeg_nchi2Gl;
    int muNeg_found, muNeg_pixeLayers, muNeg_nValidMuHits,muNeg_arbitrated;
    bool muNeg_matches,muNeg_tracker;
    int muNeg_Trigger10, muNeg_Trigger2, muNeg_Trigger21, muNeg_Trigger22;

    //Gen Level variables
    //Gen PrJpsi Variables
    double GenJpsiMass, GenJpsiPt, GenJpsiRap;
    double GenJpsiPx, GenJpsiPy, GenJpsiPz;
    double GenJpsiPhi;
    double GenJpsiEta;
    //2.) Gen muon variables 
    double GenmuPosPx, GenmuPosPy, GenmuPosPz,  GenmuPosEta, GenmuPosPt, GenmuPosPhi;
    double GenmuNegPx, GenmuNegPy, GenmuNegPz,  GenmuNegEta, GenmuNegPt, GenmuNegPhi;
    double zVtx;

    //Event variables
    tree->SetBranchAddress("eventNb",&eventNb);
    tree->SetBranchAddress("runNb",&runNb);
    tree->SetBranchAddress("lumiBlock",&lumiBlock);
    tree->SetBranchAddress("hbit1",&hbit1);
    tree->SetBranchAddress("zVtx",&zVtx);
    //tree->SetBranchAddress("vertexZ",&vertexZ);

    //Jpsi Variables
    tree->SetBranchAddress("JpsiCharge",&JpsiCharge);
    tree->SetBranchAddress("JpsiMass",&JpsiMass);
    tree->SetBranchAddress("JpsiPt",&JpsiPt);
    tree->SetBranchAddress("JpsiPhi",&JpsiPhi);
    tree->SetBranchAddress("JpsiEta",&JpsiEta);
    tree->SetBranchAddress("JpsiRap",&JpsiRap);
    tree->SetBranchAddress("JpsiVprob",&JpsiVprob);

    //muon variable
    tree->SetBranchAddress("muPosPx",&muPosPx);
    tree->SetBranchAddress("muPosPy",&muPosPy);
    tree->SetBranchAddress("muPosPz",&muPosPz);
    tree->SetBranchAddress("muPosEta",&muPosEta);
    tree->SetBranchAddress("muPosPhi",&muPosPhi);
    tree->SetBranchAddress("muNegPx", &muNegPx);
    tree->SetBranchAddress("muNegPy", &muNegPy);
    tree->SetBranchAddress("muNegPz", &muNegPz);
    tree->SetBranchAddress("muNegEta", &muNegEta);
    tree->SetBranchAddress("muNegPhi", &muNegPhi);


    //1). Positive Muon
    tree->SetBranchAddress("muPos_nchi2In", &muPos_nchi2In);
    tree->SetBranchAddress("muPos_dxy", &muPos_dxy);
    tree->SetBranchAddress("muPos_dz", &muPos_dz);
    tree->SetBranchAddress("muPos_nchi2Gl", &muPos_nchi2Gl);
    tree->SetBranchAddress("muPos_found", &muPos_found);
    tree->SetBranchAddress("muPos_pixeLayers", &muPos_pixeLayers);
    tree->SetBranchAddress("muPos_nValidMuHits", &muPos_nValidMuHits);
    tree->SetBranchAddress("muPos_matches", &muPos_matches);
    tree->SetBranchAddress("muPos_tracker", &muPos_tracker);
    tree->SetBranchAddress("muPos_arbitrated", &muPos_arbitrated);
    tree->SetBranchAddress("muPos_Trigger10", &muPos_Trigger10);
    tree->SetBranchAddress("muPos_Trigger2", &muPos_Trigger2);
    tree->SetBranchAddress("muPos_Trigger21", &muPos_Trigger21);
    tree->SetBranchAddress("muPos_Trigger22", &muPos_Trigger22);

    //2). Negative Muon                                                                            
    tree->SetBranchAddress("muNeg_nchi2In", &muNeg_nchi2In);
    tree->SetBranchAddress("muNeg_dxy", &muNeg_dxy);
    tree->SetBranchAddress("muNeg_dz", &muNeg_dz);
    tree->SetBranchAddress("muNeg_nchi2Gl", &muNeg_nchi2Gl);
    tree->SetBranchAddress("muNeg_found", &muNeg_found);
    tree->SetBranchAddress("muNeg_pixeLayers", &muNeg_pixeLayers);
    tree->SetBranchAddress("muNeg_nValidMuHits", &muNeg_nValidMuHits);
    tree->SetBranchAddress("muNeg_matches", &muNeg_matches);
    tree->SetBranchAddress("muNeg_tracker", &muNeg_tracker);
    tree->SetBranchAddress("muNeg_arbitrated", &muNeg_arbitrated);
    tree->SetBranchAddress("muNeg_Trigger10", &muNeg_Trigger10);
    tree->SetBranchAddress("muNeg_Trigger2", &muNeg_Trigger2);
    tree->SetBranchAddress("muNeg_Trigger21", &muNeg_Trigger21);
    tree->SetBranchAddress("muNeg_Trigger22", &muNeg_Trigger22);
    //====================================Gen Variables=========================================================
    //Gen Jpsi Variables
    gentree->SetBranchAddress("GenJpsiMass",   &GenJpsiMass);
    gentree->SetBranchAddress("GenJpsiPt",     &GenJpsiPt);
    gentree->SetBranchAddress("GenJpsiPhi",    &GenJpsiPhi);
    gentree->SetBranchAddress("GenJpsiRap",    &GenJpsiRap);
    gentree->SetBranchAddress("GenJpsiEta",    &GenJpsiEta);
    gentree->SetBranchAddress("GenJpsiPx",     &GenJpsiPx);
    gentree->SetBranchAddress("GenJpsiPy",     &GenJpsiPy);
    gentree->SetBranchAddress("GenJpsiPz",     &GenJpsiPz);
    //muon variable
    gentree->SetBranchAddress("GenmuPosPx",    &GenmuPosPx);
    gentree->SetBranchAddress("GenmuPosPy",    &GenmuPosPy);
    gentree->SetBranchAddress("GenmuPosPz",    &GenmuPosPz);
    gentree->SetBranchAddress("GenmuPosEta",   &GenmuPosEta);
    gentree->SetBranchAddress("GenmuPosPhi",   &GenmuPosPhi);
    gentree->SetBranchAddress("GenmuNegPx",    &GenmuNegPx);
    gentree->SetBranchAddress("GenmuNegPy",    &GenmuNegPy);
    gentree->SetBranchAddress("GenmuNegPz",    &GenmuNegPz);
    gentree->SetBranchAddress("GenmuNegEta",   &GenmuNegEta);
    gentree->SetBranchAddress("GenmuNegPhi",   &GenmuNegPhi);
    //gentree->SetBranchAddress("GenvertexZ",&GenvertexZ);

    //====================================================== Gen tree loop ================================================
    int NAccep=0;
    int nGenEntries=gentree->GetEntries();
    cout<<" Total Entries in GenLevel Tree for pT range: "<<fileName[0]<<"  "<<   nGenEntries<< " ==============="<<endl;

    for(int i=0; i< nGenEntries; i++)  {        
      //cout<<"i : "<<i<<endl;
      if(!(gentree->GetEntry(i))) continue;
      //cout<<" gentree ("<<i<<")"<<endl;
      //Only printing 
      if(i%1000000==0){
        cout<<" processing record "<<i<<"/"<<nGenEntries<<endl;
        //cout<<" Mass "<< GenJpsiMass<< " pT "<< GenJpsiPt << " Y " <<GenJpsiRap<<endl;
      }

      bool GenPosIn=0, GenNegIn=0;
      GenmuPosPt= TMath::Sqrt(GenmuPosPx*GenmuPosPx + GenmuPosPy*GenmuPosPy); 
      GenmuNegPt= TMath::Sqrt(GenmuNegPx*GenmuNegPx + GenmuNegPy*GenmuNegPy); 

      if(IsAccept(GenmuPosPt, GenmuPosEta)) {GenPosIn=1;}
      if(IsAccept(GenmuNegPt, GenmuNegEta)) {GenNegIn=1;}

      int AccJpsi = 0;
      //if(GenJpsiPt < 6.5) continue;
      if((GenJpsiPt >= minPt && GenJpsiPt <= maxPt && fabs(GenJpsiRap) >= minRap && TMath::Abs(GenJpsiRap) <= maxRap && GenPosIn == 1 && GenNegIn == 1)
          && GenJpsiMass > 2.0 && GenJpsiMass < 4.0
          ) {AccJpsi = 1;}

      if((GenPosIn ==1 && GenNegIn==1)) NAccep++;

      //cout<<"1. GenJpsiPt : "<<GenJpsiPt<<", GenJpsiEta : "<<GenJpsiEta<<", GenJpsiRap : "<<GenJpsiRap<<", |GenJpsiPsi| : "<<TMath::Abs(GenJpsiPsi)<<endl;
      //cout<<"1. GenPosIn : "<<GenPosIn<<", GenNegIn : "<<GenNegIn<<endl;
      double vars = 0.0, bin1 = 0.0, bin2 = 0.0;
      if(iSpec == 0) vars = GenJpsiPt;
      if(iSpec == 1) vars = fabs(GenJpsiRap);
      for(int j = 0; j < nBins; j++){
        if(iSpec == 0){
          bin1 = pt_bound[j]; bin2 = pt_bound[j+1];
          if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && fabs(GenJpsiRap) >= minRap && TMath::Abs(GenJpsiRap) <= maxRap && TMath::Abs(GenJpsiRap) >= minRap) {
            hGenDiMuon[j]->Fill(GenJpsiMass);
          }
        }
        if(iSpec == 1){
          bin1 = rap_bound[j]; bin2 = rap_bound[j+1];
          if( (AccJpsi==1) && (vars >= bin1 && vars < bin2) && GenJpsiPt >= minPt && GenJpsiPt <= maxPt) {
            hGenDiMuon[j]->Fill(GenJpsiMass);
          }
        }
      }
    }//gen loop end

    //cout<<" accepted no "<< NAccep<<endl;

    //=============== Rec Tree Loop ==============================================================================

    int nRecEntries=tree->GetEntries();
    cout<<"Total Entries in reconstructed Tree for pT range "<<fileName[0]<<"  "<<nRecEntries<< "====="<<endl;
    for(int i=0; i<nRecEntries; i++)  {     
      tree->GetEntry(i);
      //Only printing 
      if(i%1000000==0){
        cout<<" processing record "<<i<<"/"<<nRecEntries<<endl;
        //cout<<" processing Run  " <<runNb <<" event "<<eventNb<<" lum block "<<lumiBlock<<endl;    
        //cout<<" Mass "<< JpsiMass<< " pT "<< JpsiPt << " Y " <<JpsiRap<<"  "<<JpsiVprob<<" charge "<<JpsiCharge<<" rbin "<<rbin<<endl; 
      }
      bool PosPass=0, NegPass=0, AllCut=0 ,PosIn=0, NegIn=0;
      muPosPt= TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy); 
      muPosP = TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy+ muPosPz*muPosPz); 
      muNegPt= TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy); 
      muNegP = TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy +muNegPz*muNegPz); 

      double tnpWgt1 = 0.0, tnpWgt2 = 0.0;
      double staWgt1 = 0.0, staWgt2 = 0.0;

      double x = 0.0;
      double xta = 0.0;
      // tnp MuonID X Trg
      if(fabs(muPosEta) < 0.9){
        x = muPosPt;
        tnpWgt1 = tnpSF1->Eval(x);
      }else if(fabs(muPosEta) < 1.6){
        x = muPosPt;
        tnpWgt1 = tnpSF2->Eval(x);
      }else if(fabs(muPosEta) < 2.1){
        x = muPosPt;
        tnpWgt1 = tnpSF3->Eval(x);
      }else{
        x = muPosPt;
        tnpWgt1 = tnpSF4->Eval(x);
      }

      if(fabs(muNegEta) < 0.9){
        x = muNegPt;
        tnpWgt2 = tnpSF1->Eval(x);
      }else if(fabs(muNegEta) < 1.6){
        x = muNegPt;
        tnpWgt2 = tnpSF2->Eval(x);
      }else if(fabs(muNegEta) < 2.1){
        x = muNegPt;
        tnpWgt2 = tnpSF3->Eval(x);
      }else{
        x = muNegPt;
        tnpWgt2 = tnpSF4->Eval(x);
      }

      // tnp STA 
      if(fabs(muPosEta) < 1.6){
        xta = muPosPt;
        staWgt1 = fun1->Eval(xta);
      }else{
        xta = muPosPt;
        staWgt1 = fun2->Eval(xta);
      }

      if(fabs(muNegEta) < 1.6){
        xta = muNegPt;
        staWgt2 = fun1->Eval(xta);
      }else{
        xta = muNegPt;
        staWgt2 = fun2->Eval(xta);
      }

      double RecWeight = 1.0;
      RecWeight = tnpWgt1*tnpWgt2*staWgt1*staWgt2;

      if(IsAccept(muPosPt, muPosEta)){PosIn=1;}
      if(IsAccept(muNegPt, muNegEta)){NegIn=1;}

      int AccJpsi = 0;
      //if(JpsiPt < 6.5) continue;
      if((JpsiPt >= minPt && JpsiPt <= maxPt && fabs(JpsiRap) >= minRap && TMath::Abs(JpsiRap) <= maxRap && PosIn == 1 && NegIn == 1)
          && (JpsiMass >= 2.95 && JpsiMass < 3.25)
          ) {AccJpsi = 1;}

      bool mu_Global = ((muPos_nchi2Gl >=0) && (muNeg_nchi2Gl >=0));
      bool mu_Tracker = ((muPos_tracker==1) && (muNeg_tracker==1));

      if(muPos_found > 10 
          && muPos_pixeLayers > 0 
          && muPos_nchi2In < 4.0 
          && TMath::Abs(muPos_dxy) < 3 
          && TMath::Abs(muPos_dz) < 15 
          && muPos_nchi2Gl < 20 
          && muPos_arbitrated==1 
          && muPos_tracker==1){PosPass=1;}     

      if(muNeg_found > 10 
          && muNeg_pixeLayers > 0 
          && muNeg_nchi2In < 4.0 
          && TMath::Abs(muNeg_dxy) < 3 
          && TMath::Abs(muNeg_dz) < 15 
          && muNeg_nchi2Gl < 20 
          && muNeg_arbitrated==1 
          && muNeg_tracker==1){NegPass=1;}


      // Trigger10 : HLT_PAL2DoubleMu3_v1, Trigger 2 : HLT_PAL1DoubleMu0_HighQ_v1, Trigger 1 : HLT_PAL1DoubleMu0_v1
      // muPos_matches : HLT_PAL1DoubleMuOpen_v1
      //if((muPos_Trigger21==1 && muNeg_Trigger21==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}
      if(hbit1 == 1 && (muPos_Trigger22==1 && muNeg_Trigger22==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}
      //if(hbit1 == 1 && (muPos_Trigger2==1 && muNeg_Trigger2==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}
      //if((muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}
      //if((muPos_Trigger10==1 && muNeg_Trigger10==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}

      // without trigger matched
      // if((PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}

      //Eff loop for reco
      double vars = 0.0, bin1 = 0.0, bin2 = 0.0;
      if(iSpec == 0) vars = JpsiPt;
      if(iSpec == 1) vars = fabs(JpsiRap);
      if((JpsiCharge == 0) && (JpsiVprob > 0.01)) {     
        for(int j = 0; j < nBins; j++){
          if(iSpec == 0){
            bin1 = pt_bound[j]; bin2 = pt_bound[j+1];
            if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && TMath::Abs(JpsiRap) <= maxRap && TMath::Abs(JpsiRap) >= minRap) {
              hRecoDiMuon[j]->Fill(JpsiMass,RecWeight);
            }
          }
          if(iSpec == 1){ 
            bin1 = rap_bound[j]; bin2 = rap_bound[j+1];
            if( AccJpsi == 1 && (AllCut == 1) && (vars >= bin1 && vars < bin2) && JpsiPt >= minPt && JpsiPt <= maxPt) {
              hRecoDiMuon[j]->Fill(JpsiMass,RecWeight);
            }
          }
        }
      }
    } //rec tree loop ends

    //======================  File loop Starts ============================

    ///////////////////////////////////////////////////////////////////
    cout<< " adding "<<endl;
    char gtmp[512], gtmp1[512];
    char rtmp[512], rtmp1[512];

    for(int i = 0; i < nBins; i++){
      if(iSpec == 1) sprintf(gtmp,"hGenDiMuon_Pt_%0.1f_%0.1f",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(gtmp,"hGenDiMuon_Rap_%0.1f_%0.1f",rap_bound[i],rap_bound[i+1]);
      if(iSpec == 1) sprintf(gtmp1,"plots/Gen/hGenDiMuon_Pt_%0.1f_%0.1f.png",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(gtmp1,"plots/Gen/hGenDiMuon_Rap_%0.1f_%0.1f.png",rap_bound[i],rap_bound[i+1]);
      if(iSpec == 1) sprintf(rtmp,"hRecDiMuon_Pt_%0.1f_%0.1f",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(rtmp,"hRecDiMuon_Rap_%0.1f_%0.1f",rap_bound[i],rap_bound[i+1]);
      if(iSpec == 1) sprintf(rtmp1,"plots/Rec/hRecDiMuon_Pt_%0.1f_%0.1f.png",pt_bound[i],pt_bound[i+1]);
      if(iSpec == 2) sprintf(rtmp1,"plots/Rec/hRecDiMuon_Rap_%0.1f_%0.1f.png",rap_bound[i],rap_bound[i+1]);
      hGenDiMuon[i]->SetName(gtmp);
      hRecoDiMuon[i]->SetName(rtmp);
    }

    cout<<"Starts to calculate efficiency"<<endl;
    //dataFile<<""<<endl;
    //=====================Loop for eff========================================================================================//
    //define stuff here for error on weighted samples
    for(int i = 0; i < nBins; i++){
      int Gbinlow =hGenDiMuon[i]->GetXaxis()->FindBin(2.0);//2.95
      int Gbinhi=hGenDiMuon[i]->GetXaxis()->FindBin(4.0);//2.95

      int Rbinlow =hRecoDiMuon[i]->GetXaxis()->FindBin(2.95);//2.95
      int Rbinhi=hRecoDiMuon[i]->GetXaxis()->FindBin(3.25);//2.95

      genNo[i] = hGenDiMuon[i]->IntegralAndError(Gbinlow, Gbinhi, genErr[i]); 
      recoNo[i] = hRecoDiMuon[i]->IntegralAndError(Rbinlow, Rbinhi, recoErr[i]);
      //calculate Eff         
      if(genNo[i] == 0 || recoNo[i] == 0) {
        eff[i] = 0;
        effErr[i] = 0;
      }else{
        eff[i] = recoNo[i]/genNo[i]; 

        effErr[i] = recoNo[i]/genNo[i]*TMath::Sqrt(genErr[i]*genErr[i]/(genNo[i]*genNo[i]) + recoErr[i]*recoErr[i]/(recoNo[i]*recoNo[i]));

        //error without weight
        //dataFile<<" Bin ["<<i<<"] - "<< " Reco Jpsi : "<< recoNo[i]  <<", Gen Jpsi : "<< genNo[i] <<endl;
        //dataFile<<" Eff ["<<i<<"] - "<< eff[i] <<" Error "<< effErr[i] <<endl;
        cout<<" Bin ["<<i<<"] - "<< " Reco Jpsi : "<< recoNo[i]  <<", Gen Jpsi : "<< genNo[i] <<endl;
        cout<<" Eff ["<<i<<"] - "<< eff[i] <<" Error "<< effErr[i] <<endl;
      }
    }
    TH1F *hEff = new TH1F();
    int nEffBins = 0;
    if(iSpec == 0) {hEff = new TH1F("hEff","hEff;p_{T} GeV/c;Efficiency",nPtBins,pt_bound); nEffBins = nPtBins;}
    if(iSpec == 1) {hEff = new TH1F("hEff","hEff;y;Efficiency",nRapBins,rap_bound); nEffBins = nRapBins;}

    //dataFile<<"Efficiency"<<endl;
    for(int i = 0; i < nEffBins; i++){
      hEff->SetBinContent(i+1,eff[i]);
      hEff->SetBinError(i+1,effErr[i]);
      cout<<"Trying to measure eff vs "<<cSp[iSpec]<<endl;
      cout<<"Eff : "<<eff[i]<<", err : "<<effErr[i]<<endl;
      dataFile<<eff[i]<<endl;
    }

    //dataFile<<""<<endl;
    //dataFile<<"Errors"<<endl;
    for(int i = 0; i < nEffBins; i++){
      hEff->SetBinContent(i+1,eff[i]);
      hEff->SetBinError(i+1,effErr[i]);
      cout<<"Trying to measure eff vs "<<cSp[iSpec]<<endl;
      cout<<"Eff : "<<eff[i]<<", err : "<<effErr[i]<<endl;
      //dataFile<<effErr[i]<<endl;
    }


    outfile->cd();
    hEff->Draw();
    char tmp_histo[512];
    sprintf(tmp_histo,"hEff_%s",cSp[iSpec]);
    hEff->SetName(tmp_histo);
    hEff->Write();
    //dataFile<<""<<endl;
    //dataFile.close();
  }
  outfile->Write();
}
示例#6
0
void EffRapPbPb1S(){      // Change function name
        gROOT->Macro("logon.C+");

	TChain myTree("hionia/myTree");   // Change source of tree
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_00_03_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");   //  Change tree being added (Different for pp1S, pp2S, PbPb1S, PbPb2S)
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_03_06_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_06_09_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_09_12_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_12_15_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");
        myTree.Add("/scratch_menkar/CMS_Trees/OniaTrees_2015_5TeV/PbPb_MC_Official/OniaTree_Pythia8_Ups1SMM_ptUps_15_30_Hydjet_MB_HINPbPbWinter16DR-75X_mcRun2_HeavyIon_v13-v1.root");

	Float_t         muMiDxy;
	Float_t         muMiDz;
	Int_t           muMiNPxlLayers;
	Int_t           muMiNTrkLayers;
	Bool_t          muMiGoodMu;
	Float_t         muPlDxy;
	Float_t         muPlDz;
	Int_t           muPlNPxlLayers;
	Int_t           muPlNTrkLayers;
	Bool_t          muPlGoodMu;
	Float_t         vProb;

	double          ptWeight; 
        double          ptWeightArr[6]={3.10497,4.11498,2.2579,1.2591,0.567094,0.783399};
	double          centWeight;
        double          RapBin[nRapBin] = {0.6,1.8};   //  RapBin
        double          RapBinErr[nRapBin] = {0.6,0.6};  // RapBinErr
        Float_t         RapBinEdges[nRapBin+1] = {0,1.2,2.4};
        double          ptReweight;

	Int_t           Centrality; 
	ULong64_t       HLTriggers;
	Int_t           Reco_QQ_size;
	Int_t           Reco_QQ_sign[36];   //[Reco_QQ_size]
	TClonesArray    *Reco_QQ_4mom;
	TClonesArray    *Reco_QQ_mupl_4mom;
	TClonesArray    *Reco_QQ_mumi_4mom;
	ULong64_t       Reco_QQ_trig[36];   //[Reco_QQ_size]
	Float_t         Reco_QQ_VtxProb[36];   //[Reco_QQ_size]
	Bool_t          Reco_QQ_mupl_isGoodMuon[36];   //[Reco_QQ_size]
	Bool_t          Reco_QQ_mumi_isGoodMuon[36];   //[Reco_QQ_size]
	Int_t           Reco_QQ_mupl_nPixWMea[36];   //[Reco_QQ_size]
	Int_t           Reco_QQ_mumi_nPixWMea[36];   //[Reco_QQ_size]
	Int_t           Reco_QQ_mupl_nTrkWMea[36];   //[Reco_QQ_size]
	Int_t           Reco_QQ_mumi_nTrkWMea[36];   //[Reco_QQ_size]
	Float_t         Reco_QQ_mupl_dxy[36];   //[Reco_QQ_size]
	Float_t         Reco_QQ_mumi_dxy[36];   //[Reco_QQ_size]
	Float_t         Reco_QQ_mupl_dz[36];   //[Reco_QQ_size]
	Float_t         Reco_QQ_mumi_dz[36];   //[Reco_QQ_size]



	Int_t           Gen_QQ_size;
	Int_t           Gen_QQ_sign[36];   //[Gen_QQ_size]
	TClonesArray    *Gen_QQ_4mom;
	TClonesArray    *Gen_QQ_mupl_4mom;
	TClonesArray    *Gen_QQ_mumi_4mom;
	Float_t         Gen_QQ_VtxProb[36];   //[Gen_QQ_size]
	Bool_t          Gen_QQ_mupl_isGoodMuon[36];   //[Gen_QQ_size]
	Bool_t          Gen_QQ_mumi_isGoodMuon[36];   //[Gen_QQ_size]
	Int_t           Gen_QQ_mupl_nPixWMea[36];   //[Gen_QQ_size]
	Int_t           Gen_QQ_mumi_nPixWMea[36];   //[Gen_QQ_size]
	Int_t           Gen_QQ_mupl_nTrkWMea[36];   //[Gen_QQ_size]
	Int_t           Gen_QQ_mumi_nTrkWMea[36];   //[Gen_QQ_size]
	Float_t         Gen_QQ_mupl_dxy[36];   //[Gen_QQ_size]
	Float_t         Gen_QQ_mumi_dxy[36];   //[Gen_QQ_size]
	Float_t         Gen_QQ_mupl_dz[36];   //[Gen_QQ_size]
	Float_t         Gen_QQ_mumi_dz[36];   //[Gen_QQ_size]





	TBranch        *b_Centrality;   //!
	TBranch        *b_HLTriggers;   //!
	TBranch        *b_Reco_QQ_size;   //!
	TBranch        *b_Reco_QQ_sign;   //!
	TBranch        *b_Reco_QQ_4mom;   //!
	TBranch        *b_Reco_QQ_mupl_4mom;   //!
	TBranch        *b_Reco_QQ_mumi_4mom;   //!
	TBranch        *b_Reco_QQ_trig;   //!
	TBranch        *b_Reco_QQ_VtxProb;   //!
	TBranch        *b_Reco_QQ_mupl_isGoodMuon;   //!
	TBranch        *b_Reco_QQ_mumi_isGoodMuon;   //!
	TBranch        *b_Reco_QQ_mupl_nPixWMea;   //!
	TBranch        *b_Reco_QQ_mumi_nPixWMea;   //!
	TBranch        *b_Reco_QQ_mupl_nTrkWMea;   //!
	TBranch        *b_Reco_QQ_mumi_nTrkWMea;   //!
	TBranch        *b_Reco_QQ_mupl_dxy;   //!
	TBranch        *b_Reco_QQ_mumi_dxy;   //!
	TBranch        *b_Reco_QQ_mupl_dz;   //!
	TBranch        *b_Reco_QQ_mumi_dz;   //!


	TBranch        *b_Gen_QQ_size;   //
	TBranch        *b_Gen_QQ_4mom;   //!
	TBranch        *b_Gen_QQ_mupl_4mom;   //!
	TBranch        *b_Gen_QQ_mumi_4mom;   //!


	//Set object pointer, Initialize
	Reco_QQ_4mom = 0;
	Reco_QQ_mupl_4mom = 0;
	Reco_QQ_mumi_4mom = 0;	
	
	Gen_QQ_4mom = 0;
	Gen_QQ_mupl_4mom = 0;
	Gen_QQ_mumi_4mom = 0;	


	myTree.SetBranchAddress("Centrality", &Centrality, &b_Centrality);
	myTree.SetBranchAddress("HLTriggers", &HLTriggers, &b_HLTriggers);
	myTree.SetBranchAddress("Reco_QQ_size", &Reco_QQ_size, &b_Reco_QQ_size);
	myTree.SetBranchAddress("Reco_QQ_sign", Reco_QQ_sign, &b_Reco_QQ_sign);
	myTree.SetBranchAddress("Reco_QQ_4mom", &Reco_QQ_4mom, &b_Reco_QQ_4mom);
	myTree.SetBranchAddress("Reco_QQ_mupl_4mom", &Reco_QQ_mupl_4mom, &b_Reco_QQ_mupl_4mom);
	myTree.SetBranchAddress("Reco_QQ_mumi_4mom", &Reco_QQ_mumi_4mom, &b_Reco_QQ_mumi_4mom);
	myTree.SetBranchAddress("Reco_QQ_trig", Reco_QQ_trig, &b_Reco_QQ_trig);
	myTree.SetBranchAddress("Reco_QQ_VtxProb", Reco_QQ_VtxProb, &b_Reco_QQ_VtxProb);
	myTree.SetBranchAddress("Reco_QQ_mupl_isGoodMuon", Reco_QQ_mupl_isGoodMuon, &b_Reco_QQ_mupl_isGoodMuon);
	myTree.SetBranchAddress("Reco_QQ_mumi_isGoodMuon", Reco_QQ_mumi_isGoodMuon, &b_Reco_QQ_mumi_isGoodMuon);
	myTree.SetBranchAddress("Reco_QQ_mupl_nPixWMea", Reco_QQ_mupl_nPixWMea, &b_Reco_QQ_mupl_nPixWMea);
	myTree.SetBranchAddress("Reco_QQ_mumi_nPixWMea", Reco_QQ_mumi_nPixWMea, &b_Reco_QQ_mumi_nPixWMea);
	myTree.SetBranchAddress("Reco_QQ_mupl_nTrkWMea", Reco_QQ_mupl_nTrkWMea, &b_Reco_QQ_mupl_nTrkWMea);
	myTree.SetBranchAddress("Reco_QQ_mumi_nTrkWMea", Reco_QQ_mumi_nTrkWMea, &b_Reco_QQ_mumi_nTrkWMea);
	myTree.SetBranchAddress("Reco_QQ_mupl_dxy", Reco_QQ_mupl_dxy, &b_Reco_QQ_mupl_dxy);
	myTree.SetBranchAddress("Reco_QQ_mumi_dxy", Reco_QQ_mumi_dxy, &b_Reco_QQ_mumi_dxy);
	myTree.SetBranchAddress("Reco_QQ_mupl_dz", Reco_QQ_mupl_dz, &b_Reco_QQ_mupl_dz);
	myTree.SetBranchAddress("Reco_QQ_mumi_dz", Reco_QQ_mumi_dz, &b_Reco_QQ_mumi_dz);



	myTree.SetBranchAddress("Gen_QQ_size", &Gen_QQ_size, &b_Gen_QQ_size);
	myTree.SetBranchAddress("Gen_QQ_4mom", &Gen_QQ_4mom, &b_Gen_QQ_4mom);
	myTree.SetBranchAddress("Gen_QQ_mupl_4mom", &Gen_QQ_mupl_4mom, &b_Gen_QQ_mupl_4mom);
	myTree.SetBranchAddress("Gen_QQ_mumi_4mom", &Gen_QQ_mumi_4mom, &b_Gen_QQ_mumi_4mom);


	TH1D  *RecoEvents=new TH1D("RecoEvents","Reconstructed", nRapBin, RapBinEdges);

        TH1D  *GenEvents=new TH1D("GenEvents","Generated", nRapBin, RapBinEdges);

        RecoEvents->Sumw2();
        GenEvents->Sumw2();

//        TH1D* hEff = new TH1D("Eff", "", nRapBin, RapBinEdges);

        TFile* ReweightFunctions = new TFile("dNdpT_root5.root", "Open");
        ReweightFunctions->GetObject("AA1S", AA1S);
        ReweightFunctions->GetObject("AA1Smc", AA1Smc);


	Long64_t nentries = myTree.GetEntries();
	cout << nentries<<endl;

	for (Long64_t jentry=0; jentry<nentries; jentry++){
		myTree.GetEntry(jentry);

		//looping over all the dimuons 
		for (int iQQ=0; iQQ<Reco_QQ_size;iQQ++){
			TLorentzVector *qq4mom = (TLorentzVector*) Reco_QQ_4mom->At(iQQ);
			TLorentzVector *mumi4mom = (TLorentzVector*) Reco_QQ_mumi_4mom->At(iQQ);
			TLorentzVector *mupl4mom = (TLorentzVector*) Reco_QQ_mupl_4mom->At(iQQ);

			//--Muid cuts for muon minus
			muMiDxy=Reco_QQ_mumi_dxy[iQQ];
			muMiDz=Reco_QQ_mumi_dz[iQQ];
			muMiNPxlLayers=Reco_QQ_mumi_nPixWMea[iQQ];
			muMiNTrkLayers=Reco_QQ_mumi_nTrkWMea[iQQ];
			muMiGoodMu = Reco_QQ_mumi_isGoodMuon[iQQ];

			//--Muid cuts for muon plus
			muPlDxy=Reco_QQ_mupl_dxy[iQQ];
			muPlDz=Reco_QQ_mupl_dz[iQQ];
			muPlNPxlLayers=Reco_QQ_mupl_nPixWMea[iQQ];
			muPlNTrkLayers=Reco_QQ_mupl_nTrkWMea[iQQ];
			muPlGoodMu = Reco_QQ_mupl_isGoodMuon[iQQ];
			vProb = Reco_QQ_VtxProb[iQQ];

			bool mupl_cut = 0;
			bool mumi_cut = 0;
			bool acceptMu = 0;
			bool trigL1Dmu = 0;
			bool PtCutPass = 0;
			bool MassCutPass = 0;

			//--Muon id cuts
			if( (muPlGoodMu==1) && muPlNTrkLayers> 5 &&  muPlNPxlLayers > 0 && TMath::Abs(muPlDxy) < 0.3 && TMath::Abs(muPlDz) < 20 && vProb > 0.01){mupl_cut = 1;}    
			if( (muMiGoodMu==1) && muMiNTrkLayers> 5 &&  muMiNPxlLayers > 0 && TMath::Abs(muMiDxy) < 0.3 && TMath::Abs(muMiDz) < 20 ){mumi_cut = 1;}

			//check if muons are in acceptance
			if(IsAccept(mupl4mom) && IsAccept(mumi4mom)){acceptMu = 1;}
			if (PtCut(mupl4mom) && PtCut(mumi4mom)){ PtCutPass = 1; }
			MassCutPass = MassCut(qq4mom, m1S_low, m1S_high);			

			//check if trigger bit is matched to dimuon
			if((HLTriggers&1)==1 && (Reco_QQ_trig[iQQ]&1)==1){trigL1Dmu = 1;}

			//weights only needed for PbPb
			 double weight = 0;
			 ptWeight=0;
			 centWeight = FindCenWeight(Centrality);
                         if(qq4mom->Pt()<=3){ptWeight = ptWeightArr[0];}
                         if(qq4mom->Pt()>3 && qq4mom->Pt()<=6){ptWeight = ptWeightArr[1];}
                         if(qq4mom->Pt()>6 && qq4mom->Pt()<=9){ptWeight = ptWeightArr[2];}
                         if(qq4mom->Pt()>9 && qq4mom->Pt()<=12){ptWeight = ptWeightArr[3];}
                         if(qq4mom->Pt()>12 && qq4mom->Pt()<=15){ptWeight = ptWeightArr[4];}
                         if(qq4mom->Pt()>15 && qq4mom->Pt()<=30){ptWeight = ptWeightArr[5];}
//                         if(qq4mom->Pt()<=30){ptReweight = PtReweight(qq4mom, PbPb1S_coefficient, PbPb1S_constant);}
                         if(qq4mom->Pt()<=30){ptReweight = (AA1S->Eval(qq4mom->Pt()))/(AA1Smc->Eval(qq4mom->Pt()));}
			 weight = centWeight*ptWeight*ptReweight;

			bool L1Pass=0;

			if (Reco_QQ_sign[iQQ]==0 && acceptMu && mupl_cut && mumi_cut && trigL1Dmu){L1Pass=1;}

                        if(qq4mom->Pt()<30 && Centrality < 160){
			for(int i = 0; i<nRapBin;i++){
				if(TMath::Abs(qq4mom->Rapidity())>(RapBin[i]-RapBinErr[i]) && TMath::Abs(qq4mom->Rapidity())<(RapBin[i]+RapBinErr[i])){
					if(L1Pass == 1 && PtCutPass ==1 && MassCutPass == 1){RecoEvents->Fill(TMath::Abs(qq4mom->Rapidity()),weight);}
				}

			}
// */
//			if(L1Pass == 1 && PtCutPass ==1 && MassCutPass == 1){RecoEvents->Fill(TMath::Abs(qq4mom->Rapidity()),weight);}
			}
	}


		//Dinonminator loop
		for (int iQQ=0; iQQ<Gen_QQ_size;iQQ++){
			TLorentzVector *qq4mom = (TLorentzVector*) Gen_QQ_4mom->At(iQQ);
			TLorentzVector *mumi4mom = (TLorentzVector*) Gen_QQ_mumi_4mom->At(iQQ);
			TLorentzVector *mupl4mom = (TLorentzVector*) Gen_QQ_mupl_4mom->At(iQQ);

			//--Muid cuts for muon minus
			 muMiDxy=Gen_QQ_mumi_dxy[iQQ];
			 muMiDz=Gen_QQ_mumi_dz[iQQ];
			 muMiNPxlLayers=Gen_QQ_mumi_nPixWMea[iQQ];
			 muMiNTrkLayers=Gen_QQ_mumi_nTrkWMea[iQQ];
			 muMiGoodMu = Gen_QQ_mumi_isGoodMuon[iQQ];

			//--Muid cuts for muon plus
			 muPlDxy=Gen_QQ_mupl_dxy[iQQ];
			 muPlDz=Gen_QQ_mupl_dz[iQQ];
			 muPlNPxlLayers=Gen_QQ_mupl_nPixWMea[iQQ];
			 muPlNTrkLayers=Gen_QQ_mupl_nTrkWMea[iQQ];
			 muPlGoodMu = Gen_QQ_mupl_isGoodMuon[iQQ];
			 vProb = Gen_QQ_VtxProb[iQQ];

			bool mupl_cut = 0;
			bool mumi_cut = 0;
			bool acceptMu = 0;
			bool trigL1Dmu = 0;
//			bool trigL3Dmu = 0;
			bool PtCutPass = 0;
			bool MassCutPass = 0;


			//--Muon id cuts
			if( (muPlGoodMu==1) && muPlNTrkLayers> 5 &&  muPlNPxlLayers > 0 && TMath::Abs(muPlDxy) < 0.3 && TMath::Abs(muPlDz) < 20 && vProb > 0.01){mupl_cut = 1;}
			if( (muMiGoodMu==1) && muMiNTrkLayers> 5 &&  muMiNPxlLayers > 0 && TMath::Abs(muMiDxy) < 0.3 && TMath::Abs(muMiDz) < 20 ){mumi_cut = 1;}

			//check if muons are in acceptance
			if(IsAccept(mupl4mom) && IsAccept(mumi4mom)){acceptMu = 1;}
			if (PtCut(mupl4mom) && PtCut(mumi4mom)){ PtCutPass = 1; }
			MassCutPass = MassCut(qq4mom, m1S_low, m1S_high);


			//check if trigger bit is matched to dimuon
//			if((HLTriggers&1)==1){trigL1Dmu = 1;}
//			if((HLTriggers&262144)==262144 && (Gen_QQ_trig[iQQ]&262144)==262144){trigL3Dmu = 1;}

			//weights only needed for PbPb
			 double weight = 0;
			 ptWeight=0;
                         centWeight = FindCenWeight(Centrality);
                         if(qq4mom->Pt()<=3){ptWeight = ptWeightArr[0];}
                         if(qq4mom->Pt()>3 && qq4mom->Pt()<=6){ptWeight = ptWeightArr[1];}
                         if(qq4mom->Pt()>6 && qq4mom->Pt()<=9){ptWeight = ptWeightArr[2];}
                         if(qq4mom->Pt()>9 && qq4mom->Pt()<=12){ptWeight = ptWeightArr[3];}
                         if(qq4mom->Pt()>12 && qq4mom->Pt()<=15){ptWeight = ptWeightArr[4];}
                         if(qq4mom->Pt()>15 && qq4mom->Pt()<=30){ptWeight = ptWeightArr[5];}			 
//			 if(qq4mom->Pt()<=30){ptReweight = PtReweight(qq4mom, PbPb1S_coefficient, PbPb1S_constant);}
                         if(qq4mom->Pt()<=30){ptReweight = (AA1S->Eval(qq4mom->Pt()))/(AA1Smc->Eval(qq4mom->Pt()));}
                         weight = centWeight*ptWeight*ptReweight;

//			bool L1Pass=0;
//			bool L3Pass=0;
//			if (acceptMu){L1Pass=1;}
//			if (acceptMu){L3Pass=1;}

                        if(qq4mom->Pt()<30 && Centrality < 160){
			for(int i = 0; i<nRapBin;i++){
				if(TMath::Abs(qq4mom->Rapidity())>(RapBin[i]-RapBinErr[i]) && TMath::Abs(qq4mom->Rapidity())<(RapBin[i]+RapBinErr[i])){
					if(acceptMu == 1 && PtCutPass == 1 && MassCutPass == 1){GenEvents->Fill(TMath::Abs(qq4mom->Rapidity()), weight);}
				}
			}
// */
//			if(acceptMu == 1 && PtCutPass == 1 && MassCutPass == 1){GenEvents->Fill(TMath::Abs(qq4mom->Rapidity()), weight);}
			}

		}


	}





TCanvas *c1 = new TCanvas("c1","c1",1000,680);


//From Ota

//     	 RecoEvents->Sumw2();
//         GenEvents->Sumw2();
//         hEff->Divide(RecoEvents, GenEvents);

//	TCanvas *c1 = new TCanvas("c1","c1",600,400);
	// Will use TGraphAsymmErrors
//	TGraphAsymmErrors *TrigEff = new TGraphAsymmErrors(hEff);
//	TGraphAsymmErrors *TrigEff = new TGraphAsymmErrors(nPtBin);
//	TrigEff->Divide(RecoEvents, GenEvents, "cl=0.683 b(1,1) mode");

//	TGraphErrors *TrigEff = new TGraphErrors(nPtBin,ptBin , Efficiency,ptBinErr , EfficiencyErr);

        TGraphAsymmErrors *TrigEff = new TGraphAsymmErrors(nRapBin);
        TrigEff->BayesDivide(RecoEvents, GenEvents);
        TrigEff->SetName("Eff");

	TrigEff->SetMarkerSize(2.0);
        TrigEff->SetMarkerColor(kBlue);
        TrigEff->SetMarkerStyle(21);
        TrigEff->SetLineColor(kBlue);

	TrigEff->SetTitle("");
//	TrigEff->SetMarkerStyle(21);
//	TrigEff->SetMarkerColor(2);
	TrigEff->GetYaxis()->SetTitle("Efficiency[#varUpsilon(1S)]_{PbPb}");
	TrigEff->GetXaxis()->SetTitle("|y|");
	TrigEff->GetYaxis()->SetRangeUser(0,1);
        TrigEff->GetXaxis()->SetRangeUser(0.0, 2.4);
        TrigEff->GetXaxis()->CenterTitle();
        TrigEff->GetYaxis()->CenterTitle();

	TrigEff->Draw("AP");	// */
	c1->Update();
	c1->SaveAs("EfficiencyVsRapidityUpsilonPbPb1S.png");

 TFile* MyFileEff;
          MyFileEff = new TFile("RapPbPbEff1S.root", "Recreate");
  TrigEff->Write();

  MyFileEff->Close();

        for (Int_t i = 0; i < (nRapBin); i++){
        cout << TrigEff->Eval(RapBin[i]) << " , - " << TrigEff->GetErrorYlow(i) << " , + " << TrigEff->GetErrorYhigh(i) << endl;
        }



        ReweightFunctions->Close();


}
void UpsilonMassFit_PolWeights(int iSpec = 3, int PutWeight=1)
{

  double PtCut=4;

  //minbias integrated, |y|<1.2 and |y|\in[1.2,2.4], centrality [0,10][10,20][20,100]%,  pt [0,6.5], [6.5, 10] [10,20]

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(0);
  gStyle->SetCanvasColor(0);
  gStyle->SetTitleFillColor(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadBorderSize(0);
  gStyle->SetCanvasBorderSize(0);
  gStyle->SetOptTitle(1); // at least most of the time
  gStyle->SetOptStat(1); // most of the time, sometimes "nemriou" might be useful to display name, 
  //number of entries, mean, rms, integral, overflow and underflow
  gStyle->SetOptFit(1); // set to 1 only if you want to display fit results

  //==================================== Define Histograms====================================================
  ofstream dataFile(Form("Eff_Upsilon.txt"));
  
  TH1D *diMuonsInvMass_Gen = new TH1D("diMuonsInvMass_Gen","diMuonsInvMass_Gen", 100,8.0,12.0);
  TH1D *diMuonsPt_Gen = new TH1D("diMuonsPt_Gen","diMuonsPt_Gen", 100,0,30);
  //Rapidity Gen
  TH1D *diMuonsRap_Gen0 = new TH1D("diMuonsRap_Gen0","diMuonsRap_Gen0", 100,-5,5);
  TH1D *diMuonsRap_Gen1 = new TH1D("diMuonsRap_Gen1","diMuonsRap_Gen1", 100,-5,5);
  TH1D *diMuonsRap_Gen2 = new TH1D("diMuonsRap_Gen2","diMuonsRap_Gen2", 100,-5,5);
  TH1D *diMuonsRap_Gen3 = new TH1D("diMuonsRap_Gen3","diMuonsRap_Gen3", 100,-5,5);
  TH1D *diMuonsRap_Gen4 = new TH1D("diMuonsRap_Gen4","diMuonsRap_Gen4", 100,-5,5);
  TH1D *diMuonsRap_Gen5 = new TH1D("diMuonsRap_Gen5","diMuonsRap_Gen5", 100,-5,5);
  ////Rapidity Reco
  TH1D *diMuonsRap_Rec0 = new TH1D("diMuonsRap_Rec0","diMuonsRap_Rec0", 100,-5,5);
  diMuonsRap_Rec0->SetLineColor(2);
  TH1D *diMuonsRap_Rec1 = new TH1D("diMuonsRap_Rec1","diMuonsRap_Rec1", 100,-5,5);
  diMuonsRap_Rec1->SetLineColor(2);
  TH1D *diMuonsRap_Rec2 = new TH1D("diMuonsRap_Rec2","diMuonsRap_Rec2", 100,-5,5);
  diMuonsRap_Rec2->SetLineColor(2);
  TH1D *diMuonsRap_Rec3 = new TH1D("diMuonsRap_Rec3","diMuonsRap_Rec3", 100,-5,5);
  diMuonsRap_Rec3->SetLineColor(2);
  TH1D *diMuonsRap_Rec4 = new TH1D("diMuonsRap_Rec4","diMuonsRap_Rec4", 100,-5,5);
  diMuonsRap_Rec4->SetLineColor(2);
  TH1D *diMuonsRap_Rec5 = new TH1D("diMuonsRap_Rec5","diMuonsRap_Rec5", 100,-5,5);
  diMuonsRap_Rec5->SetLineColor(2);
  TH1D *Bin_Gen = new TH1D("Bin_Gen","Bin_Gen", 40,0,40);

  //==============================================Define AccEff Stuff here===========================================
  // Pt bin sizes
  int Nptbin=1;
  double pt_bound[100] = {0};
  if(iSpec == 1) { 
    Nptbin = 5;
    pt_bound[0] = 0;
    pt_bound[1] = 20.0;
    pt_bound[2] = 0.0;
    pt_bound[3] = 6.5;
    pt_bound[4] = 10.0;
    pt_bound[5] = 20.0;
   

    pt_bound[6] = 30.0;
    pt_bound[7] = 35;
    pt_bound[8] = 40;
    pt_bound[9] = 45;
    pt_bound[10] = 50;
  }
  
  if(iSpec == 2) { 
    Nptbin = 2;
    pt_bound[0] = 0.0; 
    pt_bound[1] = 1.2; 
    pt_bound[2] = 2.4; 
    
    pt_bound[3] = 0.0; 
    pt_bound[4] = 0.8; 
    pt_bound[5] = 1.8; 
    //pt_bound[7] = 2.0; 
    pt_bound[6] = 2.4; 
  
  }
  
  
  if(iSpec == 3) {
    Nptbin = 3;
    //for plots
    pt_bound[0] = 0.0;//0
    pt_bound[1] = 4.0;//10
    pt_bound[2] = 8.0;//20
    pt_bound[3] = 40.0;//100
   

    //pt_bound[4] = 16.0;//50
    //pt_bound[5] = 20.0;//100
    //pt_bound[6] = 24.0;
    //pt_bound[7] = 32.0;
    //pt_bound[8] = 40.0;
    //pt_bound[9] = 40.0;
  }
  //X Axis error on Eff graph 
  double PT[100], DelPT[100], mom_err[100];
  for (Int_t ih = 0; ih < Nptbin; ih++) {
    PT[ih] = (pt_bound[ih] + pt_bound[ih+1])/2.0;
    DelPT[ih] = pt_bound[ih+1] - pt_bound[ih];
    mom_err[ih] = DelPT[ih]/2.0;
  }
  
  double genError, recError;
  double gen_pt[100]={0}, gen_ptError[100]={0}; 
  double rec_pt[100]={0}, rec_ptError[100]={0}; 
  double Eff_cat_1[100]={0},Err_Eff_cat_1[100]={0};  
  
  // Histogram arrays
  TH1D *diMuonsInvMass_GenA[10][1000];
  TH1D *diMuonsInvMass_RecA[10][1000];
  TH1D *diMuonsPt_GenA[10][1000];
  TH1D *diMuonsPt_RecA[10][1000];
  char nameGen[10][500], nameRec[10][500], nameGenPt[10][500], nameRecPt[10][500];
 
  
  for (int ifile = 0; ifile <= 5; ifile++) {
    for (Int_t ih = 0; ih < Nptbin; ih++) {
      sprintf(nameGen[ifile],"DiMuonMassGen_pt_%d_%d",ih,ifile);
      sprintf(nameRec[ifile],"DiMuonMassRec_pt_%d_%d",ih,ifile);

      sprintf(nameGenPt[ifile],"DiMuonPtGen_pt_%d_%d",ih,ifile);
      sprintf(nameRecPt[ifile],"DiMuonPtRec_pt_%d_%d",ih,ifile);
      
      diMuonsInvMass_GenA[ifile][ih]= new TH1D(nameGen[ifile],nameGen[ifile],  100,8.0,12.0); //for eff Gen;
      diMuonsInvMass_GenA[ifile][ih]->Sumw2();
      diMuonsInvMass_GenA[ifile][ih]->SetMarkerStyle(7);
      diMuonsInvMass_GenA[ifile][ih]->SetMarkerColor(4);
      diMuonsInvMass_GenA[ifile][ih]->SetLineColor(4);

      diMuonsInvMass_RecA[ifile][ih] = new TH1D(nameRec[ifile],nameRec[ifile], 100,8.0,12.0); //for eff Rec;
      diMuonsInvMass_RecA[ifile][ih]->Sumw2();
      diMuonsInvMass_RecA[ifile][ih]->SetMarkerStyle(8);
      diMuonsInvMass_RecA[ifile][ih]->SetMarkerColor(4);
      diMuonsInvMass_RecA[ifile][ih]->SetLineColor(4);


      diMuonsPt_GenA[ifile][ih]= new TH1D(nameGenPt[ifile],nameGenPt[ifile],  100,0,40); //for eff Gen;
      diMuonsPt_RecA[ifile][ih]= new TH1D(nameRecPt[ifile],nameRecPt[ifile],  100,0,40); //for eff Rec;
    }
  }
  
  //===========================================Input Root File============================================================
  char fileName[10][500];
  //0.0380228 	0.0480769 	0.0293255 	0.0125156 	0.00336587 	0.00276319*2/5 = 0.001105276
  //Scales
  double scale[10]={0};  
  scale[0]=(6.8802); // pT [0-3]
  scale[1]=(8.6995); // pT [3-6]
  scale[2]=(5.3065); // pT [6-9]
  scale[3]=(2.2647); // pT [9-12] 
  scale[4]=(3.0453); // pT [12-15] 
  scale[5]=(1.0000); // pT [15-30]
  
  sprintf(fileName[0],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt03_N.root");
  sprintf(fileName[1],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt36_N.root");
  sprintf(fileName[2],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt69_N.root");
  sprintf(fileName[3],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt912_N.root");
  sprintf(fileName[4],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1215_N.root");
  sprintf(fileName[5],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1530_N.root");



  //double scale[10]={0};  
  //scale[0]=(0.0380228/0.001105276);
  //scale[1]=(0.0480769/0.001105276);
  //scale[2]=(0.0293255/0.001105276);
  //scale[3]=(0.0125156/0.001105276);
  //scale[4]=(0.00336587/0.001105276);
  //scale[5]=(0.001105276/0.001105276);
   //34.55 , 43.70 , 26.65 , 11.37 , 3.05 , 1
  //sprintf(fileName[0],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt03.root");
  //sprintf(fileName[1],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt36.root");
  //sprintf(fileName[2],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt69.root");
  //sprintf(fileName[3],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt912.root");
  //sprintf(fileName[4],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1215.root");
  //sprintf(fileName[5],"/home/vineet/HiData/UpsilonData/UpsilonEff/DimuonOnia2Dplots_UpsilonPt1530.root");
  
  TFile *infile;
  TTree *tree;
  TTree *gentree;
  
  //===========File loop ======================
  
  for(int ifile =0; ifile<=5; ifile++){
    
    infile=new TFile(fileName[ifile],"R");
    tree=(TTree*)infile->Get("SingleMuonTree");
    gentree=(TTree*)infile->Get("SingleGenMuonTree");
    
    //Event variables
    int eventNb,runNb,lumiBlock, gbin, rbin;
    //Jpsi Variables
    Double_t JpsiMass,JpsiPt,JpsiPx,JpsiPy,JpsiPz,JpsiRap, JpsiCharge,JpsiE;
    Double_t JpsiVprob;
    //2.) muon variables RECO                                                                       
    double muPosPx, muPosPy, muPosPz,  muPosEta, muPosPt,muPosP,muPosPhi;
    double muNegPx, muNegPy, muNegPz,  muNegEta, muNegPt,muNegP,muNegPhi;
    //(1).Positive Muon                                     
    double muPos_nchi2In, muPos_dxy, muPos_dz, muPos_nchi2Gl;
    int muPos_found, muPos_pixeLayers, muPos_nValidMuHits,muPos_arbitrated;
    bool muPos_matches,muPos_tracker;
    //(2).Negative Muon                                     
    double muNeg_nchi2In, muNeg_dxy, muNeg_dz, muNeg_nchi2Gl;
    int muNeg_found, muNeg_pixeLayers, muNeg_nValidMuHits,muNeg_arbitrated;
    bool muNeg_matches,muNeg_tracker;
    //Gen Level variables
    //Gen JPsi Variables
    double GenJpsiMass, GenJpsiPt, GenJpsiRap;
    double GenJpsiPx, GenJpsiPy, GenJpsiPz, GenJpsiE;
   
    //2.) Gen muon variables 
    double GenmuPosPx, GenmuPosPy, GenmuPosPz,  GenmuPosEta, GenmuPosPt, GenmuPosPhi;
    double GenmuNegPx, GenmuNegPy, GenmuNegPz,  GenmuNegEta, GenmuNegPt, GenmuNegPhi;

    //Event variables
    tree->SetBranchAddress("eventNb",&eventNb);
    tree->SetBranchAddress("runNb",&runNb);
    tree->SetBranchAddress("lumiBlock",&lumiBlock);
    
    //Jpsi Variables
    tree->SetBranchAddress("JpsiCharge",&JpsiCharge);
    tree->SetBranchAddress("JpsiMass",&JpsiMass);
    tree->SetBranchAddress("JpsiPt",&JpsiPt);
    tree->SetBranchAddress("JpsiPx",&JpsiPx);
    tree->SetBranchAddress("JpsiPy",&JpsiPy);
    tree->SetBranchAddress("JpsiPz",&JpsiPz);
    tree->SetBranchAddress("JpsiRap",&JpsiRap);
    tree->SetBranchAddress("JpsiVprob",&JpsiVprob);
    tree->SetBranchAddress("rbin",&rbin);
    
    //muon variable
    tree->SetBranchAddress("muPosPx",&muPosPx);
    tree->SetBranchAddress("muPosPy",&muPosPy);
    tree->SetBranchAddress("muPosPz",&muPosPz);
    tree->SetBranchAddress("muPosEta",&muPosEta);
    tree->SetBranchAddress("muPosPhi",&muPosPhi);
    tree->SetBranchAddress("muNegPx", &muNegPx);
    tree->SetBranchAddress("muNegPy",    &muNegPy);
    tree->SetBranchAddress("muNegPz",    &muNegPz);
    tree->SetBranchAddress("muNegEta",    &muNegEta);
     tree->SetBranchAddress("muNegPhi", &muNegPhi);

    //1). Positive Muon
    tree->SetBranchAddress("muPos_nchi2In", &muPos_nchi2In);
    tree->SetBranchAddress("muPos_dxy", &muPos_dxy);
    tree->SetBranchAddress("muPos_dz", &muPos_dz);
    tree->SetBranchAddress("muPos_nchi2Gl", &muPos_nchi2Gl);
    tree->SetBranchAddress("muPos_found", &muPos_found);
    tree->SetBranchAddress("muPos_pixeLayers", &muPos_pixeLayers);
    tree->SetBranchAddress("muPos_nValidMuHits", &muPos_nValidMuHits);
    tree->SetBranchAddress("muPos_matches", &muPos_matches);
    tree->SetBranchAddress("muPos_tracker", &muPos_tracker);
    tree->SetBranchAddress("muPos_arbitrated", &muPos_arbitrated);  
    
    //2). Negative Muon                                                                            
    tree->SetBranchAddress("muNeg_nchi2In", &muNeg_nchi2In);
    tree->SetBranchAddress("muNeg_dxy", &muNeg_dxy);
    tree->SetBranchAddress("muNeg_dz", &muNeg_dz);
    tree->SetBranchAddress("muNeg_nchi2Gl", &muNeg_nchi2Gl);
    tree->SetBranchAddress("muNeg_found", &muNeg_found);
    tree->SetBranchAddress("muNeg_pixeLayers", &muNeg_pixeLayers);
    tree->SetBranchAddress("muNeg_nValidMuHits", &muNeg_nValidMuHits);
    tree->SetBranchAddress("muNeg_matches", &muNeg_matches);
    tree->SetBranchAddress("muNeg_tracker", &muNeg_tracker);
    tree->SetBranchAddress("muNeg_arbitrated", &muNeg_arbitrated);
    



//====================================Gen Variables=========================================================
    //Gen Jpsi Variables
    gentree->SetBranchAddress("GenJpsiMass",   &GenJpsiMass);
    gentree->SetBranchAddress("GenJpsiPt",     &GenJpsiPt);
    gentree->SetBranchAddress("GenJpsiRap",    &GenJpsiRap);
    gentree->SetBranchAddress("GenJpsiPx",     &GenJpsiPx);
    gentree->SetBranchAddress("GenJpsiPy",     &GenJpsiPy);
    gentree->SetBranchAddress("GenJpsiPz",     &GenJpsiPz);
    gentree->SetBranchAddress("gbin",&gbin);
    //muon variable
    gentree->SetBranchAddress("GenmuPosPx",    &GenmuPosPx);
    gentree->SetBranchAddress("GenmuPosPy",    &GenmuPosPy);
    gentree->SetBranchAddress("GenmuPosPz",    &GenmuPosPz);
    gentree->SetBranchAddress("GenmuPosEta",    &GenmuPosEta);
     gentree->SetBranchAddress("GenmuPosPhi",    &GenmuPosPhi);
    gentree->SetBranchAddress("GenmuNegPx",    &GenmuNegPx);
    gentree->SetBranchAddress("GenmuNegPy",    &GenmuNegPy);
    gentree->SetBranchAddress("GenmuNegPz",    &GenmuNegPz);
    gentree->SetBranchAddress("GenmuNegEta",    &GenmuNegEta);
    gentree->SetBranchAddress("GenmuNegPhi",    &GenmuNegPhi);

    //====================================================== Gen tree loop ================================================
    int NAccep=0;
    int nGenEntries=gentree->GetEntries();
    cout<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ========="<<endl;
    //dataFile<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ====="<<endl;
    
    
    for(int i=0; i< nGenEntries; i++)  {	    
      gentree->GetEntry(i);
      
      if(i%1000==0){
	cout<<" processing record "<<i<<endl;
	cout<<" Mass "<< GenJpsiMass<< " pT "<< GenJpsiPt << " Y " <<GenJpsiRap<<endl; 
      }
      
      bool GenPosIn=0, GenNegIn=0,GenPosPass=0,GenNegPass=0;

      GenmuPosPt= TMath::Sqrt(GenmuPosPx*GenmuPosPx + GenmuPosPy*GenmuPosPy); 
      GenmuNegPt= TMath::Sqrt(GenmuNegPx*GenmuNegPx + GenmuNegPy*GenmuNegPy); 
      
      GenJpsiE= TMath::Sqrt( GenJpsiPx*GenJpsiPx+GenJpsiPy*GenJpsiPy+GenJpsiPz*GenJpsiPz + 9.46*9.46);


      
      //============================ calculate Pol weight ========================================================================================= //
      Float_t w1,w2,w3,w4,w5;
      // this is the beam energy: 2760GeV->/2->1380GeV each beam
      // the mp=0.938272 is the mass of the proton, as we are looking at p+p collisions
      double E=1380; double pz = sqrt(E*E - 0.938272*0.938272);
      TLorentzVector h1; // beam 1
      TLorentzVector h2; // beam 2
     
      TLorentzVector genJpsi;    // generated upsilon (mother of the single muons) --> if you look at jpsi-> genJpsi (prompt or non-prompt)
      TLorentzVector genMuPlus,genMuMinus; // generator positive muon (charge=+1)
      
      //int mp;
      Float_t cosThetaStarHel; // cosTheta in the Helicity frame
      Float_t cosThetaStarCS;  // cosTheta in the Collins-Soper frame
      TVector3 zCS;             // collins-soper variable

      // put the coordinates of the parent in a TLorentzVector
      // ATTENTION: the last coordinate is the MASS of the parent, which in this case, since it's about Upsilon, it's 9.46
      // when you'll do this for Jpsi, this value has to change to m_jpsi=3.097
      genJpsi.SetPxPyPzE(GenJpsiPx, GenJpsiPy, GenJpsiPz, GenJpsiE); 
      TLorentzRotation boost(-genJpsi.BoostVector()); // boost it
   
      // put the muon in a LorentzVector
      genMuPlus.SetPtEtaPhiM(GenmuPosPt, GenmuPosEta, GenmuPosPhi, 0.106);
      genMuPlus *= boost; // boost it
      
      //genMuMinus.SetPtEtaPhiM(GenmuNegPt, GenmuNegEta, GenmuNegPhi, 0.106);
      //genMuMinus *= boost; // boost it


      //and get the cosTheta in the helicity frame
      cosThetaStarHel = genMuPlus.Vect().Dot(genJpsi.Vect())/(genMuPlus.Vect().Mag()*genJpsi.Vect().Mag());
     
      //int genMuCharge = 1;
      //int mp = genMuCharge>0 ? 0 : 1;
      //cout << genMuCharge << "     " << mp << endl;


      h1.SetPxPyPzE(0,0,pz,E);  // TLorentzVector for beam 1
      h2.SetPxPyPzE(0,0,-pz,E); //  TLorentzVector for beam 2
      h1*=boost;
      h2*=boost;
      // calculate cosTheta CS

      zCS = ( h1.Vect().Unit() - h2.Vect().Unit() ).Unit();
      cosThetaStarCS = genMuPlus.Vect().Dot(zCS)/genMuPlus.Vect().Mag();

      // setup the weights
      w1 = 1;
      w2 = 1 + cosThetaStarHel*cosThetaStarHel;
      w3 = 1 - cosThetaStarHel*cosThetaStarHel;
      w4 = 1 + cosThetaStarCS*cosThetaStarCS;
      w5 = 1 - cosThetaStarCS*cosThetaStarCS;
      

      //w5=1; 

      // cout<<" gen "<<w2<<"   "<<w3<<"  "<<w4<<"  "<<w5<<endl;

      //==============================================================================================================================================//


    
      diMuonsInvMass_Gen->Fill(GenJpsiMass);
      diMuonsPt_Gen->Fill(GenJpsiPt);
 
      if(IsAccept(GenmuPosPt, GenmuPosEta)) {GenPosIn=1;}
      if(IsAccept(GenmuNegPt, GenmuNegEta)) {GenNegIn=1;}
  

    
      if(GenPosIn && GenNegIn ) NAccep++;
      
      if(GenPosIn==1 && GenmuPosPt>PtCut ) {GenPosPass=1;}
      if(GenNegIn==1 && GenmuNegPt>PtCut ) {GenNegPass=1;}

      double GenCenWeight=0,GenWeight=0;
      GenCenWeight=FindCenWeight(gbin);
      Bin_Gen->Fill(gbin);
      GenWeight=GenCenWeight*scale[ifile];
      if(PutWeight==0){GenWeight=1;}

      if(GenPosIn && GenNegIn){
	if(ifile==0){diMuonsRap_Gen0->Fill(GenJpsiRap);}
	if(ifile==1){diMuonsRap_Gen1->Fill(GenJpsiRap);}
	if(ifile==2){diMuonsRap_Gen2->Fill(GenJpsiRap);}
	if(ifile==3){diMuonsRap_Gen3->Fill(GenJpsiRap);}
	if(ifile==4){diMuonsRap_Gen4->Fill(GenJpsiRap);}
	if(ifile==5){diMuonsRap_Gen5->Fill(GenJpsiRap);}
      }
 

      for (Int_t ih = 0; ih < Nptbin; ih++) {
	//adding pT of all pt bins to see diss is cont
	if(iSpec == 1)  if( (GenPosPass==1 && GenNegPass==1) && (TMath::Abs(GenJpsiRap)<2.4 ) && (GenJpsiPt>pt_bound[ih] && GenJpsiPt<=pt_bound[ih+1])){diMuonsPt_GenA[ifile][ih]->Fill(GenJpsiPt,GenWeight*w5);}
	if(iSpec == 1)  if( (GenPosPass==1 && GenNegPass==1) && (TMath::Abs(GenJpsiRap)<2.4 )&&(GenJpsiPt>pt_bound[ih] && GenJpsiPt<=pt_bound[ih+1])){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight*w5);}
	if(iSpec == 2)  if((GenPosPass==1 && GenNegPass==1) &&  (GenJpsiPt<20.0) && (TMath::Abs(GenJpsiRap) > pt_bound[ih] && TMath::Abs(GenJpsiRap) <=pt_bound[ih+1] )){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight*w5);}
	if(iSpec == 3)  if( (GenPosPass==1 && GenNegPass==1) && (GenJpsiPt < 20.0) &&  (TMath::Abs(GenJpsiRap)<2.4 ) && (gbin>=pt_bound[ih] && gbin<pt_bound[ih+1])){diMuonsInvMass_GenA[ifile][ih]->Fill(GenJpsiMass,GenWeight*w5);}
      

      }

    }//gen loop end
    
    cout<<" accepted no "<< NAccep<<endl;
    //dataFile<<" accepted no "<< NAccep<<endl;
     
    //   new TCanvas;
    //diMuonsInvMass_Gen->Draw();
    //gPad->Print("plots/diMuonsInvMass_Gen.png");
    
    //new TCanvas;
    //diMuonsPt_Gen->Draw();
    //gPad->Print("plots/diMuonsPt_Gen.png");
  
    //new TCanvas;
    //diMuonsRap_Gen0->Draw();
    //sprintf(PlotName,"plots/diMuonsRap_Gen_%d.pdf",ifile);   
    //gPad->Print(PlotName);
    
    //new TCanvas;
    //Bin_Gen->Draw();
    //gPad->Print("plots/Bin_Gen.png");
    
  

    //=============== Rec Tree Loop ==============================================================================
    
    int nRecEntries=tree->GetEntries();
    cout<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<< "====="<<endl;
    //dataFile<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<<endl;
    
    for(int i=0; i<nRecEntries; i++)  {	    
      tree->GetEntry(i);
      if(i%100000==0){
	cout<<" processing record "<<i<<endl;
	cout<<" processing Run  " <<runNb <<" event "<<eventNb<<" lum block "<<lumiBlock<<endl;    
	cout<<" Mass "<< JpsiMass<< " pT "<< JpsiPt << " Y " <<JpsiRap<<"  "<<JpsiVprob<<" charge "<<JpsiCharge<<endl; 
      }
      
      bool PosPass=0, NegPass=0, AllCut=0 ,PosIn=0, NegIn=0;
      muPosPt= TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy); 
      muPosP = TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy+ muPosPz*muPosPz); 
      muNegPt= TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy); 
      muNegP = TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy +muNegPz*muNegPz); 
    
      
      JpsiE= TMath::Sqrt(JpsiPx*JpsiPx+JpsiPy*JpsiPy+JpsiPz*JpsiPz + JpsiMass*JpsiMass);
      //============================ calculate Pol weight rec ========================================================================================= //
      Float_t w1=0,w2=0,w3=0,w4=0,w5=0;
      double E=1380; double pz = sqrt(E*E - 0.938272*0.938272);
      TLorentzVector h1; // beam 1
      TLorentzVector h2; // beam 2
      
      TLorentzVector Jpsi;    
      TLorentzVector MuPlus;       
      Float_t cosThetaStarHel; // cosTheta in the Helicity frame
      Float_t cosThetaStarCS;  // cosTheta in the Collins-Soper frame
      TVector3 zCS;             // collins-soper variable
      
      Jpsi.SetPxPyPzE(JpsiPx, JpsiPy, JpsiPz, JpsiE); 
      TLorentzRotation boost(-Jpsi.BoostVector()); // boost it
      
      // put the muon in a LorentzVector
      MuPlus.SetPtEtaPhiM(muPosPt, muPosEta, muPosPhi, 0.106);
      MuPlus *= boost; // boost it
      //and get the cosTheta in the helicity frame
      cosThetaStarHel = MuPlus.Vect().Dot(Jpsi.Vect())/(MuPlus.Vect().Mag()*Jpsi.Vect().Mag());
      h1.SetPxPyPzE(0,0,pz,E);  // TLorentzVector for beam 1
      h2.SetPxPyPzE(0,0,-pz,E); //  TLorentzVector for beam 2
      h1*=boost;
      h2*=boost;
      zCS = ( h1.Vect().Unit() - h2.Vect().Unit() ).Unit();
      cosThetaStarCS = MuPlus.Vect().Dot(zCS)/MuPlus.Vect().Mag();
      // setup the weights
      w1 = 1;
      w2 = 1 + cosThetaStarHel*cosThetaStarHel;
      w3 = 1 - cosThetaStarHel*cosThetaStarHel;
      w4 = 1 + cosThetaStarCS*cosThetaStarCS;
      w5 = 1 - cosThetaStarCS*cosThetaStarCS;
      


      //w5=1;
      //cout<<" rec "<<w2<<"   "<<w3<<"  "<<w4<<"  "<<w5<<endl; 
      //================================================== Pol weights  ===============================================================================//
      
      if(IsAccept(muPosPt, muPosEta)){PosIn=1;}
      if(IsAccept(muNegPt, muNegEta)){NegIn=1;}



      if(muPos_found > 10 && muPos_pixeLayers > 0 && muPos_nchi2In < 4.0 && muPos_dxy < 3 && muPos_dz < 15 && muPos_nchi2Gl < 20 &&  muPos_arbitrated==1 && muPos_tracker==1){PosPass=1;}	  
      
      if(muNeg_found >10 && muNeg_pixeLayers >0 && muNeg_nchi2In <4.0 && muNeg_dxy < 3 && muNeg_dz < 15 && muNeg_nchi2Gl < 20 && muNeg_arbitrated==1 && muNeg_tracker==1){NegPass=1;}

      // cout<<muPos_matches<<"  "<<muNeg_matches<<endl;
      
      if((muPosPt > PtCut && muNegPt > PtCut) && (muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1 )  &&   (PosPass==1 && NegPass==1)){AllCut=1;}
    
      double RecCenWeight=0,RecWeight=0;
      RecCenWeight=FindCenWeight(rbin);
      RecWeight=RecCenWeight*scale[ifile];
      if(PutWeight==0){RecWeight=1;}
     
      if(i%100000==0){
	cout<<" eff loop for reco "<<endl;
      }
      
      if(AllCut==1){
	if(ifile==0){diMuonsRap_Rec0->Fill(JpsiRap);}
	if(ifile==1){diMuonsRap_Rec1->Fill(JpsiRap);}
	if(ifile==2){diMuonsRap_Rec2->Fill(JpsiRap);}
	if(ifile==3){diMuonsRap_Rec3->Fill(JpsiRap);}
	if(ifile==4){diMuonsRap_Rec4->Fill(JpsiRap);}
	if(ifile==5){diMuonsRap_Rec5->Fill(JpsiRap);}
      }

      //Eff loop for reco
      if((JpsiCharge == 0) && (JpsiVprob > 0.01)) {
	for (Int_t ih = 0; ih < Nptbin; ih++) {
	  //to see cont reco pT
	  if(iSpec == 1)if((AllCut==1) && (TMath::Abs(JpsiRap) < 2.4) && (JpsiPt>pt_bound[ih] && JpsiPt<=pt_bound[ih+1])) {diMuonsPt_RecA[ifile][ih]->Fill(JpsiPt,RecWeight*w5);}
	  
	  if(iSpec == 1)if((AllCut==1) && (TMath::Abs(JpsiRap)<2.4 ) && (JpsiPt > pt_bound[ih] && JpsiPt <=pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass, RecWeight*w5);}
	  if(iSpec == 2) if( (AllCut==1) &&  (JpsiPt<20.0) && (TMath::Abs(JpsiRap) > pt_bound[ih] && TMath::Abs(JpsiRap) <=pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass,RecWeight*w5);}
	  if(iSpec == 3) if((AllCut==1) &&  (JpsiPt<20.0) && (TMath::Abs(JpsiRap) < 2.4) && (rbin>=pt_bound[ih] &&  rbin < pt_bound[ih+1])){diMuonsInvMass_RecA[ifile][ih]->Fill(JpsiMass,RecWeight*w5);}
	



	}
      }
    }
  
    /*
    new TCanvas;
    if(ifile==0){diMuonsRap_Gen0->Draw();new TCanvas; diMuonsRap_Rec0->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen0.png");}
    if(ifile==1){diMuonsRap_Gen1->Draw();new TCanvas; diMuonsRap_Rec1->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen1.png");}
    if(ifile==2){diMuonsRap_Gen2->Draw();new TCanvas; diMuonsRap_Rec2->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen2.png");}
    if(ifile==3){diMuonsRap_Gen3->Draw();new TCanvas; diMuonsRap_Rec3->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen3.png");}
    if(ifile==4){diMuonsRap_Gen4->Draw();new TCanvas; diMuonsRap_Rec4->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen4.png");}
    if(ifile==5){diMuonsRap_Gen5->Draw();new TCanvas; diMuonsRap_Rec5->Draw(); gPad->Print("plots/NPdiMuonsRap_Gen5.png");}
    */
  }  // file loop ends 

  ///////////////////////////////////////////////////////////////////

  cout<< " adding "<<endl;
  TH1D *diMuonsInvMass_RecA1[100];
  TH1D *diMuonsInvMass_GenA1[100];
  TH1D *diMuonsPt_GenA1[100];
  TH1D *diMuonsPt_RecA1[100];
  TF1 *backfun_1;
  char namePt_1B[500];//for bkg func
 
  for(Int_t ih = 0; ih < Nptbin; ih++){
    diMuonsInvMass_RecA1[ih] = diMuonsInvMass_RecA[0][ih];
    diMuonsInvMass_GenA1[ih] = diMuonsInvMass_GenA[0][ih];
    diMuonsPt_GenA1[ih] = diMuonsPt_GenA[0][ih];
    diMuonsPt_RecA1[ih] = diMuonsPt_RecA[0][ih];
    
    for (int ifile = 1; ifile <= 5; ifile++) {
      diMuonsInvMass_RecA1[ih]->Add(diMuonsInvMass_RecA[ifile][ih]);
      diMuonsInvMass_GenA1[ih]->Add(diMuonsInvMass_GenA[ifile][ih]);     
      
      diMuonsPt_GenA1[ih]->Add(diMuonsPt_GenA[ifile][ih]); 
      diMuonsPt_RecA1[ih]->Add(diMuonsPt_RecA[ifile][ih]); 
    }
  }
  
  //===========================Fitting===================================================================//
  // Fit ranges
  double mass_low, mass_high;
  double MassUpsilon, WeidthUpsilon;
  
  // Low mass range upsilon width 54 KeV
  MassUpsilon = 9.46; WeidthUpsilon = 0.055;
  //MassUpsilon = 9.46; WeidthUpsilon = 0.068;
  mass_low = 9.0; mass_high = 10.0;  // Fit ranges
  
  // Fit Function crystall ball
  TF1 *GAUSPOL = new TF1("GAUSPOL",CrystalBall,8.0,12.0,6);
  GAUSPOL->SetParNames("Yield (#Upsilon)","BinWidth","Mean","Sigma","#alpha","n");
  GAUSPOL->SetParameter(2, MassUpsilon);
  GAUSPOL->SetParameter(3, WeidthUpsilon);
  //GAUSPOL->SetParLimits(3, 0.1*WeidthUpsilon,2.0*WeidthUpsilon);
  GAUSPOL->SetParameter(4, 1.0);
  GAUSPOL->SetParameter(5, 20.0);
  GAUSPOL->SetLineWidth(2.0);
  GAUSPOL->SetLineColor(2);




  //=====================Loop for eff===========================================================
  double GenNo[100]={0};
  double Eff[100]={0};
  double GenError[100]={0};
  double RecError[100]={0};
  double errEff_cat_S1[100]={0};
  double errEff_cat_S2[100]={0};
  double errEff_cat_S1_1[100]={0},errEff_cat_S1_2[100]={0};
  double errEff_cat_S2_1[100]={0},errEff_cat_S2_2[100]={0};
  char PlotName[500],PlotName1[500], PlotName2[500]; 
  char GPlotName[500],GPlotName1[500], GPlotName2[500];

  for (Int_t ih = 0; ih < Nptbin; ih++) {
   
    cout<<" no of gen dimuons from diMuons Pt histo    "<<diMuonsPt_GenA1[ih]->Integral(1,100)<<endl;
    cout<<" no of gen dimuons from diMuons Mass histo  "<<diMuonsInvMass_GenA1[ih]->Integral(1,100)<<endl;
    

    //from pT histogram
    //gen_pt[ih] =diMuonsPt_GenA1[ih]->IntegralAndError(1,100,genError);
   
    gen_pt[ih] = diMuonsInvMass_GenA1[ih]->IntegralAndError(1,100,genError);
    gen_ptError[ih]= genError;
    
    if(iSpec==1) sprintf(PlotName,"plots/DiMuonMass_PtBin_%d.png",ih);
    if(iSpec==2) sprintf(PlotName,"plots/DiMuonMass_RapBin_%d.png",ih);
    if(iSpec==3) sprintf(PlotName,"plots/DiMuonMass_CentBin_%d.png",ih);
    
    if(iSpec==1) sprintf(PlotName1,"plots/DiMuonMass_PtBin_%d.pdf",ih);
    if(iSpec==2) sprintf(PlotName1,"plots/DiMuonMass_RapBin_%d.pdf",ih);
    if(iSpec==3) sprintf(PlotName1,"plots/DiMuonMass_CentBin_%d.pdf",ih);
    
    if(iSpec==1) sprintf(PlotName2,"plots/DiMuonMass_PtBin_%d.eps",ih);
    if(iSpec==2) sprintf(PlotName2,"plots/DiMuonMass_RapBin_%d.eps",ih);
    if(iSpec==3) sprintf(PlotName2,"plots/DiMuonMass_CentBin_%d.eps",ih);
 

    //giving inetial value for crystall ball fourth parameter 
    diMuonsInvMass_RecA1[ih]->Rebin(2);
    GAUSPOL->SetParameter(0, diMuonsInvMass_RecA1[ih]->Integral(0,50));
    GAUSPOL->FixParameter(1, diMuonsInvMass_RecA1[ih]->GetBinWidth(1));
    
    new TCanvas; 
    diMuonsInvMass_RecA1[ih]->Fit("GAUSPOL","LLMERQ", "", mass_low, mass_high);
   
    double UpsilonMass = GAUSPOL->GetParameter(2);
    double UpsilonWidth = GAUSPOL->GetParameter(3);
   
    double UpsilonYield = GAUSPOL->GetParameter(0);
    double UpsilonYieldError = GAUSPOL->GetParError(0);
 
    double par[20];
    GAUSPOL->GetParameters(par);
    sprintf(namePt_1B,"pt_1B_%d",ih);
    backfun_1 = new TF1(namePt_1B, Pol2, mass_low, mass_high, 3);
    backfun_1->SetParameters(&par[4]);
   

    double MassLow=(UpsilonMass-3*UpsilonWidth);
    double MassHigh=(UpsilonMass+3*UpsilonWidth);
    
    int binlow =diMuonsInvMass_RecA1[ih]->GetXaxis()->FindBin(MassLow);
    int binhi =diMuonsInvMass_RecA1[ih]->GetXaxis()->FindBin(MassHigh);
    double binwidth=diMuonsInvMass_RecA1[ih]->GetBinWidth(1);
        
    //yield by function 
    //rec_pt[ih] = UpsilonYield;
    //rec_ptError[ih]= UpsilonYieldError;

    cout<<"Rec diMuons from Pt histo "<<diMuonsPt_RecA1[ih]->Integral(1,100)<<endl;  
    cout<<"Rec dimuons from mass "<<diMuonsInvMass_RecA1[ih]->Integral(1,100)<<endl; 
    

      
    //from pT histo
    //rec_pt[ih]=diMuonsPt_RecA1[ih]->IntegralAndError(1, 100,recError);

    //yield by histogram integral
    rec_pt[ih] = diMuonsInvMass_RecA1[ih]->IntegralAndError(binlow, binhi,recError);
    rec_ptError[ih]= recError;
  
    //Cal eff 
    Eff_cat_1[ih] = rec_pt[ih]/gen_pt[ih]; 
  
    //calculate error on eff
    GenNo[ih]=gen_pt[ih];
    Eff[ih]= Eff_cat_1[ih];
    GenError[ih]=gen_ptError[ih];
    RecError[ih]=rec_ptError[ih];
    //error    
    errEff_cat_S1_1[ih]= ( (Eff[ih] * Eff[ih]) /(GenNo[ih] * GenNo[ih]) );
    errEff_cat_S1_2[ih]= (RecError[ih] * RecError[ih]);
    errEff_cat_S1[ih]= (errEff_cat_S1_1[ih] * errEff_cat_S1_2[ih]);
   

    errEff_cat_S2_1[ih]= ( (1 - Eff[ih])* (1 - Eff[ih]) ) / ( GenNo[ih] * GenNo[ih]);
    errEff_cat_S2_2[ih]= (GenError[ih] * GenError[ih] ) - ( RecError[ih] * RecError[ih] );


    errEff_cat_S2[ih]=errEff_cat_S2_1[ih]*errEff_cat_S2_2[ih];
   

    Err_Eff_cat_1[ih]=sqrt(errEff_cat_S1[ih] + errEff_cat_S2[ih]);
   
    //error for no weights
    //Err_Eff_cat_1[ih]= Eff_cat_1[ih]*TMath::Sqrt(gen_ptError[ih]*gen_ptError[ih]/(gen_pt[ih]*gen_pt[ih]) + rec_ptError[ih]*rec_ptError[ih]/(rec_pt[ih]* rec_pt[ih]));

    cout<<"Upsilon Yield by integral of histo:  "<< diMuonsInvMass_RecA1[ih]->IntegralAndError(binlow, binhi,recError) <<"  error "<< rec_ptError[ih]<<endl; 
    cout<<"UpsilonYield by Gauss yield det:     "<< UpsilonYield << " UpsilonWidth "<< UpsilonWidth<<" UpsilonMass "<<UpsilonMass <<endl;
    cout<<"Upsilon Yield by Function integral:  "<< GAUSPOL->Integral(MassLow,MassHigh)/binwidth <<endl;
    cout<<" rec_pt[ih]  "<<  rec_pt[ih] <<" gen_pt[ih] "<<gen_pt[ih]<<endl;
    //dataFile<<" rec_pt[ih]  "<<  rec_pt[ih] <<" gen_pt[ih] "<<gen_pt[ih]<<endl;
    cout<<" eff "<< Eff_cat_1[ih]<<" error "<<Err_Eff_cat_1[ih]<<endl;
    
    dataFile<<"ih " <<ih<<" eff "<< Eff_cat_1[ih]<<" error "<<Err_Eff_cat_1[ih]<<endl;
    
    if(iSpec==1) sprintf(GPlotName,"plots/GenDiMuonMass_PtBin_%d.png",ih);
    if(iSpec==2) sprintf(GPlotName,"plots/GenDiMuonMass_RapBin_%d.png",ih);
    if(iSpec==3) sprintf(GPlotName,"plots/GenDiMuonMass_CentBin_%d.png",ih);
    
    if(iSpec==1) sprintf(GPlotName1,"plots/GenDiMuonMass_PtBin_%d.pdf",ih);
    if(iSpec==2) sprintf(GPlotName1,"plots/GenDiMuonMass_RapBin_%d.pdf",ih);
    if(iSpec==3) sprintf(GPlotName1,"plots/GenDiMuonMass_CentBin_%d.pdf",ih);
    
    if(iSpec==1) sprintf(GPlotName2,"plots/GenDiMuonMass_PtBin_%d.eps",ih);
    if(iSpec==2) sprintf(GPlotName2,"plots/GenDiMuonMass_RapBin_%d.eps",ih);
    if(iSpec==3) sprintf(GPlotName2,"plots/GenDiMuonMass_CentBin_%d.eps",ih);
       
    backfun_1->SetLineColor(4);
    backfun_1->SetLineWidth(1);
    //backfun_1->Draw("same");
    gPad->Print(PlotName);
    gPad->Print(PlotName1);
    gPad->Print(PlotName2);

    new TCanvas;
    diMuonsInvMass_GenA1[ih]->Draw();
    gPad->Print(GPlotName);
    gPad->Print(GPlotName1);
    gPad->Print(GPlotName2);


    //new TCanvas;
    //diMuonsPt_GenA1[ih]->Draw();
    //new TCanvas;
    //diMuonsPt_RecA1[ih]->Draw();
 
}
  dataFile.close();

  TGraphErrors *Eff_Upsilon = new TGraphErrors(Nptbin, PT, Eff_cat_1, mom_err,Err_Eff_cat_1);
  Eff_Upsilon->SetMarkerStyle(21);
  Eff_Upsilon->SetMarkerColor(2);
  Eff_Upsilon->GetYaxis()->SetTitle("Reco Eff");
 
  if(iSpec==1) Eff_Upsilon->GetXaxis()->SetTitle("#Upsilon pT (GeV/c^{2})");
  if(iSpec==2) Eff_Upsilon->GetXaxis()->SetTitle("#Upsilon rapidity");
  if(iSpec==3) Eff_Upsilon->GetXaxis()->SetTitle("bin");
   Eff_Upsilon->GetYaxis()->SetRangeUser(0,1.0);

  TLegend *legend_GP = new TLegend( 0.50,0.79,0.80,0.89);
  legend_GP->SetBorderSize(0);
  legend_GP->SetFillStyle(0);
  legend_GP->SetFillColor(0);
  legend_GP->SetTextSize(0.032);
  legend_GP->AddEntry(Eff_Upsilon,"PythiaEvtGen + HydjetBass", "P");
  
  new TCanvas;
  Eff_Upsilon->Draw("AP");
  legend_GP->Draw("Same");
    
  if(iSpec==1){ gPad->Print("plots/Eff_Upsilon_Pt.pdf");gPad->Print("plots/Eff_Upsilon_Pt.png");gPad->Print("plots/Eff_Upsilon_Pt.eps");}
  if(iSpec==2){ gPad->Print("plots/Eff_Upsilon_Rap.pdf");gPad->Print("plots/Eff_Upsilon_Rap.png");  gPad->Print("plots/Eff_Upsilon_Rap.eps");}
  if(iSpec==3){ gPad->Print("plots/Eff_Upsilon_Cent.pdf");gPad->Print("plots/Eff_Upsilon_Cent.png"); gPad->Print("plots/Eff_Upsilon_Cent.eps"); }

}
示例#8
0
void MCCTRapDep()   
{
  for(int iSpec = 0; iSpec < 3; iSpec++){
    int Prompt =1; int PutWeight = 1;
    bool bDefault = true; // true : cowboy only, false : salior
    bool bCowboy = false; // true : cowboy only, false : salior
    bool bSailor = false; // true : cowboy only, false : salior
    double fake_v2 = 0.3;

    // iSpec : choose the condition for default/cowboy/sailor
    if(iSpec == 0) {bDefault = true;bCowboy = false;bSailor = false;}
    if(iSpec == 1) {bDefault = false;bCowboy = true;bSailor = false;}
    if(iSpec == 2) {bDefault = false;bCowboy = false;bSailor = true;}

    char cCond[512];
    int iCond = 0; // the number of the cases, 1 : default, 2 : cowboy, 3 : sailor
    if(bDefault) {sprintf(cCond, "default"); iCond = 0;}
    if(bCowboy)  {sprintf(cCond, "cowboy"); iCond = 1;}
    if(bSailor)  {sprintf(cCond, "sailor"); iCond = 2;}

    // iCat : decide the categories for Rapidity (1: 0.0 - 1.2, 2: 1.2 - 1.6, 3: 1.6 - 2.4)
    for(int iCat = 0; iCat < 4; iCat++){
      gROOT->SetStyle("Plain");
      gStyle->SetPalette(1);
      gStyle->SetFrameBorderMode(0);
      gStyle->SetFrameFillColor(0);
      gStyle->SetCanvasColor(0);
      gStyle->SetTitleFillColor(0);
      gStyle->SetStatColor(0);
      gStyle->SetPadBorderSize(0);
      gStyle->SetCanvasBorderSize(0);
      gStyle->SetOptTitle(0); // at least most of the time
      gStyle->SetOptStat("emri");
      gStyle->SetOptFit(1); // set to 1 only if you want to display fit results
      //==================================== Define Histograms====================================================
      char OutTextFile[100]; 
      if(iCat == 0) sprintf(OutTextFile,"MCCT_PrJpsi_Raps_0012_dPhi_%s.tex", cCond);
      if(iCat == 1) sprintf(OutTextFile,"MCCT_PrJpsi_Raps_1216_dPhi_%s.tex", cCond);
      if(iCat == 2) sprintf(OutTextFile,"MCCT_PrJpsi_Raps_1624H_dPhi_%s.tex", cCond);
      if(iCat == 3) sprintf(OutTextFile,"MCCT_PrJpsi_Raps_1624L_dPhi_%s.tex", cCond);
      ofstream dataFile(Form(OutTextFile));
      TH1D *diMuonsInvMass_Gen = new TH1D("diMuonsInvMass_Gen","diMuonsInvMass_Gen", 100,2.98,3.16);

      TH1D *diMuonsPt_Gen = new TH1D("diMuonsPt_Gen","diMuonsPt_Gen", 100,0,50);
      TH1D *Bin_Gen = new TH1D("Bin_Gen","Bin_Gen", 40,0,40);
      //==============================================Define Acc Eff Stuff here===========================================
      // Pt bin sizes
      // 0-1.5, 1.5-3, 3-4.5, 4.5-6, 6-7.5...

      const int nFiles = 6;
      const int ndPhiBins = 4;
      double dphi_bound[100] = {0};

      dphi_bound[0] = 0.0;
      dphi_bound[1] = TMath::Pi()*2/16;
      dphi_bound[2] = TMath::Pi()*4/16;
      dphi_bound[3] = TMath::Pi()*6/16;
      dphi_bound[4] = TMath::Pi()*8/16;

      //X Axis error on Eff graph 
      double xdphi_bound[ndPhiBins] = {0.0};
      for(int i = 0; i < ndPhiBins; i++){
        xdphi_bound[i] = dphi_bound[i] + (dphi_bound[i+1]-dphi_bound[i])/2;
        cout<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
        dataFile<<"xdphi_bound["<<i<<"] : "<<xdphi_bound[i]<<endl;
      }

      double mom_err[ndPhiBins] = {0.0};

      double genError, recError;
      double gen_pt[100]={0}, gen_ptError[100]={0}; 
      double rec_pt[100]={0}, rec_ptError[100]={0}; 
      double Eff_cat_1[100]={0},Err_Eff_cat_1[100]={0};  

      // Histogram 2D Arrays
      TH1D *diMuonsInvMass_GenA[10][1000];
      TH1D *diMuonsInvMass_RecA[10][1000];
      TH1D *diMuonsPt_GenA[10][1000];
      TH1D *diMuonsPt_RecA[10][1000];
      char nameGen[10][500], nameRec[10][500], nameGenPt[10][500], nameRecPt[10][500];
      char namePt_1B[500];//for bkg func
      for (int ifile = 0; ifile < nFiles; ifile++) {
        for (Int_t idphi = 0; idphi < ndPhiBins; idphi++) {
          sprintf(nameGen[ifile],"DiMuonMassGen_pt_%d_%d_%d",ifile,iCat,idphi);
          sprintf(nameRec[ifile],"DiMuonMassRec_pt_%d_%d_%d",ifile,iCat,idphi);

          sprintf(nameGenPt[ifile],"DiMuonPtGen_pt_%d_%d_%d",ifile,iCat,idphi);
          sprintf(nameRecPt[ifile],"DiMuonPtRec_pt_%d_%d_%d",ifile,iCat,idphi);

          diMuonsInvMass_GenA[ifile][idphi]= new TH1D(nameGen[ifile],nameGen[ifile],  100,2.98,3.16); //for eff Gen;
          diMuonsInvMass_GenA[ifile][idphi]->Sumw2();
          diMuonsInvMass_GenA[ifile][idphi]->SetMarkerStyle(7);
          diMuonsInvMass_GenA[ifile][idphi]->SetMarkerColor(4);
          diMuonsInvMass_GenA[ifile][idphi]->SetLineColor(4);

          diMuonsInvMass_RecA[ifile][idphi] = new TH1D(nameRec[ifile],nameRec[ifile], 100,2.98,3.16); //for eff Rec;
          diMuonsInvMass_RecA[ifile][idphi]->Sumw2();
          diMuonsInvMass_RecA[ifile][idphi]->SetMarkerStyle(8);
          diMuonsInvMass_RecA[ifile][idphi]->SetMarkerColor(4);
          diMuonsInvMass_RecA[ifile][idphi]->SetLineColor(4);

          diMuonsPt_GenA[ifile][idphi]= new TH1D(nameGenPt[ifile],nameGenPt[ifile],  100,0,40); //for eff Gen;
          diMuonsPt_RecA[ifile][idphi]= new TH1D(nameRecPt[ifile],nameRecPt[ifile],  100,0,40); //for eff Rec;
        }
      }

      //===========================================Input Root File============================================================
      char fileName[10][500];
      //scales for different pT bins
      double scale[10]={0};  
      scale[0]=2.35829e-07;
      scale[1]=1.99854e-07;
      scale[2]=4.48263e-08;
      scale[3]=1.01144e-08;
      scale[4]=4.89604e-09;
      scale[5]=2.62102e-09;

      if(PutWeight==0){scale[0]=(1);scale[1]=(1);scale[2]=(1);scale[3]=(1);scale[4]=(1);scale[5]=(1);}

      if(Prompt ==1){
        cout<<"==================Prompt PrJpsi================================================"<<endl;
        sprintf(fileName[0],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt0003_total.root");
        sprintf(fileName[1],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt0306_total.root");
        sprintf(fileName[2],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt0609_total.root");
        sprintf(fileName[3],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt0912_total.root");
        sprintf(fileName[4],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt1215_total.root");
        sprintf(fileName[5],"/Users/donghomoon/Analysis/2011HIRData/2011JpsiV2/20111126_Jpsi_Psi/EffCorrection/JpsiEff_New_0329_NewBins_3DEff/0430_4Deff_lowPt_With_dPhi_JpsiGenPsi_HighPt/gRpRootFiles/DiMuonTTree_PromptJpsi_Pt1530_total.root");

        //sprintf(fileName[6],"../RootFiles/DiMuonTTree_PromptJpsi_Pt30XX_total.root");
      }

      TFile *infile;
      TTree *tree;
      TTree *gentree;

      //======================  File loop Starts ============================
      for(int ifile = 0; ifile < nFiles; ifile++){
        infile=new TFile(fileName[ifile],"R");
        tree=(TTree*)infile->Get("SingleMuonTree");
        gentree=(TTree*)infile->Get("SingleGenMuonTree");
        //Event variables
        int eventNb,runNb,lumiBlock, gbin, rbin;
        //Jpsi Variables
        Double_t JpsiMass,JpsiPt,JpsiRap, JpsiCharge;
        Double_t JpsiVprob;
        Double_t JpsiPhi;
        Double_t JpsiEta;
        Double_t JpsiPsi[38];
        Double_t JpsiGenPsi;
        //2.) muon variables RECO                                                                       
        double muPosPx, muPosPy, muPosPz,  muPosEta, muPosPt,muPosP, muPosPhi;
        double muNegPx, muNegPy, muNegPz,  muNegEta, muNegPt,muNegP, muNegPhi;
        //(1).Positive Muon                                     
        double muPos_nchi2In, muPos_dxy, muPos_dz, muPos_nchi2Gl;
        int muPos_found, muPos_pixeLayers, muPos_nValidMuHits,muPos_arbitrated;
        bool muPos_matches,muPos_tracker;
        //(2).Negative Muon                                     
        double muNeg_nchi2In, muNeg_dxy, muNeg_dz, muNeg_nchi2Gl;
        int muNeg_found, muNeg_pixeLayers, muNeg_nValidMuHits,muNeg_arbitrated;
        bool muNeg_matches,muNeg_tracker;

        //Gen Level variables
        //Gen PrJpsi Variables
        double GenJpsiMass, GenJpsiPt, GenJpsiRap;
        double GenJpsiPx, GenJpsiPy, GenJpsiPz;
        double GenJpsiPhi;
        double GenJpsiEta;
        double GenJpsiPsi;
        //2.) Gen muon variables 
        double GenmuPosPx, GenmuPosPy, GenmuPosPz,  GenmuPosEta, GenmuPosPt, GenmuPosPhi;
        double GenmuNegPx, GenmuNegPy, GenmuNegPz,  GenmuNegEta, GenmuNegPt, GenmuNegPhi;

        // HLTrigger
        int hbit1;

        //Event variables
        tree->SetBranchAddress("eventNb",&eventNb);
        tree->SetBranchAddress("runNb",&runNb);
        tree->SetBranchAddress("lumiBlock",&lumiBlock);
        tree->SetBranchAddress("hbit1",&hbit1);

        //Jpsi Variables
        tree->SetBranchAddress("JpsiCharge",&JpsiCharge);
        tree->SetBranchAddress("JpsiMass",&JpsiMass);
        tree->SetBranchAddress("JpsiPt",&JpsiPt);
        tree->SetBranchAddress("JpsiPhi",&JpsiPhi);
        tree->SetBranchAddress("JpsiEta",&JpsiEta);
        tree->SetBranchAddress("JpsiPsi",&JpsiPsi);
        tree->SetBranchAddress("JpsiGenPsi",&JpsiGenPsi);
        //tree->SetBranchAddress("JpsiPsi",&JpsiPsi[38]);
        tree->SetBranchAddress("JpsiRap",&JpsiRap);
        tree->SetBranchAddress("JpsiVprob",&JpsiVprob);
        tree->SetBranchAddress("rbin",&rbin);

        //muon variable
        tree->SetBranchAddress("muPosPx",&muPosPx);
        tree->SetBranchAddress("muPosPy",&muPosPy);
        tree->SetBranchAddress("muPosPz",&muPosPz);
        tree->SetBranchAddress("muPosEta",&muPosEta);
        tree->SetBranchAddress("muPosPhi",&muPosPhi);
        tree->SetBranchAddress("muNegPx", &muNegPx);
        tree->SetBranchAddress("muNegPy", &muNegPy);
        tree->SetBranchAddress("muNegPz", &muNegPz);
        tree->SetBranchAddress("muNegEta", &muNegEta);
        tree->SetBranchAddress("muNegPhi", &muNegPhi);


        //1). Positive Muon
        tree->SetBranchAddress("muPos_nchi2In", &muPos_nchi2In);
        tree->SetBranchAddress("muPos_dxy", &muPos_dxy);
        tree->SetBranchAddress("muPos_dz", &muPos_dz);
        tree->SetBranchAddress("muPos_nchi2Gl", &muPos_nchi2Gl);
        tree->SetBranchAddress("muPos_found", &muPos_found);
        tree->SetBranchAddress("muPos_pixeLayers", &muPos_pixeLayers);
        tree->SetBranchAddress("muPos_nValidMuHits", &muPos_nValidMuHits);
        tree->SetBranchAddress("muPos_matches", &muPos_matches);
        tree->SetBranchAddress("muPos_tracker", &muPos_tracker);
        tree->SetBranchAddress("muPos_arbitrated", &muPos_arbitrated);

        //2). Negative Muon                                                                            
        tree->SetBranchAddress("muNeg_nchi2In", &muNeg_nchi2In);
        tree->SetBranchAddress("muNeg_dxy", &muNeg_dxy);
        tree->SetBranchAddress("muNeg_dz", &muNeg_dz);
        tree->SetBranchAddress("muNeg_nchi2Gl", &muNeg_nchi2Gl);
        tree->SetBranchAddress("muNeg_found", &muNeg_found);
        tree->SetBranchAddress("muNeg_pixeLayers", &muNeg_pixeLayers);
        tree->SetBranchAddress("muNeg_nValidMuHits", &muNeg_nValidMuHits);
        tree->SetBranchAddress("muNeg_matches", &muNeg_matches);
        tree->SetBranchAddress("muNeg_tracker", &muNeg_tracker);
        tree->SetBranchAddress("muNeg_arbitrated", &muNeg_arbitrated);
        //====================================Gen Variables=========================================================
        //Gen Jpsi Variables
        gentree->SetBranchAddress("GenJpsiMass",   &GenJpsiMass);
        gentree->SetBranchAddress("GenJpsiPt",     &GenJpsiPt);
        gentree->SetBranchAddress("GenJpsiPhi",    &GenJpsiPhi);
        gentree->SetBranchAddress("GenJpsiEta",    &GenJpsiEta);
        gentree->SetBranchAddress("GenJpsiPsi",    &GenJpsiPsi);
        gentree->SetBranchAddress("GenJpsiRap",    &GenJpsiRap);
        gentree->SetBranchAddress("GenJpsiPx",     &GenJpsiPx);
        gentree->SetBranchAddress("GenJpsiPy",     &GenJpsiPy);
        gentree->SetBranchAddress("GenJpsiPz",     &GenJpsiPz);
        gentree->SetBranchAddress("gbin",&gbin);
        //muon variable
        gentree->SetBranchAddress("GenmuPosPx",    &GenmuPosPx);
        gentree->SetBranchAddress("GenmuPosPy",    &GenmuPosPy);
        gentree->SetBranchAddress("GenmuPosPz",    &GenmuPosPz);
        gentree->SetBranchAddress("GenmuPosEta",    &GenmuPosEta);
        gentree->SetBranchAddress("GenmuPosPhi",    &GenmuPosPhi);
        gentree->SetBranchAddress("GenmuNegPx",    &GenmuNegPx);
        gentree->SetBranchAddress("GenmuNegPy",    &GenmuNegPy);
        gentree->SetBranchAddress("GenmuNegPz",    &GenmuNegPz);
        gentree->SetBranchAddress("GenmuNegEta",    &GenmuNegEta);
        gentree->SetBranchAddress("GenmuNegPhi",    &GenmuNegPhi);

        //====================================================== Gen tree loop ================================================
        int NAccep=0;
        int nGenEntries=gentree->GetEntries();
        cout<<" Total Entries in GenLevel Tree for pT range: "<<fileName[ifile]<<"  "<<   nGenEntries<< " ==============="<<endl;

        for(int i=0; i< nGenEntries; i++)  {        
          gentree->GetEntry(i);
          //Only printing 
          if(i%100000==0){
            //cout<<" processing record "<<i<<"/"<<nGenEntries<<endl;
            //cout<<" Mass "<< GenJpsiMass<< " pT "<< GenJpsiPt << " Y " <<GenJpsiRap<<endl;
          }

          //if(GenJpsiPt < 6.5) continue;

          bool GenPosIn=0, GenNegIn=0;
          GenmuPosPt= TMath::Sqrt(GenmuPosPx*GenmuPosPx + GenmuPosPy*GenmuPosPy); 
          GenmuNegPt= TMath::Sqrt(GenmuNegPx*GenmuNegPx + GenmuNegPy*GenmuNegPy); 

          diMuonsInvMass_Gen->Fill(GenJpsiMass);
          diMuonsPt_Gen->Fill(GenJpsiPt);

          if(IsAccept(GenmuPosPt, GenmuPosEta)) {GenPosIn=1;}
          if(IsAccept(GenmuNegPt, GenmuNegEta)) {GenNegIn=1;}

          int AccHighPtJpsi = 0;
          int AccLowPtJpsi = 0;
          if(GenJpsiPt >= 6.5 && fabs(GenJpsiRap) < 2.4 && GenPosIn == 1 && GenNegIn == 1) AccHighPtJpsi = 1;
          if(GenJpsiPt < 6.5 && GenJpsiPt >= 3.0 && fabs(GenJpsiRap) >= 1.6 && fabs(GenJpsiRap) < 2.4 && GenPosIn == 1 && GenNegIn == 1) AccLowPtJpsi = 1;

          double gdPhi2mu = GenmuPosPhi - GenmuNegPhi;
          while (gdPhi2mu > TMath::Pi()) gdPhi2mu -= 2*TMath::Pi();
          while (gdPhi2mu <= -TMath::Pi()) gdPhi2mu += 2*TMath::Pi();
          double gchkCowboy = 1*gdPhi2mu;

          if(bCowboy && !(gchkCowboy > 0.)) continue;
          if(bSailor && (gchkCowboy > 0.)) continue;

          if((GenPosIn ==1 && GenNegIn==1)) NAccep++;

          Bin_Gen->Fill(gbin);

          double GenCenWeight =0, GenWeight =0;
          GenCenWeight=FindCenWeight(gbin);
          
          double gJpsidPhi = TMath::Abs(GenJpsiPsi);
          double gmean_dphi = TMath::Pi()/16;
          if (gJpsidPhi>TMath::Pi()/8) gmean_dphi = 3*TMath::Pi()/16;
          if (gJpsidPhi>TMath::Pi()/4) gmean_dphi = 5*TMath::Pi()/16;
          if (gJpsidPhi>3*TMath::Pi()/8) gmean_dphi = 7*TMath::Pi()/16;
          GenWeight=GenCenWeight*scale[ifile]*(2.0/TMath::Pi()*(1+2*fake_v2*cos(2*gJpsidPhi)*TMath::Gaus(GenJpsiPt,15,4,0)));

          if(PutWeight==0) GenWeight=1; 


          for (Int_t idphi = 0; idphi < ndPhiBins; idphi++) {

            //adding pT of all pt bins to see diss is cont

            if(iCat == 0) {
              if(  AccHighPtJpsi == 1 && (gbin >= 4 && gbin < 24) && (GenPosIn==1 && GenNegIn==1) && (GenJpsiPt >= 6.5 && GenJpsiPt < 40.0) &&  
                  (TMath::Abs(GenJpsiRap)<1.2 && TMath::Abs(GenJpsiRap) >= 0.0 ) && 
                  (TMath::Abs(GenJpsiPsi)>=dphi_bound[idphi] && TMath::Abs(GenJpsiPsi)<dphi_bound[idphi+1])){diMuonsInvMass_GenA[ifile][idphi]->Fill(GenJpsiMass,GenWeight);}
            }
            if(iCat == 1) {
              if(  AccHighPtJpsi == 1 && (gbin >= 4 && gbin < 24) && (GenPosIn==1 && GenNegIn==1) && (GenJpsiPt >= 6.5 && GenJpsiPt < 40.0) &&  
                  (TMath::Abs(GenJpsiRap)<1.6 && TMath::Abs(GenJpsiRap) >= 1.2 ) && 
                  (TMath::Abs(GenJpsiPsi)>=dphi_bound[idphi] && TMath::Abs(GenJpsiPsi)<dphi_bound[idphi+1])){diMuonsInvMass_GenA[ifile][idphi]->Fill(GenJpsiMass,GenWeight);}
            }
            if(iCat == 2) {
              if(  AccHighPtJpsi == 1 &&  (gbin >= 4 && gbin < 24) && (GenPosIn==1 && GenNegIn==1) && (GenJpsiPt >= 6.5 && GenJpsiPt < 40.0) &&  
                  (TMath::Abs(GenJpsiRap)<2.4 && TMath::Abs(GenJpsiRap) >= 1.6 ) && 
                  (TMath::Abs(GenJpsiPsi)>=dphi_bound[idphi] && TMath::Abs(GenJpsiPsi)<dphi_bound[idphi+1])){diMuonsInvMass_GenA[ifile][idphi]->Fill(GenJpsiMass,GenWeight);}
            }
            if(iCat == 3) {
              if(  AccLowPtJpsi == 1 && (gbin >= 4 && gbin < 24) && (GenPosIn==1 && GenNegIn==1) && (GenJpsiPt >= 3.0 && GenJpsiPt < 6.5) &&  
                  (TMath::Abs(GenJpsiRap)<2.4 && TMath::Abs(GenJpsiRap) >= 1.6 ) && 
                  (TMath::Abs(GenJpsiPsi)>=dphi_bound[idphi] && TMath::Abs(GenJpsiPsi)<dphi_bound[idphi+1])){diMuonsInvMass_GenA[ifile][idphi]->Fill(GenJpsiMass,GenWeight);}
            }
          }
        }//gen loop end



        cout<<" accepted no "<< NAccep<<endl;
        dataFile<<" accepted no "<< NAccep<<endl;

        //dataFile<<" accepted no "<< NAccep<<endl;
        //   new TCanvas;
        //diMuonsInvMass_Gen->Draw();
        //gPad->Print("plots/diMuonsInvMass_Gen.png");

        new TCanvas;
        diMuonsPt_Gen->Draw();

        //=============== Rec Tree Loop ==============================================================================

         // start to fill up reco corrected by efficiency
        char tmp_eff_input[512], tmp_input_histo[512];
        if(!(iCat == 3)) sprintf(tmp_eff_input,"../../EffRoots_New/PrJpsi_HighPt_%s.root", cCond);
        if(iCat == 3) sprintf(tmp_eff_input,"../../EffRoots_New/PrJpsi_LowPt_%s.root", cCond);
        sprintf(tmp_input_histo,"eff_%s", cCond);

        TFile *eff_input;
        eff_input=new TFile(tmp_eff_input,"R");

        int nRecEntries=tree->GetEntries();
        cout<<"Total Entries in reconstructed Tree for pT range "<<fileName[ifile]<<"  "<<nRecEntries<< "====="<<endl;
        for(int i=0; i<nRecEntries; i++)  {     
          tree->GetEntry(i);
          //if(bCowboy && !(gchkCowboy > 0.)) {cout<<"This is not Cowboy from Gen"<<endl; continue;}
          //if(bSailor && (gchkCowboy > 0.)) {cout<<"This is not Sailor from Gen"<<endl; continue;}
          //Only printing 
          if(i%10000==0){
            //cout<<" processing record "<<i<<"/"<<nRecEntries<<endl;
            //cout<<" processing Run  " <<runNb <<" event "<<eventNb<<" lum block "<<lumiBlock<<endl;    
            //cout<<" Mass "<< JpsiMass<< " pT "<< JpsiPt << " Y " <<JpsiRap<<"  "<<JpsiVprob<<" charge "<<JpsiCharge<<" rbin "<<rbin<<endl; 
          }
          //if(JpsiPt < 6.5) continue;
          bool PosPass=0, NegPass=0, AllCut=0 ,PosIn=0, NegIn=0;
          muPosPt= TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy); 
          muPosP = TMath::Sqrt(muPosPx*muPosPx + muPosPy*muPosPy+ muPosPz*muPosPz); 
          muNegPt= TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy); 
          muNegP = TMath::Sqrt(muNegPx*muNegPx + muNegPy*muNegPy +muNegPz*muNegPz); 

          if(IsAccept(muPosPt, muPosEta)){PosIn=1;}
          if(IsAccept(muNegPt, muNegEta)){NegIn=1;}

          bool mu_Global = ((muPos_nchi2Gl >=0) && (muNeg_nchi2Gl >=0));
          bool mu_Tracker = ((muPos_tracker==1) && (muNeg_tracker==1));

          double dPhi2mu = muPosPhi - muNegPhi;
          while (dPhi2mu > TMath::Pi()) dPhi2mu -= 2*TMath::Pi();
          while (dPhi2mu <= -TMath::Pi()) dPhi2mu += 2*TMath::Pi();
          double chkCowboy = 1*dPhi2mu;

          if(bCowboy && !(chkCowboy > 0.)) continue;
          if(bSailor && (chkCowboy > 0.)) continue;

          if(muPos_found > 10 && muPos_pixeLayers > 0 && muPos_nchi2In < 4.0 && TMath::Abs(muPos_dxy) < 3 && TMath::Abs(muPos_dz) < 15 && muPos_nchi2Gl < 20  &&
              muPos_arbitrated==1 && muPos_tracker==1){PosPass=1;}
          if(muNeg_found >10 && muNeg_pixeLayers >0 && muNeg_nchi2In <4.0 && TMath::Abs(muNeg_dxy) < 3 && TMath::Abs(muNeg_dz) < 15 && muNeg_nchi2Gl < 20 &&
              muNeg_arbitrated==1 && muNeg_tracker==1){NegPass=1;}

          //cout<<"Cut checks, muPos_matches : "<<muPos_matches<<", muNeg_matches : "<<muNeg_matches<<", PosIn : "<<PosIn<<", NegIn : "<<NegIn
          //    <<", PosPass : "******", NegPass : "******", mu_Global : "<<mu_Global<<", mu_Tracker : "<<mu_Tracker<<endl;
          //if((PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}
          int AccHighPtJpsi = 0;
          int AccLowPtJpsi = 0;
          if(JpsiPt >= 6.5 && fabs(JpsiRap) < 2.4 && PosIn == 1 && NegIn == 1) AccHighPtJpsi = 1;
          if(JpsiPt < 6.5 && JpsiPt >= 3.0 && fabs(JpsiRap) >= 1.6 && fabs(JpsiRap) < 2.4 && PosIn == 1 && NegIn == 1) AccLowPtJpsi = 1;

          if(hbit1 == 1 && (muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}

          //AllCut = 1;
          //without ID cut
          // if((muPos_matches==1 && muNeg_matches==1) && (PosIn==1 && NegIn==1) && mu_Global && mu_Tracker){AllCut=1;}

          //without trigger matched
          //if((PosIn==1 && NegIn==1) && (PosPass==1 && NegPass==1)&& mu_Global && mu_Tracker){AllCut=1;}

          double eff_cor[2];
          double RecCenWeight=0,RecWeight=0;
          RecCenWeight=FindCenWeight(rbin);
          
          double JpsidPhi = 0.0;
          JpsidPhi = TMath::Abs(JpsiGenPsi);

          double rmean_dphi = TMath::Pi()/16;
          if (JpsidPhi>TMath::Pi()/8) rmean_dphi = 3*TMath::Pi()/16;
          if (JpsidPhi>TMath::Pi()/4) rmean_dphi = 5*TMath::Pi()/16;
          if (JpsidPhi>3*TMath::Pi()/8) rmean_dphi = 7*TMath::Pi()/16;
          RecWeight=RecCenWeight*scale[ifile]*(2.0/TMath::Pi()*(1+2*fake_v2*cos(2*JpsidPhi)*TMath::Gaus(JpsiPt,15,4,0)));


          if(PutWeight==0)RecWeight=1;


          //Eff loop for reco
          for (Int_t idphi = 0; idphi < ndPhiBins; idphi++) {
            if((JpsiCharge == 0) && (JpsiVprob > 0.01)) {     
                if(iCat == 0) {
                  if(AccHighPtJpsi == 1 && (AllCut==1) && (rbin >= 4 && rbin < 24) && (JpsiPt>=6.5 && JpsiPt<40.0) && 
                      (TMath::Abs(JpsiRap) < 1.2 && TMath::Abs(JpsiRap) >= 0.0) && 
                      (TMath::Abs(JpsiGenPsi) > dphi_bound[idphi] && TMath::Abs(JpsiGenPsi) <=dphi_bound[idphi+1])){
                  DoEffCor3D(eff_input, iCond, rbin, JpsiPt, fabs(JpsiRap), eff_cor);
                  diMuonsInvMass_RecA[ifile][idphi]->Fill(JpsiMass,RecWeight*((double)1.0/eff_cor[0]));}
                }
                if(iCat == 1) {
                  if(AccHighPtJpsi == 1 && (AllCut==1) && (rbin >= 4 && rbin < 24) && (JpsiPt>=6.5 && JpsiPt<40.0) && 
                      (TMath::Abs(JpsiRap) < 1.6 && TMath::Abs(JpsiRap) >= 1.2) && 
                      (TMath::Abs(JpsiGenPsi) > dphi_bound[idphi] && TMath::Abs(JpsiGenPsi) <=dphi_bound[idphi+1])){
                  DoEffCor3D(eff_input, iCond, rbin, JpsiPt, fabs(JpsiRap), eff_cor);
                  diMuonsInvMass_RecA[ifile][idphi]->Fill(JpsiMass,RecWeight*((double)1.0/eff_cor[0]));}
                }
                if(iCat == 2) {
                  if(AccHighPtJpsi == 1 && (AllCut==1) && (rbin >= 4 && rbin < 24) && (JpsiPt>=6.5 && JpsiPt<40.0) && 
                      (TMath::Abs(JpsiRap) < 2.4 && TMath::Abs(JpsiRap) >= 1.6) && 
                      (TMath::Abs(JpsiGenPsi) > dphi_bound[idphi] && TMath::Abs(JpsiGenPsi) <=dphi_bound[idphi+1])){
                  DoEffCor3D(eff_input, iCond, rbin, JpsiPt, fabs(JpsiRap), eff_cor);
                  diMuonsInvMass_RecA[ifile][idphi]->Fill(JpsiMass,RecWeight*((double)1.0/eff_cor[0]));}
                }
                if(iCat == 3) {
                  if(AccLowPtJpsi == 1 && (AllCut==1) && (rbin >= 4 && rbin < 24) && (JpsiPt>=3.0 && JpsiPt<6.5) && 
                      (TMath::Abs(JpsiRap) < 2.4 && TMath::Abs(JpsiRap) >= 1.6) && 
                      (TMath::Abs(JpsiGenPsi) > dphi_bound[idphi] && TMath::Abs(JpsiGenPsi) <=dphi_bound[idphi+1])){
                  DoEffCor3D(eff_input, iCond, rbin, JpsiPt, fabs(JpsiRap), eff_cor);
                  diMuonsInvMass_RecA[ifile][idphi]->Fill(JpsiMass,RecWeight*((double)1.0/eff_cor[0]));}
                }
            }
          }
        }//rec tree loop ends
      }  // file loop ends

      ///////////////////////////////////////////////////////////////////
      cout<< " adding "<<endl;
      TH1D *diMuonsInvMass_RecA1[100];
      TH1D *diMuonsInvMass_GenA1[100];
      TH1D *diMuonsPt_GenA1[100];
      TH1D *diMuonsPt_RecA1[100];
      TF1 *backfun_1;

      for(Int_t idphi = 0; idphi < ndPhiBins; idphi++){
        diMuonsInvMass_RecA1[idphi] = diMuonsInvMass_RecA[0][idphi];
        diMuonsInvMass_GenA1[idphi] = diMuonsInvMass_GenA[0][idphi];
        diMuonsPt_GenA1[idphi] = diMuonsPt_GenA[0][idphi];
        diMuonsPt_RecA1[idphi] = diMuonsPt_RecA[0][idphi];

        for (int ifile = 1; ifile < nFiles; ifile++) {
          diMuonsInvMass_RecA1[idphi]->Add(diMuonsInvMass_RecA[ifile][idphi]);
          diMuonsInvMass_GenA1[idphi]->Add(diMuonsInvMass_GenA[ifile][idphi]);     
          diMuonsPt_GenA1[idphi]->Add(diMuonsPt_GenA[ifile][idphi]); 
          diMuonsPt_RecA1[idphi]->Add(diMuonsPt_RecA[ifile][idphi]); 
        }
      }
      //===========================Fitting=================================================================================//
      // Fit ranges
      double mass_low, mass_high;
      double MassJpsi, WeidthJpsi;
      // Fit Function crystall ball
      // Jpsi Settings
      MassJpsi = 3.096; 
      WeidthJpsi = 0.028;
      mass_low = 2.945; 
      mass_high = 3.24;  // Fit ranges

      TF1 *GAUSPOL = new TF1("GAUSPOL",CrystalBall,2.4,3.8,6);//2.4,3.8,6);
      GAUSPOL->SetParNames("Yield (J/#psi)","BinWidth","Mean","Sigma","#alpha","n");
      GAUSPOL->SetParameter(2, MassJpsi);
      GAUSPOL->SetParameter(3, WeidthJpsi);
      //GAUSPOL->SetParLimits(3, 0.1*WeidthJpsi,2.0*WeidthJpsi);
      GAUSPOL->SetParameter(4, 1.2);
      GAUSPOL->SetParameter(5, 20.0);
      GAUSPOL->SetLineWidth(2.0);
      GAUSPOL->SetLineColor(2);


      //=====================Loop for eff========================================================================================//
      //define stuff here for error on weighted samples
      double GenNo[100]={0};
      double GenError[100]={0};
      double RecError[100]={0};

      for (Int_t idphi = 0; idphi < ndPhiBins; idphi++) {

        gen_pt[idphi] = diMuonsInvMass_GenA1[idphi]->IntegralAndError(1, 100, genError);
        gen_ptError[idphi]= genError;
        cout<<" gen_pt[idphi] "<< gen_pt[idphi] <<" error   "<<  gen_ptError[idphi]<<endl;

        // cout<<" *********************** "<<diMuonsInvMass_RecA1[idphi]->GetMaximum()<<endl;
        //giving inetial value for crystall ball fourth parameter 
        GAUSPOL->SetParameter(0, diMuonsInvMass_RecA1[idphi]->Integral(0,50));
        GAUSPOL->FixParameter(1, diMuonsInvMass_RecA1[idphi]->GetBinWidth(1));
        //GAUSPOL->SetParameter(0, diMuonsInvMass_RecA1[idphi]->GetMaximum());
        //new TCanvas;
        //diMuonsInvMass_RecA1[idphi]->Draw();

        new TCanvas;
        diMuonsInvMass_RecA1[idphi]->Fit("GAUSPOL","EMRQ", "", mass_low, mass_high); // Jpsi
        //diMuonsInvMass_RecA1[idphi]->Fit("GAUSPOL","LLMERQ", "", 8.5,10.5); // Jpsi
        //diMuonsInvMass_RecA1[idphi]->Fit("GAUSPOL","LLMER", "", mass_low, mass_high); // Jpsi
        diMuonsInvMass_RecA1[idphi]->DrawCopy("EPLsame");

        // new TCanvas;
        //diMuonsInvMass_RecA1[idphi]->Fit("GAUSPOL_1","LLMER", "", mass_low, mass_high);
        //diMuonsInvMass_RecA1[idphi]->DrawCopy("EPLsame");
        //gPad->Print(PlotName);
        //gPad->Print(PlotName1);

        //  cout << GAUSPOL_1->GetChisquare()<<endl;
        //for(int i=0;i<=100;i++) {cout<<i<<"  "<<diMuonsInvMass_RecA1[idphi]->GetBinContent(i)<<endl;}
        //return;
        //double JpsiMass = GAUSPOL_1->GetParameter(2);
        //double JpsiWidth = GAUSPOL_1->GetParameter(3);
        //double JpsiYield = GAUSPOL_1->GetParameter(4); 

        double JpsiMass = GAUSPOL->GetParameter(2);
        double JpsiWidth = GAUSPOL->GetParameter(3);

        double JpsiYield = GAUSPOL->GetParameter(0); 
        Double_t JpsiYieldError = GAUSPOL->GetParError(0); 

        //cout<<JpsiYieldError<<"*****************"<<endl;

        //if(TMath::IsNan(JpsiYieldError)=1) {JpsiYieldError=TMath::Sqrt(JpsiYield);}

        double par[20];
        GAUSPOL->GetParameters(par);
        sprintf(namePt_1B,"pt_1B_%d",idphi);

        backfun_1 = new TF1(namePt_1B, Pol2, mass_low, mass_high, 3);
        backfun_1->SetParameters(&par[3]);

        double MassLow=(JpsiMass-3*JpsiWidth);
        double MassHigh=(JpsiMass+3*JpsiWidth);


        int binlow =diMuonsInvMass_RecA1[idphi]->GetXaxis()->FindBin(MassLow);
        int binhi =diMuonsInvMass_RecA1[idphi]->GetXaxis()->FindBin(MassHigh);

        //double binwidth=diMuonsInvMass_RecA1[idphi]->GetBinWidth(1);
        //yield by function 
        //rec_pt[idphi] = JpsiYield;
        //rec_ptError[idphi]= JpsiYieldError;

        //yield by histogram integral
        binlow = 1;
        binhi = 100;
        rec_pt[idphi] = diMuonsInvMass_RecA1[idphi]->IntegralAndError(binlow, binhi,recError);
        rec_ptError[idphi]= recError;
      }

      TFile *outfile;
      char tmp_output[512];
      if(iCat == 0) sprintf(tmp_output,"MCCT_PrJpsi_Raps_0012_dPhi_%s.root",cCond);
      if(iCat == 1) sprintf(tmp_output,"MCCT_PrJpsi_Raps_1216_dPhi_%s.root",cCond);
      if(iCat == 2) sprintf(tmp_output,"MCCT_PrJpsi_Raps_1624H_dPhi_%s.root",cCond);
      if(iCat == 3) sprintf(tmp_output,"MCCT_PrJpsi_Raps_1624L_dPhi_%s.root",cCond);
      outfile =new TFile(tmp_output,"Recreate");

      double gsum = 0.0;
      double gen_pt_Norm[ndPhiBins];
      double gen_ptError_Norm[ndPhiBins];
      for(int i = 0; i < ndPhiBins; i++){
        gsum += gen_pt[i];
      }
      gsum = gsum*(dphi_bound[1]-dphi_bound[0]);

      for(int i = 0; i < ndPhiBins; i++){
        gen_pt_Norm[i] = gen_pt[i]/gsum;
        gen_ptError_Norm[i] = gen_ptError[i]/gsum;
      }

      double sum = 0.0;
      double rec_pt_Norm[ndPhiBins];
      double rec_ptError_Norm[ndPhiBins];
      for(int i = 0; i < ndPhiBins; i++){
        sum += rec_pt[i];
      }
      sum = sum*(dphi_bound[1]-dphi_bound[0]);

      for(int i = 0; i < ndPhiBins; i++){
        rec_pt_Norm[i] = rec_pt[i]/sum;
        rec_ptError_Norm[i] = rec_ptError[i]/sum;
      }

      TH1F *hJpsi_Gen = new TH1F("hJpsi_Gen","hJpsi_Gen",ndPhiBins,0,TMath::Pi()/2);
      TH1F *hJpsi_Reco = new TH1F("hJpsi_Reco","hJpsi_Reco",ndPhiBins,0,TMath::Pi()/2);
      hJpsi_Gen->Sumw2();
      hJpsi_Reco->Sumw2();
      for(int i = 0; i < ndPhiBins; i++){
        hJpsi_Gen->SetBinContent(i+1,gen_pt[i]);
        hJpsi_Gen->SetBinError(i+1,gen_ptError[i]);
        hJpsi_Reco->SetBinContent(i+1,rec_pt[i]);
        hJpsi_Reco->SetBinError(i+1,rec_ptError[i]);
      }

      TGraphErrors *Jpsi_Reco_Norm = new TGraphErrors(ndPhiBins, xdphi_bound, rec_pt_Norm, mom_err, rec_ptError_Norm);
      TGraphErrors *Jpsi_Gen_Norm = new TGraphErrors(ndPhiBins, xdphi_bound, gen_pt_Norm, mom_err, gen_ptError_Norm);
      if(iCat == 0){
        hJpsi_Gen->SetName("hGen_Jpsi_Raps_0012");
        hJpsi_Reco->SetName("hReco_Jpsi_Raps_0012");
        Jpsi_Reco_Norm->SetName("nReco_Jpsi_Raps_0012");
        Jpsi_Gen_Norm->SetName("nGen_Jpsi_Raps_0012");
      }
      if(iCat == 1){
        hJpsi_Gen->SetName("hGen_Jpsi_Raps_1216");
        hJpsi_Reco->SetName("hReco_Jpsi_Raps_1216");
        Jpsi_Reco_Norm->SetName("nReco_Jpsi_Raps_1216");
        Jpsi_Gen_Norm->SetName("nGen_Jpsi_Raps_1216");
      }
      if(iCat == 2){
        hJpsi_Gen->SetName("hGen_Jpsi_Raps_1624H");
        hJpsi_Reco->SetName("hReco_Jpsi_Raps_1624H");
        Jpsi_Reco_Norm->SetName("nReco_Jpsi_Raps_1624H");
        Jpsi_Gen_Norm->SetName("nGen_Jpsi_Raps_1624H");
      }
      if(iCat == 3){
        hJpsi_Gen->SetName("hGen_Jpsi_Raps_1624L");
        hJpsi_Reco->SetName("hReco_Jpsi_Raps_1624L");
        Jpsi_Reco_Norm->SetName("nReco_Jpsi_Raps_1624L");
        Jpsi_Gen_Norm->SetName("nGen_Jpsi_Raps_1624L");
      }
      hJpsi_Gen->Write();
      hJpsi_Reco->Write();
      Jpsi_Reco_Norm->Write();
      Jpsi_Gen_Norm->Write();

      outfile->Write();
      outfile->Close();
    }
  }
}
示例#9
0
void ReadPatMuWT(){

  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameFillColor(0);
  gStyle->SetCanvasColor(0);
  gStyle->SetTitleFillColor(0);
  gStyle->SetStatColor(0);
  gStyle->SetPadBorderSize(0);
  gStyle->SetCanvasBorderSize(0);
  gStyle->SetOptTitle(0); // at least most of the time
  gStyle->SetOptStat(1); // most of the time, sometimes "nemriou" might be useful to display name, 
  //number of entries, mean, rms, integral, overflow and underflow
  gStyle->SetOptFit(1); // set to 1 only if you want to display fit results

  Double_t massMu=0.105658;
  
  TFile *infile;
  TTree *SingleWTPATMuonTree;
   char fileName[100];
   //boosted sample
   //sprintf(fileName,"../Upsilon1S_OniaTree_All02Feb.root");
   //un boosted sample
   sprintf(fileName,"test_Coll_Unboosted.root");
   
   cout<<" ===================================================================== "<<endl;
   cout<<" opening root file : "<<fileName<<endl;
   
  infile=new TFile(fileName,"R");
  SingleWTPATMuonTree=(TTree*)infile->Get("SingleWTPATMuonTree");



   //Declaration of leaves types
   Int_t           eventNbWTPAT;
   Int_t           runNbWTPAT;
   Int_t           lumiBlockWTPAT;
   Int_t           rbinWTPAT;
   
   Int_t           PAT_vertexNbEv;
   Double_t        PAT_vertexTrkEv;
   Bool_t          PAT_isVtxFakeEv;
   Double_t        PAT_vertexXEv;
   Double_t        PAT_vertexYEv;
   Double_t        PAT_vertexZEv;
   Double_t        PAT_vertexRhoEv;
   Int_t           PAT_eventTrigger1;
   Int_t           PAT_eventTrigger2;
   Int_t           PAT_eventTrigger3;
   Int_t           PAT_eventTrigger4;
   Int_t           PAT_eventTrigger5;
   Int_t           PAT_eventTrigger6;




   Int_t           PAT_GenParNo;
   Int_t           PAT_GenParSize;
   Double_t        PAT_GenvertexXEv;
   Double_t        PAT_GenvertexYEv;
   Double_t        PAT_GenvertexZEv;
   Double_t        PAT_GenParPx[10];
   Double_t        PAT_GenParPy[10];
   Double_t        PAT_GenParPz[10];
   Double_t        PAT_GenParPt[10];
   Double_t        PAT_GenParEta[10];
   Double_t        PAT_GenParPhi[10];
   Double_t        PAT_GenParRap[10];
   Double_t        PAT_GenParCharge[10];
   Double_t        PAT_GenParId[10];
   Double_t        PAT_GenParStatus[10];
   Double_t        PAT_GenParMomId[10];
   Double_t        PAT_GenParMomStatus[10];


   Int_t           WTPATMuSize;
   Double_t        WTPATMuPx[11];
   Double_t        WTPATMuPy[11];
   Double_t        WTPATMuPz[11];
   Double_t        WTPATMuPt[11];
   Double_t        WTPATMuEta[11];
   Double_t        WTPATMuPhi[11];
   Double_t        WTPATMuCharge[11];
   
   Double_t        WTPATMu_found[11];
   Double_t        WTPATMu_nchi2In[11];
   Double_t        WTPATMu_arbitrated[11];
   Double_t        WTPATMu_stationTight[11];
   Double_t        WTPATMu_trackerLayers[11];
   Double_t        WTPATMu_pixeLayers[11];
   Double_t        WTPATMu_dxy[11];
   Double_t        WTPATMu_dz[11];
   
   Double_t        WTPATMu_nValidMuHits[11];
   Double_t        WTPATMu_nchi2Gl[11];
   Int_t           WTPATMu_Trigger1[11];
   Int_t           WTPATMu_Trigger2[11];
   Int_t           WTPATMu_Trigger3[11];
   Int_t           WTPATMu_Trigger4[11];
   Int_t           WTPATMu_Trigger5[11];
   Int_t           WTPATMu_Trigger6[11];
   Int_t           WTPATMu_Trigger7[11];
   Int_t           WTPATMu_Trigger8[11];
   Int_t           WTPATMu_Trigger9[11];
   Int_t           WTPATMu_Trigger10[11];
   Int_t           WTPATMu_Trigger11[11];
   Int_t           WTPATMu_Trigger12[11];
 


   Int_t           WTPATMu_global[11];
   Int_t           WTPATMu_tracker[11];
   Int_t           WTPATMu_sta[11];
   Int_t           WTPATMu_sta_noglb[11];




   // Set branch addresses.
   SingleWTPATMuonTree->SetBranchAddress("eventNbWTPAT",&eventNbWTPAT);
   SingleWTPATMuonTree->SetBranchAddress("runNbWTPAT",&runNbWTPAT);
   SingleWTPATMuonTree->SetBranchAddress("lumiBlockWTPAT",&lumiBlockWTPAT);
   SingleWTPATMuonTree->SetBranchAddress("rbinWTPAT",&rbinWTPAT);

   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexNbEv",&PAT_vertexNbEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexTrkEv",&PAT_vertexTrkEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_isVtxFakeEv",&PAT_isVtxFakeEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexXEv",&PAT_vertexXEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexYEv",&PAT_vertexYEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexZEv",&PAT_vertexZEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_vertexRhoEv",&PAT_vertexRhoEv);
   
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger1",&PAT_eventTrigger1);
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger2",&PAT_eventTrigger2);
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger3",&PAT_eventTrigger3);
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger4",&PAT_eventTrigger4);
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger5",&PAT_eventTrigger5);
   SingleWTPATMuonTree->SetBranchAddress("PAT_eventTrigger6",&PAT_eventTrigger6);




   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParNo",&PAT_GenParNo);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParSize",&PAT_GenParSize);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenvertexXEv",&PAT_GenvertexXEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenvertexYEv",&PAT_GenvertexYEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenvertexZEv",&PAT_GenvertexZEv);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParPx",PAT_GenParPx);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParPy",PAT_GenParPy);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParPz",PAT_GenParPz);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParPt",PAT_GenParPt);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParEta",PAT_GenParEta);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParPhi",PAT_GenParPhi);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParRap",PAT_GenParRap);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParCharge",PAT_GenParCharge);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParId",PAT_GenParId);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParStatus",PAT_GenParStatus);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParMomId",PAT_GenParMomId);
   SingleWTPATMuonTree->SetBranchAddress("PAT_GenParMomStatus",PAT_GenParMomStatus);


   SingleWTPATMuonTree->SetBranchAddress("WTPATMuSize",&WTPATMuSize);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuPx",WTPATMuPx);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuPy",WTPATMuPy);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuPz",WTPATMuPz);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuPt",WTPATMuPt);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuEta",WTPATMuEta);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuPhi",WTPATMuPhi);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMuCharge",WTPATMuCharge);
   
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_found",WTPATMu_found);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_nchi2In",WTPATMu_nchi2In);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_arbitrated",WTPATMu_arbitrated);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_stationTight",WTPATMu_stationTight);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_trackerLayers",WTPATMu_trackerLayers);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_pixeLayers",WTPATMu_pixeLayers);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_dxy",WTPATMu_dxy);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_dz",WTPATMu_dz);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_nValidMuHits",WTPATMu_nValidMuHits);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_nchi2Gl",WTPATMu_nchi2Gl);
   
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger1",WTPATMu_Trigger1);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger2",WTPATMu_Trigger2);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger3",WTPATMu_Trigger3);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger4",WTPATMu_Trigger4);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger5",WTPATMu_Trigger5);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger6",WTPATMu_Trigger6);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger7",WTPATMu_Trigger7);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger8",WTPATMu_Trigger8);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger9",WTPATMu_Trigger9);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger10",WTPATMu_Trigger10);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger11",WTPATMu_Trigger11);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_Trigger12",WTPATMu_Trigger12);
   
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_global",WTPATMu_global);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_tracker",WTPATMu_tracker);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_sta",WTPATMu_sta);
   SingleWTPATMuonTree->SetBranchAddress("WTPATMu_sta_noglb",WTPATMu_sta_noglb);

   




   TH1D *muPt = new TH1D("muPt","muPt", 100,0,10.0);
   muPt->Sumw2();
   
   TH1D *muEta = new TH1D("muEta","muEta", 100,-3.0,3.0);
   muEta->Sumw2();


   TH1D *muPtWT = new TH1D("muPtWT","muPtWT", 100,0,10.0);
   muPtWT->Sumw2();
   
   TH1D *muEtaWT = new TH1D("muEtaWT","muEtaWT", 100,-3.0,3.0);
   muEtaWT->Sumw2();
   

   TH1D *muPhi = new TH1D("muPhi","muPhi", 100,-5.0,5.0);
   muPhi->Sumw2();

   TH1D *muDxy = new TH1D("muDxy","muDxy", 100,-30.0,30.0);
   TH1D *muDz = new TH1D("muDz","muDz", 100,-50.0,50.0);



   TH1D *VtxX = new TH1D("VtxX","VtxX", 100,-30.0,30.0);
   TH1D *VtxY = new TH1D("VtxY","VtxY", 100,-30.0,30.0);
   TH1D *VtxZ = new TH1D("VtxZ","VtxZ", 100,-50.0,50.0);


   TH1D *genmuPt = new TH1D("genmuPt","genmuPt", 100,0,10.0);
   genmuPt->Sumw2();
   TH1D *genmuEta = new TH1D("genmuEta","genmuEta", 100,-3.0,3.0);
   genmuEta->Sumw2();
   TH1D *genmuPhi = new TH1D("genmuPhi","genmuPhi", 100,-5.0,5.0);
   genmuPhi->Sumw2();

   

   Long64_t nentries = SingleWTPATMuonTree->GetEntries();
   
   for (Long64_t i=0; i<nentries;i++) {
     
     SingleWTPATMuonTree->GetEntry(i);
     
     if(i%25000==0){cout<<i<<endl;}

     VtxX->Fill(PAT_vertexXEv); 
     VtxY->Fill(PAT_vertexYEv); 
     VtxZ->Fill(PAT_vertexZEv);

     
     if(PAT_GenParSize>0){
              
       for (Int_t genpar1=0; genpar1<PAT_GenParSize;genpar1++){
	 
	 int genmuIn=0;

	 if(TMath::Abs(PAT_GenParId[genpar1])==13 && IsAccept(PAT_GenParPt[genpar1], PAT_GenParEta[genpar1]) ){genmuIn=1;}else{genmuIn=0;}
	
	   if(TMath::Abs(PAT_GenParId[genpar1])==13 && genmuIn==1 ){
	 genmuPt->Fill(PAT_GenParPt[genpar1]);
	 genmuEta->Fill(PAT_GenParEta[genpar1]);
	 genmuPhi->Fill(PAT_GenParPhi[genpar1]);
	 }
       }
     
     }//gen par size >0


     if(WTPATMuSize>0){
       //cout<<" PAT mu size "<<WTPATMuSize<<endl;
       for (Int_t patmu1=0; patmu1<WTPATMuSize;patmu1++){
	 //cout<<"PatMu Eta: "<<WTPATMuEta[patmu1]<<endl;	 
	    
	 int patmuPass=0;
	 int patmuIn=0;

	 if( WTPATMu_trackerLayers[patmu1] > 5.0 && TMath::Abs(WTPATMu_nchi2In[patmu1]) < 1.8 && TMath::Abs(WTPATMu_dxy[patmu1]) < 3.0 
	     && TMath::Abs(WTPATMu_dz[patmu1]) < 30.0 &&  WTPATMu_arbitrated[patmu1]==1 && WTPATMu_stationTight[patmu1]==1 
	     && WTPATMu_tracker[patmu1]==1 ){patmuPass=1;}else{patmuPass=0;}	  

	 if(IsAccept(WTPATMuPt[patmu1],WTPATMuEta[patmu1])){patmuIn=1;}else{patmuIn=0;}


	 if( WTPATMu_tracker[patmu1]==1){
	   muDxy->Fill(WTPATMu_dxy[patmu1]);
	   muDz->Fill(WTPATMu_dz[patmu1]);
	 }
	 
	 //patmuPass=1;
	 //patmuIn=1;


	 if(patmuPass==1 && patmuIn==1 ){
	   
	   muPt->Fill(WTPATMuPt[patmu1]);
	   muEta->Fill(WTPATMuEta[patmu1]);
	   muPhi->Fill(WTPATMuPhi[patmu1]);
	   }


	 if(patmuPass==1 && patmuIn==1 && WTPATMu_Trigger1[patmu1]==1){

	 //if(patmuPass==1 && patmuIn==1 && PAT_eventTrigger4==1){
	   muPtWT->Fill(WTPATMuPt[patmu1]);
	   muEtaWT->Fill(WTPATMuEta[patmu1]);
	   
	 }

	 //for (Int_t patmu2=patmu1+1; patmu2<WTPATMuSize;patmu2++){ 
	 //}
       }
     }

   }
   
   //TEfficeincy *t=new TEfficiency(muEta,genmuEta);
   //new TCanvas;
   //t->Draw();

   //TH1D *hratio1 = (TH1D*)h2->Clone("ratio")
   
   

   new TCanvas;
   genmuPt->Draw();
   gPad->SaveAs("genmuPt.png");
   new TCanvas;
   genmuEta->Draw();
   gPad->SaveAs("genmuEta.png");
   new TCanvas;
   genmuPhi->Draw();
   gPad->SaveAs("genmuPhi.png");

   new TCanvas;
   muPt->Draw();
   gPad->SaveAs("muPt.png");

   new TCanvas;
   muEta->Draw();
   gPad->SaveAs("muEta.png");

   new TCanvas;
   muPhi->Draw();
   gPad->SaveAs("muPhi.png");





   new TCanvas;
   muPtWT->Draw();
   gPad->SaveAs("muPtWT.png");

   new TCanvas;
   muEtaWT->Draw();
   gPad->SaveAs("muEtaWT.png");

   



   TH1D* RatioEta=(TH1D*)muEta->Clone("ratioEta");
   RatioEta->Divide(genmuEta);
   new TCanvas;
   RatioEta->Draw("E");



   TH1D* RatioPt=(TH1D*)muPt->Clone("ratioPt");
   RatioPt->Divide(genmuPt);
   new TCanvas;
   RatioPt->Draw("E");



   TH1D* RatioEtaT=(TH1D*)muEtaWT->Clone("ratioEtaT");
   RatioEtaT->Divide(muEta);
   new TCanvas;
   RatioEtaT->Draw("E");



   TH1D* RatioPtT=(TH1D*)muPtWT->Clone("ratioPtT");
   RatioPtT->Divide(muPt);
   RatioPtT->GetYaxis()->SetRangeUser(0.0,1.0);
   new TCanvas;
   RatioPtT->Draw("E");
   /*
   TEfficiency* ptEff = 0;
   ptEff = new TEfficiency(*muPtWT,*muPt);
   new TCanvas;
   //ptEff->GetYaxis()->SetRangeUser(0.0,1.0);
   ptEff->Draw("AP");
   RatioPtT->Draw("same");

   TEfficiency* EtaEff = 0;
   EtaEff = new TEfficiency(*muEtaWT,*muEta);
   new TCanvas;
   EtaEff->Draw("AP");
   */

   //TGraphAsymmErrors::TGraphAsymmErrors(const TH1* pass,const TH1* total,Option_t* opt) 
   TGraphAsymmErrors *GrfTrgPt=0;

   GrfTrgPt=new TGraphAsymmErrors(muPtWT,muPt);
   GrfTrgPt->GetYaxis()->SetRangeUser(0.0,1.0);
   GrfTrgPt->GetXaxis()->SetTitle("#mu p_{T}");
   new TCanvas;
   GrfTrgPt->Draw("AP");
   gPad->SaveAs("TrgEffPt.png");
   
   TGraphAsymmErrors *GrfTrgEta=0;
   GrfTrgEta=new TGraphAsymmErrors(muEtaWT,muEta);
   GrfTrgEta->GetYaxis()->SetRangeUser(0.0,1.0);
   GrfTrgEta->GetXaxis()->SetTitle("#eta^{#mu}");
   new TCanvas;
   GrfTrgEta->Draw("AP");
   gPad->SaveAs("TrgEffEta.png");


   return;



   TCanvas *CanVtxX=new TCanvas;
   CanVtxX->SetLogy();
   VtxX->Draw();
   gPad->SaveAs("VtxX.png");


   TCanvas *CanVtxY=new TCanvas;
   CanVtxY->SetLogy();
   VtxY->Draw();
   gPad->SaveAs("VtxY.png");



   TCanvas *CanVtxZ=new TCanvas;
   CanVtxZ->SetLogy();
   VtxZ->Draw();
   gPad->SaveAs("VtxZ.png");



   TCanvas *CanDxy=new TCanvas;
   CanDxy->SetLogy();
   muDxy->Draw();
   gPad->SaveAs("muDxy.png");

   TCanvas *CanDz=new TCanvas;
   CanDz->SetLogy();
   muDz->Draw();
   gPad->SaveAs("muDz.png");





}