void storePUHisto(TString inputfilepath, TString outputfilepath, TString histoname = "MCPileUpHisto"){

  TFile *inputFile = TFile::Open(inputfilepath.Data());
  //This should have always the same name
  TH1F *histo = (TH1F*)inputFile->Get("diphotonSignalMCAnalyzer/fpu_n_BeforeCuts");
  //set the name according to the name provided in cfg for crab
  histo->SetName(histoname.Data());
  histo->SetBins(80,0,80);
  //Open a new file to store this histo to
  TFile *outputFile = new TFile(outputfilepath.Data(),"recreate");
  outputFile->cd();
  histo->Write();
  outputFile->cd();
  outputFile->Close();
}
Int_t DstPiAnalysis::WeightEvents(){
 
  
  if(!OpenReducedNtuple())return 0;
  SetReducedNtupleBranches();
  if(!OpenEfficiency()){cout<<"Could not open efficiecy file"<<endl;return 0;}


  //
  TH1F HDstPi("HDstPi","HDstPi",2000,2.1,4.1);
 
  //
  TH1F HDstPiEff("HDstPiEff","HDstPiEff",2000,2.1,4.1);
  HDstPiEff.Sumw2();
    
  //mass cut abs(heli)>.75
  TH1F HDstPiMD2420Helicity("HDstPiMD2420Helicity","HDstPiMD2420Helicity",2000,2.1,4.1);
  HDstPiMD2420Helicity.Sumw2();

  //mass cut abs(heli)<.5
  TH1F HDstPiMD2460Helicity("HDstPiMD2460Helicity","HDstPiMD2460Helicity",2000,2.1,4.1);
  HDstPiMD2460Helicity.Sumw2();


  //mass vs helicity .2 slices
  TH1F* HDstPiMVsHelicity[10];
  for(Int_t i=0;i<10;i++){
    HDstPiMVsHelicity[i]=new TH1F(TString("HDstPiMVsHelicity")+(long)i,"HDstPiMVsHelicity",2000,2.1,4.1);
    HDstPiMVsHelicity[i]->Sumw2();
  }  

  //mass vs helicity .1 slices
  TH1F* HDstPiMVsHelicityFine[20];
  for(Int_t i=0;i<20;i++){
    HDstPiMVsHelicityFine[i]=new TH1F(TString("HDstPiMVsHelicityFine")+(long)i,"HDstPiMVsHelicityFine",2000,2.1,4.1);
    HDstPiMVsHelicityFine[i]->Sumw2();
  }  


  //mass vs helicity .2 slices cut -.5 cosD*
  TH1F* HDstPiMVsHelicityN5Cos[10];
  for(Int_t i=0;i<10;i++){
    HDstPiMVsHelicityN5Cos[i]=new TH1F(TString("HDstPiMVsHelicityN5Cos")+(long)i,"HDstPiMVsHelicityN5Cos",2000,2.1,4.1);
    HDstPiMVsHelicityN5Cos[i]->Sumw2();
  }  


  //mass vs helicity .2 slices cut -.9 cosD*
  TH1F* HDstPiMVsHelicityN9Cos[10];
  for(Int_t i=0;i<10;i++){
    HDstPiMVsHelicityN9Cos[i]=new TH1F(TString("HDstPiMVsHelicityN9Cos")+(long)i,"HDstPiMVsHelicityN9Cos",2000,2.1,4.1);
    HDstPiMVsHelicityN9Cos[i]->Sumw2();
  }  


  //mass vs helicity in slices of .25, for dependent mass widths 
  TH1F* HDstPiMVsHelicityMW[8];
  for(Int_t i=0;i<8;i++){
    HDstPiMVsHelicityMW[i]=new TH1F(TString("HDstPiMVsHelicityMW")+(long)i,"HDstPiMVsHelicityMW",2000,2.1,4.1);
    HDstPiMVsHelicityMW[i]->Sumw2();
  }


  //mass vs helicity in slices of .25, for dependent mass widths 
  TH1F* HDstPiMVsCoarseAbsHelicity[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsCoarseAbsHelicity[i]=new TH1F(TString("HDstPiMVsCoarseAbsHelicity")+(long)i,"HDstPiMVsCoarseAbsHelicity",2000,2.1,4.1);
    HDstPiMVsCoarseAbsHelicity[i]->Sumw2();
  }




  //efficiency corrected mass vs p* coarse
  TH1F* HDstPiMVsPstar[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPstar[i]=new TH1F(TString("HDstPiMVsPstar")+(long)i,"HDstPiMVsPstar",2000,2.1,4.1);
    HDstPiMVsPstar[i]->Sumw2();
  } 
  TH1* HDstPiMVsPstarNoEff[4];//for p* dependent mass-width systematics with no helicity cut
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPstarNoEff[i]=new TH1F(TString("HDstPiMVsPstarNoEff")+(long)i,"HDstPiMVsPstarNoEff",2000,2.1,4.1); 

  TH1* HDstPiMVsPstarD2420[4];//for p* systematics for MC this should not be weighted due to low stats
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPstarD2420[i]=new TH1F(TString("HDstPiMVsPstarD2420")+(long)i,"HDstPiMVsPstarD2420",2000,2.1,4.1); 
    HDstPiMVsPstarD2420[i]->Sumw2();
  }
  TH1* HDstPiMVsPstarD2460[4];//for p* systematics
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPstarD2460[i]=new TH1F(TString("HDstPiMVsPstarD2460")+(long)i,"HDstPiMVsPstarD2460",2000,2.1,4.1); 
    HDstPiMVsPstarD2460[i]->Sumw2();
  }


  //efficiency corrected mass vs p* for crossections  
  TH1F* HDstPiMVsPstarFine[10];
  for(Int_t i=0;i<10;i++){
    HDstPiMVsPstarFine[i]=new TH1F(TString("HDstPiMVsPstarFine")+(long)i,"HDstPiMVsPstarFine",2000,2.1,4.1);
    HDstPiMVsPstarFine[i]->Sumw2();
  }   
  TH1F* HDstPiMVsEnergyFine[30];
  for(Int_t i=0;i<30;i++){
    HDstPiMVsEnergyFine[i]=new TH1F(TString("HDstPiMVsEnergyFine")+(long)i,"HDstPiMVsEnergyFine",2000,2.1,4.1);
    HDstPiMVsEnergyFine[i]->Sumw2();
  }   
  

  //efficiency corrected mass vs cos Dstar  
  TH1F* HDstPiMVsCosDstar[20];
  for(Int_t i=0;i<20;i++){
    HDstPiMVsCosDstar[i]=new TH1F(TString("HDstPiMVsCosDstar")+(long)i,"HDstPiMVsCosDstar",2000,2.1,4.1);
    HDstPiMVsCosDstar[i]->Sumw2();
  }


  //efficiency corrected mass vs cosD* in slices of .25, for dependent mass widths 
  TH1F* HDstPiMVsCosDstarMW[8];
  for(Int_t i=0;i<8;i++){
    HDstPiMVsCosDstarMW[i]=new TH1F(TString("HDstPiMVsCosDstarMW")+(long)i,"HDstPiMVsCosDstarMW",2000,2.1,4.1);
    HDstPiMVsCosDstarMW[i]->Sumw2();
  }

  //efficiency corrected mass vs cosD* in slices of .25, for dependent mass widths , for D2420 cut helicity>.75
  TH1F* HDstPiMVsCosDstarD2420MW[8];
  for(Int_t i=0;i<8;i++){
    HDstPiMVsCosDstarD2420MW[i]=new TH1F(TString("HDstPiMVsCosDstarD2420MW")+(long)i,"HDstPiMVsCosDstarD2420MW",2000,2.1,4.1);
    HDstPiMVsCosDstarD2420MW[i]->Sumw2();
  }
  //efficiency corrected mass vs cosD* in slices of .25, for dependent mass widths , for D2420 cut helicity<.5
  TH1F* HDstPiMVsCosDstarD2460MW[8];
  for(Int_t i=0;i<8;i++){
    HDstPiMVsCosDstarD2460MW[i]=new TH1F(TString("HDstPiMVsCosDstarD2460MW")+(long)i,"HDstPiMVsCosDstarD2460MW",2000,2.1,4.1);
    HDstPiMVsCosDstarD2460MW[i]->Sumw2();
  }




  //efficiency corrected mass vs phi*
  TH1F* HDstPiMVsPhi[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPhi[i]=new TH1F(TString("HDstPiMVsPhi")+(long)i,"HDstPiMVsPhi",2000,2.1,4.1);
    HDstPiMVsPhi[i]->Sumw2();
  } 
  TH1F* HDstPiMVsPhiNoEff[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPhiNoEff[i]=new TH1F(TString("HDstPiMVsPhiNoEff")+(long)i,"HDstPiMVsPhiNoEff",2000,2.1,4.1);
    HDstPiMVsPhiNoEff[i]->Sumw2();
  } 
  TH1* HDstPiMVsPhiD2420[4];//for  systematics
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPhiD2420[i]=new TH1F(TString("HDstPiMVsPhiD2420")+(long)i,"HDstPiMVsPhiD2420",2000,2.1,4.1);
    HDstPiMVsPhiD2420[i]->Sumw2();
  }
  TH1* HDstPiMVsPhiD2460[4];//for   systematics 
  for(Int_t i=0;i<4;i++){
    HDstPiMVsPhiD2460[i]=new TH1F(TString("HDstPiMVsPhiD2460")+(long)i,"HDstPiMVsPhiD2460",2000,2.1,4.1); 
    HDstPiMVsPhiD2460[i]->Sumw2();
  }




  //efficiency corrected mass vs cos*
  TH1F* HDstPiMVsCos[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsCos[i]=new TH1F(TString("HDstPiMVsCos")+(long)i,"HDstPiMVsCos",2000,2.1,4.1);
    HDstPiMVsCos[i]->Sumw2();
  }
  TH1F* HDstPiMVsCosNoEff[4];
  for(Int_t i=0;i<4;i++){
    HDstPiMVsCosNoEff[i]=new TH1F(TString("HDstPiMVsCosNoEff")+(long)i,"HDstPiMVsCosNoEff",2000,2.1,4.1);
    HDstPiMVsCosNoEff[i]->Sumw2();
  } 
  TH1* HDstPiMVsCosD2420[4];//for  systematics  
  for(Int_t i=0;i<4;i++){
    HDstPiMVsCosD2420[i]=new TH1F(TString("HDstPiMVsCosD2420")+(long)i,"HDstPiMVsCosD2420",2000,2.1,4.1); 
    HDstPiMVsCosD2420[i]->Sumw2();
  }
  TH1* HDstPiMVsCosD2460[4];//for  systematics 
  for(Int_t i=0;i<4;i++){
    HDstPiMVsCosD2460[i]=new TH1F(TString("HDstPiMVsCosD2460")+(long)i,"HDstPiMVsCosD2460",2000,2.1,4.1); 
    HDstPiMVsCosD2460[i]->Sumw2();
  }





  //Legendre projections
  TH1F* HDstPiMLegendre[NPROJS];
  for(Int_t i=0;i<NPROJS;i++){
    HDstPiMLegendre[i]=new TH1F(TString("HDstPiMLegendre")+(long)i,"HDstPiMLegendre",2000,2.1,4.1);
    HDstPiMLegendre[i]->Sumw2();
  }  
  //slice P2 in cosD* 
  TH1F* HDstPiMP2VsCosDstar[8];
  for(Int_t i=0;i<8;i++){
    HDstPiMP2VsCosDstar[i]=new TH1F(TString("HDstPiMP2VsCosDstar")+(long)i,"HDstPiMP2VsCosDstar",2000,2.1,4.1);
    HDstPiMP2VsCosDstar[i]->Sumw2();
  }  



  //2D Helicity vs mass
  TH2F H2DstPiMVsDstarHel("H2DstPiMVsDstarHel","H2DstPiMVsDstarHel",2000,2.1,4.1,20,-1.0,1.0);
  H2DstPiMVsDstarHel.Sumw2();
  TH2F H2DstPiMVsDstarHelNoEff("H2DstPiMVsDstarHelNoEff","H2DstPiMVsDstarHelNoEff",2000,2.1,4.1,20,-1.0,1.0);
  H2DstPiMVsDstarHelNoEff.Sumw2();

  TH2F* H2DstPiMVsHelCosDstar[4];
  for(Int_t i=0;i<4;i++){
    H2DstPiMVsHelCosDstar[i]=new TH2F(TString("H2DstPiMVsHelCosDstar")+(long)i,TString("H2DstPiMVsHelCosDstar")+(long)i,2000,2.1,4.1,20,-1.0,1.0);
    H2DstPiMVsHelCosDstar[i]->Sumw2();
  }  



  ///------------------------------------------
  ////efficiencies after eff corrections
  //----------------------------
  TH3F H3PvsMVsDstA; 

  TH2F H2PvsM;   

  TH1F HDstarHelicityNoShape;

  TH1F HDstarHelicity;
 
  TH1F HDstPiMNoShape;

  TH1F HDstPiDM;

  TH1F HPstarNoShape;

  TH1F HDstarAngleNoShape;

  TH1F HDstarAngle;
  
  TH1F HDstPiCosstarNoShape;
    
  //fully efficiency corrected mass
  TH1F HDstPiMFineNoRes;

  //Generated mass vs p*   
  TH1* HMCMVsPstar[4];

  if(_TruthMatch){
   
    if(!OpenReducedFile())return 0;

    HMCDstarHelicity=(TH1F*)ReducedFile->Get("HMCDstarHelicity");
    if(!HMCDstarHelicity)return 0;
  
    HMCXMass=(TH1F*)ReducedFile->Get("HMCXMass");
    if(!HMCXMass)return 0;  
  
    HMCXp3CM=(TH1F*)ReducedFile->Get("HMCXp3CM");
    if(!HMCXp3CM)return 0;

    HMCXcosthCM=(TH1F*)ReducedFile->Get("HMCXcosthCM");
    if(!HMCXcosthCM)return 0;

    HMCDstarAngle=(TH1F*)ReducedFile->Get("HMCDstarAngle");
    if(!HMCDstarAngle)return 0;

    H3MCPvsMassVsDstAngle=(TH3F*)ReducedFile->Get("H3MCPvsMassVsDstAngle");                                                    
    if(!H3MCPvsMassVsDstAngle){
      cout<<"No histogram found"<<endl;
      return 0;
    }
    
    H2MCPvsMass=(TH2F*)ReducedFile->Get("H2MCPvsMass");                                                    
    if(!H2MCPvsMass){
      cout<<"No histogram found"<<endl;
      return 0;
    }

    H2MCPvsMassFine=(TH2F*)ReducedFile->Get("H2MCPvsMassFine");                                                    
    if(!H2MCPvsMass){
      cout<<"No histogram found"<<endl;
      return 0;
    }


    //----------------------------
    H3PvsMVsDstA.SetNameTitle("H3PvsMVsDstA","H3PvsMVsDstA");
    H3PvsMVsDstA.SetBins(H3MCPvsMassVsDstAngle->GetXaxis()->GetNbins(),
		      H3MCPvsMassVsDstAngle->GetXaxis()->GetXmin(),
		      H3MCPvsMassVsDstAngle->GetXaxis()->GetXmax(),
		      H3MCPvsMassVsDstAngle->GetYaxis()->GetNbins(),
		      H3MCPvsMassVsDstAngle->GetYaxis()->GetXmin(),
		      H3MCPvsMassVsDstAngle->GetYaxis()->GetXmax(),
		      H3MCPvsMassVsDstAngle->GetZaxis()->GetNbins(),
		      H3MCPvsMassVsDstAngle->GetZaxis()->GetXmin(),
		      H3MCPvsMassVsDstAngle->GetZaxis()->GetXmax()); 
    H3PvsMVsDstA.Sumw2();


    //----------------------------
    H2PvsM.SetNameTitle("H2PvsM","H2PvsM");
    H2PvsM.SetBins(H2MCPvsMass->GetXaxis()->GetNbins(),
		H2MCPvsMass->GetXaxis()->GetXmin(),
		H2MCPvsMass->GetXaxis()->GetXmax(),
		H2MCPvsMass->GetYaxis()->GetNbins(),
		H2MCPvsMass->GetYaxis()->GetXmin(),
		H2MCPvsMass->GetYaxis()->GetXmax());   

    H2PvsM.Sumw2();


    //
    HDstarHelicityNoShape.SetNameTitle("HDstarHelicityNoShape","HDstarHelicityNoShape");
    HDstarHelicityNoShape.SetBins(HMCDstarHelicity->GetXaxis()->GetNbins(),HMCDstarHelicity->GetXaxis()->GetXmin(),HMCDstarHelicity->GetXaxis()->GetXmax());
    HDstarHelicityNoShape.Sumw2();
    
    
    HDstarHelicity.SetNameTitle("HDstarHelicity","HDstarHelicity");
    HDstarHelicity.SetBins(HMCDstarHelicity->GetXaxis()->GetNbins(),HMCDstarHelicity->GetXaxis()->GetXmin(),HMCDstarHelicity->GetXaxis()->GetXmax());
    HDstarHelicity.Sumw2();
    
    //efficiency corrected mass
    HDstPiMNoShape.SetNameTitle("HDstPiMNoShape","HDstPiMNoShape");
    HDstPiMNoShape.SetBins(HMCXMass->GetXaxis()->GetNbins(),HMCXMass->GetXaxis()->GetXmin(),HMCXMass->GetXaxis()->GetXmax());
    HDstPiMNoShape.Sumw2();
    
    //fully efficiency corrected delta mass
    HDstPiDM.SetNameTitle("HDstPiDM","HDstPiDM");
    HDstPiDM.SetBins(HMCXMass->GetXaxis()->GetNbins(),HMCXMass->GetXaxis()->GetXmin(),HMCXMass->GetXaxis()->GetXmax());
    HDstPiDM.Sumw2();
    
    HPstarNoShape.SetNameTitle("HPstarNoShape","HPstarNoShape");
    HPstarNoShape.SetBins(HMCXp3CM->GetXaxis()->GetNbins(),HMCXp3CM->GetXaxis()->GetXmin(),HMCXp3CM->GetXaxis()->GetXmax());
    HPstarNoShape.Sumw2();
    
    HDstarAngleNoShape.SetNameTitle("HDstarAngleNoShape","HDstarAngleNoShape");
    HDstarAngleNoShape.SetBins(HMCDstarAngle->GetXaxis()->GetNbins(),HMCDstarAngle->GetXaxis()->GetXmin(),HMCDstarAngle->GetXaxis()->GetXmax());
    HDstarAngleNoShape.Sumw2();

    HDstarAngle.SetNameTitle("HDstarAngle","HDstarAngle");
    HDstarAngle.SetBins(HMCDstarAngle->GetXaxis()->GetNbins(),HMCDstarAngle->GetXaxis()->GetXmin(),HMCDstarAngle->GetXaxis()->GetXmax());
    HDstarAngle.Sumw2();
    
    HDstPiCosstarNoShape.SetNameTitle("HDstPiCosstarNoShape","HDstPiCosstarNoShape");
    HDstPiCosstarNoShape.SetBins(HMCXcosthCM->GetXaxis()->GetNbins(),HMCXcosthCM->GetXaxis()->GetXmin(),HMCXcosthCM->GetXaxis()->GetXmax());
    HDstPiCosstarNoShape.Sumw2();
    
    //efficiency corrected mass
    HDstPiMFineNoRes.SetNameTitle("HDstPiMFineNoRes","HDstPiMFineNoRes");
    HDstPiMFineNoRes.SetBins(2000,2.1,4.1);
    HDstPiMFineNoRes.Sumw2();
   
   
    //Generated mass vs p*   
    for(Int_t i=0;i<4;i++){
      HMCMVsPstar[i]=H2MCPvsMassFine->ProjectionY(TString("HMCMVsPstar")+(long)i,i+1,i+1,"");
      if(!HMCMVsPstar[i]){cout<<"bad projection of HMCMVsPstar"<<endl; return 0;}
    } 


    if(!CloseReducedFile())return 0;
  
  }


  ///
  Float_t eff;
  Float_t effnoshape;
  Int_t e=0;
  while(ReducedNtuple->GetEntry(e)){
    e++;
    if(e%50000==0)cout<<e<<" cands done"<<endl;


    //for checking eff shapes after 3-D eff correction
    if(_TruthMatch){
      effnoshape=GetEfficiencyNoShape();
      if(effnoshape>0){//note resolution is removed so should not attemp to fit these 
	HDstarHelicityNoShape.Fill(dstarhelicity,1./effnoshape);
	HDstPiMNoShape.Fill(dstpimdm-dstpidmres,1./effnoshape);
	HPstarNoShape.Fill(dstpipstar,1./effnoshape);
	HDstarAngleNoShape.Fill(dstarcostheta,1./effnoshape);
	HDstPiCosstarNoShape.Fill(dstpicosstar,1./effnoshape);
	H3PvsMVsDstA.Fill(dstpipstar,dstpimdm-dstpidmres,dstarcostheta,1./effnoshape);
	H2PvsM.Fill(dstpipstar,dstpimdm-dstpidmres,1./effnoshape);
      }

    }
    

    HDstPi.Fill(dstpimdm);   
    H2DstPiMVsDstarHelNoEff.Fill(dstpimdm,dstarhelicity);
    ///
    HDstPiDM.Fill(dstpideltam);
    if(fabs(dstarhelicity)>.75)
      HDstPiMD2420Helicity.Fill(dstpimdm);
    if(fabs(dstarhelicity)<.5)
      HDstPiMD2460Helicity.Fill(dstpimdm);

    //for bumps
    for(Int_t i=0;i<10;i++){
      if((-1+.2*i)<dstarhelicity&&dstarhelicity<(-1+.2*(i+1))){
	if(dstarcostheta<-.5)
	  HDstPiMVsHelicityN5Cos[i]->Fill(dstpimdm);
	
	if(dstarcostheta<-.9)
	  HDstPiMVsHelicityN9Cos[i]->Fill(dstpimdm);
	
      }
    }

    for(Int_t i=0;i<4;i++){
      if(.25*i<fabs(dstarhelicity)&&fabs(dstarhelicity)<.25*(i+1))
	HDstPiMVsCoarseAbsHelicity[i]->Fill(dstpimdm);
    }


    //
    for(Int_t i=0;i<10;i++){
      if((-1+.2*i)<dstarhelicity&&dstarhelicity<(-1+.2*(i+1))){
	HDstPiMVsHelicity[i]->Fill(dstpimdm);
      }
    }
    for(Int_t i=0;i<20;i++){
      if((-1+.1*i)<dstarhelicity&&dstarhelicity<(-1+.1*(i+1)))
	HDstPiMVsHelicityFine[i]->Fill(dstpimdm);
    }
    for(Int_t i=0;i<8;i++)
      if((-1+.25*i)<dstarhelicity&&dstarhelicity<(-1.+.25*(i+1)))
	HDstPiMVsHelicityMW[i]->Fill(dstpimdm);



    //p* dependence
    for(Int_t i=0;i<4;i++)
      if((3+.5*i)<dstpipstar&&dstpipstar<(3+.5*(i+1)))
	HDstPiMVsPstar[i]->Fill(dstpimdm); 
    for(Int_t i=0;i<10;i++)
      if((3+.2*i)<dstpipstar&&dstpipstar<(3+.2*(i+1)))
	HDstPiMVsPstarFine[i]->Fill(dstpimdm);
    Float_t energyfraction=sqrt(dstpimass*dstpimass+dstpipstar*dstpipstar)/(OnPeakEnergy/2.);
    for(Int_t i=0;i<30;i++)
      if((.5+.02*i)<energyfraction&&energyfraction<(.5+.02*(i+1)))
	HDstPiMVsEnergyFine[i]->Fill(dstpimdm);

    //cos D*
    for(Int_t i=0;i<20;i++)
      if((-1+.1*i)<dstarcostheta&&dstarcostheta<(-1.+.1*(i+1)))
	HDstPiMVsCosDstar[i]->Fill(dstpimdm);
    for(Int_t i=0;i<8;i++)
      if((-1+.25*i)<dstarcostheta&&dstarcostheta<(-1.+.25*(i+1)))
	HDstPiMVsCosDstarMW[i]->Fill(dstpimdm);
    for(Int_t i=0;i<4;i++){
      if((-1+.5*i)<dstarcostheta&&dstarcostheta<(-1.+.5*(i+1)))
	H2DstPiMVsHelCosDstar[i]->Fill(dstpimdm,dstarhelicity);
    }
    for(Int_t i=0;i<8;i++)
      if((-1+.25*i)<dstarcostheta&&dstarcostheta<(-1.+.25*(i+1))&&fabs(dstarhelicity)>.75)
	HDstPiMVsCosDstarD2420MW[i]->Fill(dstpimdm);
    for(Int_t i=0;i<8;i++)
      if((-1+.25*i)<dstarcostheta&&dstarcostheta<(-1.+.25*(i+1))&&fabs(dstarhelicity)<.5)
	HDstPiMVsCosDstarD2460MW[i]->Fill(dstpimdm);

        
    //phi dependence
    for(Int_t i=0;i<4;i++)
      if((-mypi+i*mypi/2)<dstpiphistar&&dstpiphistar<(-mypi+(i+1)*mypi/2))
	HDstPiMVsPhi[i]->Fill(dstpimdm);



    //cos* dependence
    for(Int_t i=0;i<4;i++)
      if((-1+.5*i)<dstpicosstar&&dstpicosstar<(-1.+(i+1)*.5))
	HDstPiMVsCos[i]->Fill(dstpimdm);



    // p* systematics
    for(Int_t i=0;i<4;i++)
      if((3+.5*i)<dstpipstar&&dstpipstar<(3+.5*(i+1)))
	HDstPiMVsPstarNoEff[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)>.75)
      for(Int_t i=0;i<4;i++)
	if((3+.5*i)<dstpipstar&&dstpipstar<(3+.5*(i+1)))
	  HDstPiMVsPstarD2420[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)<.5)
      for(Int_t i=0;i<4;i++)
	if((3+.5*i)<dstpipstar&&dstpipstar<(3+.5*(i+1)))
	  HDstPiMVsPstarD2460[i]->Fill(dstpimdm);
    
    //phi
    for(Int_t i=0;i<4;i++)
      if((-mypi+i*mypi/2)<dstpiphistar&&dstpiphistar<(-mypi+(i+1)*mypi/2))
	HDstPiMVsPhiNoEff[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)>.75)
      for(Int_t i=0;i<4;i++)
	if((-mypi+i*mypi/2)<dstpiphistar&&dstpiphistar<(-mypi+(i+1)*mypi/2))
	  HDstPiMVsPhiD2420[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)<.5)
      for(Int_t i=0;i<4;i++)
	if((-mypi+i*mypi/2)<dstpiphistar&&dstpiphistar<(-mypi+(i+1)*mypi/2))
	  HDstPiMVsPhiD2460[i]->Fill(dstpimdm);

    //cos(theta*)
    for(Int_t i=0;i<4;i++)
      if((-1+.5*i)<dstpicosstar&&dstpicosstar<(-1.+(i+1)*.5))
	HDstPiMVsCosNoEff[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)>.75)
      for(Int_t i=0;i<4;i++)
	if((-1+.5*i)<dstpicosstar&&dstpicosstar<(-1.+(i+1)*.5))
	  HDstPiMVsCosD2420[i]->Fill(dstpimdm);
    if(fabs(dstarhelicity)<.5)
      for(Int_t i=0;i<4;i++)
	if((-1+.5*i)<dstpicosstar&&dstpicosstar<(-1.+(i+1)*.5))
	  HDstPiMVsCosD2460[i]->Fill(dstpimdm);
   


    //
    eff=GetEfficiency();
    if(eff<=0)continue;

    HDstPiEff.Fill(dstpimdm,1./eff);  

    //angles
    HDstarAngle.Fill(dstarcostheta,1./eff);
    HDstarHelicity.Fill(dstarhelicity,1./eff);

    HDstPiMFineNoRes.Fill(dstpimdm-dstpidmres,1./eff);


    ///
    H2DstPiMVsDstarHel.Fill(dstpimdm,dstarhelicity,1./eff);
 
    for(Int_t i=0;i<NPROJS;i++){
      HDstPiMLegendre[i]->Fill(dstpimdm,((2.*i+1.)/2.)*legendre(i,dstarhelicity)/eff);
    }
    for(Int_t i=0;i<8;i++)//weight by P2
      if((-1+.25*i)<dstarcostheta&&dstarcostheta<(-1.+.25*(i+1)))
	HDstPiMP2VsCosDstar[i]->Fill(dstpimdm,((2.*2+1.)/2.)*legendre(2,dstarhelicity)/eff);



  }
  if(CloseReducedNtuple()!=1)return 0;
  CloseEfficiency();


  //   //add error from the efficiency;
  //   TArrayD* sw2=HDstPiMFine.GetSumw2();
  //   for(Int_t i=1;i<=HDstPiMFine.GetNbinsX();i++){
  //     (*sw2)[i]=(*sw2)[i]+pow(.0*HDstPiMFine.GetBinContent(i),2);    
  //     if(i>300&&i<350)cout<<" "<<HDstPiMFine.GetBinError(i)/HDstPiMFine.GetBinContent(i)<<endl;        
  //   }


  TFile HistosForFit(_OutputDir+"/HistosForFit.root","recreate");  

  if(_TruthMatch){
    HDstarHelicityNoShape.Write();
    HDstPiMNoShape.Write();
    HPstarNoShape.Write();
    HDstarAngleNoShape.Write();
    HDstPiCosstarNoShape.Write();
    H3PvsMVsDstA.Write();
    H2PvsM.Write();
    HDstPiMFineNoRes.Write();

    for(Int_t i=0;i<4;i++)      
      HMCMVsPstar[i]->Write();
    
  }

  //
  HDstPi.Write();
  HDstPiEff.Write();
  HDstPiMD2420Helicity.Write();
  HDstPiMD2460Helicity.Write();
  

  //
  HDstarHelicity.Write();
  HDstarAngle.Write();
  HDstPiDM.Write();

  for(Int_t i=0;i<10;i++)
    HDstPiMVsHelicityN5Cos[i]->Write();
  for(Int_t i=0;i<10;i++)
    HDstPiMVsHelicityN9Cos[i]->Write();
  for(Int_t i=0;i<10;i++)
    HDstPiMVsHelicity[i]->Write();
  for(Int_t i=0;i<20;i++)
    HDstPiMVsHelicityFine[i]->Write();
  for(Int_t i=0;i<8;i++)
    HDstPiMVsHelicityMW[i]->Write();

  for(Int_t i=0;i<4;i++)
    HDstPiMVsCoarseAbsHelicity[i]->Write();

  for(Int_t i=0;i<4;i++)
    HDstPiMVsPstar[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPstarNoEff[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPstarD2420[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPstarD2460[i]->Write();
  for(Int_t i=0;i<10;i++)
    HDstPiMVsPstarFine[i]->Write();
  for(Int_t i=0;i<30;i++)
    HDstPiMVsEnergyFine[i]->Write();


  for(Int_t i=0;i<20;i++)
    HDstPiMVsCosDstar[i]->Write();
  for(Int_t i=0;i<8;i++)
    HDstPiMVsCosDstarMW[i]->Write();
  for(Int_t i=0;i<4;i++)
    H2DstPiMVsHelCosDstar[i]->Write();
  for(Int_t i=0;i<8;i++)
    HDstPiMVsCosDstarD2420MW[i]->Write();
  for(Int_t i=0;i<8;i++)
    HDstPiMVsCosDstarD2460MW[i]->Write();



  for(Int_t i=0;i<4;i++)
    HDstPiMVsPhi[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPhiNoEff[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPhiD2420[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsPhiD2460[i]->Write();

  for(Int_t i=0;i<4;i++)
    HDstPiMVsCos[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsCosNoEff[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsCosD2420[i]->Write();
  for(Int_t i=0;i<4;i++)
    HDstPiMVsCosD2460[i]->Write();

  H2DstPiMVsDstarHel.Write();
  H2DstPiMVsDstarHelNoEff.Write();


  for(Int_t i=0;i<NPROJS;i++){
    HDstPiMLegendre[i]->Write();
  }
  for(Int_t i=0;i<8;i++)
    HDstPiMP2VsCosDstar[i]->Write();


  HistosForFit.ls();
  HistosForFit.Close();


  //-------------
  //clean up
  
  for(Int_t i=0;i<NPROJS;i++)
    delete HDstPiMLegendre[i];
  for(Int_t i=0;i<8;i++)
    delete HDstPiMP2VsCosDstar[i];
  
  for(Int_t i=0;i<10;i++)
    delete HDstPiMVsHelicityN5Cos[i];
  for(Int_t i=0;i<10;i++)
    delete HDstPiMVsHelicityN9Cos[i];
  for(Int_t i=0;i<10;i++)
    delete HDstPiMVsHelicity[i];  
  for(Int_t i=0;i<20;i++)
    delete HDstPiMVsHelicityFine[i];
  for(Int_t i=0;i<8;i++)
    delete HDstPiMVsHelicityMW[i];
 
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsCoarseAbsHelicity[i];

  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPstar[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPstarNoEff[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPstarD2420[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPstarD2460[i];
  for(Int_t i=0;i<10;i++)
    delete HDstPiMVsPstarFine[i];
  for(Int_t i=0;i<30;i++)
    delete HDstPiMVsEnergyFine[i];

  for(Int_t i=0;i<20;i++)
    delete HDstPiMVsCosDstar[i];
  for(Int_t i=0;i<8;i++)
    delete HDstPiMVsCosDstarMW[i];
  for(Int_t i=0;i<4;i++)
    delete H2DstPiMVsHelCosDstar[i];
  for(Int_t i=0;i<8;i++)
    delete HDstPiMVsCosDstarD2420MW[i];
  for(Int_t i=0;i<8;i++)
    delete HDstPiMVsCosDstarD2460MW[i];

  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPhi[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPhiNoEff[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPhiD2420[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsPhiD2460[i];

  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsCos[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsCosNoEff[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsCosD2420[i];
  for(Int_t i=0;i<4;i++)
    delete HDstPiMVsCosD2460[i];
  
 
  return 1;
}
Esempio n. 3
0
void drawingEff_TnPToyStudy::loadHisto(vector<string> *outname) {
  if (!finput) cout << "Cannot open input file!\n" << endl;

  TFile *foutput = new TFile(Form("%s_toySFToy.root",outname->at(1).c_str()),"recreate");

  setTDRStyle();
  gStyle->SetEndErrorSize(3);

  TLatex *lat = new TLatex(); lat->SetNDC(); lat->SetTextSize(0.035);
  TCanvas *can = new TCanvas("can","can",600,600);
  TLegend *leg = new TLegend(0.55,0.60,0.94,0.88);
  SetLegendStyle(leg);
 
  cout << "\n\t" << outname->at(0) << " " << outname->at(1) << endl;

  // Load denominator, numerators
  ///// Eff vs centrality in 4+1 |y| regions (6.5-50 GeV/c), forward & low pT region
  TObjArray *hnum = (TObjArray*)finput->Get("hnum_cent_rap0024");
  TObjArray *hden = (TObjArray*)finput->Get("hden_cent_rap0024");
  bool dofixcent = true; // True: Only if it is centrality drawing (no matter if it's pp or pbpb)
  TProfile *tp_cent_rap[5];
  tp_cent_rap[0] = simpleEff(hnum, hden, dofixcent);
  tp_cent_rap[0]->SetName("tnpSFToy_cent_rap0024");
  
  TH1F *haxes = new TH1F("haxes",";;Efficiency",1,0,100);
  haxes->GetXaxis()->SetTitle("Centrality [%]");
  haxes->Draw();

  tp_cent_rap[0]->Draw("same");
  leg->AddEntry(tp_cent_rap[0],"|y|: 0-2.4, 6.5-50 GeV/c","p");
  leg->Draw();
  lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
  can->SaveAs(Form("%s_cent_rap_0.png",outname->at(1).c_str()));
  can->SaveAs(Form("%s_cent_rap_0.pdf",outname->at(1).c_str()));

  vector<TH1*> tps;
  vector<string> yname;
 
  yname.push_back("$|y|<2.4$");
  tps.push_back(tp_cent_rap[0]);
  TString texname = outname->at(1) + "_cent.tex";
  inittex(texname.Data(), "Centrality [\\%]", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  delete can;
  delete leg;

  for (int i=0; i<nbins_4rap; i++) {
    TObjArray *hnum1 = (TObjArray*)finput->Get( Form("hnum_cent_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    TObjArray *hden1 = (TObjArray*)finput->Get( Form("hden_cent_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    tp_cent_rap[i+1] = simpleEff(hnum1, hden1, dofixcent);
    tp_cent_rap[i+1]->SetName( Form("tnpSFToy_cent_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    
    can = new TCanvas("can","can",600,600);
    leg = new TLegend(0.55,0.60,0.94,0.88);
    SetLegendStyle(leg);
    haxes->Draw();
    tp_cent_rap[i+1]->Draw("same");
    leg->AddEntry(tp_cent_rap[i+1],Form("|y|: %.1f-%.1f, 6.5-50 GeV/c",bins_4rap[i],bins_4rap[i+1]),"p");
    leg->Draw();
    lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
    can->SaveAs(Form("%s_cent_rap_%d.png",outname->at(1).c_str(),i+1));
    can->SaveAs(Form("%s_cent_rap_%d.pdf",outname->at(1).c_str(),i+1));
    
    yname.push_back(Form("$%0.1f<|y|<%0.1f$",bins_4rap[i],bins_4rap[i+1]));
    tps.push_back(tp_cent_rap[i+1]);

    delete can;
    delete leg;
  }
  texname = outname->at(1) + "_cent_pt65500.tex";
  inittex(texname.Data(), "Centrality [\\%]", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();


  hnum = (TObjArray*)finput->Get("hnum_cent_rap1824_pt3065");
  hden = (TObjArray*)finput->Get("hden_cent_rap1824_pt3065");
  tp_cent_rap[4] = simpleEff(hnum, hden, dofixcent);
  tp_cent_rap[4]->SetName("tnpSFToy_cent_rap1824_pt3065");
  
  can = new TCanvas("can","can",600,600);
  leg = new TLegend(0.55,0.60,0.94,0.88);
  SetLegendStyle(leg);
  haxes->Draw();
  tp_cent_rap[4]->Draw("same");
  leg->AddEntry(tp_cent_rap[4],Form("|y|: %.1f-%.1f, 3-6.5 GeV/c",bins_4rap[nbins_4rap-1],bins_4rap[nbins_4rap]),"p");
  leg->Draw();
  lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
  can->SaveAs(Form("%s_cent_rap_%d.png",outname->at(1).c_str(),nbins_4rap));
  can->SaveAs(Form("%s_cent_rap_%d.pdf",outname->at(1).c_str(),nbins_4rap));
  
  yname.push_back(Form("$%0.1f<|y|<%0.1f$, $3<p_{T}<6.5$ GeV/c",bins_4rap[nbins_4rap-1],bins_4rap[nbins_4rap]));
  tps.push_back(tp_cent_rap[4]);

  texname = outname->at(1) + "_cent_pt30500.tex";
  inittex(texname.Data(), "Centrality [\\%]", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  delete can;
  delete leg;  

  ///// Eff vs pT in 4+1 |y| regions
  hnum = (TObjArray*)finput->Get("hnum_pt_rap0024");
  hden = (TObjArray*)finput->Get("hden_pt_rap0024");
  dofixcent = false;
  TProfile *tp_pt_rap[5];
  tp_pt_rap[0] = simpleEff(hnum, hden, dofixcent);
  tp_pt_rap[0]->SetName("tnpSFToy_pt_rap0024");

  haxes->GetXaxis()->SetTitle("p_{T} [GeV/c]");
  haxes->SetBins(1,0,50);
  
  can = new TCanvas("can","can",600,600);
  leg = new TLegend(0.6,0.68,0.9,0.88);
  SetLegendStyle(leg);
  haxes->Draw();
  tp_pt_rap[0]->Draw("same");
  if (ispbpb) leg->AddEntry(tp_pt_rap[0],"|y|: 0-2.4, 0-100%","p");
  else leg->AddEntry(tp_pt_rap[0],"|y|: 0-2.4","p");
  leg->Draw();
  lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
  can->SaveAs(Form("%s_pt_rap_0.png",outname->at(1).c_str()));
  can->SaveAs(Form("%s_pt_rap_0.pdf",outname->at(1).c_str()));

  yname.push_back("$|y|<2.4$");
  tps.push_back(tp_pt_rap[0]);
  
  // |y| ranges have different number of pT bins -> create separate tex files
  texname = outname->at(1) + "_pt_rap__rap0024.tex";
  inittex(texname.Data(), "$p_{T}$ $\\mathrm{[GeV/c]}$", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  delete can;
  delete leg;

  for (int i=0; i<nbins_4rap-1; i++) {
    TObjArray *hnum1 = (TObjArray*)finput->Get( Form("hnum_pt_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    TObjArray *hden1 = (TObjArray*)finput->Get( Form("hden_pt_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    tp_pt_rap[i+1] = simpleEff(hnum1, hden1, dofixcent);
    tp_pt_rap[i+1]->SetName( Form("tnpSFToy_pt_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    
    can = new TCanvas("can","can",600,600);
    leg = new TLegend(0.6,0.68,0.9,0.88);
    SetLegendStyle(leg);
    haxes->Draw();
    tp_pt_rap[i+1]->Draw("same");
    if (ispbpb) leg->AddEntry(tp_pt_rap[i+1],Form("|y|: %.1f-%.1f, 0-100%%",bins_4rap[i],bins_4rap[i+1]),"p");
    else leg->AddEntry(tp_pt_rap[i+1],Form("|y|: %.1f-%.1f",bins_4rap[i],bins_4rap[i+1]),"p");
    leg->Draw();
    lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
    can->SaveAs(Form("%s_pt_rap_%d.png",outname->at(1).c_str(),i+1));
    can->SaveAs(Form("%s_pt_rap_%d.pdf",outname->at(1).c_str(),i+1));

    yname.push_back(Form("$%0.1f<|y|<%0.1f$",bins_4rap[i],bins_4rap[i+1]));
    tps.push_back(tp_pt_rap[i+1]);

    delete can;
    delete leg;
  }
  texname = outname->at(1) + "_pt_rap__pt65500.tex";
  inittex(texname.Data(), "$p_{T}$ $\\mathrm{[GeV/c]}$", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  for (int i=nbins_4rap-1; i<nbins_4rap; i++) {
    TObjArray *hnum1 = (TObjArray*)finput->Get( Form("hnum_pt_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    TObjArray *hden1 = (TObjArray*)finput->Get( Form("hden_pt_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    tp_pt_rap[i+1] = simpleEff(hnum1, hden1, dofixcent);
    tp_pt_rap[i+1]->SetName( Form("tnpSFToy_cent_rap%02.0f%02.0f",bins_4rap[i]*10,bins_4rap[i+1]*10) );
    
    can = new TCanvas("can","can",600,600);
    leg = new TLegend(0.6,0.68,0.9,0.88);
    SetLegendStyle(leg);
    haxes->Draw();
    tp_pt_rap[i+1]->Draw("same");
    if (ispbpb) leg->AddEntry(tp_pt_rap[i+1],Form("|y|: %.1f-%.1f, 0-100%%",bins_4rap[i],bins_4rap[i+1]),"p");
    else leg->AddEntry(tp_pt_rap[i+1],Form("|y|: %.1f-%.1f",bins_4rap[i],bins_4rap[i+1]),"p");
    leg->Draw();
    lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
    can->SaveAs(Form("%s_pt_rap_%d.png",outname->at(1).c_str(),i+1));
    can->SaveAs(Form("%s_pt_rap_%d.pdf",outname->at(1).c_str(),i+1));

    yname.push_back(Form("$%0.1f<|y|<%0.1f$",bins_4rap[i],bins_4rap[i+1]));
    tps.push_back(tp_pt_rap[i+1]);

    delete can;
    delete leg;
  }
  texname = outname->at(1) + "_pt_rap__pt30500.tex";
  inittex(texname.Data(), "$p_{T}$ $\\mathrm{[GeV/c]}$", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  ///// Eff vs pT in 3 centrality regions
  TProfile *tp_pt_cent[nbins_3cent];
  for (int i=0; i<(ispbpb?nbins_3cent-1:1); i++) { // for pp, only the 1st bin is necessary
    TObjArray *hnum1 = (TObjArray*)finput->Get( Form("hnum_pt_cent%02.0f%02.0f",bins_3cent[i],bins_3cent[i+1]) );
    TObjArray *hden1 = (TObjArray*)finput->Get( Form("hden_pt_cent%02.0f%02.0f",bins_3cent[i],bins_3cent[i+1]) );
    tp_pt_cent[i] = simpleEff(hnum1, hden1, dofixcent);
    tp_pt_cent[i]->SetName( Form("tnpSFToy_pt_cent%02.0f%02.0f",bins_3cent[i],bins_3cent[i+1]) );
    
    haxes->GetXaxis()->SetTitle("p_{T} [GeV/c]");
    haxes->SetBins(1,0,50);
    
    can = new TCanvas("can","can",600,600);
    leg = new TLegend(0.67,0.68,0.9,0.88);
    SetLegendStyle(leg);
    haxes->Draw();
    tp_pt_cent[i]->Draw("same");
    if (ispbpb) {
      leg->AddEntry(tp_pt_cent[i],Form("%.0f-%.0f%%",bins_3cent[i],bins_3cent[i+1]),"p");
      leg->Draw();
    }
    lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
    lat->DrawLatex(0.2,0.80,"|y| < 2.4");
    can->SaveAs(Form("%s_pt_cent_%d.png",outname->at(1).c_str(),i));
    can->SaveAs(Form("%s_pt_cent_%d.pdf",outname->at(1).c_str(),i));

    yname.push_back(Form("$%0.1f-%0.1f$\\%%",bins_3cent[i],bins_3cent[i+1]));
    tps.push_back(tp_pt_cent[i]);

    delete can;
    delete leg;
  }
  if (ispbpb) {
    hnum = (TObjArray*)finput->Get( Form("hnum_pt_cent%02.0f%03.0f",bins_3cent[nbins_3cent-1],bins_3cent[nbins_3cent]) );
    hden = (TObjArray*)finput->Get( Form("hden_pt_cent%02.0f%03.0f",bins_3cent[nbins_3cent-1],bins_3cent[nbins_3cent]) );
    tp_pt_cent[nbins_3cent-1] = simpleEff(hnum, hden, dofixcent);
    tp_pt_cent[nbins_3cent-1]->SetName( Form("tnpSFToy_pt_cent%02.0f%03.0f",bins_3cent[nbins_3cent-1],bins_3cent[nbins_3cent]) );
    
    haxes->GetXaxis()->SetTitle("p_{T} [GeV/c]");
    haxes->SetBins(1,0,50);
    
    can = new TCanvas("can","can",600,600);
    leg = new TLegend(0.67,0.68,0.9,0.88);
    SetLegendStyle(leg);
    haxes->Draw();
    tp_pt_cent[nbins_3cent-1]->Draw("same");
    leg->AddEntry(tp_pt_cent[nbins_3cent-1],Form("%.0f-%.0f%%",bins_3cent[nbins_3cent-1],bins_3cent[nbins_3cent]),"p");
    leg->Draw();
    lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
    lat->DrawLatex(0.2,0.80,"|y| < 2.4");
    can->SaveAs(Form("%s_pt_cent_%d.png",outname->at(1).c_str(),nbins_3cent));
    can->SaveAs(Form("%s_pt_cent_%d.pdf",outname->at(1).c_str(),nbins_3cent));

    yname.push_back(Form("$%0.1f-%0.1f$\\%%",bins_3cent[nbins_3cent-1],bins_3cent[nbins_3cent]));
    tps.push_back(tp_pt_cent[nbins_3cent-1]);
    
    delete can;
    delete leg;
  }

  texname = outname->at(1) + "_pt_cent.tex";
  inittex(texname.Data(), "$p_{T}$ $\\mathrm{[GeV/c]}$", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  ///// Eff vs rap integrated
  hnum = (TObjArray*)finput->Get("hnum_rap");
  hden = (TObjArray*)finput->Get("hden_rap");
  TProfile *tp_rap = simpleEff(hnum, hden, dofixcent);
  tp_rap->SetName("tnpSFToy_rap");

  haxes->GetXaxis()->SetTitle("|y|");
  haxes->SetBins(1,0,2.4);
    
  can = new TCanvas("can","can",600,600);
  haxes->Draw();
  tp_rap->Draw("same");
  lat->DrawLatex(0.2,0.85,outname->at(0).c_str());
  lat->DrawLatex(0.2,0.80,"6.5-50 GeV/c");
  if (ispbpb) lat->DrawLatex(0.2,0.75,"0-100%");
  can->SaveAs(Form("%s_rap.png",outname->at(1).c_str()));
  can->SaveAs(Form("%s_rap.pdf",outname->at(1).c_str()));

  yname.push_back("$6.5<p_{T}<50$ GeV/c, 0-100%");
  tps.push_back(tp_rap);

  texname = outname->at(1) + "_rap.tex";
  inittex(texname.Data(), "|y|", yname);
  printHist(tps, texname.Data());
  closetex(texname.Data());
  tps.clear();
  yname.clear();

  delete can;
  delete lat;


  ///// Write TProfiles into .root file
  foutput->cd();
  for (int i=0; i<5; i++)
    tp_cent_rap[i]->Write();
  for (int i=0; i<nbins_4rap+1; i++) 
    tp_pt_rap[i]->Write();
  for (int i=0; i<(ispbpb?nbins_3cent:1); i++) 
    tp_pt_cent[i]->Write();
  tp_rap->Write(); 
  

  // Clean up memory
  for (int i=0; i<5; i++)
    delete tp_cent_rap[i];
  for (int i=0; i<nbins_4rap+1; i++) 
    delete tp_pt_rap[i];
  for (int i=0; i<(ispbpb?nbins_3cent:1); i++) 
    delete tp_pt_cent[i];
  delete tp_rap;
}
Esempio n. 4
0
double runGraphicHistos(TString ifile = "tmpfitdirc.root", bool verbose_out=true, double ienergy = 5, int iupdown = 0)
{
	TCanvas *c1 = new TCanvas("myc1","myc1",1000,1200);

	if (verbose_out == false)
	{
		//supresses canvas message 
		//gErrorIgnoreLevl=kInfo;
		gROOT->ProcessLine( "gErrorIgnoreLevel = kWarning;");
	}

	double hmin = -100;
	double hmax = 100;

	hmin = -25;
	hmax = 25;

	double energy = ienergy;

	double pi_mass = .13957;
	double k_mass = .49367;

	double pi_beta = sqrt(1-pi_mass*pi_mass/(energy*energy));
	double k_beta = sqrt(1-k_mass*k_mass/(energy*energy));

	double quartz_index = 1.47;

	double pi_mrad = 1000*acos(1/(pi_beta*quartz_index));
	double k_mrad = 1000*acos(1/(k_beta*quartz_index));

	//double seperation = 5.2;
	double seperation = pi_mrad - k_mrad;
	double mean_pion = 0;
	double mean_kaon = mean_pion + seperation;
	if (verbose_out == true)
	{
		printf("Energy: %4.02f\n",energy);
		printf("Mrad Seperation: %8.03f\n",seperation);
	}
	//denominator pf spread

	TRandom3* randgen = new TRandom3();

	TFile *f1 = new TFile(ifile);
	TH1F *hpion;
	TH1F *hkaon;
	TH1F *phots_pion;
	if (iupdown == 0)
	{
		hpion = (TH1F*) f1->Get("ll_diff_pion");
		hkaon = (TH1F*) f1->Get("ll_diff_kaon");
		phots_pion = (TH1F*) f1->Get("phot_found_pion");
	}
	else if (iupdown == 1)
	{
		hpion = (TH1F*) f1->Get("ll_diff_pion_up");
		hkaon = (TH1F*) f1->Get("ll_diff_kaon_up");
		phots_pion = (TH1F*) f1->Get("phot_found_pion_up");
	}
	else if (iupdown == -1)
	{
		hpion = (TH1F*) f1->Get("ll_diff_pion_down");
		hkaon = (TH1F*) f1->Get("ll_diff_kaon_down");
		phots_pion = (TH1F*) f1->Get("phot_found_pion_down");
	}
	else
	{
		printf("Unrecognize updown arguement: %d \nFailing....\n",iupdown);
		return -1;
	}
	if (verbose_out == true)
	{
		printf("pion_ll mean, spread: %12.04f, %12.04f\n",hpion->GetMean(),hpion->GetRMS());
		printf("kaon_ll mean, spread: %12.04f, %12.04f\n",hkaon->GetMean(),hkaon->GetRMS());
	}
	double spread = seperation/2;
	double spreadsq2 = 2*spread*spread;
	//Swap kaon and pion numbers

	for (int i = 1; i < hpion->GetNbinsX()/2; i++)
	{
		double t_swap = hpion->GetBinContent(i);
		hpion->SetBinContent(i,hpion->GetBinContent(hpion->GetNbinsX() - i + 1));
		hpion->SetBinContent(hpion->GetNbinsX() - i + 1, t_swap);
	}
	for (int i = 1; i < hkaon->GetNbinsX()/2; i++)
	{
		double t_swap = hkaon->GetBinContent(i);
		hkaon->SetBinContent(i,hkaon->GetBinContent(hkaon->GetNbinsX() - i + 1));
		hkaon->SetBinContent(hkaon->GetNbinsX() - i + 1, t_swap);
	}

	double titlesize=1.2*.04;

	int rebin = 20;
	rebin = 160;
	hpion->Rebin(rebin);
	hkaon->Rebin(rebin);

	hpion->SetAxisRange(hmin,hmax);
	hkaon->SetAxisRange(hmin,hmax);

	hkaon->GetXaxis()->SetTitle("Loglikelihood difference");
	hkaon->GetYaxis()->SetTitle("A.U.");
	hkaon->GetXaxis()->SetTitleSize(titlesize);
	hkaon->GetYaxis()->SetTitleSize(titlesize);

	hpion->SetStats(false);
	hkaon->SetStats(false);

	hpion->SetLineColor(kCyan);
	//hpion->SetFillColorAlpha(kRed,.5);

	hkaon->SetLineColor(kBlue);
	//hkaon->SetFillColorAlpha(kBlue,.5);

	TLegend *leg_ll = new TLegend(.6,.6,.8,.8);
	leg_ll->AddEntry(hpion,"Pion");
	leg_ll->AddEntry(hkaon,"Kaon");
	leg_ll->SetBorderSize(0);


	hkaon->SetTitle("log(P(Pi)/P(K)) for actual Pi (red) and K (blue) at 5 GeV");



	TH1F *pion_veto_eff = new TH1F(*hpion);
	TH1F *kaon_missid = new TH1F(*hkaon);

	pion_veto_eff->SetName("pion_veto_eff");
	pion_veto_eff->SetTitle("");

	kaon_missid->SetName("kaon_missid");
	kaon_missid->SetTitle("");



	for (int i = 0; i < pion_veto_eff->GetNbinsX(); i++)
	{
		pion_veto_eff->SetBinContent(i,hpion->Integral(0,i));
		kaon_missid->SetBinContent(i,hkaon->Integral(i,kaon_missid->GetNbinsX()));
		//	printf("%12.04f %12.04f %d\n",1,1,i);
	}

	pion_veto_eff->SetAxisRange(0,10000,"Y");

	double scale_int = 1/hpion->Integral(0,pion_veto_eff->GetNbinsX());
	pion_veto_eff->Scale(scale_int);
	scale_int = 1/hkaon->Integral(0,kaon_missid->GetNbinsX());
	kaon_missid->Scale(scale_int);

	hkaon->SetTitle("");
	hpion->SetTitle("");


	if (verbose_out == true)
	{
		hkaon->Draw();
		hpion->Draw("SAME H");
		leg_ll->Draw("SAME");
		c1->SetWindowSize(1000,800);

		c1->Print("overlap.pdf");
	}

	if (verbose_out == true)
	{
		pion_veto_eff->Draw("");
		kaon_missid->Draw("SAME H");

		c1->SetWindowSize(1000,800);
		c1->Print("overlap_integral.pdf");
	}

	double linewidth=6;
	TGraph* roc_graph;
	int roc_n = pion_veto_eff->GetNbinsX();
	TVectorF xr(roc_n);//gross
	TVectorF yr(roc_n);
	double ival = 0;

	for (int i = 0; i < pion_veto_eff->GetNbinsX(); i++)
	{
		xr[i] = pion_veto_eff->GetBinContent(i);
		yr[i] = kaon_missid->GetBinContent(i);

		//	printf("%8.04f %8.04f\n",xr[i],yr[i]);	
	}

	double y1,y2,x1,x2;
	x1 = pion_veto_eff->GetBinContent(0);
	double last_x = pion_veto_eff->GetBinContent(0);
	double last_y = kaon_missid->GetBinContent(0);

	for (int i = 0; i < pion_veto_eff->GetNbinsX()-1; i++)
	{
		ival += (yr[i]+last_y)*(xr[i] - last_x)/2;
                //printf("%6d %12.09f %12.04f %12.04f %12.04f %12.04f\n",i,ival,xr[i],yr[i],last_x,last_y);
		last_x = xr[i];
		last_y = yr[i];
		
	}

	if (verbose_out == true)
	{
		printf("ROC integral: %12.04f\n",ival);
	}

	roc_graph = new TGraph(xr,yr);
	roc_graph->SetLineColor(2);
	roc_graph->SetLineWidth(4);
	//roc_graph->SetMarkerColor(4);
	//roc_graph->SetMarkerStyle(21);
	roc_graph->SetTitle("");
	roc_graph->GetXaxis()->SetTitle("Kaon Efficiency");
	roc_graph->GetYaxis()->SetTitle("Pion Rejection");
	roc_graph->GetXaxis()->SetTitleSize(titlesize);
	roc_graph->GetYaxis()->SetTitleSize(titlesize);
	roc_graph->GetXaxis()->SetLimits(0,1.01);
	roc_graph->SetMinimum(0);
	roc_graph->SetMaximum(1.01);
	roc_graph->SetLineWidth(linewidth);

	if (verbose_out == true)
	{
		roc_graph->Draw("ACP");
		c1->Print("roc_curve.gif");
	}
	spread = find_sig_val(seperation,ival,spread); 


	//FAKE version stuff below
	/*---------------------------------------------------------------------------------------------------------------------------*/




	TH1F *fhpion = (TH1F*) f1->Get("ll_diff_pion");
	TH1F *fhkaon = (TH1F*) f1->Get("ll_diff_kaon");

	fhpion->Reset();
	fhkaon->Reset();

	fhpion->SetBins(1000,hmin,hmax);
	fhkaon->SetBins(1000,hmin,hmax);

	double pion_obs, kaon_obs;
	double pion_ll_diff, kaon_ll_diff;

	for (int ii = 0; ii < 10000; ii++)
	{
		pion_obs = randgen->Gaus(mean_pion,spread);
		kaon_obs = randgen->Gaus(mean_kaon,spread);

		pion_ll_diff = -1*(pion_obs - mean_pion)*(pion_obs - mean_pion);
		pion_ll_diff += (pion_obs - mean_kaon)*(pion_obs - mean_kaon);
		pion_ll_diff /= spreadsq2;

		kaon_ll_diff = - (kaon_obs - mean_pion)*(kaon_obs - mean_pion);
		kaon_ll_diff += (kaon_obs - mean_kaon)*(kaon_obs - mean_kaon);
		kaon_ll_diff /= spreadsq2;

		fhpion->Fill(pion_ll_diff);
		fhkaon->Fill(kaon_ll_diff);
	}


	fhpion->SetAxisRange(hmin,hmax);
	fhkaon->SetAxisRange(hmin,hmax);

	fhpion->SetLineColor(kRed);
	//hpion->SetFillColorAlpha(kRed,.5);

	fhkaon->SetLineColor(kBlue);
	//hkaon->SetFillColorAlpha(kBlue,.5);

	fhkaon->SetTitle("");

	TH1F *fpion_veto_eff = new TH1F(*fhpion);
	TH1F *fkaon_missid = new TH1F(*fhkaon);

	fpion_veto_eff->SetName("pion_veto_eff");
	fpion_veto_eff->SetTitle("");

	fkaon_missid->SetName("kaon_missid");
	fkaon_missid->SetTitle("");

	for (int i = 0; i < fpion_veto_eff->GetNbinsX(); i++)
	{
		fpion_veto_eff->SetBinContent(i,fhpion->Integral(i,fpion_veto_eff->GetNbinsX()));
		fkaon_missid->SetBinContent(i,fhkaon->Integral(0,i));
	}

	fpion_veto_eff->SetAxisRange(0,10000,"Y");

	double fscale_int = 1/fhpion->Integral(0,fpion_veto_eff->GetNbinsX());
	fpion_veto_eff->Scale(fscale_int);
	fscale_int = 1/fhkaon->Integral(0,fkaon_missid->GetNbinsX());
	fkaon_missid->Scale(fscale_int);




	TGraph* froc_graph;
	int froc_n = fpion_veto_eff->GetNbinsX();
	TVectorF fxr(froc_n);
	TVectorF fyr(froc_n);

	double fival = 0;
	double flast_x = fpion_veto_eff->GetBinContent(0);
	double flast_y = fkaon_missid->GetBinContent(0);
	for (int i = 0; i < fpion_veto_eff->GetNbinsX(); i++)
	{
		fxr[i] = fpion_veto_eff->GetBinContent(i);
		fyr[i] = fkaon_missid->GetBinContent(i);


		fival -= (fyr[i]+flast_y)*(fxr[i] - flast_x)/2;
		flast_x = fxr[i];
		flast_y = fyr[i];
	}
	ival = 0;
	flast_x = fpion_veto_eff->GetBinContent(0);
	flast_y = fkaon_missid->GetBinContent(0);
	for (int i = 0; i < fpion_veto_eff->GetNbinsX(); i++)
	{

		//Why oh why is Erf not the standard definition
		double t = hmin + i*(hmax-hmin)/fpion_veto_eff->GetNbinsX();
		fxr[i] = .5 + TMath::Erf(t/(sqrt(2)*spread))/2;
		fyr[i] = .5 - TMath::Erf((t-seperation)/(sqrt(2)*spread))/2;

		fival -= (fyr[i]+flast_y)*(fxr[i] - flast_x)/2;
		flast_x = fxr[i];
		flast_y = fyr[i];
	}	
	//printf("Fake ROC integral: %12.04f\n",fival);


	froc_graph = new TGraph(fxr,fyr);
	if (verbose_out == true)
	{
		froc_graph->SetLineColor(4);
		froc_graph->SetLineWidth(linewidth);
		froc_graph->SetLineStyle(2);
		froc_graph->SetTitle("");
		froc_graph->GetXaxis()->SetTitle("\"Kaon Efficiency\"");
		froc_graph->GetYaxis()->SetTitle("\"Pion Rejection\"");
		froc_graph->GetXaxis()->SetTitleSize(titlesize);
		froc_graph->GetYaxis()->SetTitleSize(titlesize);
		froc_graph->GetXaxis()->SetLimits(0,1.01);
		froc_graph->SetMinimum(0);
		froc_graph->SetMaximum(1.01);


		roc_graph->SetFillColorAlpha(kWhite,1);
		froc_graph->SetFillColorAlpha(kWhite,1);
		TLegend *leg_roc = new TLegend(.3,.5,.7,.7);
		leg_roc->AddEntry(roc_graph,"ROC Curve");
		leg_roc->AddEntry(froc_graph,"Matched Gaussian ROC Curve");
		leg_roc->SetBorderSize(0);
		leg_roc->SetTextSize(0.04*1.1);

		froc_graph->Draw("SAME");
		leg_roc->Draw("SAME");
		c1->Print("roc_curve_overlay.pdf");
	}



	if (verbose_out == true)
	{
		printf("Matching resolution: %6.03f\n",spread);
		printf("Matching resolution per photon: %6.03f\n",spread*sqrt(phots_pion->GetMean()));
	}
	else
	{
		printf("%6.04f\n",spread);
	}
	return spread;

}