コード例 #1
0
int main(int argc, char** argv)
{
  // Set style options
  setTDRStyle();
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);
  gStyle->SetOptTitle(0); 
  gStyle->SetOptStat(1110); 
  gStyle->SetOptFit(1); 


  float totDAevts = 0;
  float DAevtsHIHI = 0;
  
  // Set fitting options
  TVirtualFitter::SetDefaultFitter("Fumili2");
  

  /// Fitting functions
  /////////////// vs R9                                                                              
  //    TF1* R9_low_2011 = new TF1("R9_low_2011", "[0] + [1]*x + [2]*pow(x,2) + [3]*pow(x,3)", 0.7, 0.94);
  //    R9_low_2011->SetParameters(1.04602750038440662e-01, -5.12399137504689572e-01, 7.26103911422236403e-01, -3.14041397400686317e-01);
  //    TF1* R9_hig_2011 = new TF1("R9_hig_2011", "[0] + [1]*x + [2]*pow(x,2) ", 0.94, 1.02);
  //    R9_hig_2011->SetParameters(-2.65392390285653734e+00, 5.32070916148806727e+00, -2.65288628795334347e+00);
  

  //    TF1* R9_low_2012 = new TF1("R9_low_2012", "[0] + [1]*x + [2]*pow(x,2) + [3]*pow(x,3)", 0.7, 0.94);
  //    R9_low_2012->SetParameters(6.53048217081556359e-01, -2.41796796111481704e+00, 2.90336305058258182e+00, -1.13417753246979647e+00);
  
  // //    TF1* R9_low_2012 = new TF1("R9_low_2012", "[0] ", 0., 0.94);
  // //    R9_low_2012->SetParameter(0, 3.05197369770460669e-03);
  
  //    TF1* R9_hig_2012 = new TF1("R9_hig_2012", "[0] + [1]*x + [2]*pow(x,2) ", 0.94, 1.02);
  //    R9_hig_2012->SetParameters(-2.30712976989725455e-01, 2.92312432577749526e-01, -4.51976365389429174e-02);
  
  // //   ////////////// vs Et
  TF1* Et_highR9_2011 = new TF1("Et_highR9_2011", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  Et_highR9_2011->SetParameters(1.59984924630326465e-02, 4.14188316002253587e-02, -6.49126732859059939e-03);
  TF1* Et_lowR9_2011 = new TF1("Et_lowR9_2011", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  Et_lowR9_2011->SetParameters(2.20638739628473586e-02, 6.98744642383235803e-02, -1.85601207959524978e-02);
  
  TF1* Et_highR9_2012 = new TF1("Et_highR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  Et_highR9_2012->SetParameters(1.76747992064786620e-02, 3.73408739026924591e-02, -7.82929065282905561e-03);
  TF1* Et_lowR9_2012 = new TF1("Et_lowR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  Et_lowR9_2012->SetParameters(1.97205016874162468e-02, 4.41133183909690751e-02, -1.58915655671104904e-02);


  //MC 52X
  //stimate senza PU
  //   TF1* Et_highR9_2012 = new TF1("Et_highR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  //   Et_highR9_2012->SetParameters(1.71373322900473177e-02, 1.55744254105185699e-02,  -2.11477940336727904e-03);
  //   TF1* Et_lowR9_2012 = new TF1("Et_lowR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  //   Et_lowR9_2012->SetParameters(2.63075655765558566e-02, 4.57322846169432515e-02, -2.09413281975727485e-02);

  //stimate con PU
  //   TF1* Et_highR9_2012 = new TF1("Et_highR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  //   Et_highR9_2012->SetParameters(1.71373322900473177e-02, 1.55744254105185699e-02,  -2.11477940336727904e-03);
  //   TF1* Et_lowR9_2012 = new TF1("Et_lowR9_2012", "[0] * (1 - exp(-[1] * x) ) +[2] ",0., 100.);
  //   Et_lowR9_2012->SetParameters(1.69896128648113487e-02, 1.20797862827948261e-02, -5.86630884749932049e-03);
  
  
  // Settings for corrections
  //  bool UsePhotonRegression = false;
  bool UsePhotonRegression = true;
  
  //     bool correctEt = true;
  bool correctEt = false;
  
  //  bool useShCorr = false;
  bool useShCorr = true;
  
  //-----------------
  // Input parameters
  
  std::cout << "\n*******************************************************************************************************************" << std::endl;
  std::cout << "arcg: " << argc << std::endl;
  char* EBEE = argv[1];
  char* LOWHIGH = argv[2];
  char* ENE = argv[3];
  int PU = atoi(argv[4]);
  int evtsPerPoint = atoi(argv[5]);
  std::string string_year = argv[6];
  int year = atoi(argv[6]);
  std::string doVsEach = argv[7];
  char* SortVariable = argv[8];

  std::cout << "EBEE:         " << EBEE         << std::endl;
  std::cout << "LOWHIGH:      " << LOWHIGH       << std::endl;
  std::cout << "ENE:          " << ENE           << std::endl;
  std::cout << "PU:           " << PU            << std::endl;
  std::cout << "evtsPerPoint: " << evtsPerPoint  << std::endl;
  std::cout << "year:      " << year       << std::endl;
  std::cout << "doVsEach:      " << doVsEach       << std::endl;
  std::cout << "SortVariable:      " << SortVariable       << std::endl;
  

  TPileupReweighting* puReweighting;

  //   //2012 prompt           
  if(year == 2012) puReweighting =
    new TPileupReweighting("/afs/cern.ch/work/a/amartell/public/weights/PUweights_DYJetsToLL_Summer12_53X_ShSkim_ABC_TrueNumInteractions.root","hweights");
  //     new TPileupReweighting("/afs/cern.ch/work/a/amartell/public/weights/PUweights_DYJetsToLL_Summer12_ABC_TrueNumInteractions.root","pileup");
  //     new TPileupReweighting("/afs/cern.ch/work/a/amartell/public/weights/PUweights_DYJetsToLL_Summer12_Prompt_TrueNumInteractions.root","hweights");
  
  //   //2011                                                                                                                                          
  if(year == 2011) puReweighting =
    new TPileupReweighting("/afs/cern.ch/work/a/amartell/public/weights/PUweights_2011_DYJetsToLL_Fall2011_TrueNumInteractions.root", "hweights");
  
  
  std::string R9MOD = std::string(LOWHIGH);
  std::string ENERGY = std::string(ENE);
  std::string SortV = std::string(SortVariable);

  //-------------------
  // Define in/outfiles
  std::string folderName;
  if(PU == 0)
  	folderName = std::string(EBEE)+"_"+std::string(LOWHIGH)+"_"+std::string(ENE)+"_noPU";
  if(PU == 1)
        folderName = std::string(EBEE)+"_"+std::string(LOWHIGH)+"_"+std::string(ENE);
  //if( strcmp(LOWHIGH,"")==0 ) folderName = std::string(EBEE);
  //if( strcmp(EBEE,"")==0 ) folderName = std::string(LOWHIGH);
  
  
  // Get trees
  std::cout << std::endl;
  std::string nameNtuples = "simpleNtupleEoverP/SimpleNtupleEoverP";
  std::string nameNtuplesMC = "simpleNtupleEoverP/SimpleNtupleEoverP";
  //   if(year == 2011) nameNtuples = "ntu";  
  //   if(year == 2011) nameNtuplesMC = "ntu";  
  
  //  if(year == 2012) nameNtuplesMC = "simpleNtupleEoverPSh/SimpleNtupleEoverP";
  TChain* ntu_MC = new TChain(nameNtuplesMC.c_str());
  TChain* ntu_DA = new TChain(nameNtuples.c_str());
  


  if(year == 2012){
    ntu_MC->Add("/tmp/amartell/DYToEE_M-20_CT10_TuneZ2star_v2_8TeV-powheg-pythia6_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM.root");
    ntu_MC->Add("/tmp/amartell/DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_2.root");
    ntu_DA->Add("/tmp/amartell/DoubleElectronAB_13Jul2012.root");
    ntu_DA->Add("/tmp/amartell/DoubleElectron_C_Prompt.root");

//     ntu_MC->Add("/tmp/amartell/WJetsToLNu_START53_V7A.root");
//     ntu_DA->Add("/tmp/amartell/Single_AB_Prompt.root");
//     ntu_DA->Add("/tmp/amartell/Single_C_Prompt.root");
  }

  if(year == 2011){                                                                                                                      
    ntu_DA->Add("/tmp/amartell/DoubleElectron-RUN2011AB.root");          
    ntu_MC->Add("/tmp/amartell/DYJetsToLL_Fall11_START44_V9B.root");
  }                                                                                                                                      

  std::cout << "     REFERENCE: " << std::setw(8) << ntu_MC->GetEntries() << " entries" << std::endl;
  std::cout << "     DATA: " << std::setw(8) << ntu_DA->GetEntries() << " entries" << std::endl;
  
  if(ntu_DA->GetEntries() == 0 || ntu_MC->GetEntries() == 0 )
  {
    std::cout << "Error: At least one file is empty" << std::endl; 
    return -1;
  }

  
  std::vector<int> run_DA, time_DA, Z_DA, PV_DA;
  std::vector<int> run_MC, time_MC, Z_MC, PV_MC;
  std::vector<float> scE_DA, scEt_reg_DA,scE_reg_DA, R9_DA, P_DA, EoP_DA, Et_DA, scEta_DA, elePhi_DA, ES_DA, isEB_DA, e3x3_DA,e5x5_DA, scERaw_DA;
  std::vector<float> scE_MC, scEt_reg_MC, scE_reg_MC, R9_MC, P_MC, EoP_MC, Et_MC, scEta_MC, elePhi_MC, ES_MC, isEB_MC, puRe, e3x3_MC, e5x5_MC, scERaw_MC;
  std::vector<float> cloneSortVar_DA;
  std::vector<float> cloneSortVar_MC;
  std::vector<float> scEtRaw_DA, scEt_3x3_DA, scEt_5x5_DA;
  std::vector<float> scEtRaw_MC, scEt_3x3_MC, scEt_5x5_MC;
  std::vector<float> ele1ele2_scM_DA, ele1ele2_scM_MC;
  std::vector<int> charge_DA, charge_MC;
  // Set branch addresses
  int isZ,runId,timeStamp,nVtx;
  float npu;
  
  ntu_DA->SetBranchStatus("*",0);
  ntu_DA->SetBranchStatus("runId",1);            ntu_DA->SetBranchAddress("runId", &runId);  
  ntu_DA->SetBranchStatus("timeStampHigh",1);    ntu_DA->SetBranchAddress("timeStampHigh", &timeStamp);  
  ntu_DA->SetBranchStatus("isZ",1);              ntu_DA->SetBranchAddress("isZ", &isZ);
  ntu_DA->SetBranchStatus("PV_n",1);             ntu_DA->SetBranchAddress("PV_n",&nVtx);

 
  ntu_MC->SetBranchStatus("*",0);
  ntu_MC->SetBranchStatus("PUit_TrueNumInteractions", 1);      ntu_MC->SetBranchAddress("PUit_TrueNumInteractions", &npu);
  ntu_MC->SetBranchStatus("runId",1);                          ntu_MC->SetBranchAddress("runId", &runId);  
  ntu_MC->SetBranchStatus("timeStampHigh",1);                  ntu_MC->SetBranchAddress("timeStampHigh", &timeStamp);  
  ntu_MC->SetBranchStatus("isZ",1);                            ntu_MC->SetBranchAddress("isZ", &isZ);
  ntu_MC->SetBranchStatus("PV_n",1);                           ntu_MC->SetBranchAddress("PV_n",&nVtx);


  
  // Electron data
  float scEne1, scEneReg1, EoP1, scEt1, scEta1, elePhi1, ES1, P1, scERaw1, e3x31, e5x51, ele1ele2_scM;
  float scEne2, scEneReg2, EoP2, scEt2, scEta2, elePhi2, ES2, P2, scERaw2, e3x32, e5x52;
  //  float R9_pho1, R9_pho2;
  int isEB1,isEB2;
  int ele1_charge, ele2_charge; 

  ntu_DA->SetBranchStatus("ele1_scE", 1);       ntu_DA->SetBranchAddress("ele1_scE", &scEne1);
  ntu_DA->SetBranchStatus("ele1_scEt", 1);      ntu_DA->SetBranchAddress("ele1_scEt", &scEt1);
  ntu_DA->SetBranchStatus("ele1_scEta", 1);     ntu_DA->SetBranchAddress("ele1_scEta", &scEta1);
  ntu_DA->SetBranchStatus("ele1ele2_scM", 1);   ntu_DA->SetBranchAddress("ele1ele2_scM", &ele1ele2_scM);
  if(!UsePhotonRegression)  {
    ntu_DA->SetBranchStatus("ele1_scE_regression", 1);      ntu_DA->SetBranchAddress("ele1_scE_regression", &scEneReg1);
    ntu_DA->SetBranchStatus("ele2_scE_regression",1);       ntu_DA->SetBranchAddress("ele2_scE_regression", &scEneReg2);
  }
  else {
    ntu_DA->SetBranchStatus("ele1_scE_regression_PhotonTuned", 1);    ntu_DA->SetBranchAddress("ele1_scE_regression_PhotonTuned", &scEneReg1);
    ntu_DA->SetBranchStatus("ele2_scE_regression_PhotonTuned",1);     ntu_DA->SetBranchAddress("ele2_scE_regression_PhotonTuned", &scEneReg2);
  }
  ntu_DA->SetBranchStatus("ele1_scERaw",1);      ntu_DA->SetBranchAddress("ele1_scERaw",&scERaw1);
  ntu_DA->SetBranchStatus("ele1_e3x3",1);        ntu_DA->SetBranchAddress("ele1_e3x3", &e3x31);
  ntu_DA->SetBranchStatus("ele1_e5x5",1);        ntu_DA->SetBranchAddress("ele1_e5x5", &e5x51);
  ntu_DA->SetBranchStatus("ele1_EOverP",1);      ntu_DA->SetBranchAddress("ele1_EOverP",&EoP1);
  ntu_DA->SetBranchStatus("ele1_isEB",1);        ntu_DA->SetBranchAddress("ele1_isEB",&isEB1);
  ntu_DA->SetBranchStatus("ele1_es", 1);         ntu_DA->SetBranchAddress("ele1_es", &ES1);
  ntu_DA->SetBranchStatus("ele1_tkP",1);         ntu_DA->SetBranchAddress("ele1_tkP", &P1);
  ntu_DA->SetBranchStatus("ele1_charge",1);      ntu_DA->SetBranchAddress("ele1_charge", &ele1_charge);
  ntu_DA->SetBranchStatus("ele2_scE", 1);        ntu_DA->SetBranchAddress("ele2_scE", &scEne2);
  ntu_DA->SetBranchStatus("ele2_scEta", 1);      ntu_DA->SetBranchAddress("ele2_scEta", &scEta2);
  ntu_DA->SetBranchStatus("ele2_scEt", 1);       ntu_DA->SetBranchAddress("ele2_scEt", &scEt2);
  ntu_DA->SetBranchStatus("ele2_e3x3",1);        ntu_DA->SetBranchAddress("ele2_e3x3", &e3x32);
  ntu_DA->SetBranchStatus("ele2_e5x5",1);        ntu_DA->SetBranchAddress("ele2_e5x5", &e5x52);

  ntu_DA->SetBranchStatus("ele2_scERaw",1);      ntu_DA->SetBranchAddress("ele2_scERaw",&scERaw2);
  ntu_DA->SetBranchStatus("ele2_EOverP",1);      ntu_DA->SetBranchAddress("ele2_EOverP",&EoP2);
  ntu_DA->SetBranchStatus("ele2_isEB",1);        ntu_DA->SetBranchAddress("ele2_isEB",&isEB2);
  ntu_DA->SetBranchStatus("ele2_es", 1);         ntu_DA->SetBranchAddress("ele2_es", &ES2);
  ntu_DA->SetBranchStatus("ele2_tkP",1);         ntu_DA->SetBranchAddress("ele2_tkP", &P2);
  ntu_DA->SetBranchStatus("ele2_charge",1);      ntu_DA->SetBranchAddress("ele2_charge", &ele2_charge);

  ntu_DA->SetBranchStatus("ele1_phi", 1);   ntu_DA->SetBranchAddress("ele1_phi", &elePhi1);
  ntu_DA->SetBranchStatus("ele2_phi", 1);   ntu_DA->SetBranchAddress("ele2_phi", &elePhi2);
  
  ///////////////////////
  ntu_MC->SetBranchStatus("ele1_scE", 1);       ntu_MC->SetBranchAddress("ele1_scE", &scEne1);
  ntu_MC->SetBranchStatus("ele1_scEt", 1);      ntu_MC->SetBranchAddress("ele1_scEt", &scEt1);
  ntu_MC->SetBranchStatus("ele1_scEta", 1);     ntu_MC->SetBranchAddress("ele1_scEta", &scEta1);
  ntu_MC->SetBranchStatus("ele1ele2_scM", 1);   ntu_MC->SetBranchAddress("ele1ele2_scM", &ele1ele2_scM);
  if(!UsePhotonRegression)  {
    ntu_MC->SetBranchStatus("ele1_scE_regression", 1);      ntu_MC->SetBranchAddress("ele1_scE_regression", &scEneReg1);
    ntu_MC->SetBranchStatus("ele2_scE_regression",1);       ntu_MC->SetBranchAddress("ele2_scE_regression", &scEneReg2);
  }
  else {
    ntu_MC->SetBranchStatus("ele1_scE_regression_PhotonTuned", 1);    ntu_MC->SetBranchAddress("ele1_scE_regression_PhotonTuned", &scEneReg1);
    ntu_MC->SetBranchStatus("ele2_scE_regression_PhotonTuned",1);     ntu_MC->SetBranchAddress("ele2_scE_regression_PhotonTuned", &scEneReg2);
  }
  ntu_MC->SetBranchStatus("ele1_scERaw",1);      ntu_MC->SetBranchAddress("ele1_scERaw",&scERaw1);
  ntu_MC->SetBranchStatus("ele1_e3x3",1);        ntu_MC->SetBranchAddress("ele1_e3x3", &e3x31);
  ntu_MC->SetBranchStatus("ele1_e5x5",1);        ntu_MC->SetBranchAddress("ele1_e5x5", &e5x51);
  ntu_MC->SetBranchStatus("ele1_EOverP",1);      ntu_MC->SetBranchAddress("ele1_EOverP",&EoP1);
  ntu_MC->SetBranchStatus("ele1_isEB",1);        ntu_MC->SetBranchAddress("ele1_isEB",&isEB1);
  ntu_MC->SetBranchStatus("ele1_es", 1);         ntu_MC->SetBranchAddress("ele1_es", &ES1);
  ntu_MC->SetBranchStatus("ele1_tkP",1);         ntu_MC->SetBranchAddress("ele1_tkP", &P1);
  ntu_MC->SetBranchStatus("ele1_charge",1);      ntu_MC->SetBranchAddress("ele1_charge", &ele1_charge);
  ntu_MC->SetBranchStatus("ele2_scE", 1);        ntu_MC->SetBranchAddress("ele2_scE", &scEne2);
  ntu_MC->SetBranchStatus("ele2_scEta", 1);      ntu_MC->SetBranchAddress("ele2_scEta", &scEta2);
  ntu_MC->SetBranchStatus("ele2_scEt", 1);       ntu_MC->SetBranchAddress("ele2_scEt", &scEt2);
  ntu_MC->SetBranchStatus("ele2_e3x3",1);        ntu_MC->SetBranchAddress("ele2_e3x3", &e3x32);
  ntu_MC->SetBranchStatus("ele2_e5x5",1);        ntu_MC->SetBranchAddress("ele2_e5x5", &e5x52);

  ntu_MC->SetBranchStatus("ele2_scERaw",1);      ntu_MC->SetBranchAddress("ele2_scERaw",&scERaw2);
  ntu_MC->SetBranchStatus("ele2_EOverP",1);      ntu_MC->SetBranchAddress("ele2_EOverP",&EoP2);
  ntu_MC->SetBranchStatus("ele2_isEB",1);        ntu_MC->SetBranchAddress("ele2_isEB",&isEB2);
  ntu_MC->SetBranchStatus("ele2_es", 1);         ntu_MC->SetBranchAddress("ele2_es", &ES2);
  ntu_MC->SetBranchStatus("ele2_tkP",1);         ntu_MC->SetBranchAddress("ele2_tkP", &P2);
  ntu_MC->SetBranchStatus("ele2_charge",1);      ntu_MC->SetBranchAddress("ele2_charge", &ele2_charge);

  ntu_MC->SetBranchStatus("ele1_phi", 1);   ntu_MC->SetBranchAddress("ele1_phi", &elePhi1);
  ntu_MC->SetBranchStatus("ele2_phi", 1);   ntu_MC->SetBranchAddress("ele2_phi", &elePhi2);
  //////////////////////
  
  for(int ientry = 0; ientry < ntu_DA -> GetEntries(); ientry++)
  {
  	if( (ientry%100000 == 0) ) std::cout << "reading DATA entry " << ientry << "\r" << std::flush;
        ntu_DA->GetEntry(ientry);  

	if(isZ == 0) continue;

	++totDAevts;
	if(e3x31/scERaw1 > 0.94 && e3x32/scERaw2 > 0.94) ++DAevtsHIHI;

        run_DA.push_back(runId);

	float corrEtR9_1 = 1.;
	float corrEtR9_2 = 1.;

	if(correctEt == true){
 	  if(year == 2012){
   	    if(e3x31/scERaw1 < 0.94 ) corrEtR9_1 = corrEtR9_1 / (1. + Et_lowR9_2012->Eval(scEneReg1));
   	    if(e3x32/scERaw2 < 0.94 ) corrEtR9_2 = corrEtR9_2 / (1. + Et_lowR9_2012->Eval(scEneReg2));
	    if(e3x31/scERaw1 >= 0.94 ) corrEtR9_1 = corrEtR9_1 / (1. + Et_highR9_2012->Eval(scEneReg1));
 	    if(e3x32/scERaw2 >= 0.94 ) corrEtR9_2 = corrEtR9_2 / (1. + Et_highR9_2012->Eval(scEneReg2));
 	  }
 	  if(year == 2011){
 	    if(e3x31/scERaw1 < 0.94 ) corrEtR9_1 = corrEtR9_1 / (1. + Et_lowR9_2011->Eval(scEneReg1));
   	    if(e3x32/scERaw2 < 0.94 ) corrEtR9_2 = corrEtR9_2 / (1. + Et_lowR9_2011->Eval(scEneReg2));
	    if(e3x31/scERaw1 >= 0.94 ) corrEtR9_1 = corrEtR9_1 / (1. + Et_highR9_2011->Eval(scEneReg1));
 	    if(e3x32/scERaw2 >= 0.94 ) corrEtR9_2 = corrEtR9_2 / (1. + Et_highR9_2011->Eval(scEneReg2));
 	  }
	}

	if(useShCorr == true){
	  corrEtR9_1 = corrEtR9_1 * GetShervingCorrections(scEta1, e3x31/scERaw1, runId);
	  corrEtR9_2 = corrEtR9_2 * GetShervingCorrections(scEta2, e3x32/scERaw2, runId);
	}

	charge_DA.push_back(ele1_charge);
	charge_DA.push_back(ele2_charge);

        time_DA.push_back(timeStamp);
        Z_DA.push_back(isZ);
	PV_DA.push_back(nVtx);    

	scE_DA.push_back(scEne1);
	scE_DA.push_back(scEne2);

	scE_reg_DA.push_back(scEneReg1*corrEtR9_1);
	scE_reg_DA.push_back(scEneReg2*corrEtR9_2);

	float Rt1 = sin(2*atan(exp(-scEta1)) );
	float Rt2 = sin(2*atan(exp(-scEta2)) );

	scEt_reg_DA.push_back(scEneReg1*Rt1*corrEtR9_1);
 	scEt_reg_DA.push_back(scEneReg2*Rt2*corrEtR9_2);

	R9_DA.push_back(e3x31/scERaw1);
	R9_DA.push_back(e3x32/scERaw2);

	P_DA.push_back(P1);
	P_DA.push_back(P2);
	EoP_DA.push_back(EoP1);
	EoP_DA.push_back(EoP2);
	Et_DA.push_back(scEt1);
        Et_DA.push_back(scEt2);
	scEta_DA.push_back(scEta1);
	scEta_DA.push_back(scEta2);
        ES_DA.push_back(ES1);
	ES_DA.push_back(ES2);
	isEB_DA.push_back(isEB1);
	isEB_DA.push_back(isEB2);

	e3x3_DA.push_back(e3x31);
	e3x3_DA.push_back(e3x32);
	e5x5_DA.push_back(e5x51);
	e5x5_DA.push_back(e5x52);

	scERaw_DA.push_back(scERaw1);
	scERaw_DA.push_back(scERaw2);
	scEtRaw_DA.push_back(scERaw1*scEt1/scEne1);
	scEtRaw_DA.push_back(scERaw2*scEt2/scEne2);

	scEt_3x3_DA.push_back(e3x31*scEt1/scEne1);
	scEt_3x3_DA.push_back(e3x32*scEt2/scEne2);
	scEt_5x5_DA.push_back(e5x51*scEt1/scEne1);
	scEt_5x5_DA.push_back(e5x52*scEt2/scEne2);

	if(SortV == "Et"){
	  cloneSortVar_DA.push_back(scEneReg1*Rt1*corrEtR9_1);
	  cloneSortVar_DA.push_back(scEneReg2*Rt2*corrEtR9_2);
	}
	if(SortV == "R9"){
	  cloneSortVar_DA.push_back(e3x31/scERaw1);
	  cloneSortVar_DA.push_back(e3x32/scERaw2);
       	}

  }
  
  std::cout << std::endl;
  float ww = 1.;
  for(int ientry = 0; ientry < ntu_MC -> GetEntries(); ientry++)
  {
  	if( (ientry%100000 == 0) ) std::cout << "reading MC entry " << ientry << "\r" << std::flush;
        ntu_MC->GetEntry(ientry);  
  
	if(isZ == 0) continue;
	//	if(nVtx > 20) continue;

	float R9_ele1 = e3x31/scERaw1;
	if(year == 2012 && isEB1 == 1) R9_ele1 = 0.0010 + 1.0045 * e3x31/scERaw1;
	if(year == 2012 && isEB1 == 0) R9_ele1 = -0.0007 + 1.0086 * e3x31/scERaw1;
	if(year == 2011) R9_ele1 = 1.0035 * e3x31/scERaw1;
	float R9_ele2 = e3x32/scERaw2;
	if(year == 2012 && isEB2 == 1) R9_ele2 = 0.0010 + 1.0045 * e3x32/scERaw2;
	if(year == 2012 && isEB2 == 0) R9_ele2 = -0.0007 + 1.0086 * e3x32/scERaw2;
	if(year == 2011) R9_ele2 = 1.0035 * e3x32/scERaw2;

	float energySmearing1 = gRandom->Gaus(1.,0.0075);
	float energySmearing2 = gRandom->Gaus(1.,0.0075);

	energySmearing1 = gRandom->Gaus(1., GetSmearings(scEta1, R9_ele1, year, isEB1));
	energySmearing2 = gRandom->Gaus(1., GetSmearings(scEta2, R9_ele2, year, isEB2));

	charge_MC.push_back(ele1_charge);
	charge_MC.push_back(ele2_charge);

	ww = puReweighting->GetWeight((int)npu);
        puRe.push_back(ww);

        run_MC.push_back(runId);
        time_MC.push_back(timeStamp);
        Z_MC.push_back(isZ);
	PV_MC.push_back(nVtx);    

	scE_MC.push_back(scEne1);
	scE_MC.push_back(scEne2);

	scE_reg_MC.push_back(scEneReg1 * energySmearing1);
	scE_reg_MC.push_back(scEneReg2 * energySmearing2);

	scEt_reg_MC.push_back(scEneReg1/scEne1*scEt1*energySmearing1);
 	scEt_reg_MC.push_back(scEneReg2/scEne2*scEt2*energySmearing2);

	P_MC.push_back(P1);
	P_MC.push_back(P2);
	EoP_MC.push_back(EoP1);
	EoP_MC.push_back(EoP2);

	Et_MC.push_back(scEt1);
        Et_MC.push_back(scEt2);
	scEta_MC.push_back(scEta1);
	scEta_MC.push_back(scEta2);
        ES_MC.push_back(ES1);
	ES_MC.push_back(ES2);
	isEB_MC.push_back(isEB1);
	isEB_MC.push_back(isEB2);

	e5x5_MC.push_back(e5x51);
	e5x5_MC.push_back(e5x52);

	scERaw_MC.push_back(scERaw1);
	scERaw_MC.push_back(scERaw2);

	R9_MC.push_back(R9_ele1);
	R9_MC.push_back(R9_ele2);

	scEtRaw_MC.push_back(scERaw1*scEt1/scEne1);
	scEtRaw_MC.push_back(scERaw2*scEt2/scEne2);

	scEt_5x5_MC.push_back(e5x51*scEt1/scEne1);
	scEt_5x5_MC.push_back(e5x52*scEt2/scEne2);

 	e3x3_MC.push_back(R9_ele1*scERaw1);
 	e3x3_MC.push_back(R9_ele2*scERaw2);
 	scEt_3x3_MC.push_back(R9_ele1*scERaw1*scEt1/scEne1);
 	scEt_3x3_MC.push_back(R9_ele2*scERaw2*scEt2/scEne2);

	if(SortV == "Et"){
	  cloneSortVar_MC.push_back(scEneReg1/scEne1*scEt1*energySmearing1);
	  cloneSortVar_MC.push_back(scEneReg2/scEne2*scEt2*energySmearing2);
	}
	if(SortV == "R9"){
	  cloneSortVar_MC.push_back(R9_ele1);
	  cloneSortVar_MC.push_back(R9_ele2);
       	}
  }
  

//   std::cout << "   totDAevts = " << totDAevts << std::endl;
//   std::cout << "   DAevtsHIHI = " << DAevtsHIHI << std::endl;
  //  return 200;


  // Loop and sort events
  std::cout << std::endl;
  std::cout << "***** Sort events and define bins *****" << std::endl;
  
  int nEntries = cloneSortVar_DA.size();
  int nSavePts = 0;
  std::vector<bool> isSavedEntries(nEntries);
  std::vector<SorterLC> sortedEntries;
  
  for(int ientry = 0; ientry < nEntries; ++ientry)
  {
    isSavedEntries.at(ientry) = false;

    // save only what is needed for the analysis!!!
    if(strcmp(EBEE,"EE")==0 && (fabs(scEta_DA.at(ientry)) < 1.566 || fabs(scEta_DA.at(ientry)) > 2.5 )) continue; 
    if(strcmp(EBEE,"EB")==0 && (fabs(scEta_DA.at(ientry)) > 1.4442 )) continue;

    if(std::string(EBEE) == "BC" && (fabs(scEta_DA.at(ientry)) > 1. )) continue;
    if(std::string(EBEE) == "B4" && (fabs(scEta_DA.at(ientry)) < 1. || fabs(scEta_DA.at(ientry)) > 1.4442)) continue;
    if(std::string(EBEE) == "EL" && (fabs(scEta_DA.at(ientry)) < 1.566 || fabs(scEta_DA.at(ientry)) > 2. )) continue;
    if(std::string(EBEE) == "EH" && (fabs(scEta_DA.at(ientry)) > 2.5 )) continue;

    //    if(R9_DA.at(ientry) < 0.7) continue;
    if(scEt_reg_DA.at(ientry) <  25.) continue;

    //to be fixed -> categories as in Hgg
    if(std::string(LOWHIGH) == "LOW" && R9_DA.at(ientry) >= 0.94) continue;
    if(std::string(LOWHIGH) == "HIGH" && R9_DA.at(ientry) < 0.94 ) continue;

    isSavedEntries.at(ientry) = true;

    SorterLC dummy;
    dummy.laserCorr = cloneSortVar_DA.at(ientry);
    dummy.entry = ientry;
    sortedEntries.push_back(dummy);
    nSavePts++;   
  }

  std::cout << " Effective entries = " << nSavePts << std::endl;
  std::cout << " Effective entries sortedEntries.size()= " << sortedEntries.size() << std::endl;

  std::sort(sortedEntries.begin(),sortedEntries.end(),SorterLC());
  std::cout << "Sorting variable vs " << SortV << std::endl;
  std::cout << "DATA sorted in " << EBEE << " - " << nSavePts << " events" << std::endl;
  
  
   std::map<int,int> antiMap;
   for(unsigned int iSaved = 0; iSaved < sortedEntries.size(); ++iSaved)
   antiMap[sortedEntries.at(iSaved).entry] = iSaved; 
   
  // bins with evtsPerPoint events per bin
   std::cout << " nSavePts = " << nSavePts << std::endl;
   std::cout << " evtsPerPoint = " << evtsPerPoint << std::endl;
  int nBins = std::max(1, int(nSavePts/evtsPerPoint));
   std::cout << " nBins = " << nBins << std::endl;
  int nBinPts = int( nSavePts/nBins );
   std::cout << " nBinPts = " << nBinPts << std::endl;
  int nBinTempPts = 0;

  std::cout << "nBins = " << nBins << std::endl;
  
  std::vector<int> binEntryMax;
  binEntryMax.push_back(0);
  for(int iSaved = 0; iSaved < nSavePts; ++iSaved)
  {
    ++nBinTempPts;
    
    if( nBinTempPts == nBinPts )
    {
      binEntryMax.push_back( iSaved );      
      nBinTempPts = 0;
    }
  }
  binEntryMax.at(nBins) = nSavePts;
  
  std::cout << " fine : nBins = " << nBins << std::endl;
 
  TVirtualFitter::SetDefaultFitter("Fumili2");
  
  // histogram definition
  
  TH1F** h_EoP_DA = new TH1F*[nBins];
  TH1F** h_EoP_MC = new TH1F*[nBins];
  TH1F** h_SortV = new TH1F*[nBins];
  TH1F** h_SortV_MC = new TH1F*[nBins];

  TH1F* h_Et_allDA = new TH1F("h_Et_allDA", "", 5000, 0., 1000.);
  TH1F* h_Et_allMC = new TH1F("h_Et_allMC", "", 5000, 0., 1000.);
  TH1F* h_R9_allDA = new TH1F("h_R9_allDA", "", 2200, 0., 1.1);
  TH1F* h_R9_allMC = new TH1F("h_R9_allMC", "", 2200, 0., 1.1);

  TH1F* h_scE_DA = new TH1F("h_scE_DA", "", 4000, 0., 200.);
  TH1F* h_scReg_DA = new TH1F("h_scReg_DA", "", 4000, 0., 200.);
  TH1F* h_scRaw_DA = new TH1F("h_scRaw_DA", "", 4000, 0., 200.);

  TH1F* h_Vtx_DA = new TH1F("h_Vtx_DA", "", 200, 0., 200.);
  TH1F* h_Vtx_MC = new TH1F("h_Vtx_MC", "", 200, 0., 200.);

  TH2F* h_R9_vsET_MC = new TH2F("h_R9_vsET_MC", "", 200, 0., 200., 220, 0., 1.1);
  TH2F* h_R9_vsET_DA = new TH2F("h_R9_vsET_DA", "", 200, 0., 200., 220, 0., 1.1);
  TProfile* p_R9_vsET_MC = new TProfile("p_R9_vsET_MC", "", 200, 0., 200.);
  TProfile* p_R9_vsET_DA = new TProfile("p_R9_vsET_DA", "", 200, 0., 200.);


  h_Et_allDA->Sumw2();
  h_Et_allMC->Sumw2();
  h_R9_allDA->Sumw2();
  h_R9_allMC->Sumw2();
  h_scE_DA->Sumw2();
  h_scReg_DA->Sumw2();
  h_scRaw_DA->Sumw2();
  h_Vtx_DA->Sumw2();
  h_Vtx_MC->Sumw2();
  h_R9_vsET_MC->Sumw2();
  h_R9_vsET_DA->Sumw2();
  p_R9_vsET_MC->Sumw2();
  p_R9_vsET_DA->Sumw2();

  h_Et_allDA->SetLineColor(kRed+2);
  h_R9_allDA->SetLineColor(kRed+2);
  h_Vtx_DA->SetLineColor(kRed+2);

  h_Et_allMC->SetLineColor(kGreen+2);
  h_R9_allMC->SetLineColor(kGreen+2);
  h_Vtx_MC->SetLineColor(kGreen+2);

  std::vector<float> EtBinEdge;
  EtBinEdge.clear();
  std::vector<float> xNorm_single;


  for(int i = 0; i < nBins; ++i)
  {
    char histoName[80];    
      sprintf(histoName, "EoP_DA_%d", i);
      if(SortV == "Et") h_EoP_DA[i] = new TH1F(histoName, histoName, 600, 0., 3.);
      if(SortV == "R9") h_EoP_DA[i] = new TH1F(histoName, histoName, 400, 0., 2.);
      h_EoP_DA[i]->SetFillColor(kRed+2);
      h_EoP_DA[i]->SetFillStyle(3004);
      h_EoP_DA[i]->SetMarkerStyle(7);
      h_EoP_DA[i]->SetMarkerColor(kRed+2); 
      h_EoP_DA[i]->SetLineColor(kRed+2); 
    
      sprintf(histoName, "EoP_MC_%d", i);
      if(SortV == "Et") h_EoP_MC[i] = new TH1F(histoName, histoName, 600, 0., 3.);
      if(SortV == "R9") h_EoP_MC[i] = new TH1F(histoName, histoName, 400, 0., 2.);
      h_EoP_MC[i] -> SetFillColor(kGreen+2);
      h_EoP_MC[i] -> SetFillStyle(3004);
      h_EoP_MC[i] -> SetMarkerStyle(7);
      h_EoP_MC[i] -> SetMarkerColor(kGreen+2);
      h_EoP_MC[i] -> SetLineColor(kGreen+2);
      
      sprintf(histoName, (SortV+"_%d").c_str(), i);
      if(SortV == "Et") h_SortV[i] = new TH1F(histoName, histoName, 5000, 0., 1000.);
      if(SortV == "R9") h_SortV[i] = new TH1F(histoName, histoName, 2200, 0, 1.1);
      h_SortV[i]->SetLineColor(kRed+2);

      sprintf(histoName, (SortV+"_MC_%d").c_str(), i);
      if(SortV == "Et") h_SortV_MC[i] = new TH1F(histoName, histoName, 5000, 0., 1000.);
      if(SortV == "R9") h_SortV_MC[i] = new TH1F(histoName, histoName, 2200, 0, 1.1);
      h_SortV_MC[i]->SetLineColor(kGreen+2);
      
      h_EoP_DA[i]->Sumw2();
      h_EoP_MC[i]->Sumw2();
      h_SortV[i]->Sumw2();
      h_SortV_MC[i]->Sumw2();
  }

  std::cout << " cloneSortVar_DA.size() = " << cloneSortVar_DA.size() << std::endl;
  std::cout << " cloneSortVar_MC.size() = " << cloneSortVar_MC.size() << std::endl;


  std::vector<float> x;
  std::vector<float> ex;
  std::vector<float> y;
  std::vector<float> ey;
  
  TGraphErrors* finalGraph = new TGraphErrors();
  
  // function definition
  TF1** f_EoP = new TF1*[nBins];
 
  // loop on the saved and sorted events
  std::cout << std::endl;
  std::cout << "***** Fill and fit histograms *****" << std::endl;

  int DAEntries = cloneSortVar_DA.size();
  for(unsigned int ientry = 0; ientry < DAEntries; ++ientry){
    if( (ientry%100000 == 0) ) std::cout << "reading entry " << ientry << std::endl;
      
    if( isSavedEntries.at(ientry) == false) continue;
     
    int iSaved = antiMap[ientry];
    int bin = -1;
    
    for(bin = 0; bin < nBins; ++bin)
      if( iSaved >= binEntryMax.at(bin) && iSaved < binEntryMax.at(bin+1) )
	break;

    h_EoP_DA[bin]->Fill((scE_reg_DA.at(ientry)-ES_DA.at(ientry))/(P_DA.at(ientry)-ES_DA.at(ientry)));
    h_SortV[bin]->Fill(cloneSortVar_DA.at(ientry) );
    h_Et_allDA->Fill(scEt_reg_DA.at(ientry) );
    h_R9_allDA->Fill(R9_DA.at(ientry));
    
    h_Vtx_DA->Fill(PV_DA.at(int(ientry/2)) );
    h_scE_DA->Fill(scE_DA.at(ientry) );
    h_scReg_DA->Fill(scE_reg_DA.at(ientry));
    h_scRaw_DA->Fill(scERaw_DA.at(ientry));

    h_R9_vsET_DA->Fill(scEt_reg_DA.at(ientry), R9_DA.at(ientry));
    p_R9_vsET_DA->Fill(scEt_reg_DA.at(ientry), R9_DA.at(ientry));
  }
  
  std::cout << " dati fillati " << std::endl;
  std::cout << std::endl;
  
  for(int bin = 0; bin < nBins; bin++)
  {
    std::cout << "h_SortV[bin]->GetEntries() =  " << h_SortV[bin]->GetEntries() << std::endl;
    std::cout << "h_EoP_DA[bin]->GetEntries() =  " << h_EoP_DA[bin]->GetEntries() << std::endl;
    for(int i = 1; i < h_SortV[bin]->GetNbinsX()+1; i++)
      {
	if(h_SortV[bin]->GetBinContent(i) > 0) {
	  EtBinEdge.push_back(h_SortV[bin]->GetBinCenter(i)-h_SortV[bin]->GetBinWidth(i) );
	  break;
	}  
      }
  }

  int MCEntries = cloneSortVar_MC.size();
  for(unsigned int ientry = 0; ientry < MCEntries; ++ientry)
    {    
      if( (ientry%100000 == 0) ) std::cout << "reading entry " << ientry << std::endl;
      
      if (strcmp(EBEE,"EE")==0 && (fabs(scEta_MC.at(ientry)) < 1.566 || fabs(scEta_MC.at(ientry)) > 2.5 )) continue; 
      if (strcmp(EBEE,"EB")==0 && (fabs(scEta_MC.at(ientry)) > 1.4442 )) continue;
      
      //      if(R9_MC.at(ientry) < 0.7) continue;
      if(scEt_reg_MC.at(ientry) < 25.) continue;

      //to be fixed -> categories as in Hgg 
      if(std::string(EBEE) == "BC" && (fabs(scEta_MC.at(ientry)) > 1. )) continue; 
      if(std::string(EBEE) == "B4" && (fabs(scEta_MC.at(ientry)) < 1. || fabs(scEta_MC.at(ientry)) > 1.4442)) continue; 
      if(std::string(EBEE) == "EL" && (fabs(scEta_MC.at(ientry)) < 1.566 || fabs(scEta_MC.at(ientry)) > 2. )) continue; 
      if(std::string(EBEE) == "EH" && (fabs(scEta_MC.at(ientry)) > 2.5 )) continue; 
                                                                                                     
      if(std::string(LOWHIGH) == "LOW" && R9_MC.at(ientry) >= 0.94 ) continue;
      if(std::string(LOWHIGH) == "HIGH" && R9_MC.at(ientry) < 0.94 ) continue;

      for(unsigned int bin = 0; bin < EtBinEdge.size(); ++bin){
	if( (bin != EtBinEdge.size()-1 && cloneSortVar_MC.at(ientry) > EtBinEdge.at(bin) && cloneSortVar_MC.at(ientry) < EtBinEdge.at(bin+1)) ||
	    (bin == EtBinEdge.size()-1 && cloneSortVar_MC.at(ientry) > EtBinEdge.at(bin) ) ){
	  if(PU == 0)
	    {
	      h_EoP_MC[(int)bin]->Fill((scE_reg_MC.at(ientry)-ES_MC.at(ientry))/(P_MC.at(ientry)-ES_MC.at(ientry)));
	      h_SortV_MC[int(bin)]->Fill(cloneSortVar_MC.at(ientry));
	      break;
	  }
          if(PU == 1)
	    {
	      h_EoP_MC[(int)bin]->Fill((scE_reg_MC.at(ientry)-ES_MC.at(ientry))/(P_MC.at(ientry)-ES_MC.at(ientry)), puRe.at(int(ientry/2)));
	      h_SortV_MC[int(bin)]->Fill(cloneSortVar_MC.at(ientry), puRe.at(int(ientry/2)) );
	      break;
	    }
	} // get the ok bin
	}//loop over bins
      if(PU == 0){
    	h_Et_allMC->Fill(scEt_reg_MC.at(ientry));
	h_R9_allMC->Fill(R9_MC.at(ientry));
	h_Vtx_MC->Fill(PV_MC.at(int(ientry/2)));
    }
    if(PU == 1){
      h_Et_allMC->Fill(scEt_reg_MC.at(ientry), puRe.at(int(ientry/2)));
      h_R9_allMC->Fill(R9_MC.at(ientry), puRe.at(int(ientry/2)));
      h_Vtx_MC->Fill(PV_MC.at(int(ientry/2)), puRe.at(int(ientry/2)) );
      h_R9_vsET_MC->Fill(scEt_reg_MC.at(ientry), R9_MC.at(ientry), puRe.at(int(ientry/2)));
      p_R9_vsET_MC->Fill(scEt_reg_MC.at(ientry), R9_MC.at(ientry), puRe.at(int(ientry/2)));
    }
    }

  std::cout << " fino a qui ci sono = MC fillati" <<  std::endl;

  for(int i = 0; i < nBins; ++i){
    //------------------------------------
    // Fill the graph for uncorrected data
    // define the fitting function
    // N.B. [0] * ( [1] * f( [1]*(x-[2]) ) )


    float xNorm = h_EoP_DA[i]->Integral()/h_EoP_MC[i]->Integral(); //  * h_EoP_DA[i]->GetBinWidth(1)/h_EoP_MC[i]->GetBinWidth(1);  
    h_EoP_MC[i]->Scale(xNorm);

    float xNorm_all = h_Et_allDA->Integral()/h_Et_allMC->Integral(); //* h_Et_allDA->GetBinWidth()/h_Et_allMC->GetBinWidth();
    if(SortV == "Et") xNorm_all = h_R9_allDA->Integral()/h_R9_allMC->Integral();
    h_Et_allMC->Scale(xNorm_all);
    h_R9_allMC->Scale(xNorm_all);
    h_Vtx_MC->Scale(xNorm_all);

    float xNormSV = h_SortV[i]->Integral()/h_SortV_MC[i]->Integral(); //*h_Et[i]->GetBinWidth()/h_Et_MC[i]->GetBinWidth();  
    h_SortV_MC[i]->Scale(xNormSV);
    
//     std::cout << " i = " << i << " h_EoP_DA[i]->Integral() = " << h_EoP_DA[i]->Integral() << std::endl;
//     std::cout << " i = " << i << " h_Et[i]->Integral() = " << h_Et[i]->Integral() << std::endl;
//     std::cout << " i = " << i << " h_EoP_MC[i]->Integral() = " << h_EoP_MC[i]->Integral() << std::endl;
//     std::cout << " i = " << i << " h_Et_MC[i]->Integral() = " << h_Et_MC[i]->Integral() << std::endl;

//     std::cout << " i = " << i << " h_EoP_DA[i]->Integral() = " << h_EoP_DA[i]->Integral() << std::endl;
//     std::cout << " i = " << i << " h_EoP_MC[i]->Integral() = " << h_EoP_MC[i]->Integral() << std::endl;
//     std::cout << " xNorm = " << xNorm << std::endl;
//     std::cout << " xNormEt = " << xNormEt << std::endl;


    //    h_EoP_MC[i]->Smooth(2);

    //    if(reweightZtoH  == false && reweightEta == false && reweightR9 == false){                              

    if(SortV == "Et"){
    if(std::string(EBEE) == "EB" && year == 2011 && std::string(LOWHIGH) == "HIGH"){h_EoP_MC[i]->Smooth(1); }
    if(std::string(EBEE) == "EB" && year == 2011 && std::string(LOWHIGH) == "LOW"){ h_EoP_MC[i]->Smooth(1); }
    if(std::string(EBEE) == "EB" && year == 2012 && std::string(LOWHIGH) == "HIGH"){ h_EoP_MC[i]->Smooth(2); }
    if(std::string(EBEE) == "EB" && year == 2012 && std::string(LOWHIGH) == "LOW"){h_EoP_MC[i]->Smooth(2); }

    if(std::string(EBEE) == "EE" && year == 2011 && std::string(LOWHIGH) == "HIGH")
      { h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2011 && std::string(LOWHIGH) == "LOW")
      { h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2012 && std::string(LOWHIGH) == "HIGH")
      {   h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2012 && std::string(LOWHIGH) == "LOW")
      {h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    }
                                                                                                     
    if(SortV == "R9"){
     //    h_EoP_DA[i]->Rebin(2); h_EoP_MC[i]->Rebin(2);                                                              
    //    if(reweightZtoH  == false && reweightEta == false && reweightR9 == false){                                 
    if(std::string(EBEE) == "EB" && year == 2011 && std::string(LOWHIGH) == "HIGH"){h_EoP_MC[i]->Smooth(1); }
    if(std::string(EBEE) == "EB" && year == 2011 && std::string(LOWHIGH) == "LOW"){ h_EoP_MC[i]->Smooth(1); }
    if(std::string(EBEE) == "EB" && year == 2012 && std::string(LOWHIGH) == "HIGH"){ h_EoP_MC[i]->Smooth(2); }
    if(std::string(EBEE) == "EB" && year == 2012 && std::string(LOWHIGH) == "LOW"){h_EoP_MC[i]->Smooth(2); }
    if(std::string(EBEE) == "EE" && year == 2011 && std::string(LOWHIGH) == "HIGH")
      { h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2011 && std::string(LOWHIGH) == "LOW")
      { h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2012 && std::string(LOWHIGH) == "HIGH")
      {   h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    if(std::string(EBEE) == "EE" && year == 2012 && std::string(LOWHIGH) == "LOW")
      {h_EoP_MC[i]->Smooth(150); h_EoP_DA[i]->Rebin(4); h_EoP_MC[i]->Rebin(4);}
    }                                                                                                        


    histoFunc* templateHistoFunc = new histoFunc(h_EoP_MC[i]);
    char funcName[50];
    sprintf(funcName,"f_EoP_%d",i);
    f_EoP[i] = new TF1(funcName, templateHistoFunc, 0.7, 1.3, 3, "histoFunc");
    if(std::string(EBEE) == "EE") f_EoP[i] = new TF1(funcName, templateHistoFunc, 0.5, 2.5, 3, "histoFunc");
    if(std::string(EBEE) == "EE" && SortV == "R9")  f_EoP[i] = new TF1(funcName, templateHistoFunc, 0.7, 1.4, 3, "histoFunc");    

    f_EoP[i]->SetParName(0,"Norm"); 
    f_EoP[i]->SetParName(1,"Scale factor"); 
    f_EoP[i]->SetLineWidth(1); 
    f_EoP[i]->SetNpx(10000);
    
    xNorm = 1.;    
    f_EoP[i]->FixParameter(0, xNorm);
    //    f_EoP[i] -> SetParameter(1, gRandom->Gaus(1.,0.005));
    f_EoP[i]->SetParameter(1, 0.99);
    f_EoP[i]->FixParameter(2, 0.);
    f_EoP[i]->SetLineColor(kRed+2); 

    TFitResultPtr rp = h_EoP_DA[i]->Fit(funcName, "QERLS+");
    int fStatus = rp;
    int nTrials = 0;
    while( (fStatus != 0) && (nTrials < 100) )
    {
      rp = h_EoP_DA[i]->Fit(funcName, "QERLS+");
      fStatus = rp;
      if(fStatus == 0) break;
      ++nTrials;
    }

    double eee = f_EoP[i]->GetParError(1); 
    double k = 1./f_EoP[i]->GetParameter(1);


    // Fill the graph      
    if (fStatus == 0 && eee*k > 0.1*h_EoP_DA[i]->GetRMS()/sqrt(evtsPerPoint))
    {
      x.push_back(h_SortV[i]->GetMean());
      ex.push_back((h_SortV[i]->GetRMS())/sqrt(h_SortV[i]->GetEntries()));
      y.push_back(k-1);
      ey.push_back(eee * k * k);
    }
    else
    std::cout << "Fitting uncorrected Et bin: " << i << "   Fail status: " << fStatus << "   sigma: " << eee << std::endl;

  }
   
  for(unsigned int i = 0; i < x.size(); ++i)
  {
    finalGraph->SetPoint(i,  x.at(i) , y.at(i));
    finalGraph->SetPointError(i, ex.at(i), ey.at(i));
  }

  if(year == 2012) finalGraph->SetMarkerColor(kBlue);
  if(year == 2011) finalGraph->SetMarkerColor(kCyan);
  if(strcmp(LOWHIGH,"HIGH")==0 )  finalGraph->GetYaxis()->SetRangeUser(-0.004, 0.014);
  if(strcmp(LOWHIGH,"LOW")==0 )  finalGraph->GetYaxis()->SetRangeUser(-0.03, 0.03);
  finalGraph->GetYaxis()->SetTitle("E/p_{data} - E/p_{mc}");
  finalGraph->GetXaxis()->SetRangeUser(0., 130.);
  finalGraph->GetXaxis()->SetTitle(SortV.c_str());

//   std::cout << "   totDAevts = " << totDAevts << std::endl;
//   std::cout << "   DAevtsHIHI = " << DAevtsHIHI << std::endl;

  std::string plotFolderName = "PLOTS_vs"+SortV;
  if(doVsEach == "true") plotFolderName = "PLOTS_true"; 

  TFile pippo((plotFolderName+"/results_"+folderName+"_"+string_year+".root").c_str(),"recreate");
  finalGraph->Write("finalGraph");
  h_Et_allMC->Write();
  h_Et_allDA->Write();
  h_R9_allMC->Write();
  h_R9_allDA->Write();
  h_R9_vsET_MC->Write();
  h_R9_vsET_DA->Write();
  p_R9_vsET_MC->Write();
  p_R9_vsET_DA->Write();

  h_Vtx_DA->Write();
  h_Vtx_MC->Write();
  h_scE_DA->Write();
  h_scReg_DA->Write();
  h_scRaw_DA->Write();

  for(int i = 0; i < nBins; ++i){
    h_EoP_DA[i]->Write();
    h_EoP_MC[i]->Write();
    h_SortV[i]->Write();
    h_SortV_MC[i]->Write();
  }
  pippo.Close();
  
//   /*
//   // Drawings
//   TPaveStats** s_EoP = new TPaveStats*[nBins];
  
//   TCanvas *c1[100]; 
//   for(int i = 0; i < nBins; ++i)
//   {    
//     char canvasName[50];
//     sprintf(canvasName, "Fits-%0d", i); 
//     c1[i] = new TCanvas(canvasName, canvasName);
//     c1[i]->cd();
//     h_EoP_DA[i] -> GetXaxis() -> SetTitle("E/p");  
//     h_EoP_DA[i] -> GetYaxis() -> SetRangeUser(0., std::max(h_EoP_DA[i]->GetMaximum(), h_EoP_MC[i]->GetMaximum()) + 10.); 
//     h_EoP_DA[i] -> GetXaxis() -> SetRangeUser(0.5,1.5); 
//     //    h_EoP_DA[i] -> Draw("e");
//     h_EoP_DA[i] -> Draw();
//     gPad->Update();
//     s_EoP[i]= (TPaveStats*)(h_EoP_DA[i]->GetListOfFunctions()->FindObject("stats"));
//     s_EoP[i]->SetTextColor(kRed+2);
//     f_EoP[i]->Draw("same");
    
//     h_EoP_MC[i] -> Draw("same");
    
//     char Name[100];
//     if(PU == 0)      sprintf(Name, (plotFolderName+"/"+folderName+"/noPU_fit_%d_"+string_year+".png").c_str(),i);
//     if(PU == 1)      sprintf(Name, (plotFolderName+"/"+folderName+"/fit_%d_"+string_year+".png").c_str(),i);
//     c1[i] -> Print(Name,".png");     
//   }
  
//   TCanvas *c2[100]; 
//   for(int i = 0; i < nBins; ++i)
//   {
//     char canvasName[50];
//     sprintf(canvasName, "Et_DA-%0d", i); 
//     c2[i] = new TCanvas(canvasName, canvasName);
//     c2[i]->cd();

//     h_Et[i]->GetXaxis() -> SetTitle("Et");
//     h_Et[i]->GetYaxis()->SetRangeUser(0, std::max(h_Et[i]->GetMaximum(), h_Et_MC[i]->GetMaximum()) + 10. ); 
//     if(i<nBins-1) h_Et[i]->GetXaxis()->SetRangeUser(EtBinEdge.at(i), EtBinEdge.at(i+1)); 
//     else h_Et[i]->GetXaxis()->SetRangeUser(EtBinEdge.at(i), 150.); 
//     //    h_Et[i] -> Draw("e");
//     h_Et[i]->Draw();
//     h_Et_MC[i]->Draw("same");
//     /*gPad->Update();
//     s_Las[i]= (TPaveStats*)(h_Et[i]->GetListOfFunctions()->FindObject("stats"));
//     s_Las[i]->SetTextColor(kBlack);*/
    
//     char Name[100];
//     if(PU == 0)      sprintf(Name, (plotFolderName+"/"+folderName+"/noPU_Et_%d_"+string_year+".png").c_str(),i);
//     if(PU == 1)      sprintf(Name, (plotFolderName+"/"+folderName+"/Et_%d_"+string_year+".png").c_str(),i);
//     c2[i]->Print(Name,".png");      
//   }



//   TCanvas* Et_spectrum = new TCanvas;
//   gPad->SetLogy();
//   //  h_Et_allDA->GetYaxis()->SetRangeUser(0.1, 10000.);
//   h_Et_allDA->GetXaxis()->SetRangeUser(0., 150.);
//   h_Et_allDA->GetXaxis()->SetTitle("Et ");
//   h_Et_allDA->SetMarkerColor(kRed+2);
//   h_Et_allDA->SetMarkerStyle(7);
//   h_Et_allDA->Draw("e");
//    for(int jj = 0; jj < nBins; ++jj){
//      h_Et_MC[jj]->GetXaxis()->SetRangeUser(0., 150.);
//      h_Et_MC[jj]->Draw("same");
//    }
//   TLegend *tspec = new TLegend(0.64,0.80,0.99,0.99);
//   tspec->SetFillColor(0);
//   tspec->SetTextFont(42); 
//   tspec->AddEntry(h_Et_allDA,"DATA","PL");
//   tspec->AddEntry(h_Et_MC[0],"MC ","PL");
//   tspec->Draw(); 
//   Et_spectrum->Print((plotFolderName+"/"+folderName+"/Et_spectrum_"+string_year+".png").c_str(), ".png");


//   TCanvas* cVtx = new TCanvas();
//   h_Vtx_DA->Draw();
//   h_Vtx_MC->SetLineColor(kGreen+2);
//   h_Vtx_MC->Draw("same");
//   cVtx->Print((plotFolderName+"/"+folderName+"/Vtx_"+string_year+".png").c_str(),".png");


//   std::sort(y.begin(), y.end());
//   std::sort(ey.begin(), ey.end());
//   TCanvas* cplot = new TCanvas("gplot", "gplot",100,100,725,500);
//   cplot->cd();

//   std::cout << " sortato range " << std::endl;

//   TPad *cLeft  = new TPad("pad_0","pad_0",0.00,0.00,1.00,1.00);
//   cLeft->SetLeftMargin(0.17); 
//   cLeft->SetRightMargin(0.025); 
//   cLeft->SetBottomMargin(0.17); 
  
//   cLeft->Draw();
 
//   float tYoffset = 1.75; 
//   float tXoffset = 1.6; 
//   float labSize = 0.04;
//   float labSize2 = 0.07;

//   cLeft->cd(); 
//   cLeft->SetGridx();
//   cLeft->SetGridy();
  
//   float x_min = x.at(0)-ex.at(ex.size()-1)-10;
//   float x_max = x.at(x.size()-1)+ex.at(ex.size()-1)+10;
// //   float y_min = y.at(0)-ey.at(ey.size()-1)-0.002;
// //   float y_max = y.at(y.size()-1)+ey.at(ey.size()-1)+0.002;

// //   float y_min = y.at(0)-ey.at(ey.size()-1)-0.005;
// //   float y_max = y.at(y.size()-1)+ey.at(ey.size()-1)+0.005;

//   float y_min = -0.004;
//   float y_max = 0.014;


//   // pad settings
//   TH1F *hPad = (TH1F*)gPad->DrawFrame(0,y_min,130,y_max);
//   hPad->GetXaxis()->SetTitle("E_{T}");
//   hPad->GetYaxis()->SetTitle("E/p_{data}-E/p_{mc}");
//   hPad->GetYaxis()->SetTitleOffset(tYoffset);
//   hPad->GetXaxis()->SetTitleOffset(tXoffset);
//   hPad->GetXaxis()->SetLabelSize(labSize);
//   hPad->GetXaxis()->SetTitleSize(labSize);
//   hPad->GetYaxis()->SetLabelSize(labSize);
//   hPad->GetYaxis()->SetTitleSize(labSize);
//   finalGraph->Draw("P");
//   cplot->Print((plotFolderName+"/"+folderName+"/EoP_vs_Et_"+string_year+".png").c_str(),".png");
// */
  
  std::cout << " plottato tutto " << std::endl;
  //std::cout << "CREATI I FILES" << std::endl;
  return (0);

}
コード例 #2
0
int main(int argc, char** argv)
{
  float xtalWidth = 0.01745329;

  //---- variables for selections
  float etaMax = 1.44;
  float r9min = 0.0 ;
  float r9max = 0.94 ;
  float bcNmin = 2;
  float bcNmax = 9999;
  
  bool useW         = true;
  bool useZ         = false;
  bool usePUweights = true;
  bool useOddCry    = false;
  bool useEvenCry   = false;

  //---- output file to save graphs
  char outfilename[100];
  sprintf(outfilename,"GraphsLocalPhi_regression_nBCgt1_lowR9.root");

  //---- PU weights for MC
  TPileupReweighting *puReweighting;
  if (useW) puReweighting = new TPileupReweighting("../CommonTools/weights/PUweights_2011_0100_73500_WJetsToLL_Fall11_S6.root","hweights"); 
  if (useZ) puReweighting = new TPileupReweighting("../CommonTools/weights/PUweights_2011_0100_73500_DYJetsToLL_Fall11_S6.root","hweights"); 
 
  //---- NTUPLES 
  TChain *ntu_MC = new TChain("ntu");
  TChain *ntu_Data = new TChain("ntu");

  //---- MC fall 2011 
  if (useW)
    ntu_MC->Add("../NTUPLES/Fall11/WZAnalysis/WZAnalysis_WJetsToLNu_TuneZ2_7TeV-madgraph-tauola_Fall11-PU_S6_START42_V14B-v1.root");
  if (useZ)
    ntu_MC->Add("../NTUPLES/Fall11/WZAnalysis/WZAnalysis_DYJetsToLL_TuneZ2_M-50_7TeV-madgraph-tauola_Fall11-PU_S6_START42_V14B-v1.root");
 
  //---- DATA
  if (useW){
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_SingleElectron_Run2011A-WElectron-May10ReReco-v1_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_SingleElectron_Run2011A-WElectron-PromptSkim-v4_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_SingleElectron_Run2011A-WElectron-PromptSkim-v5_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_SingleElectron_Run2011A-WElectron-PromptSkim-v6_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011B/WZAnalysis/WZAnalysis_SingleElectron_Run2011B-WElectron-PromptSkim-v1_42XReReco_FT_R_42_V21B.root");
  }
  if (useZ){
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_DoubleElectron_Run2011A-ZElectron-May10ReReco-v1_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_DoubleElectron_Run2011A-ZElectron-PromptSkim-v4_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_DoubleElectron_Run2011A-ZElectron-PromptSkim-v5_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011A/WZAnalysis/WZAnalysis_DoubleElectron_Run2011A-ZElectron-PromptSkim-v6_42XReReco_FT_R_42_V21B.root");
    ntu_Data->Add("../NTUPLES/Run2011B/WZAnalysis/WZAnalysis_DoubleElectron_Run2011B-ZElectron-PromptSkim-v1_42XReReco_FT_R_42_V21B.root");
  }

  std::cout << "     MC    : " << ntu_MC->GetEntries() << " entries in  MC  sample" << std::endl;
  std::cout << "     Data  : " << ntu_Data->GetEntries() << " entries in  Data  sample" << std::endl;

  //---- observables
  int npu;
  float EoP, scEta, scPhi;
  float scE3x3, scE, scE_regression;  
  float charge, scLocalEta, scLocalPhi,crackCorr,scLocalCorr; 
  int bcN;
  float R9;
  
  //---- Set branch addresses for MC  
  ntu_MC->SetBranchAddress("PUit_NumInteractions", &npu);
  ntu_MC->SetBranchAddress("ele1_scEta", &scEta);
  ntu_MC->SetBranchAddress("ele1_scPhi", &scPhi);
  ntu_MC->SetBranchAddress("ele1_EOverP", &EoP);
  ntu_MC->SetBranchAddress("ele1_e3x3", &scE3x3);
  ntu_MC->SetBranchAddress("ele1_scE", &scE);
  ntu_MC->SetBranchAddress("ele1_scE_regression", &scE_regression);
  ntu_MC->SetBranchAddress("ele1_charge", &charge);
  ntu_MC->SetBranchAddress("ele1_scLocalPhi",&scLocalPhi); 
  ntu_MC->SetBranchAddress("ele1_scLocalEta",&scLocalEta); 
  ntu_MC->SetBranchAddress("ele1_scCrackCorr",&crackCorr); 
  ntu_MC->SetBranchAddress("ele1_scLocalContCorr",&scLocalCorr); 


  //---- Set branch addresses for Data
  ntu_Data->SetBranchAddress("ele1_scEta", &scEta);
  ntu_Data->SetBranchAddress("ele1_scPhi", &scPhi);
  ntu_Data->SetBranchAddress("ele1_EOverP", &EoP);
  ntu_Data->SetBranchAddress("ele1_e3x3", &scE3x3);
  ntu_Data->SetBranchAddress("ele1_scE", &scE);
  ntu_Data->SetBranchAddress("ele1_scE_regression", &scE_regression);
  ntu_Data->SetBranchAddress("ele1_charge", &charge);
  ntu_Data->SetBranchAddress("ele1_scLocalPhi",&scLocalPhi); 
  ntu_Data->SetBranchAddress("ele1_scLocalEta",&scLocalEta); 
  ntu_Data->SetBranchAddress("ele1_scCrackCorr",&crackCorr); 
  ntu_Data->SetBranchAddress("ele1_scLocalContCorr",&scLocalCorr); 
  ntu_Data->SetBranchAddress("ele1_bcN", &bcN);

  const unsigned int nBins = 20;
  const int Ntempl = 4;
  std::cout << "nBins = " << nBins << std::endl;
  
  // histogram definition
  TH1F* h_EoP_MC[nBins][Ntempl] ;   
  TH1F* h_EoC_MC[nBins][Ntempl] ;
  TH1F* h_EoP_Data[nBins][Ntempl];
  TH1F* h_EoC_Data[nBins][Ntempl] ;

  for(int mod=0; mod<Ntempl; mod++){
    for(unsigned int i = 0; i < nBins; ++i)
      {
	char histoName[80];

	sprintf(histoName, "EoP_MC_%d_mod%d", i,mod+1);
	h_EoP_MC[i][mod] = new TH1F(histoName, histoName, 1200, 0., 3.);
	h_EoP_MC[i][mod] -> SetFillColor(4);
	h_EoP_MC[i][mod] -> SetFillStyle(3004);
	sprintf(histoName, "EoC_MC_%d_mod%d", i,mod+1);
	h_EoC_MC[i][mod] = new TH1F(histoName, histoName, 1200, 0., 3.);
	h_EoC_MC[i][mod] -> SetFillColor(3);
	h_EoC_MC[i][mod] -> SetFillStyle(3004);

	sprintf(histoName, "EoP_Data_%d_mod%d", i,mod+1);
	h_EoP_Data[i][mod] = new TH1F(histoName, histoName, 1200, 0., 3.);
	h_EoP_Data[i][mod] -> SetFillColor(4);
	h_EoP_Data[i][mod] -> SetFillStyle(3004);
	sprintf(histoName, "EoC_Data_%d_mod%d", i,mod+1);
	h_EoC_Data[i][mod] = new TH1F(histoName, histoName, 1200, 0., 3.);
	h_EoC_Data[i][mod] -> SetFillColor(3);
	h_EoC_Data[i][mod] -> SetFillStyle(3004);
      }
  }

  
  //---- book templates
  // [0] --> uncorrected
  // [1] --> corrected
  TH1F* h_template_MC[Ntempl][2];
  TH1F* h_template_Data[Ntempl][2];
  for(unsigned int i = 0; i < Ntempl; ++i){
    char histoName[100];
    sprintf(histoName, "template_MC_%d", i);
    h_template_MC[i][0] = new TH1F(histoName, "", 1200, 0., 3.);   
    sprintf(histoName, "template_DATA_%d", i);
    h_template_Data[i][0] = new TH1F(histoName, "", 1200, 0., 3.);   
    sprintf(histoName, "template_MCcorr_%d", i);
    h_template_MC[i][1] = new TH1F(histoName, "", 1200, 0., 3.);   
    sprintf(histoName, "template_DATAcorr_%d", i);
    h_template_Data[i][1] = new TH1F(histoName, "", 1200, 0., 3.);   
  }

  //******************************************************************************************
  //*************************************** MC  ********************************************** 
  std::cout << "Loop over MC events ... " << std::endl; 
  float ww = 1 ;
 
  //---- loop on MC, make reference and fit dist
  for(int entry = 0; entry < ntu_MC->GetEntries(); ++entry) {
    if( entry%200000 == 0 ) std::cout << "reading saved entry " << entry << std::endl;
    //    if (entry>1000) break;

    ntu_MC->GetEntry(entry);

    // -- PU weights
    if (usePUweights) ww = puReweighting->GetWeight(npu);
   
    R9 = scE3x3/scE;

    //-- eta or R9 cuts
    if ( fabs(scEta) > etaMax ) continue;
    if ( R9 < r9min || R9 > r9max ) continue; 
    //if ( (scEta*charge)<0 ) continue;
    if ( bcN < bcNmin || bcN > bcNmax) continue;

    //-- remove phi cracks
    float phi = (scPhi+3.1415926536)/xtalWidth;
    float modphi = (int)phi%20;
    if (fabs(modphi-10)<2.) continue;
    
    //-- use only even/odd crystals (for data)
    if ( useOddCry  && int(modphi)%2 == 0) continue; 
    if ( useEvenCry && int(modphi)%2 != 0) continue; 
 
    //-- remove gaps
    float fetaCry = fabs (scEta) / xtalWidth;
    if( IsEtaGap(fetaCry) ) continue;
   
    //-- fill templates for each mod
    int mod = templIndex(fetaCry);
    float correction = scE_regression/scE;
    h_template_MC[mod][0]-> Fill(EoP,ww);
    h_template_MC[mod][1]-> Fill(EoP*correction,ww);

    //-- fill MC histos in phi bins
    float locPhi = scLocalPhi+0.5; 
    if ( fabs(locPhi-0.5) >= 0.5 ) continue;
 
    int bin = nBins * locPhi; 
    if (bin>nBins-1 || bin < 0 ) {
      std::cout << "Error in bins: " << bin << " " << scLocalPhi << std::endl;
      continue;
    }
        
    h_EoP_MC[bin][mod] -> Fill(EoP,ww);
    h_EoC_MC[bin][mod] -> Fill(EoP*correction,ww);
  }
  
    
  //******************************************************************************************
  //*************************************** DATA ********************************************** 
  std::cout << "Loop on Data events ... " << std::endl; 
  //---- loop on data
  for(int entry = 0; entry < ntu_Data->GetEntries(); ++entry) {
    if( entry%200000 == 0 ) std::cout << "reading saved entry " << entry << std::endl;
    //    if (entry>1000) break;

    ntu_Data->GetEntry(entry);

    R9 = scE3x3/scE;

    //-- eta or R9 cuts
    if ( fabs(scEta) > etaMax ) continue;
    if ( R9 < r9min || R9 > r9max ) continue; 
    //if ( (scEta*charge)<0 ) continue;
    if ( bcN < bcNmin || bcN > bcNmax) continue;

    //-- remove phi cracks
    float phi = (scPhi+3.141592653)/xtalWidth;
    float modphi = (int)phi%20;
    if (fabs(modphi-10)<2.) continue;
        
    //-- use only even/odd crystals (for data)
    if ( useOddCry  && int(modphi)%2 == 0) continue; 
    if ( useEvenCry && int(modphi)%2 != 0) continue; 

    //-- remove eta gaps
    float fetaCry = fabs (scEta) / xtalWidth;
    if( IsEtaGap(fetaCry) ) continue;
    

    //-- fill template for each mod
    int mod = templIndex(fetaCry);
    float correction = scE_regression/scE;
    h_template_Data[mod][0]-> Fill(EoP);
    h_template_Data[mod][1]-> Fill(EoP*correction);

    //-- fill data histos in phi bins
    float locPhi = scLocalPhi + 0.5; 
    if ( fabs(locPhi-0.5) >= 0.5 ) continue;
     
    int bin = nBins * (locPhi); 
    if (bin>nBins-1 || bin < 0 ) {
      std::cout << "Error in bins: " << bin << " " << scLocalPhi << std::endl;
      continue;
    }

    
    h_EoP_Data[bin][mod] -> Fill(EoP);
    h_EoC_Data[bin][mod] -> Fill(EoP*correction);
  }

 
  ///////////////****************** Fit the histograms and fill the graphs *************** ////////////////////////
    
  int rebin = 4;
  
  TGraphErrors* g_EoP_MC[Ntempl];
  TGraphErrors* g_EoC_MC[Ntempl];
  TGraphErrors* g_ratio_MC[Ntempl];

  TGraphErrors* g_EoP_Data[Ntempl];
  TGraphErrors* g_EoC_Data[Ntempl];
  TGraphErrors* g_ratio_Data[Ntempl];

  TGraphErrors* g_ratio_uncorr[Ntempl];
  TGraphErrors* g_ratio_corr[Ntempl];

  TH1F* spread_EoP_MC[Ntempl];
  TH1F* spread_EoC_MC[Ntempl];
  
  TH1F* spread_EoP_Data[Ntempl];
  TH1F* spread_EoC_Data[Ntempl];
  
  for (int mod=0; mod<4; mod++){
    char histoName[100];
    
    sprintf(histoName, "gEoP_MC_mod%d", mod+1);
    g_EoP_MC[mod]   = new TGraphErrors(); 
    g_EoP_MC[mod]->SetName(histoName);
    
    sprintf(histoName, "gEoC_MC_mod%d", mod+1);
    g_EoC_MC[mod]   = new TGraphErrors(); 
    g_EoC_MC[mod]->SetName(histoName);
    
    sprintf(histoName, "gRatio_MC_mod%d", mod+1);
    g_ratio_MC[mod]   = new TGraphErrors(); 
    g_ratio_MC[mod]->SetName(histoName);

    sprintf(histoName, "gEoP_Data_mod%d", mod+1);
    g_EoP_Data[mod]   = new TGraphErrors(); 
    g_EoP_Data[mod]->SetName(histoName);
    
    sprintf(histoName, "gEoC_Data_mod%d", mod+1);
    g_EoC_Data[mod]   = new TGraphErrors(); 
    g_EoC_Data[mod]->SetName(histoName);
    
    sprintf(histoName, "gRatio_Data_mod%d", mod+1);
    g_ratio_Data[mod]   = new TGraphErrors(); 
    g_ratio_Data[mod]->SetName(histoName);

    sprintf(histoName, "gRatio_uncorr_mod%d", mod+1);
    g_ratio_uncorr[mod]   = new TGraphErrors(); 
    g_ratio_uncorr[mod]->SetName(histoName);

    sprintf(histoName, "gRatio_corr_mod%d", mod+1);
    g_ratio_corr[mod]   = new TGraphErrors(); 
    g_ratio_corr[mod]->SetName(histoName);

    sprintf(histoName, "spreadEoP_MC_mod%d", mod+1);
    spread_EoP_MC[mod]   = new TH1F(histoName, histoName,200,0.9,1.1); 
    
    sprintf(histoName, "spreadEoC_MC_mod%d", mod+1);
    spread_EoC_MC[mod]   = new TH1F(histoName, histoName,200,0.9,1.1); 

    sprintf(histoName, "spreadEoP_Data_mod%d", mod+1);
    spread_EoP_Data[mod]   = new TH1F(histoName, histoName,200,0.9,1.1); 

    sprintf(histoName, "spreadEoC_Data_mod%d", mod+1);
    spread_EoC_Data[mod]   = new TH1F(histoName, histoName,200,0.9,1.1); 
    
  }


  //************************************* FITTING ***************************************************//
  
  for(int mod=0;mod<4;mod++){

    //--- define func template
    histoFunc *templateHistoFuncMC[2]; 
    histoFunc *templateHistoFuncData[2]; 
    for (int ll = 0; ll < 2; ll++){
      
      if (mod<3) {
	h_template_MC[mod][ll]   -> Rebin(rebin);
	h_template_Data[mod][ll] -> Rebin(rebin);
      }
      else {
	h_template_MC[mod][ll]   -> Rebin(rebin*2);
	h_template_Data[mod][ll] -> Rebin(rebin*2);    
      }
      
      templateHistoFuncMC[ll]   = new histoFunc(h_template_MC[mod][ll]);
      templateHistoFuncData[ll] = new histoFunc(h_template_Data[mod][ll]);
    }  
    
    // loop over bins
    for(unsigned int i = 0; i < nBins; ++i)
      {
	std::cout << "***** Fitting :  mod: " <<mod <<"  bin:  "<< i << std::endl; 

	if (mod < 3){
	  h_EoP_MC[i][mod] -> Rebin(rebin);    
	  h_EoC_MC[i][mod] -> Rebin(rebin);    
	  h_EoP_Data[i][mod] -> Rebin(rebin);    
	  h_EoC_Data[i][mod] -> Rebin(rebin);    
	}
	else {
	  h_EoP_MC[i][mod] -> Rebin(rebin*2);    
	  h_EoC_MC[i][mod] -> Rebin(rebin*2);    
	  h_EoP_Data[i][mod] -> Rebin(rebin*2);    
	  h_EoC_Data[i][mod] -> Rebin(rebin*2);
	}

	
	//************************ MC ****************************************************************
	TF1 * templateFuncMC = new TF1("templateFuncMC", templateHistoFuncMC[0], 0.7, 1.3, 3, "histoFunc");
	templateFuncMC -> SetNpx(10000);

	float xval = (i+0.5)*1/(float)nBins - 0.5;

	//--- uncorrected MC   
	double xNorm = h_EoP_MC[i][mod]->Integral()/h_template_MC[mod][0]->Integral() *
	               h_EoP_MC[i][mod]->GetBinWidth(1)/h_template_MC[mod][0]->GetBinWidth(1); 

	templateFuncMC -> FixParameter(0, xNorm);
	templateFuncMC -> SetParameter(1, 1.05 );
	templateFuncMC -> FixParameter(2, 0.);
    

	h_EoP_MC[i][mod] -> Fit("templateFuncMC", "MRQLN+");
	g_EoP_MC[mod] -> SetPoint(i, xval , 1./templateFuncMC->GetParameter(1));
	g_EoP_MC[mod] -> SetPointError(i, 0., templateFuncMC->GetParError(1));
	spread_EoP_MC[mod] -> Fill(1./templateFuncMC->GetParameter(1));
	//    if ( templateFuncMC->GetParError(1) < 0.003) g_EoP -> SetPointError(i, 0., 0.003);
	//    cout  << " ***** " <<  1./templateFuncMC->GetParameter(1) << " " << templateFuncMC->GetParError(1) << endl; 

	float scaleMC  = 1./templateFuncMC->GetParameter(1);
	float escaleMC = templateFuncMC->GetParError(1)/scaleMC/scaleMC;

	//--- corrected MC  
	templateFuncMC = new TF1("templateFuncMC", templateHistoFuncMC[1], 0.7, 1.3, 3, "histoFunc");
	templateFuncMC -> SetNpx(10000);

	xNorm = h_EoC_MC[i][mod]->Integral()/h_template_MC[mod][1]->Integral() *
	        h_EoC_MC[i][mod]->GetBinWidth(1)/h_template_MC[mod][1]->GetBinWidth(1); 

	templateFuncMC -> FixParameter(0, xNorm);
	templateFuncMC -> SetParameter(1, 1.05 );
	templateFuncMC -> FixParameter(2, 0.);

	h_EoC_MC[i][mod] -> Fit("templateFuncMC", "MRQLN+");
	g_EoC_MC[mod] -> SetPoint(i, xval , 1./templateFuncMC->GetParameter(1));
	g_EoC_MC[mod] -> SetPointError(i, 0., templateFuncMC->GetParError(1));
	spread_EoC_MC[mod] -> Fill(1./templateFuncMC->GetParameter(1));

    	float scaleMCcorr  = 1./templateFuncMC->GetParameter(1);
	float escaleMCcorr = templateFuncMC->GetParError(1)/scaleMCcorr/scaleMCcorr;




	//************************ DATA **********************************************
	TF1 * templateFuncData = new TF1("templateFuncData", templateHistoFuncData[0], 0.7, 1.3, 3, "histoFunc");
	templateFuncData -> SetNpx(10000);

	//--- uncorrected Data   
	xNorm = h_EoP_Data[i][mod]->Integral()/h_template_Data[mod][0]->Integral() *
	        h_EoP_Data[i][mod]->GetBinWidth(1)/h_template_Data[mod][0]->GetBinWidth(1); 
 
	templateFuncData -> FixParameter(0, xNorm);
	templateFuncData -> SetParameter(1, 1.05 );
	templateFuncData -> FixParameter(2, 0.);
    
	h_EoP_Data[i][mod] -> Fit("templateFuncData", "MRQLN+");
	g_EoP_Data[mod] -> SetPoint(i, xval , 1./templateFuncData->GetParameter(1));
	g_EoP_Data[mod] -> SetPointError(i, 0., templateFuncData->GetParError(1));
	spread_EoP_Data[mod] -> Fill(1./templateFuncData->GetParameter(1));
	//    if ( templateFuncData->GetParError(1) < 0.003) g_EoP -> SetPointError(i, 0., 0.003);
	//    cout  << " ***** " <<  1./templateFuncData->GetParameter(1) << " " << templateFuncData->GetParError(1) << endl; 
	
	float scaleDA  = 1./templateFuncData->GetParameter(1);
	float escaleDA = templateFuncData->GetParError(1)/scaleDA/scaleDA;

	//--- corrected Data 
	templateFuncData = new TF1("templateFuncData", templateHistoFuncData[1], 0.7, 1.3, 3, "histoFunc");
	templateFuncData -> SetNpx(10000);
	xNorm = h_EoC_Data[i][mod]->Integral()/h_template_Data[mod][1]->Integral() *
	        h_EoC_Data[i][mod]->GetBinWidth(1)/h_template_Data[mod][1]->GetBinWidth(1); 

	templateFuncData -> FixParameter(0, xNorm);
	templateFuncData -> SetParameter(1, 1.05 );
	templateFuncData -> FixParameter(2, 0.);
    
	h_EoC_Data[i][mod] -> Fit("templateFuncData", "SNQR+");
	g_EoC_Data[mod] -> SetPoint(i, xval, 1./templateFuncData->GetParameter(1));
	g_EoC_Data[mod] -> SetPointError(i, 0., templateFuncData->GetParError(1));
	spread_EoC_Data[mod] -> Fill(1./templateFuncData->GetParameter(1));
    	
	float scaleDAcorr  = 1./templateFuncData->GetParameter(1);
	float escaleDAcorr = templateFuncData->GetParError(1)/scaleDAcorr/scaleDAcorr;
		
	
	//--- ratio finalization MC corr/uncorr
	float ratioMC = scaleMCcorr/scaleMC;
	float eratioMC = ratioMC*sqrt(pow(escaleMC/scaleMC,2) + pow(escaleMCcorr/scaleMCcorr,2)); 
	g_ratio_MC[mod] -> SetPoint(i, xval , ratioMC);
	g_ratio_MC[mod] -> SetPointError(i, 0., eratioMC);

	//--- ratio finalization DATA corr/uncorr
	float ratioDA  = scaleDAcorr/scaleDA;
	float eratioDA = ratioDA*sqrt(pow(escaleDA/scaleDA,2) + pow(escaleDAcorr/scaleDAcorr,2)); 
	g_ratio_Data[mod] -> SetPoint(i, xval, ratioDA);
	g_ratio_Data[mod] -> SetPointError(i, 0., eratioDA);

	//--- ratio finalization data/MC uncorrected
	float ratioU = scaleDA/scaleMC;
	float eratioU = ratioU*sqrt(pow(escaleMC/scaleMC,2) + pow(escaleDA/scaleDA,2)); 
	g_ratio_uncorr[mod] -> SetPoint(i, xval , ratioU);
	g_ratio_uncorr[mod] -> SetPointError(i, 0., eratioU);

	//--- ratio finalization data/MC corrected
	float ratioC = scaleDAcorr/scaleMCcorr;
	float eratioC = ratioC*sqrt(pow(escaleMCcorr/scaleMCcorr,2) + pow(escaleDAcorr/scaleDAcorr,2)); 
	g_ratio_corr[mod] -> SetPoint(i, xval , ratioC);
	g_ratio_corr[mod] -> SetPointError(i, 0., eratioC);
    
      }
  }
  
  TFile fout(outfilename,"recreate");
  for(int mod=0;mod<4;mod++) {
    g_EoP_MC[mod]->Write();
    g_EoC_MC[mod]->Write();
    g_EoP_Data[mod]->Write();
    g_EoC_Data[mod]->Write();
    g_ratio_MC[mod]->Write();
    g_ratio_Data[mod]->Write();
    g_ratio_uncorr[mod]->Write();
    g_ratio_corr[mod]->Write();
    spread_EoP_MC[mod]->Write();
    spread_EoC_MC[mod]->Write();
    spread_EoP_Data[mod]->Write();
    spread_EoC_Data[mod]->Write();
  }

  fout.Close();
  }