コード例 #1
0
ファイル: getXsecExtended.C プロジェクト: ikrav/usercode
void getXsecExtended(const TString mc_input, int debugMode=0, bool useFEWZ=true, int fineGrid=0)
{
  // check whether it is a calculation
  if (mc_input.Contains("_DebugRun_")) {
    std::cout << "getXsec: _DebugRun_ detected. Terminating the script\n";
    return;
  }

  if (debugMode==1) std::cout << "\n\n\tDEBUG MODE is ON\n\n";
  if (debugMode==-1) std::cout << "\n\n\tPLOT ONLY MODE is ON\n\n";
  std::cout << "DYTools::analysisTag=" << DYTools::analysisTag << "\n";

  if (DYTools::study2D && fineGrid) {
    std::cout << "fineGrid=1 is allowed only for study2D=0\n";
    return;
  }

  // normal calculation

  gBenchmark->Start("getXsec");

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //==============================================================================================================
  
  //Bool_t doSave  = false;    // save plots?
  TString format = "png";   // output file format
  MCInputFileMgr_t inpMgr;
  TString fineGridStr;
  if (fineGrid==1) fineGridStr="fineGrid_";
  else if (fineGrid==2) fineGridStr="summer2011Grid_";
  else if (fineGrid==3) fineGridStr="summer2011specGrid_";
  
  Double_t massLow  = DYTools::massBinLimits[0];
  Double_t massHigh = DYTools::massBinLimits[DYTools::nMassBins];

  // fine grid: 0, 1, 2, ..., (fineGrid_1GeVstop-1), fineGrid_1GeVstop, fineGrid_1GeVstop+fineGridLargerStep, fineGrid_1GeVstop+2*fineGridLargerStep, ..., 1500
  //
  double fineGrid_1GeVstop=200.;
  double fineGrid_LargerStep=20.;

  int locMassBinCount=DYTools::nMassBins;
  double *massRangeEdges=NULL;
  if (!fineGrid) {
    massRangeEdges=new double[locMassBinCount+1];
    for (int i=0; i<=DYTools::nMassBins; ++i) {
      massRangeEdges[i]=DYTools::massBinLimits[i];
    }
  }
  else if (fineGrid==1) {
    // 1GeV grid
    locMassBinCount=int(fineGrid_1GeVstop-massLow+1e-3);
    std::cout << "1GeV grid size=" << locMassBinCount << "\n";
    // fineGrid_LargerStep
    double upperRange= (massHigh-fineGrid_1GeVstop);
    double upperCount= upperRange/fineGrid_LargerStep + 1e-3;
    if (upperCount - trunc(upperCount) > 1e-3) {
      std::cout << "(upperCount -1e-3)=" << (upperCount -1e-3) << "\n";
      std::cout << "should be integer value\n";
      return;
    }
    locMassBinCount += int(upperCount);
    std::cout << "mass grid[" << locMassBinCount << "]\n";

    massRangeEdges=new double[locMassBinCount+1];
    double m=massLow, dm=1;
    int count=0;
    while (m<=massHigh) {
      massRangeEdges[count]=m;
      if (1) {
	if (m<massHigh) std::cout << "count=" << count << ", massRange=" << m << " .. " << (m+dm) << "\n";
	else std::cout << "last edge=" << m << "\n";
      }
      count++;
      m+=dm;
      if (m==fineGrid_1GeVstop) dm=fineGrid_LargerStep;
    }
  }
  else if (fineGrid==2) {
    const int nMassBinTh=518;
    Double_t mxl = 14.0;
    locMassBinCount=nMassBinTh;
    massRangeEdges=new double[nMassBinTh+1];

    for(int iTh=0; iTh<=nMassBinTh; iTh++){
      // mass limits
      if     ( iTh >=   0 && iTh <  11 ) {mxl += 1.0;}
      else if( iTh >=  11 && iTh <  18 ) {mxl += 5.0;}
      else if( iTh >=  18 && iTh < 118 ) {mxl += 1.0;}
      else if( iTh >= 118 && iTh < 340 ) {mxl += 2.0;}
      else if( iTh >= 340 && iTh < nMassBinTh)   {mxl += 5.0; }
      else if( iTh == nMassBinTh)                {mxl = 1500; }
      massRangeEdges[iTh]=mxl;
    }
  }
  else if (fineGrid==3) {
    const int nMassBinTh=518;
    Double_t mxl = 14.0;
    locMassBinCount=nMassBinTh;
    massRangeEdges=new double[nMassBinTh+1];

    int iTh_new=0, tmpCounter=0;
    for(int iTh=0; iTh<=nMassBinTh; iTh++, iTh_new++){
      // mass limits
      if     ( iTh >=   0 && iTh <  11 ) {mxl += 1.0;}
      else if( iTh >=  11 && iTh <  18 ) {mxl += 5.0;}
      else if( iTh >=  18 && iTh < 118 ) {mxl += 1.0;}
      else if( iTh >= 118 && iTh < 340 ) {
	if (iTh>=139) {
	  std::cout << "iTh=" << iTh << ", current mxl=" << mxl << " +2\n";
	  if (iTh==139) tmpCounter=10;
	  tmpCounter--;
	  if (tmpCounter>0) iTh_new--;
	  else if (tmpCounter==0) {
	    tmpCounter=10;
	    std::cout << "iTh=" << iTh << ", iTh_new=" << iTh_new << ", current mxl=" << mxl << " +10\n";
	  }
	}
	mxl += 2.0;
      }
      else if( iTh >= 340 && iTh < nMassBinTh)   {
	std::cout << "iTh=" << iTh << ", current mxl=" << mxl << " +5\n";
	if (iTh<342) iTh_new--;
	else {
	  if ((iTh-342)%4>0) iTh_new--;
	  else std::cout << "iTh=" << iTh << ", iTh_new=" << iTh_new << ", current mxl=" << mxl << " +10\n";
	}
	mxl += 5.0; 
      }
      else if( iTh == nMassBinTh)                {mxl = 1500; }
      massRangeEdges[iTh_new]=mxl;
    }
    massRangeEdges[iTh_new-2]=1500.;
    std::cout << "iTh_new=" << iTh_new << "\n";
    locMassBinCount=iTh_new-2;
  }

  TVectorD massGrid(locMassBinCount+1);
  for (int i=0; i<=locMassBinCount; ++i) massGrid[i]=massRangeEdges[i];
  TH1F hMassIdx("h_massIdx","h_massIdx",locMassBinCount-1,massRangeEdges);
  //delete massRangeEdges;

  if (0) {
    printHisto(&hMassIdx);
    if (0) {
      for (int i=0; i<int(massHigh); ++i) {
	double m=i+0.5;
	std::cout << "i=" << i << ", m=" << m << ", idx=" << (hMassIdx.FindBin(m)-1) << "\n";
      }
    }
    return;
  }

  //std::cout << "massHigh=" << massHigh << "\n";
  //std::cout << "locMassBinCount=" << locMassBinCount << "\n";

  if (!inpMgr.Load(mc_input)) {
    return;
  }
  
  //--------------------------------------------------------------------------------------------------------------
  // Main code 
  //==============================================================================================================
  
  //  
  // Set up histograms
  //

  //vector<TH1D*> hZMassv;
  
  Double_t   nZv = 0;

  TMatrixD nEvents (locMassBinCount,DYTools::nYBinsMax);    // number of weigthed events
  TMatrixD nEventsDET (locMassBinCount,DYTools::nYBinsMax); // number of weighted events in the detector acceptance
  TMatrixD nEventsDETrecoPostIdx (locMassBinCount,DYTools::nYBinsMax);    // number of weigthed events
  TMatrixD w2Events (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD w2EventsDET (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD w2EventsDETrecoPostIdx (locMassBinCount,DYTools::nYBinsMax);
  Double_t nZpeak=0, w2Zpeak=0;
  double nZpeakDET=0, w2ZpeakDET=0;
  double nZpeakDETrecoPostIdx=0, w2ZpeakDETrecoPostIdx=0;

  nEvents = 0;
  w2Events = 0;
  nEventsDET = 0;
  w2EventsDET  = 0;
  nEventsDETrecoPostIdx = 0;
  w2EventsDETrecoPostIdx  = 0;

  //char hname[100];
  //for(UInt_t ifile = 0; ifile<fnamev.size(); ifile++) {
  //  sprintf(hname,"hZMass_%i",ifile); hZMassv.push_back(new TH1F(hname,"",500,0,500)); hZMassv[ifile]->Sumw2();
  //}

  // 
  // Read weights from a file
  //
  const bool useFewzWeights = useFEWZ;
  const bool cutZPT100 = true;
  FEWZ_t fewz(useFewzWeights,cutZPT100);
  if (useFewzWeights && !fewz.isInitialized()) {
    std::cout << "failed to prepare FEWZ correction\n";
    throw 2;
  }

  //
  // Access samples and fill histograms
  //  
  TFile *infile=0;
  TTree *eventTree=0;  
    
  // Data structures to store info from TTrees
  mithep::TGenInfo *gen  = new mithep::TGenInfo();

  // loop over samples  
  double lumi0=0;
  if (debugMode!=-1) {
  for(UInt_t ifile=0; ifile<inpMgr.fileNames().size(); ifile++) {
  
    // Read input file
    cout << "Processing " << inpMgr.fileName(ifile) << "..." << endl;
    infile = new TFile(inpMgr.fileName(ifile));
    assert(infile);

    // Get the TTrees
    eventTree = (TTree*)infile->Get("Events"); assert(eventTree);

    // Find weight for events for this file
    // The first file in the list comes with weight 1,
    // all subsequent ones are normalized to xsection and luminosity
    double lumi  = eventTree->GetEntries()/inpMgr.xsec(ifile);
    if (ifile==0) lumi0=lumi;
    double scale = lumi0/lumi;
    std::cout << "       -> sample weight is " << scale << endl;

    // Set branch address to structures that will store the info  
    eventTree->SetBranchAddress("Gen",&gen);
    TBranch *genBr = eventTree->GetBranch("Gen");
 
    // loop over events    
    nZv += scale * eventTree->GetEntries();

    for(UInt_t ientry=0; ientry<eventTree->GetEntries(); ientry++) {
      if (debugMode && (ientry>100000)) break;

      genBr->GetEntry(ientry);
      if (ientry%1000000==0) printProgress("ientry=",ientry,eventTree->GetEntriesFast());

      double massPreFsr = gen->vmass;   // pre-FSR
      double yPreFsr = gen->vy;    // pre-FSR
      double massPostFsr = gen->mass;   // post-FSR
      double yPostFsr = gen->y;    // post-FSR

      if ((massPreFsr < massLow) || (massPreFsr > massHigh)) continue;
      if ((fabs(yPreFsr) < DYTools::yRangeMin) || 
	  (fabs(yPreFsr) > DYTools::yRangeMax)) continue;

      int ibinMassPreFsr=-1, ibinYPreFsr=-1;
      int ibinMassPostFsr=-1, ibinYPostFsr=-1;

      if (!fineGrid) {
	ibinMassPreFsr = DYTools::findMassBin(massPreFsr);
	ibinMassPostFsr= DYTools::findMassBin(massPostFsr);
	ibinYPreFsr = DYTools::findAbsYBin(ibinMassPreFsr, yPreFsr);
	ibinYPostFsr= DYTools::findAbsYBin(ibinMassPostFsr, yPostFsr);
      }
      else {
	ibinMassPreFsr=hMassIdx.FindBin(massPreFsr)-1;
	ibinMassPostFsr=hMassIdx.FindBin(massPostFsr)-1;
	ibinYPreFsr=0;
	ibinYPostFsr=0;
	//printf("massPreFsr=%8.4lf, idx=%3d;  massPostFsr=%8.4lf, idx=%3d\n", massPreFsr,ibinMassPreFsr, massPostFsr,ibinMassPostFsr);
      }

      // We are only interested in the events, reconstructed with 
      // good mass and rapidity 
      if (ibinMassPreFsr==-1 || 
	  ibinMassPreFsr>=locMassBinCount || 
	  ibinYPreFsr==-1) {
	printf(".. skipping mass=%6.4lf, y=%6.4lf. ibinMass=%d, ibinY=%d\n",massPreFsr,yPreFsr,ibinMassPreFsr,ibinYPreFsr);
	continue;
      }

      // Find FEWZ-powheg reweighting factor 
      // that depends on pre-FSR Z/gamma* rapidity, pt, and mass
      double fewz_weight = 1.0;

      if(useFewzWeights) {
	fewz_weight=fewz.getWeight(gen->vmass,gen->vpt,gen->vy);
      }

      //std::cout << "weight=scale*gen->weight*fewz: " << scale << " * " << gen->weight << " * " << fewz_weight << "\n";
      double fullWeight = scale * gen->weight * fewz_weight;
      nEvents(ibinMassPreFsr,ibinYPreFsr) += fullWeight;
      w2Events(ibinMassPreFsr,ibinYPreFsr) += fullWeight*fullWeight;

      // Pre-FSR cross section
      if( DYTools::goodEtPair(gen->vpt_1, gen->vpt_2) &&
	  DYTools::goodEtaPair(gen->veta_1, gen->veta_2) ) {
	nEventsDET(ibinMassPreFsr,ibinYPreFsr) += fullWeight;
	w2EventsDET(ibinMassPreFsr,ibinYPreFsr) += fullWeight*fullWeight;
      }

      // Post-FSR cross section
      if(  (ibinMassPostFsr!=-1) && (ibinYPostFsr!=-1) &&
	   DYTools::goodEtPair(gen->pt_1, gen->pt_2) &&
	   DYTools::goodEtaPair(gen->eta_1, gen->eta_2) ) {
	nEventsDETrecoPostIdx(ibinMassPostFsr,ibinYPostFsr) += fullWeight;
	w2EventsDETrecoPostIdx(ibinMassPostFsr,ibinYPostFsr) += fullWeight*fullWeight;
      }
    }
    delete infile;
    infile=0, eventTree=0;
  }
  delete gen;

  // Determine Z-peak event count
  for (int i=0; i<locMassBinCount; i++) {
    int isZpeak=0;
    // bin idx is (i+1)
    if ((hMassIdx.GetBinLowEdge(i+1)>=60-1e-3) 
	&& (hMassIdx.GetBinLowEdge(i+1+1)<=120+1e-3)) isZpeak=1;
    if (isZpeak) {
      int yiMax=(fineGrid) ? 1:DYTools::nYBins[i];
      for (int yi=0; yi<yiMax; ++yi) {
	nZpeak += nEvents(i,yi);
	w2Zpeak += w2Events(i,yi);
	nZpeakDET += nEventsDET(i,yi);
	w2ZpeakDET += w2EventsDET(i,yi);
	nZpeakDETrecoPostIdx += nEventsDETrecoPostIdx(i,yi);
	w2ZpeakDETrecoPostIdx += w2EventsDETrecoPostIdx(i,yi);
      }
    }
  }
  std::cout << "\n";
  std::cout << "nZpeak=" << nZpeak << ", w2Zpeak=" << w2Zpeak << "\n";
  std::cout << "nZpeakDET=" << nZpeakDET << ", w2ZpeakDET=" << w2ZpeakDET << "\n";
  std::cout << "nZpeakDETrecoPostIdx=" << nZpeakDETrecoPostIdx << ", w2ZpeakDETrecoPostIdx=" << w2ZpeakDETrecoPostIdx << "\n";
  std::cout << "\n";


  if (nZpeak==0) {
    std::cout << "no events in the Z-peak region\n";
    return ;
  }
  } // if (debugMode!=-1)


  // Containers of the normalized event counts
  TMatrixD nEventsNorm (locMassBinCount,DYTools::nYBinsMax);    // number of weigthed events, normalized to Z-peak
  TMatrixD nEventsDETNorm (locMassBinCount,DYTools::nYBinsMax); // number of weighted events in the detector acceptance, normalized to Z-peak
  TMatrixD nEventsDETrecoPostIdxNorm (locMassBinCount,DYTools::nYBinsMax); // number of weighted events in the detector acceptance, normalized to Z-peak
  TMatrixD nEventsNormErr (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD nEventsDETNormErr (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD nEventsDETrecoPostIdxNormErr (locMassBinCount,DYTools::nYBinsMax);

  TMatrixD nEventsErr (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD nEventsDETErr (locMassBinCount,DYTools::nYBinsMax);
  TMatrixD nEventsDETrecoPostIdxErr (locMassBinCount,DYTools::nYBinsMax);

  nEventsNorm=0;
  nEventsDETNorm=0;
  nEventsDETrecoPostIdxNorm=0;
  nEventsNormErr=0;
  nEventsDETNormErr=0;
  nEventsDETrecoPostIdxNormErr=0;

  nEventsErr=0;
  nEventsDETErr=0;

  if (debugMode!=-1) {
  for(int i=0; i<locMassBinCount; i++) {
    int yiMax=(fineGrid) ? 1:DYTools::nYBins[i];
    for (int j=0; j<yiMax; j++) {
      nEventsErr(i,j)=sqrt(w2Events(i,j));
      nEventsDETErr(i,j)=sqrt(w2EventsDET(i,j));
      nEventsDETrecoPostIdxErr(i,j)=sqrt(w2EventsDETrecoPostIdx(i,j));

      nEventsNorm(i,j) = nEvents(i,j)/nZpeak;
      nEventsNormErr(i,j) = 
	getErrorOnRatio(nEvents(i,j),nEventsErr(i,j),
			nZpeak, sqrt(w2Zpeak));

      nEventsDETNorm(i,j) = nEventsDET(i,j)/nZpeakDET;
      nEventsDETNormErr(i,j) =
	getErrorOnRatio(nEventsDET(i,j),nEventsDETErr(i,j),
			nZpeakDET, sqrt(w2ZpeakDET));

      nEventsDETrecoPostIdxNorm(i,j) = nEventsDETrecoPostIdx(i,j)/nZpeakDETrecoPostIdx;
      nEventsDETrecoPostIdxNormErr(i,j) =
	getErrorOnRatio(nEventsDETrecoPostIdx(i,j),nEventsDETrecoPostIdxErr(i,j),
			nZpeakDETrecoPostIdx, sqrt(w2ZpeakDETrecoPostIdx));
    }
  }
  }



  TString outFile= TString("../root_files/xSecThExt_");
  //outFile.Append("2MCfiles_");
  if (!useFewzWeights) outFile.Append("noFEWZ_");
  if (fineGridStr.Length()) outFile.Append(fineGridStr);
  if (debugMode==1) outFile.Append("debug_");
  outFile.Append( DYTools::analysisTag + TString("_tmp.root") );


  TVectorD rapidityGrid(massGrid.GetNoElements()-1);
  rapidityGrid=1;

  if (debugMode!=-1) {
    TFile thFile(outFile,"recreate");
    massGrid.Write("massBinEdges");
    if (fineGrid) rapidityGrid.Write("rapidityBinCount");
    nEvents.Write("nGenEvents");
    nEventsErr.Write("nGenEventsErr");
    nEventsDET.Write("nGenEventsDET");
    nEventsDETErr.Write("nGenEventsDETErr");
    nEventsDETrecoPostIdx.Write("nGenEventsDETrecoPostIdx");
    nEventsDETrecoPostIdxErr.Write("nGenEventsDETRecoPostIdxErr");
    nEventsNorm.Write("nGenEventsNorm");
    nEventsNormErr.Write("nGenEventsNormErr");
    nEventsDETNorm.Write("nGenEventsDETNorm");
    nEventsDETNormErr.Write("nGenEventsDETNormErr");
    nEventsDETrecoPostIdxNorm.Write("nGenEventsDETrecoPostIdxNorm");
    nEventsDETrecoPostIdxNormErr.Write("nGenEventsDETrecoPostIdxNormErr");
    TVectorD zPeakInfo(6);
    zPeakInfo(0)=nZpeak; zPeakInfo(1)=sqrt(w2Zpeak);
    zPeakInfo(2)=nZpeakDET; zPeakInfo(3)=sqrt(w2ZpeakDET);
    zPeakInfo(4)=nZpeakDETrecoPostIdx; zPeakInfo(5)=sqrt(w2ZpeakDETrecoPostIdx);
    zPeakInfo.Write("zPeakCountAndErr");
    thFile.Close();
    std::cout << "file <" << outFile << "> created\n";
  }
  else {
    TFile thFile(outFile);
    nEvents.Read("nGenEvents");
    nEventsErr.Read("nGenEventsErr");
    nEventsDET.Read("nGenEventsDET");
    nEventsDETErr.Read("nGenEventsDETErr");
    nEventsDETrecoPostIdx.Read("nGenEventsDETrecoPostIdx");
    nEventsDETrecoPostIdxErr.Read("nGenEventsDETRecoPostIdxErr");
    nEventsNorm.Read("nGenEventsNorm");
    nEventsNormErr.Read("nGenEventsNormErr");
    nEventsDETNorm.Read("nGenEventsDETNorm");
    nEventsDETNormErr.Read("nGenEventsDETNormErr");
    nEventsDETrecoPostIdxNorm.Read("nGenEventsDETrecoPostIdxNorm");
    nEventsDETrecoPostIdxNormErr.Read("nGenEventsDETrecoPostIdxNormErr");
    TVectorD zPeakInfo(6);
    zPeakInfo.Read("zPeakCountAndErr");
    thFile.Close();
    
    nZpeak=zPeakInfo[0]; w2Zpeak=SQR(zPeakInfo[1]);
    nZpeakDET=zPeakInfo[2]; w2ZpeakDET=SQR(zPeakInfo[3]);
    nZpeakDETrecoPostIdx=zPeakInfo[4]; w2ZpeakDETrecoPostIdx=SQR(zPeakInfo[5]);
    std::cout << "file <" << outFile << "> loaded\n";
  }

  //--------------------------------------------------------------------------------------------------------------
  // Make plots 
  //==============================================================================================================  
  CPlot::sOutDir="plots" + DYTools::analysisTag;

  TString fileNamePlots=outFile;
  fileNamePlots.ReplaceAll("_tmp.root","_plots_tmp.root");
  TFile *filePlots=new TFile(fileNamePlots,"recreate");
  if (!filePlots) {
    std::cout << "failed to create file <" << fileNamePlots << ">\n";
    throw 2;
  }
  // string buffers
  //char ylabel[50];   // y-axis label


  if (DYTools::study2D) {
    TString c2Dname="canvXsectTh_2D";
    if (useFewzWeights) c2Dname.Append("_FEWZ");
    TCanvas *c2D = MakeCanvas(c2Dname,c2Dname,600,600+300*DYTools::study2D);
    std::vector<TH1F*> hXsecV;
    std::vector<CPlot*> cpV;
    hXsecV.reserve(DYTools::nMassBins);
    cpV.reserve(DYTools::nMassBins);
    for (int iM=0; iM<DYTools::nMassBins; ++iM) {
      double massMin=DYTools::massBinLimits[iM];
      double massMax=DYTools::massBinLimits[iM+1];
      CPlot *cp=new CPlot(Form("cpMass%2.0lf_%2.0lf",massMin,massMax),
			  "","|Y|","counts");
      cpV.push_back(cp);
      hXsecV.push_back( plotXsec2D("xsec",iM,nEvents,nEventsErr,cp,kBlack,1) );
    }
    c2D->Divide(2,3);
    for (int ipad=1; ipad<=6; ++ipad) {
      cpV[ipad]->Draw(c2D,false,"png",ipad);
    }
    c2D->Update();
    SaveCanvas(c2D,c2Dname);
  }

  {
    TString canvName=TString("canvXsectTh_") + fineGridStr + TString("1D");
    if (useFewzWeights) canvName.Append("_FEWZ");
    TCanvas *c1D = MakeCanvas(canvName,canvName,600,600);
    CPlot *cp=new CPlot("cplot_1D","","M_{ee} (GeV)","counts");
    TH1F *hXsec= plotXsec1D("xsec1D",nEvents,nEventsErr,cp,kBlue, fineGrid);
    hXsec->SetDirectory(0);
    cp->SetLogx();
    cp->Draw(c1D,false,"png");
    c1D->Update();
    SaveCanvas(c1D,canvName);
  }
	
  {
    TString canvName=TString("canvXsectThNorm_") + fineGridStr + TString("1D");
    if (useFewzWeights) canvName.Append("_FEWZ");
    TCanvas *c1D = MakeCanvas(canvName,canvName,600,600);
    CPlot *cp=new CPlot("cplotNorm_1D","","M_{ee} (GeV)","normalized counts");
    TH1F *hXsec= plotXsec1D("xsec1D",nEventsNorm,nEventsNormErr,cp,kBlue, fineGrid);
    hXsec->SetDirectory(0);
    cp->SetLogx();
    cp->Draw(c1D,false,"png");
    c1D->Update();
    SaveCanvas(c1D,canvName);
  }
	
  /* 

 // Z mass
  sprintf(ylabel,"a.u. / %.1f GeV/c^{2}",hZMassv[0]->GetBinWidth(1));
  CPlot plotZMass1("zmass1","","m(Z) [GeV/c^{2}]",ylabel);
  for(UInt_t i=0; i<fnamev.size(); i++) { 
    plotZMass1.AddHist1D(hZMassv[i],labelv[i],"hist",colorv[i],linev[i]); 
  }
  plotZMass1.SetLogy();
  plotZMass1.Draw(c);
  SaveCanvas(c, "zmass1");

  PlotMatrixVariousBinning(accv, "acceptance", "LEGO2",filePlots);
  filePlots->Close();
  if (DYTools::study2D==0)
    Plot1D(accv,accErrv,"acceptance1D","acceptance");
  //delete filePlots;
  
  */
  //--------------------------------------------------------------------------------------------------------------
  // Summary print out
  //==============================================================================================================

  if (!fineGrid) {
    const int printSystErr=0;
    TMatrixD zeroErr=nEventsErr; zeroErr=0;
    printYields("nGenEvents", nEvents,nEventsErr,zeroErr, printSystErr);
    printYields("nGenEventsDET", nEventsDET,nEventsDETErr,zeroErr, printSystErr);
    printYields("nGenEventsDETrecoPostIdx",nEventsDETrecoPostIdx,nEventsDETrecoPostIdxErr, zeroErr,printSystErr);
  }

  gBenchmark->Show("getXsec");
}
コード例 #2
0
ファイル: studyDYeeCS.C プロジェクト: andjuo/DYee2015
void createSystFile(TVersion_t inpVer,
		    CrossSection_t &eeCS, TVaried_t var, int doSave)
{
  std::cout << "createSystFile for inpVer=" << versionName(inpVer) << "\n";

  if (var!=_varRhoSyst) {
    std::cout << "createSystFile is not ready for var="
	      << variedVarName(var) << "\n";
    return;
  }

  TCanvas *c= eeCS.plotCrossSection("cs");
  c->Update();
  TH1D *h1cs_file= cloneHisto(eeCS.h1PreFsrCS(),"h1cs_file","h1cs_file");

  std::vector<TH1D*> h1RhoV;
  TString fname="dyee-rho-syst2.root";
  TFile fin(fname);
  if (!fin.IsOpen()) {
    std::cout << "failed to open the file <" << fin.GetName() << ">\n";
    return;
  }
  for (TTnPSystType_t syst= _tnpSyst_none; syst!=_tnpSyst_last; next(syst)) {
    TString hname="h1rho_";
    if (syst==_tnpSyst_none) hname.Append("stat");
    else hname.Append(tnpSystName(syst,1));
    TString newHName=hname;
    if (syst==_tnpSyst_none) newHName.ReplaceAll("stat","orig");
    TH1D *h1= loadHisto(fin,hname,newHName,1,h1dummy);
    h1RhoV.push_back(h1);
  }
  fin.Close();

  TCanvas *cRho=NULL;
  if (1) {
    //plotHisto(eeCS.h1Rho(),"cRho",1,0,"LPE","cs version");
    h1RhoV[0]->GetYaxis()->SetTitleOffset(1.6);
    logAxis(h1RhoV[0],1,"M_{ee} [GeV]","","\\langle\\rho\\rangle\\text{ with alt.effs}");
    cRho=plotHisto(h1RhoV[0],"cRho",1,0,"LP","orig");
    setLeftRightMargins(cRho,0.14,0.05);
    for (unsigned int i=1; i<h1RhoV.size(); i++) {
      TString legStr=h1RhoV[i]->GetName();
      legStr.ReplaceAll("h1rho_","");
      plotHistoSame(h1RhoV[i],"cRho","LP",legStr);
    }
    moveLegend(cRho,0.45,0);
  }

  std::vector<TH1D*> h1rhoRelDiffV;
  int absValues=1;
  int relativeDiff=1;
  deriveRelSyst(NULL,h1RhoV,h1rhoRelDiffV,relativeDiff,absValues);
  if (0) {
    std::cout << " : " << h1RhoV.size() << ", " << h1rhoRelDiffV.size() << "\n";
    for (unsigned int i=0; i<h1rhoRelDiffV.size(); i++) {
      printRatio(h1RhoV[0], h1RhoV[i+1]);
      printHisto(h1rhoRelDiffV[i]);
    }
    return;
  }

  TCanvas *cRhoSyst=NULL;
  if (1) {
    TString title="uncertainty";
    if (relativeDiff) title.Prepend("relative ");
    //h1rhoRelDiffV[0]->SetTitle(title);
    h1rhoRelDiffV[0]->GetYaxis()->SetRangeUser(0,0.12);
    h1rhoRelDiffV[0]->GetYaxis()->SetTitleOffset(1.6);
    TString ytitle="\\delta\\langle\\rho\\rangle";
    if (relativeDiff) ytitle= "(" + ytitle + ")_{rel}";
    //h1rhoRelDiffV[0]->GetYaxis()->SetTitle(ytitle);
    logAxis(h1rhoRelDiffV[0],3,"M_{ee} [GeV]",ytitle,title);
    cRhoSyst=plotHisto(h1rhoRelDiffV[0],"cRhoRelDiff",1,0,"LP",
				tnpSystName(TTnPSystType_t(1),1));
    setLeftRightMargins(cRhoSyst,0.14,0.05);
    for (TTnPSystType_t syst=TTnPSystType_t(2); syst!=_tnpSyst_last; next(syst))
      {
	plotHistoSame(h1rhoRelDiffV[syst-1],"cRhoRelDiff","LP",tnpSystName(syst,1));
      }
    moveLegend(cRhoSyst,0.45,0.47);
  }

  std::vector<TH1D*> h1csV;
  eeCS.var(_varRho);
  for (unsigned int i=0; i<h1RhoV.size(); i++) {
    eeCS.h1Rho(h1RhoV[i]);
    TString hname="h1cs_" + tnpSystName(TTnPSystType_t(i));
    TH1D *h1cs= cloneHisto(eeCS.calcCrossSection(0),hname,hname);
    copyStyle(h1cs,h1RhoV[i]);
    h1csV.push_back(h1cs);
  }
  std::cout << "h1csV.size=" << h1csV.size() << "\n";

  if (1) {
    TCanvas *cRhoCS= plotHisto(h1cs_file,"cRhoCS",1,1,"LP","cs_file");
    for (unsigned int i=0; i<h1csV.size(); i++) {
      plotHistoSame(h1csV[i],"cRhoCS","LP",h1csV[i]->GetName());
    }
    cRhoCS->Update();
  }

  std::vector<TH1D*> h1csRelDiffV;
  deriveRelSyst(NULL,h1csV,h1csRelDiffV,relativeDiff,absValues);
  TCanvas *cCSRhoUnc=NULL;
  if (1) {
    logAxis(h1csRelDiffV[0],3,"M_{ee} [GeV]",
	    "(\\delta\\sigma)_{\\langle\\rho\\rangle\\,syst;rel}",
	    "relative uncertainty of the cross section");
    h1csRelDiffV[0]->GetYaxis()->SetRangeUser(0,0.15);
    h1csRelDiffV[0]->GetYaxis()->SetTitleOffset(1.6);
    cCSRhoUnc= plotHisto(h1csRelDiffV[0],"cCSRhoUnc",1,0,"LP",
			 tnpSystName(TTnPSystType_t(1),1));
    setLeftRightMargins(cCSRhoUnc,0.14,0.05);
    for (TTnPSystType_t syst=TTnPSystType_t(2); syst!=_tnpSyst_last; next(syst))
      {
	plotHistoSame(h1csRelDiffV[syst-1],"cCSRhoUnc","LP",tnpSystName(syst,1));
      }
    moveLegend(cCSRhoUnc,0.45,0.47);
  }

  if (1) {
    for (unsigned int i=0; i<h1csRelDiffV.size(); i++) {
      std::cout << "\n\ni=" << i << "\n";
      printRatio(h1csRelDiffV[i],h1rhoRelDiffV[i]);
    }
  }

  if (doSave) {
    TString fname="dyeeCS-rhoSyst2.root";
    TFile fout(fname,"RECREATE");
    if (!fout.IsOpen()) {
      std::cout << "failed to create a file <" << fout.GetName() << ">\n";
      return;
    }
    for (unsigned int ih=0; ih<h1RhoV.size(); ih++) {
      h1RhoV[ih]->Write();
    }
    for (unsigned int ih=0; ih<h1rhoRelDiffV.size(); ih++) {
      h1rhoRelDiffV[ih]->Write();
    }
    for (unsigned int ih=0; ih<h1csRelDiffV.size(); ih++) {
      h1csRelDiffV[ih]->Write();
    }
    if (cRho) cRho->Write();
    if (cRhoSyst) cRhoSyst->Write();
    if (cCSRhoUnc) cCSRhoUnc->Write();
    TObjString timeTag(DayAndTimeTag(0));
    timeTag.Write("timeTag");
    fout.Close();
    std::cout << "file <" << fout.GetName() << "> created\n";
  }


  return;
}
コード例 #3
0
int plotUnfoldingMatrixRooUnfold(
		        int analysisIs2D,
			const TString conf,
			DYTools::TRunMode_t runMode=DYTools::NORMAL_RUN,
			DYTools::TSystematicsStudy_t systMode=DYTools::NO_SYST,
			TString rndStudyStr=""
			) {

  const double FSRmassDiff=1.; // largest energy of FSR photon to consider


  // check whether it is a calculation
  if (conf.Contains("_DebugRun_")) {
    std::cout << "plotUnfoldingMatrix: _DebugRun_ detected. Terminating the script\n";
    return retCodeOk;
  }

  // normal calculation
  gBenchmark->Start("makeUnfoldingMatrix");

   {
    DYTools::printExecMode(runMode,systMode);
    const int debug_print=1;
    if (!DYTools::checkSystMode(systMode,debug_print,12,
				DYTools::NO_SYST, DYTools::SYST_RND,
				DYTools::RESOLUTION_STUDY, DYTools::FSR_STUDY,
				DYTools::PU_STUDY,
				DYTools::FSR_5plus, DYTools::FSR_5minus,
				DYTools::PILEUP_5plus, DYTools::PILEUP_5minus,
				//DYTools::ESCALE_STUDY,
				DYTools::ESCALE_RESIDUAL,
				DYTools::FSR_RND_STUDY, DYTools::PU_RND_STUDY))
      return retCodeError;
  }

  if (!DYTools::setup(analysisIs2D)) {
    std::cout << "failed to initialize the analysis\n";
    return retCodeError;
  }

  int escaleResidual_global=1;

  //--------------------------------------------------------------------------------------------------------------
  // Settings
  //==============================================================================================================

  InputFileMgr_t inpMgr;
  InputFileMgr_t *yieldInpMgr=NULL; // needed for ESCALE_RESIDUAL
  if (!inpMgr.Load(conf)) return retCodeError;

  // plotDetResponse uses escale!
  if (systMode==DYTools::ESCALE_RESIDUAL) {
    yieldInpMgr= new InputFileMgr_t(inpMgr);
    // create a temporary object to set proper directories
    EventSelector_t tmpEventSelector(*yieldInpMgr,runMode,
		   DYTools::APPLY_ESCALE,"","",EventSelector::_selectDefault);
    if (escaleResidual_global) {
      inpMgr.rootFileBaseDir("root_files_reg_EScaleResidualGlobal");
      std::cout << "changed rootFileBaseDir to <" << inpMgr.rootFileBaseDir()
		<< ">\n";
    }
  }
  else if (systMode!=DYTools::RESOLUTION_STUDY) {
    // no energy correction for this evaluation
    inpMgr.clearEnergyScaleTag();
  }
  else {
    if (inpMgr.energyScaleTag() == "UNCORRECTED") {
      std::cout << "RESOLUTION_STUDY needs energy scale correction\n";
      return retCodeError;
    }
  }

  // Construct eventSelector, update mgr and plot directory
  TString extraTag=rndStudyStr;
  EventSelector_t evtSelector(inpMgr,runMode,systMode,
			      extraTag, "", EventSelector::_selectDefault);
  evtSelector.setTriggerActsOnData(false);

  // PU and FSR RND studies have to provide the seed externally
  int globalSeed=-1;
  for (int i=0; (globalSeed<=0) && (i<rndStudyStr.Length()); ++i) {
    globalSeed=atoi(rndStudyStr.Data() + i);
  }

  // Event weight handler
  EventWeight_t evWeight;
  int res=evWeight.init(inpMgr.puReweightFlag(),inpMgr.fewzFlag(),
			systMode,rndStudyStr);
  // May 01, 2014: PU weights have to be applied at all steps
  //EventWeight_t evWeightNoPU; // for FSR unfolding weights
  //if (res) res=evWeightNoPU.init(0,inpMgr.fewzFlag(),systMode,rndStudyStr);
  if (!res) {
    std::cout << "failed to prepare weights\n";
    return retCodeError;
  }

  // Prepare output directory
  inpMgr.constDir(systMode,1);


  int seedMin=inpMgr.userKeyValueAsInt("SEEDMIN");
  int seedMax=inpMgr.userKeyValueAsInt("SEEDMAX");
  int dSeed=1;
  int seedDiff=(systMode==DYTools::FSR_STUDY) ? 3 : (seedMax-seedMin+1);

  //std::cout << "seedMin..seedMax=" << seedMin << ".." << seedMax << "\n";

  //return retCodeOk;

  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code
  //==============================================================================================================


  std::cout << mainpart;

  TRandom random;
  std::vector<ElectronEnergyScale*> escaleV;
  std::vector<EventWeight_t*> specEWeightsV;
  std::vector<double> specReweightsV;
  std::vector<EventSelector_t*> evtSelectorV;
  std::vector<TH2D*> specTH2DWeightV; // used for ESCALE_RESIDUAL

  double specWeight=1.;
  int useSpecWeight=0;
  if (systMode==DYTools::FSR_5plus) { specWeight=1.05; useSpecWeight=1; }
  else if (systMode==DYTools::FSR_5minus) { specWeight=0.95; useSpecWeight=1; }
  else if (systMode==DYTools::FSR_RND_STUDY) useSpecWeight=1;

  // check random seed. Special weights use their own,
  // built-in dependencies on seed
  {
    int startSeed=-1;
    if (systMode==DYTools::SYST_RND) {
      std::cout << "setting startSeed=" << globalSeed << "\n";
      startSeed= globalSeed;
    }
    random.SetSeed(startSeed);
    gRandom->SetSeed(startSeed);
  }

  // The random seeds are needed only if we are running this script in systematics mode

  if (systMode==DYTools::FSR_STUDY) {
    specReweightsV.reserve(seedDiff);
    specEWeightsV.reserve(seedDiff);
    for (int i=0; i<seedDiff; ++i) {
      double specW= 1 + 0.05*(i-1);
      specReweightsV.push_back(specW);
      specEWeightsV.push_back(new EventWeight_t(evWeight));
    }
  }
  else if (systMode==DYTools::PU_STUDY) {
    if (inpMgr.puReweightFlag()==0) {
      std::cout << "systMode=PU_STUDY needs puReweightFlag=1 in the input file\n";
      return retCodeError;
    }
    specEWeightsV.reserve(2);
    for (int i=0; i<2; ++i) {
      DYTools::TSystematicsStudy_t study=(i==0) ? DYTools::PILEUP_5minus : DYTools::PILEUP_5plus;
      EventWeight_t *ew=new EventWeight_t();
      if (!ew->init(inpMgr.puReweightFlag(),inpMgr.fewzFlag(),study,rndStudyStr)) {
	std::cout << "in plotUnfoldingMatrix.C\n";
	return retCodeError;
      }
      specEWeightsV.push_back(ew);
    }
  }
  else if (systMode==DYTools::SYST_RND) {
    // nothing special about weights
  }
  else if (systMode==DYTools::RESOLUTION_STUDY) {
    if (seedMax==-111) {
      seedMin=-111;
      seedMax= 111;
      dSeed=seedMax-seedMin;
      seedDiff=2;
    }
    if (seedMax < seedMin) {
      printf("error: randomSeedMax=%d, seedMin=%d\n",seedMax,seedMin);
      return retCodeError;
    }
    specEWeightsV.reserve(seedDiff); // not used, but needed as a check
    escaleV.reserve(seedDiff);
    for (int i=seedMin; i<=seedMax; i+=dSeed) {
      TString escaleTag=inpMgr.energyScaleTag() +
	TString(Form("_MIRROR_RANDOMIZED%d",i));
      ElectronEnergyScale *ees= new ElectronEnergyScale(escaleTag);
      if (1) {
	std::cout << "randomSeed=" << i << ". EScale=";
	ees->print();
	std::cout<<"\n";
      }
      escaleV.push_back(ees);
      specEWeightsV.push_back(new EventWeight_t(evWeight));
      EventSelector_t *evtSel=new EventSelector_t(evtSelector,ees);
      // correction acts like on data!
      evtSel->setEScaleCorrectionType(DYTools::DATA,DYTools::ESCALE_STUDY_RND);
      //evtSel->editECName().Append(Form("_idx%d",i+seedMin));
      evtSelectorV.push_back(evtSel);
    }
  }

  // prepare tools for ESCALE_RESIDUAL
  TH2D* h2ShapeWeights=NULL;
  if (systMode==DYTools::ESCALE_RESIDUAL) {
    if (!yieldInpMgr) {
      std::cout << "yieldInpMgr had to be created\n";
      return retCodeError;
    }
    DYTools::TSystematicsStudy_t yieldSystMode=DYTools::APPLY_ESCALE;
    TString shapeFName=yieldInpMgr->signalYieldFullFileName(yieldSystMode,1);
    delete yieldInpMgr; // no longer needed
    if (rndStudyStr.Length()) {
      shapeFName.ReplaceAll(TString("__") + rndStudyStr,"");
    }
    TString subdir="ShapeReweight";
    TString field="zeeMCShapeReweight_";
    TString ddBkg=(inpMgr.userKeyValueAsInt("DDBKG")==1) ? "ddBkg" : "mcBkg";
    field.Append(ddBkg);
    std::cout << "Obtaining shape weights from <"
	      << shapeFName << ">"
	      << "(use" << ddBkg << ")\n";
    h2ShapeWeights=LoadHisto2D(field,shapeFName,subdir,1);
    if (!h2ShapeWeights) {
      std::cout << "failed to find histo \"ZeeMCShapeReweight\"\n";
      return retCodeError;
    }
    if ((DYTools::massBinningSet==DYTools::_MassBins_2012) &&
	(DYTools::study2D==1)) {
      HERE("set weights for the underflow bin to 1.");
      int ibin=1;
      for (int jbin=1; jbin<=24; jbin++) {
	h2ShapeWeights->SetBinContent(ibin,jbin, 1.);
	h2ShapeWeights->SetBinError  (ibin,jbin, 0.);
      }
    }
    std::cout << "shapeWeights:\n"; printHisto(h2ShapeWeights);

    int ensembleSize= inpMgr.userKeyValueAsInt("RESIDUAL_STUDY_SIZE");
    if (ensembleSize<=0) ensembleSize=100;
    ensembleSize++;
    std::cout << "EScale_residual ensemble size=" << ensembleSize
	      << " (one added for non-randomized entry)\n";

    std::vector<TString> tmpLabelV; // local variable for testing
    specTH2DWeightV.reserve(ensembleSize);
    tmpLabelV.reserve(ensembleSize);

    specTH2DWeightV.push_back(Clone(h2ShapeWeights,
				    "h2NonRndShapeW","h2NonRndShapeW"));
    tmpLabelV.push_back("NonRndShape");

    if (!escaleResidual_global) {
      TH2D *h2ResApply=Clone(h2ShapeWeights,"h2ResApply");
      // vary randomly and independently in each bin
      // prepare histo for randomization. Assume 10% error on the deviation
      for (int ibin=1; ibin<=h2ResApply->GetNbinsX(); ++ibin) {
	for (int jbin=1; jbin<=h2ResApply->GetNbinsY(); ++jbin) {
	double dev=h2ResApply->GetBinContent(ibin,jbin);
	//h2ResApply->SetBinError(ibin,jbin, 0.1*dev);
	h2ResApply->SetBinError(ibin,jbin, 1.);
	h2ResApply->SetBinError(ibin,jbin, dev);
	}
      }

      HistoPair2D_t hpRnd("hpRnd",h2ResApply);

      for (int i=1; i<ensembleSize; ++i) {
	TString name=Form("rndShapeWeight_%d",i);
	TH2D* h2Rnd=hpRnd.randomizedWithinErr(0,name);
	specTH2DWeightV.push_back(h2Rnd);
	tmpLabelV.push_back(name);
      }
    }
    else { // global variation
      for (int i=1; i<ensembleSize; ++i) {
	double rnd=gRandom->Gaus(0,1.);
	TString name=Form("rndShapeWeight_%d",i);
	TH2D *h2Rnd=Clone(h2ShapeWeights,name);
	for (int ibin=1; ibin<=h2Rnd->GetNbinsX(); ++ibin) {
	  for (int jbin=1; jbin<=h2Rnd->GetNbinsY(); ++jbin) {
	    double shW = h2ShapeWeights->GetBinContent(ibin,jbin);
	    double rndScale= 1 + rnd*(1-shW);
	    h2Rnd->SetBinContent(ibin,jbin, rndScale);
	  }
	}
	specTH2DWeightV.push_back(h2Rnd);
	tmpLabelV.push_back(name);
      }
    }

    if (0) {
      specTH2DWeightV.push_back(h2ShapeWeights);
      tmpLabelV.push_back("original");
      TCanvas *cx= plotProfiles("cx",specTH2DWeightV,tmpLabelV,NULL,1,
				"MC/data shape reweight");
      cx->Update();
      return retCodeStop;
    }
  }

  //
  // Set up histograms
  //
  std::vector<TH1D*> hMassv;
  std::vector<TH1D*> hMassBinsv;
  //TH1D *hSelEvents=NULL;

  // debug distributions: 1GeV bins
  //createAnyH1Vec(hMassv,"hMass_",inpMgr.sampleNames(),2500,0.,2500.,"M_{ee} [GeV]","counts/1GeV");
  createAnyH1Vec(hMassv,"hMass_",inpMgr.mcSampleNames(),1490,10.,1500.,"M_{ee} [GeV]","counts/1GeV");
  // debug distributions for current mass bin
  createBaseH1Vec(hMassBinsv,"hMassBins_",inpMgr.mcSampleNames());
  // debug: accumulate info about the selected events in the samples
  //hSelEvents=createAnyTH1D("hSelEvents","hSelEvents",inpMgr.mcSampleCount(),0,inpMgr.mcSampleCount(),"sampleId","event count");


  /*
  TH1F *hMassDiff   = new TH1F("hMassDiff","", 100, -30, 30);
  TH1F *hMassDiffBB = new TH1F("hMassDiffBB","", 100, -30, 30);
  TH1F *hMassDiffEB = new TH1F("hMassDiffEB","", 100, -30, 30);
  TH1F *hMassDiffEE = new TH1F("hMassDiffEE","", 100, -30, 30);

  // These histograms will contain (gen-reco) difference
  // for each (mass, Y) bin in a flattened format
  TH2F *hMassDiffV = new TH2F("hMassDiffV","",
			      nUnfoldingBins, -0.5, nUnfoldingBins-0.5,
			      100, -50.0, 50.0);
  TH2F *hYDiffV = new TH2F("hYDiffV","",
			   nUnfoldingBins, -0.5, nUnfoldingBins-0.5,
			   100, -5.0, 5.0);
  */

//   TH1F *hMassDiffV[nUnfoldingBins];
//   for(int i=0; i<nUnfoldingBins; i++){
//     sprintf(hname,"hMassDiffV_%d",i);
//     hMassDiffV[i] = new TH1F(hname,"",100,-50,50);
//   }

  UnfoldingMatrix_t detResponse(UnfoldingMatrix::_cDET_Response,"detResponse");
  UnfoldingMatrix_t detResponseExact(UnfoldingMatrix::_cDET_Response,"detResponseExact");
  UnfoldingMatrix_t detResponseReversed(UnfoldingMatrix::_cDET_Response,"detResponseReversed");

  UnfoldingMatrix_t fsrGood(UnfoldingMatrix::_cFSR, "fsrGood");
  UnfoldingMatrix_t fsrExact(UnfoldingMatrix::_cFSR, "fsrExact");
  UnfoldingMatrix_t fsrDET(UnfoldingMatrix::_cFSR_DET,"fsrDET"); // only relevant indices are checked for ini,fin
  UnfoldingMatrix_t fsrDETexact(UnfoldingMatrix::_cFSR_DET,"fsrDETexact"); // all indices are checked
  // a good working version: response matrix and invResponse are modified after the inversion
  UnfoldingMatrix_t fsrDET_good(UnfoldingMatrix::_cFSR_DET,"fsrDETgood");

  // Pretend to have a uniform binning
  RooUnfoldResponse rooUnfDetRes(DYTools::nUnfoldingBins,
				 -0.5,DYTools::nUnfoldingBins-0.5,
				 "rooUnfDetRes","rooUnfDetRes");
  rooUnfDetRes.UseOverflow(true);

  std::vector<UnfoldingMatrix_t*> detRespV;

  if (systMode==DYTools::NO_SYST) {}
  else if (systMode==DYTools::SYST_RND) {
    detRespV.reserve(2);
    for (int ir=0; ir<2; ++ir) {
      TString name=Form("detResponse_seed%d_replica%d",globalSeed,ir);
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix::_cDET_Response,name));
    }
  }
  else if (systMode==DYTools::RESOLUTION_STUDY) {
    detRespV.reserve(escaleV.size());
    for (int i=seedMin; i<=seedMax; i+=dSeed) {
      TString name=Form("detResponse_seed%d",i);
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix::_cDET_Response,name));
    }
  }
  else if (systMode==DYTools::FSR_STUDY) {
    detRespV.reserve(specReweightsV.size());
    for (unsigned int i=0; i<specReweightsV.size(); i++) {
      TString wStr=(i==0) ? Form("0%2.0f",specReweightsV[i]*100.) : Form("%3.0f",specReweightsV[i]*100.);
      TString name=TString("detResponse_") + wStr;
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix::_cDET_Response,name));
    }
  }
  else if (systMode==DYTools::PU_STUDY) {
    if (specEWeightsV.size()!=2) { std::cout << "expected specEWeights.size=2\n"; return retCodeError; }
    detRespV.reserve(specEWeightsV.size());
    for (unsigned int i=0; i<specEWeightsV.size(); i++) {
      TString wStr=(i==0) ? "PU5minus" : "PU5plus";
      TString name=TString("detResponse_") + wStr;
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix::_cDET_Response,name));
    }
  }
  else if (systMode==DYTools::ESCALE_RESIDUAL) {
    unsigned int count=specTH2DWeightV.size();
    detRespV.reserve(count);
    for (unsigned int i=0; i<count; ++i) {
      TString name=Form("detResponse_%s",niceNumber(i,count).Data());
      if (i==0) name="detResponse_0_nonRnd";
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix::_cDET_Response,name));
    }
  }
  /*
  else if (systMode==DYTools::ESCALE_STUDY) {
    detRespV.reserve(escaleV.size());
    for (unsigned int i=0; i<escaleV.size(); ++i) {
      TString name=TString("detResponse_") + escaleV[i]->calibrationSetShortName();
      detRespV.push_back(new UnfoldingMatrix_t(UnfoldingMatrix_t::_cDET_Response,name));
    }
  }
  */
  if (detRespV.size()) {
    std::cout << "names in detRespV:\n";
    for (unsigned int i=0; i<detRespV.size(); ++i) {
      std::cout << "  - " << detRespV[i]->getName() << "\n";
    }
  }

  // check
  if ((systMode==DYTools::RESOLUTION_STUDY) ||
      (systMode==DYTools::FSR_STUDY) ||
      (systMode==DYTools::PU_STUDY)
      //|| (systMode==DYTools::ESCALE_STUDY)
      ) {
    if (//(detRespV.size() != escaleV.size()) ||
	(detRespV.size() != specEWeightsV.size())) {
      std::cout << "error: detRespV.size=" << detRespV.size()
	//<< ", escaleV.size=" << escaleV.size()
	//<< ", specReweightsV.size=" << specReweightsV.size()
		<< ", specEWeightsV.size=" << specEWeightsV.size()
		<< "\n";
      assert(0);
    }
  }

  //
  // Access samples and fill histograms
  //
  AccessOrigNtuples_t accessInfo;

  //
  // loop over samples
  //
  if (DYTools::processData(runMode)) {

  double extraWeightFactor=1.0;
  EventCounterExt_t ecTotal("total");
  for (unsigned int isample=0; isample<inpMgr.mcSampleCount(); ++isample) {
    const CSample_t *mcSample=inpMgr.mcSampleInfo(isample);
    std::cout << "Processing " << mcSample->getLabel() << "..." << std::endl;
    std::cout << " of size " << mcSample->size() << "\n";
    if (mcSample->size()!=1) {
      std::cout << "mcSample->size is expected to be 1\n";
      return retCodeError;
    }

    for (unsigned int ifile=0; ifile<mcSample->size(); ++ifile) {
      // Read input file
      TFile *infile= new TFile(mcSample->getFName(ifile),"read");
      if (!infile || !infile->IsOpen()) {
	TString skimName=inpMgr.convertSkim2Ntuple(mcSample->getFName(ifile));
	std::cout <<  "  .. failed. Trying <" << skimName << ">" << std::endl;
	infile= new TFile(skimName,"read");
      }
      assert(infile->IsOpen());

      // Get the TTrees
      if (!accessInfo.setTree(*infile,"Events",true)) {
	return retCodeError;
      }

      // Find weight for events for this file
      // The first file in the list comes with weight 1*extraWeightFactor,
      // all subsequent ones are normalized to xsection and luminosity
      ULong_t maxEvents = accessInfo.getEntries();
      // to match old version package (DYee 7TeV paper),
      if (inpMgr.userKeyValueAsInt("USE7TEVMCWEIGHT") &&
	  (isample==0) && (ifile==0)) {
	extraWeightFactor=maxEvents / (inpMgr.totalLumi() * inpMgr.mcSampleInfo(0)->getXsec(ifile));
	//extraWeightFactor=maxEvents / inpMgr.mcSampleInfo(0)->getXsec(ifile);
      }
      //std::cout << "extraWeightFactor=" << extraWeightFactor << ", chk=" << (maxEvents0/inpMgr.mcSampleInfo(0)->getXsec(ifile)) << "\n";
      //const double extraWeightFactor=1.0;
      if (! evWeight.setWeight_and_adjustMaxEvents(maxEvents,
						   inpMgr.totalLumi(),
						   mcSample->getXsec(ifile),
						   extraWeightFactor, inpMgr.selectEventsFlag())) {
	std::cout << "adjustMaxEvents failed\n";
	return retCodeError;
      }
      std::cout << "mcSample xsec=" << mcSample->getXsec(ifile) << ", nEntries=" << maxEvents << "\n";

      std::cout << "       -> sample base weight is " << evWeight.baseWeight() << "\n";
      for (unsigned int iSt=0; iSt<specEWeightsV.size(); ++iSt) {
	specEWeightsV[iSt]->setBaseWeight(evWeight);
      }

      // loop through events
      EventCounterExt_t ec(Form("%s_file%d",mcSample->name.Data(),ifile));
      ec.setIgnoreScale(0); // 1 - count events, 0 - take weight in account
      // adjust the scale in the counter
      // if FEWZ weight should be considered, use evWeight.totalWeight() after
      // the FEWZ weight has been identified (see a line below)
      ec.setScale(evWeight.baseWeight());

      std::cout << "numEntries = " << accessInfo.getEntriesFast()
		<< ", " << maxEvents << " events will be used" << std::endl;

      for(ULong_t ientry=0; ientry<maxEvents; ientry++) {
	if (DYTools::isDebugMode(runMode) &&
	    (ientry>ULong_t(1000000)+DYTools::study2D*ULong_t(2000000))) break; // debug option
	//if (DYTools::isDebugMode(runMode) && (ientry>100)) break; // debug option
	printProgress(250000," ientry=",ientry,maxEvents);
	ec.numEvents_inc();

	// Load generator level info
	accessInfo.GetGen(ientry);
	// If the Z->ll leptons are not electrons, discard this event.
	// This is needed for signal MC samples such as Madgraph Z->ll
	// where all 3 lepton flavors are possible
	if (!accessInfo.genLeptonsAreElectrons()) continue;

	// Load event info
	accessInfo.GetInfoEntry(ientry);

	// Adjust event weight
	// .. here "false" = "not data"
	evWeight.set_PU_and_FEWZ_weights(accessInfo,false);
	//evWeightNoPU.set_PU_and_FEWZ_weights(accessInfo,false);
	if (useSpecWeight) {
	  evWeight.setSpecWeightValue(accessInfo,FSRmassDiff,specWeight);
	  //evWeightNoPU.setSpecWeightValue(accessInfo,FSRmassDiff,specWeight);
	}

	// FSR study correction for weight
	if (systMode==DYTools::FSR_STUDY) {
	  for (unsigned int iSt=0; iSt<specEWeightsV.size(); ++iSt) {
	    specEWeightsV[iSt]->setSpecWeightValue(accessInfo,FSRmassDiff,specReweightsV[iSt]);
	  }
	}

	// setup spec weights
	// .. here "false" = "not data"
	for (unsigned int iSt=0; iSt<specEWeightsV.size(); ++iSt) {
	  specEWeightsV[iSt]->set_PU_and_FEWZ_weights(accessInfo,false);
	}

	if (ientry<20) {
	  std::cout << "ientry=" << ientry << ", "; evWeight.Print(0);
	  //printf("reweight=%4.2lf, fewz_weight=%4.2lf,dE_fsr=%+6.4lf\n",reweight,fewz_weight,(gen->mass-gen->vmass));
	  if (systMode!=DYTools::RESOLUTION_STUDY) {
	    for (unsigned int iSt=0; iSt<specEWeightsV.size(); ++iSt) {
	      std::cout << " specEWeight[" << iSt << "] = "; specEWeightsV[iSt]->Print(0); //std::cout << "\n";
	    }
	  }
	  std::cout << "\n";
	}

	// adjust the scale in the counter to include FEWZ
	// (and possibly PU) weight
	//ec.setScale(evWeight.totalWeight());

	FlatIndex_t fiGenPreFsr, fiGenPostFsr;
	fiGenPreFsr.setGenPreFsrIdx(accessInfo);
	fiGenPostFsr.setGenPostFsrIdx(accessInfo);

	// begin FSR unfolding block
	fsrGood.fillIni(fiGenPreFsr , evWeight.totalWeight());
	fsrGood.fillFin(fiGenPostFsr, evWeight.totalWeight());
	if (fiGenPreFsr.isValid() && fiGenPostFsr.isValid()) {
	  fsrGood.fillMigration(fiGenPreFsr, fiGenPostFsr, evWeight.totalWeight());
	  fsrExact.fillIni(fiGenPreFsr , evWeight.totalWeight());
	  fsrExact.fillFin(fiGenPostFsr, evWeight.totalWeight());
	  fsrExact.fillMigration(fiGenPreFsr, fiGenPostFsr, evWeight.totalWeight());
	}

	int preFsrOk=0, postFsrOk=0;
	if (evtSelector.inAcceptancePreFsr(accessInfo) &&
	    fiGenPreFsr.isValid()) {
	  preFsrOk=1;
	  fsrDET     .fillIni(fiGenPreFsr, evWeight.totalWeight());
	  fsrDET_good.fillIni(fiGenPreFsr, evWeight.totalWeight());
	}

	if (evtSelector.inAcceptance(accessInfo) &&
	    fiGenPostFsr.isValid()) {
	  postFsrOk=1;
	  fsrDET     .fillFin(fiGenPostFsr, evWeight.totalWeight());
	  fsrDET_good.fillFin(fiGenPostFsr, evWeight.totalWeight());
	}

	if (preFsrOk && postFsrOk) {
	  fsrDET.fillMigration(fiGenPreFsr, fiGenPostFsr, evWeight.totalWeight());
	  fsrDET_good.fillMigration(fiGenPreFsr, fiGenPostFsr, evWeight.totalWeight());
	  fsrDETexact.fillIni(fiGenPreFsr , evWeight.totalWeight());
	  fsrDETexact.fillFin(fiGenPostFsr, evWeight.totalWeight());
	  fsrDETexact.fillMigration(fiGenPreFsr, fiGenPostFsr, evWeight.totalWeight());
	}
	// end of FSR unfolding block


	// check event trigger
	if (!evtSelector.eventTriggerOk(accessInfo)) {
	  continue; // no trigger accept? Skip to next event...	
	}
	ec.numEventsPassedEvtTrigger_inc();

	// load dielectron array
	accessInfo.GetDielectrons(ientry);

	// loop through dielectrons
	//int pass=0;
	int candCount=0;
	mithep::TDielectron uncorrDielectron;
	for(Int_t i=0; i<accessInfo.dielectronCount(); i++) {
	  mithep::TDielectron *dielectron = accessInfo.editDielectronPtr(i);
	  ec.numDielectrons_inc();

	  // keep unmodified dielectron
	  if (escaleV.size()) uncorrDielectron.restoreEScaleModifiedValues(*dielectron);

	  // escale may modify dielectron! But it should not here
	  if (!evtSelector.testDielectron(dielectron,accessInfo.evtInfoPtr(),&ec)) continue;
	  //pass=1;

          /******** We have a Z candidate! HURRAY! ********/

	  candCount++;
	  ec.numDielectronsPass_inc();
	  if (ec.numDielectronsOkSameSign_inc(dielectron->q_1,dielectron->q_2)) {
	    // same sign event
	  }

	  //
	  // Fill structures for response matrix

	  FlatIndex_t fiReco;
	  fiReco.setRecoIdx(dielectron);

	  // Fill the matrix of post-FSR generator level invariant mass and rapidity
	  double diWeight=evWeight.totalWeight();
	  detResponse.fillIni(fiGenPostFsr, diWeight);
	  detResponse.fillFin(fiReco      , diWeight);

	  int bothFIValid=fiGenPostFsr.isValid() && fiReco.isValid();
	  if (bothFIValid) {
	    ec.numDielectronsGoodMass_inc();
	    detResponse.fillMigration(fiGenPostFsr, fiReco, diWeight);

	    detResponseExact.fillIni(fiGenPostFsr, diWeight);
	    detResponseExact.fillFin(fiReco      , diWeight);
	    detResponseExact.fillMigration(fiGenPostFsr, fiReco, diWeight);
	  }

	  if (fiGenPostFsr.isValid()) {
	    if (fiReco.isValid()) {
	      rooUnfDetRes.Fill(fiReco.idx(),fiGenPostFsr.idx(), diWeight);
	    }
	    else {
	      rooUnfDetRes.Miss(fiGenPostFsr.idx(), diWeight);
	    }
	  }
	  else rooUnfDetRes.Fake(fiReco.idx(), diWeight);

	  detResponseReversed.fillIni(fiReco,       diWeight);
	  detResponseReversed.fillFin(fiGenPostFsr, diWeight);
	  if (bothFIValid) {
	    detResponseReversed.fillMigration(fiReco,fiGenPostFsr, diWeight);
	  }

	  if (systMode != DYTools::RESOLUTION_STUDY) {
	    switch(systMode) {
	    case DYTools::SYST_RND: {
	      double rnd=gRandom->Gaus(0,1.);
	      if (rnd==double(0.)) rnd=gRandom->Gaus(0,1.);
	      int idx=(rnd<double(0.)) ? 0:1;
	      detRespV[idx]->fillIni( fiGenPostFsr, diWeight );
	      detRespV[idx]->fillFin( fiReco      , diWeight );
	      if (bothFIValid) {
		detRespV[idx]->fillMigration( fiGenPostFsr, fiReco, diWeight);
	      }
	    }
	      break;

	    case DYTools::ESCALE_RESIDUAL:
	      for (unsigned int iSt=0; iSt<detRespV.size(); ++iSt) {
		const TH2D *h2Rnd= specTH2DWeightV[iSt];
		double w=1.;
		if (fiReco.isValid()) {
		  w=h2Rnd->GetBinContent(fiReco.iM()+1,fiReco.iY()+1);
		  if ((iSt==0) && (ientry<20)) {
		    std::cout << "dielectron(M,Y)=" << dielectron->mass
			      << "," << dielectron->y << ", fiReco="
			      << fiReco << ", specWeight=" << w << "\n";
		  }
		}
		double studyWeight= diWeight * w;
		detRespV[iSt]->fillIni( fiGenPostFsr, studyWeight );
		detRespV[iSt]->fillFin( fiReco      , studyWeight );
		if (bothFIValid) {
		  detRespV[iSt]->fillMigration( fiGenPostFsr, fiReco,
						studyWeight);
		}
	      }
	      break;

	    default:
	      for (unsigned int iSt=0; iSt<detRespV.size(); ++iSt) {
		double studyWeight=specEWeightsV[iSt]->totalWeight();
		detRespV[iSt]->fillIni( fiGenPostFsr, studyWeight );
		detRespV[iSt]->fillFin( fiReco      , studyWeight );
		if (bothFIValid) {
		  detRespV[iSt]->fillMigration( fiGenPostFsr, fiReco,
						studyWeight );
		}
	      }
	    }
	  }

	  if (escaleV.size() && (systMode==DYTools::RESOLUTION_STUDY)) {
	    for (unsigned int iESc=0; iESc<escaleV.size(); ++iESc) {
	      dielectron->restoreEScaleModifiedValues(uncorrDielectron);
	      if (evtSelectorV[iESc]->testDielectron(dielectron,
						accessInfo.evtInfoPtr())) {
		FlatIndex_t fiRecoMdf;
		fiRecoMdf.setRecoIdx(dielectron);
		detRespV[iESc]->fillIni(fiGenPostFsr, diWeight);
		detRespV[iESc]->fillFin(fiRecoMdf   , diWeight);
		if (fiGenPostFsr.isValid() && fiRecoMdf.isValid()) {
		  detRespV[iESc]->fillMigration(fiGenPostFsr,fiRecoMdf,
						diWeight);
		}
	      }
	    }
	  }

	  /*
	  Bool_t isB1 = DYTools::isBarrel(dielectron->scEta_1);
	  Bool_t isB2 = DYTools::isBarrel(dielectron->scEta_2);

	  hMassDiff->Fill(massResmeared - gen->mass);
	  if( isB1 && isB2 )
	    hMassDiffBB->Fill(massResmeared - gen->mass);
	  if( (isB1 && !isB2) || (!isB1 && isB2) )
	    hMassDiffEB->Fill(massResmeared - gen->mass);
	  if( !isB1 && !isB2 )
	    hMassDiffEE->Fill(massResmeared - gen->mass);

	  hMassDiffV->Fill(iIndexFlatGen, massResmeared - gen->mass);
	  hYDiffV   ->Fill(iIndexFlatGen, dielectron->y - gen->y);
	  // 	if(iIndexFlatGen != -1){
	  // 	  hMassDiffV[iIndexFlatGen]->Fill(massResmeared - gen->mass);
	  // 	}
	  */

	} // end loop over dielectrons
	if (candCount>1) ec.numMultiDielectronsOk_inc();

      } // end loop over events
      infile->Close();
      delete infile;
      std::cout << ec << "\n";
      ecTotal.add(ec);
    } // end loop over files
    std::cout << "total counts : " << ecTotal << "\n";

  } // loop over iSample
  } // runMode


  UnfoldingMatrix_t fsrDETcorrections(UnfoldingMatrix::_cFSR_DETcorrFactors,"fsrCorrFactors");

  if (DYTools::processData(runMode)) {
    // Compute the errors on the elements of migration matrix
    detResponse.finalizeDetMigrationErr();
    detResponseExact.finalizeDetMigrationErr();
    detResponseReversed.finalizeDetMigrationErr();
    fsrGood.finalizeDetMigrationErr();
    fsrExact.finalizeDetMigrationErr();
    fsrDET.finalizeDetMigrationErr();
    fsrDETexact.finalizeDetMigrationErr();
    fsrDET_good.finalizeDetMigrationErr();
    for (unsigned int i=0; i<detRespV.size(); ++i) detRespV[i]->finalizeDetMigrationErr();

  // Find response matrix, which is simply the normalized migration matrix
    std::cout << "find response matrix" << std::endl;
    detResponse.computeResponseMatrix();
    detResponseExact.computeResponseMatrix();
    detResponseReversed.computeResponseMatrix();
    fsrGood.computeResponseMatrix();
    fsrExact.computeResponseMatrix();
    fsrDET.computeResponseMatrix();
    fsrDETexact.computeResponseMatrix();
    fsrDET_good.computeResponseMatrix();
    for (unsigned int i=0; i<detRespV.size(); ++i) detRespV[i]->computeResponseMatrix();

    std::cout << "find inverted response matrix" << std::endl;
    detResponse.invertResponseMatrix();
    detResponseExact.invertResponseMatrix();
    detResponseReversed.invertResponseMatrix();
    fsrGood.invertResponseMatrix();
    fsrExact.invertResponseMatrix();
    fsrDET.invertResponseMatrix();
    fsrDETexact.invertResponseMatrix();
    fsrDET_good.invertResponseMatrix();
    for (unsigned int i=0; i<detRespV.size(); ++i) detRespV[i]->invertResponseMatrix();

    fsrDETcorrections.prepareFsrDETcorrFactors(fsrDET,fsrDETexact);
    //fsrDETcorrections.printYields();

    std::cout << "finalize fsrDET_good" << std::endl;
    fsrGood.modifyDETResponseMatrices(fsrExact);
    fsrDET_good.modifyDETResponseMatrices(fsrDETexact);

    std::cout << "prepare flat-index arrays" << std::endl;
    detResponse.prepareFIArrays();
    detResponseExact.prepareFIArrays();
    detResponseReversed.prepareFIArrays();
    fsrGood.prepareFIArrays();
    fsrExact.prepareFIArrays();
    fsrDET.prepareFIArrays();
    fsrDETexact.prepareFIArrays();
    fsrDET_good.prepareFIArrays();
    fsrDETcorrections.prepareFIArrays();
    for (unsigned int i=0; i<detRespV.size(); ++i) detRespV[i]->prepareFIArrays();
  }

  //
  // Store constants and reference arrays in files
  //
  if (DYTools::processData(runMode))  std::cout << "store constants in a file" << std::endl;

  //TString outFile=inpMgr.correctionFullFileName("unfolding",systMode,0);
  TString outputDir=inpMgr.constDir(systMode,0);

  //int saveIdxMin=-1;
  TString fnameTag=UnfoldingMatrix_t::generateFNameTag(systMode,globalSeed);
  /*
  {
    TString u="_";
    switch(systMode) {
    case DYTools::NO_SYST:
      fnameTag=DYTools::analysisTag;
      break;
    case DYTools::SYST_RND:
      fnameTag=TString("_replica_") + DYTools::analysisTag;
      //saveIdxMin=0;
     //fnameTag+=seed;
       break;
    case DYTools::RESOLUTION_STUDY:
      fnameTag=TString("_seed_") + DYTools::analysisTag;
      //fnameTag+=seed;
      break;
    case DYTools::FSR_STUDY:
      fnameTag=TString("_fsrStudy_") + DYTools::analysisTag;
      //fnameTag=TString("_reweight_") + DYTools::analysisTag;
      //fnameTag+= int(100*reweightFsr);
      break;
    case DYTools::PU_STUDY:
      fnameTag=TString("_puStudy_") + DYTools::analysisTag;
      break;
    case DYTools::ESCALE_STUDY:
      fnameTag=DYTools::analysisTag+TString("_escale") + u;
      break;
    case DYTools::ESCALE_RESIDUAL:
      fnameTag=DYTools::analysisTag+TString("_escaleResidual");
      break;
    default:
      std::cout<<"requested mode not recognized when determining fnameTag"<<std::endl;
      assert(0);
    }
  }
  */
  if (DYTools::isDebugMode(runMode)) fnameTag.Prepend("_DebugRun_");
  std::cout << "fnameTag=<" << fnameTag << ">\n";
  CPlot::sOutDir.Append(fnameTag);
  CPlot::sOutDir.ReplaceAll(DYTools::analysisTag,"");
  CPlot::sOutDir.Append(DYTools::analysisTag);

  TString callingMacro="plotUnfoldingMatrix.systMode=";
  callingMacro.Append(SystematicsStudyName(systMode));

  if (DYTools::processData(runMode)) {
    if (//(systMode!=DYTools::NORMAL_RND) &&
	(systMode!=DYTools::RESOLUTION_STUDY) &&
	//(systMode!=DYTools::FSR_STUDY) &&
	(systMode!=DYTools::ESCALE_STUDY)) {
      detResponse.autoSaveToFile(outputDir,fnameTag,callingMacro);  // detResponse, reference mc arrays
      detResponseExact.autoSaveToFile(outputDir,fnameTag,callingMacro);
      detResponseReversed.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrGood.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrExact.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrDET.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrDETexact.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrDET_good.autoSaveToFile(outputDir,fnameTag,callingMacro);
      fsrDETcorrections.autoSaveToFile(outputDir,fnameTag,callingMacro);
    }

    TString fname=Form("rooUnfDetRes_%s.root",DYTools::analysisTag.Data());
    if (DYTools::isDebugMode(runMode))
      fname.ReplaceAll(".root","_DebugRun.root");
    TFile fout(fname,"recreate");
    if (!fout.IsOpen()) {
      std::cout << "failed to create the file <" << fout.GetName() << ">\n";
      return retCodeError;
    }
    rooUnfDetRes.Write();
    writeBinningArrays(fout,"plotUnfoldingMatrixRooUnfold");
    std::cout << "file <" << fout.GetName() << "> created\n";
    fout.Close();

    for (unsigned int i=0; i<detRespV.size(); i++)
      detRespV[i]->autoSaveToFile(outputDir,fnameTag,callingMacro);

    // additional saving for systematics
    if (systMode==DYTools::FSR_STUDY) {
      detRespV[0]->autoSaveToFile(inpMgr.constDir(DYTools::FSR_5minus,0),
	  UnfoldingMatrix_t::generateFNameTag(DYTools::FSR_5minus,globalSeed),
				  callingMacro);
      detRespV[2]->autoSaveToFile(inpMgr.constDir(DYTools::FSR_5plus,0),
	  UnfoldingMatrix_t::generateFNameTag(DYTools::FSR_5plus,globalSeed),
				  callingMacro);
    }
    else if (systMode==DYTools::PU_STUDY) {
      TString dir0=inpMgr.constDir(DYTools::PILEUP_5minus,0);
      TString tag0=UnfoldingMatrix_t::generateFNameTag(DYTools::PILEUP_5minus,
						       globalSeed);
      TString dir1=inpMgr.constDir(DYTools::PILEUP_5plus,0);
      TString tag1=UnfoldingMatrix_t::generateFNameTag(DYTools::PILEUP_5plus,
						       globalSeed);
      detRespV[0]->autoSaveToFile(dir0,tag0,callingMacro);
      detRespV[1]->autoSaveToFile(dir1,tag1,callingMacro);
    }
  }
  else {
    if (//(systMode!=DYTools::NORMAL_RND) &&
	(systMode!=DYTools::RESOLUTION_STUDY) &&
	//(systMode!=DYTools::FSR_STUDY) &&
	(systMode!=DYTools::ESCALE_STUDY)) {
      if (!detResponse.autoLoadFromFile(outputDir,fnameTag) ||
	  !detResponseExact.autoLoadFromFile(outputDir,fnameTag) ||
	  !detResponseReversed.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrGood.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrExact.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrDET.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrDETexact.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrDET_good.autoLoadFromFile(outputDir,fnameTag) ||
	  !fsrDETcorrections.autoLoadFromFile(outputDir,fnameTag)) {
	std::cout << "loading failed\n";
	return retCodeError;
      }
    }
    for (unsigned int i=0; i<detRespV.size(); i++) detRespV[i]->autoLoadFromFile(outputDir,fnameTag);
  }


  UnfoldingMatrix_t detRespAvg(detResponse.kind, "detResponseAvg");
  if (1 && detRespV.size()) { //computeAverage
    const double weight=1/double(detRespV.size());
    for (unsigned int i=0; i<detRespV.size(); ++i) {
      TString name=Form("tmp_%d",i);
      UnfoldingMatrix_t tmp(*detRespV[i],name);
      tmp.squareDetMigrationErr();
      detRespAvg.addMigration(tmp,weight);
    }
    detRespAvg.finalizeDetMigrationErr();
    detRespAvg.computeResponseMatrix();
    detRespAvg.invertResponseMatrix();
    detRespAvg.prepareFIArrays();
    detRespAvg.autoSaveToFile(outputDir,fnameTag,callingMacro); // detResponse, reference mc arrays
  }


  //--------------------------------------------------------------------------------------------------------------
  // Make plots
  //==============================================================================================================

  /*
  std::cout << "making plots" << std::endl;

  TString unfoldingConstFileName, yieldsFileName;
  detResponse.getFileNames(outputDir,fnameTag, unfoldingConstFileName, yieldsFileName);
  TString unfoldingConstantsPlotFName=unfoldingConstFileName;
  unfoldingConstantsPlotFName.Replace(unfoldingConstantsPlotFName.Index(".root"),
				      sizeof(".root"),
				      "_plots.root");
  TFile *fPlots=new TFile(unfoldingConstantsPlotFName,"recreate");
  if (!fPlots) {
    std::cout << "failed to create a file <" << unfoldingConstantsPlotFName << ">\n";
  }

#ifdef CrossSection_HH
  if (1) {  // study Ini and Fin vecs
    std::vector<VXSectD_t*> dataV;
    TString canvName="canvChk";
    TString fewzTag=(useFewzWeights) ? "_withFEWZ" : "_noFEWZ";
    if (useFewzWeights && regularizeFEWZ) fewzTag="_withMdfFEWZ";
    canvName.Append(fewzTag);
    const int twoPads=0;
    TCanvas *c=new TCanvas(canvName,canvName,600*(1+twoPads),600);
    if (twoPads) {
      c->Divide(2,1);
      c->GetPad(1)->SetLogx(1);
      c->GetPad(2)->SetLogx(1);
      c->GetPad(1)->SetLogy(1);
      c->GetPad(2)->SetLogy(1);
    }
    else { c->SetLogx(1); c->SetLogy(1); }
    int ok=1;

    TH1F *hGenAvg=new TH1F("hGenAvg","hGenAvg",DYTools::nMassBins,DYTools::massBinLimits);
    TH1F *hRecAvg=new TH1F("hRecAvg","hRecAvg",DYTools::nMassBins,DYTools::massBinLimits);
    hGenAvg->SetDirectory(0);
    hRecAvg->SetDirectory(0);
    hGenAvg->Sumw2();
    hRecAvg->Sumw2();

    TH1F *hGenAvg_chk=NULL; //new TH1F("hGenAvg_chk","hGenAvg_chk",DYTools::nMassBins,DYTools::massBinLimits);
    TH1F *hRecAvg_chk=NULL; //new TH1F("hRecAvg_chk","hRecAvg_chk",DYTools::nMassBins,DYTools::massBinLimits);
    if (ok && hGenAvg_chk && hRecAvg_chk) {
      hGenAvg_chk->SetDirectory(0);
      hRecAvg_chk->SetDirectory(0);
      VXSectD_t d("yields_repAvg",DYTools::nUnfoldingBinsMax);
      TString matrixFName,yieldsFName;
      detRespAvg.getFileNames(outputDir,fnameTag, matrixFName,yieldsFName);
      ok=d.Load(matrixFName,"yieldsMcPostFsrGenFIArray","yieldsMcPostFsrRecFIArray","");
      if (ok) ok= (d.FillHisto(hGenAvg_chk,1) && d.FillHistoWithError(hRecAvg_chk));
    }

    for (unsigned int i=0; ok && (i<detRespV.size()); ++i) {
      TString name=Form("yields_rep%d",i);
      VXSectD_t *d=new VXSectD_t(name,DYTools::nUnfoldingBinsMax);
      dataV.push_back(d);
      TString matrixFName,yieldsFName;
      detRespV[i]->getFileNames(outputDir,fnameTag, matrixFName,yieldsFName);
      ok=d->Load(matrixFName,"yieldsMcPostFsrGenFIArray","yieldsMcPostFsrRecFIArray","");
      if (!ok) continue;
      TString hGenName=Form("hGen_rep%d",i);
      TString hRecName=Form("hRec_rep%d",i);
      TH1F *hGen=new TH1F(hGenName,hGenName,DYTools::nMassBins,DYTools::massBinLimits);
      TH1F *hRec=new TH1F(hRecName,hRecName,DYTools::nMassBins,DYTools::massBinLimits);
      hGen->SetDirectory(0);
      hRec->SetDirectory(0);
      if (i==0) {
	hGen->SetTitle(hGen->GetTitle() + fewzTag);
	hRec->SetTitle(hRec->GetTitle() + fewzTag);
      }
      ok= (d->FillHisto(hGen,1) && d->FillHistoWithError(hRec));
      if (!ok) continue;
      hGenAvg->Add(hGen,1/double(detRespV.size()));
      hRecAvg->Add(hRec,1/double(detRespV.size()));
      TString opt="L";
      if (i>0) opt.Append("same");
      int color = i%(50-20) + 20;
      hGen->SetLineColor(color);
      hRec->SetLineColor(color);
      hGen->GetYaxis()->SetRangeUser(5.,3.e6);
      hRec->GetYaxis()->SetRangeUser(5.,3.e6);
      if (twoPads) c->cd(1);
      hGen->Draw(opt);
      if (twoPads) c->cd(2);
      else { if (i==0) opt.Append("same"); }
      hRec->Draw(opt);
    }
    hGenAvg->SetLineColor(kAzure+1);
    hRecAvg->SetLineColor(kRed+1);
    if (twoPads) c->cd(1);
    hGenAvg->Draw("L same");
    if (hGenAvg_chk) hGenAvg_chk->Draw("L same");
    if (twoPads) c->cd(2);
    hRecAvg->Draw("L same");
    if (hRecAvg_chk) hRecAvg_chk->Draw("L same");
    c->Update();
    return;
  }
#endif


  TCanvas *c = MakeCanvas("canvZmass1","canvZmass1",800,600);

  // string buffers
  char ylabel[50];   // y-axis label

  //
  // Draw DY candidate mass at the reconstruction level. Extra
  // smearing is applied. This figure allows one to judge the
  // correctness of the weights aplied to different samples from the
  // smoothness of the combined result.
  //
  sprintf(ylabel,"a.u. / %.1f GeV/c^{2}",hZMassv[0]->GetBinWidth(1));
  CPlot plotZMass1("zmass1","","m(ee) [GeV/c^{2}]",ylabel);
  for(UInt_t i=0; i<fnamev.size(); i++) {
    plotZMass1.AddHist1D(hZMassv[i],labelv[i],"hist",colorv[i],linev[i]);
  }
  plotZMass1.SetLogy();
  plotZMass1.Draw(c);
  SaveCanvas(c,"zmass1");
//   plotZMass1.Draw(c,doSave,format);
//   if (fPlots) { fPlots->cd(); c->Write(); }

  //
  // Draw a plot that illustrates the detector resolution effects.
  // We plot (gen-rec)/gen as a function of mass and rapidity.
  //
  TMatrixD resolutionEffect(DYTools::nMassBins,DYTools::nYBinsMax);
  resolutionEffect = 0;
  for(int i=0; i < resolutionEffect.GetNrows(); i++){
    for(int j=0; j < resolutionEffect.GetNcols(); j++){
      double ngen = (*detResponse.yieldsIni)(i,j);
      double nrec = (*detResponse.yieldsFin)(i,j);
      if( ngen != 0 )
	resolutionEffect(i,j) = (ngen-nrec)/ngen;
    }
  }
  resolutionEffect.Print();
  PlotMatrixVariousBinning(resolutionEffect, "resolution_effect", "LEGO2", NULL);

  //
  // Draw a plot that illustrates the losses due to reconstruction
  // We plot (preFsrExact-preFsr)/preFsrExact as a
  // function of mass and rapidity.
  //
  TMatrixD *unfRecoEffect=detResponseExact.getReconstructionEffect(detResponse);
  unfRecoEffect->Print();
  PlotMatrixVariousBinning(*unfRecoEffect, "reconstruction_effect", "LEGO2", NULL);
  delete unfRecoEffect;

  TMatrixD *unfFsrDETRecoEffect=fsrDETexact.getReconstructionEffect(fsrDET);

  PlotMatrixVariousBinning(*unfFsrDETRecoEffect, "reconstruction_effect_fsrDET", "LEGO2", NULL);
  delete unfFsrDETRecoEffect;

  */

  // Plot response and inverted response matrices
  //std::vector<TH2F*> hResponseV, hInvResponseV;
  //std::vector<TCanvas*> canvV;
  //std::vector<CPlot*> cpResponseV;

  //TH2F *hR, *hIR;
  //TCanvas *e2;
  //CPlot *cpR, *cpIR;
  detResponse.prepareHResponse();
  fsrGood.prepareHResponse();
  fsrExact.prepareHResponse();
  fsrDET.prepareHResponse();
  fsrDETexact.prepareHResponse();
  fsrDET_good.prepareHResponse();

  for (unsigned int iESc=0; iESc<detRespV.size(); ++iESc) {
    detRespV[iESc]->prepareHResponse();
  }

  /*
  // Create a plot of detector resolution without mass binning
  TCanvas *g = MakeCanvas("canvMassDiff","canvMassDiff",600,600);
  CPlot plotMassDiff("massDiff","","reco mass - gen post-FSR mass [GeV/c^{2}]","a.u.");
  hMassDiffBB->Scale(1.0/hMassDiffBB->GetSumOfWeights());
  hMassDiffEB->Scale(1.0/hMassDiffEB->GetSumOfWeights());
  hMassDiffEE->Scale(1.0/hMassDiffEE->GetSumOfWeights());
  plotMassDiff.AddHist1D(hMassDiffBB,"EB-EB","hist",kBlack);
  plotMassDiff.AddHist1D(hMassDiffEB,"EE-EB","hist",kBlue);
  plotMassDiff.AddHist1D(hMassDiffEE,"EE-EE","hist",kRed);
  plotMassDiff.Draw(g);
  SaveCanvas(g,"massDiff");
//   if (fPlots) g->Write();

  // Create a plot of reco - gen post-FSR mass and rapidity difference
  TCanvas *h1 = MakeCanvas("canvMassDiffV","canvMassDiffV",600,600);
  CPlot plotMassDiffV("massDiffV","",
		      "flat index",
		      "reco mass - gen post-FSR mass [GeV/c^{2}]");
  plotMassDiffV.AddHist2D(hMassDiffV,"LEGO");
  plotMassDiffV.Draw(h1);
  SaveCanvas(h1,"hMassDiffV");

  // Create a plot of reco - gen post-FSR mass and rapidity difference
  TCanvas *h2 = MakeCanvas("canvYDiffV","canvYDiffV",600,600);
  CPlot plotYDiffV("massDiffV","",
		      "flat index",
		      "reco Y - gen post-FSR Y");
  plotYDiffV.AddHist2D(hYDiffV,"LEGO");
  plotYDiffV.Draw(h2);
  SaveCanvas(h2,"hYDiffV");

  if (fPlots) {
    fPlots->Close();
    delete fPlots;
    std::cout << "plots saved to a file <" << unfoldingConstantsPlotFName << ">\n";
  }

  //draw errors of Unfolding matrix
  TCanvas *cErrorsResp = MakeCanvas("cErrorsResp","detResponse.DetInvertedResponseErr", 600,600);
  detResponse.DetInvertedResponseErr->Draw("LEGO2");
  cErrorsResp->Update();
  SaveCanvas(cErrorsResp,"cErrorsResp");

  TCanvas *cFsrErrorsResp = MakeCanvas("cErrorsFsr","fsr__.DetInvertedResponseErr", 1200, 600);
  cFsrErrorsResp->Divide(2,1);
  cFsrErrorsResp->cd(1);
  fsrExact.DetInvertedResponseErr->Draw("LEGO2");
  cFsrErrorsResp->cd(2);
  fsrDET.DetInvertedResponseErr->Draw("LEGO2");
  SaveCanvas(cFsrErrorsResp,"cErrorsFsr");



  //--------------------------------------------------------------------------------------------------------------
  // Summary print out
  //==============================================================================================================
  cout << endl;
  cout << "*" << endl;
  cout << "* SUMMARY" << endl;
  cout << "*--------------------------------------------------" << endl;
  cout << endl;

  detResponse.printConditionNumber();
  fsrExact.printConditionNumber();
  fsrDET.printConditionNumber();
  fsrDETexact.printConditionNumber();

  if (0) {
    //detResponse.printMatrices();
    //fsr.printMatrices();
    //fsrDET.printMatrices();
    fsrDET_Mdf.printMatrices();
    fsrDET_good.printMatrices();
  }

  //Print errors of the Unfolding matrix when they exceed 0.1
  /
  for (int iM=0; iM<DYTools::nMassBins; iM++)
    for (int iY=0; iY<DYTools::nYBins[iM]; iY++)
      for (int jM=0; jM<DYTools::nMassBins; jM++)
        for (int jY=0; jY<DYTools::nYBins[jM]; jY++)
          {
	    int i=DYTools::findIndexFlat(iM,iY);
	    int j=DYTools::findIndexFlat(jM,jY);
             if (DetInvertedResponseErr(i,j)>0.1)
                {
                   std::cout<<"DetInvertedResponseErr("<<i<<","<<j<<")="<<DetInvertedResponseErr(i,j);
                   std::cout<<", DetInvertedResponse("<<i<<","<<j<<")="<<DetInvertedResponse(i,j)<<std::endl;
                   std::cout<<"(iM="<<iM<<", iY="<<iY<<", jM="<<jM<<", jY="<<jY<<")"<<std::endl<<std::endl;
                }
             if (DetInvertedResponseErr2(i,j)>0.1)
                {
                   std::cout<<"DetInvertedResponseErr2("<<i<<","<<j<<")="<<DetInvertedResponseErr2(i,j);
                   std::cout<<", DetInvertedResponse("<<i<<","<<j<<")="<<DetInvertedResponse(i,j)<<std::endl;
                   std::cout<<"(iM="<<iM<<", iY="<<iY<<", jM="<<jM<<", jY="<<jY<<")"<<std::endl<<std::endl;
                }
          }
  /

  /
  if (0) {
    // Printout of all constants, uncomment if needed
    //printf("DetCorrFactor:\n"); DetCorrFactor.Print();
    printf("DetMigration:\n"); DetMigration.Print();
    printf("DetResponse:\n"); DetResponse.Print();

    printf("DetInvertedResponse:\n"); DetInvertedResponse.Print();
    //printf("DetInvertedResponseErr:\n"); DetInvertedResponseErr.Print();
    //printf("DetResponseArr:\n"); DetResponseArr.Print();
    //printf("DetInvertedResponseArr:\n"); DetInvertedResponseArr.Print();
    //printf("DetInvertedResonseErrArr:\n"); DetInvertedResponseErrArr.Print();

    //   printf("Detector corr factor numerator:\n");
    //   DetCorrFactorNumerator.Print();

    printf("yieldsMcPostFsrGen:\n");
    yieldsMcPostFsrGen.Print();

    printf("yieldsMcPostFsrRec:\n");
    yieldsMcPostFsrRec.Print();


    //   printf("Detector corr factor denominator:\n");
    //   DetCorrFactorDenominator.Print();
    //   printf("yieldsMcPostFsrRecArr:\n");
    //   yieldsMcPostFsrRecArr.Print();

    //printf("yieldsMcGen:\n");
    //yieldsMcGen.Print();
  }
  /

  if (0) {
    detResponse.printYields();
    fsrExact.printYields();
    fsrDET.printYields();
  }
*/
  //gBenchmark->Show("makeUnfoldingMatrix");
  ShowBenchmarkTime("makeUnfoldingMatrix");
  return retCodeOk;
}
コード例 #4
0
ファイル: plotCmp.C プロジェクト: andjuo/DYee
void plotCmp(int eff) {
  TFile fin1("out1_EE_Jan.root","read");
  TH1D *hEff1=(TH1D*)fin1.Get("eff_postFSRcorr");
  TH1D *hAcc1=(TH1D*)fin1.Get("acc_postFSRcorr");
  hEff1->SetDirectory(0);
  hAcc1->SetDirectory(0);
  fin1.Close();
  TH1D* hAS=(eff) ? hEff1 : hAcc1;

  TString path="../root_files_reg/constants/DY_j22_19712pb/";
  TString fname2= path+ TString((!eff) ? "acceptance_1D.root" : "efficiency_1D.root");
  TString fieldName=(eff) ? "hEfficiency" : "hAcceptance";
  TString correctionName=(eff) ? "efficiency" : "acceptance";

  TFile fin2(fname2,"read");
  TH2D *h2Our=(TH2D*)fin2.Get(fieldName);
  h2Our->SetName("h2Our");
  h2Our->SetDirectory(0);
  fin2.Close();

  TH1D *hOurRaw=createProfileX(h2Our,1,fieldName + TString("Raw"));
  TH1D *hOur=removeLastBin(hOurRaw,fieldName);
  TString label1="regressed en. (20-500,500-800,800+)";
  TH1D *h2=NULL, *h3=NULL;
  TString label2,label3;

  if (0) {
    TString fname3="../root_files_reg/constants/DY_j22_19712pb_20inf/efficiency_1D.root";
    if (!eff) fname3.ReplaceAll("efficiency","acceptance");
    TFile fin3(fname3,"read");
    TH2D* h2tmp=(TH2D*)fin3.Get(fieldName);
    TH1D* h1tmp=createProfileX(h2tmp,1,"h1tmp");
    label2="regressed en. (20-inf)";
    h2=removeLastBin(h1tmp,fieldName+TString("regEn20inf"));
    delete h1tmp;
    delete h2tmp;
    fin3.Close();
  }

  if (0) {
    TString fname4="../root_files/constants/DY_j22_19789pb/efficiency_1D.root";
    if (!eff) fname4.ReplaceAll("efficiency","acceptance");
    TFile fin4(fname4,"read");
    TH2D* h2tmp=(TH2D*)fin4.Get(fieldName);
    TH1D* h1tmp=createProfileX(h2tmp,1,"h1tmp");
    h3=removeLastBin(h1tmp,fieldName+TString("summer2012"));
    label3="old n-tuples (20-500,500-800,800+)";
    delete h1tmp;
    delete h2tmp;
    fin4.Close();
  }

  if (0 && !eff) {
    TString fname3="../root_files_reg/constants/DY_j22_19712pb/acceptance_1D-PU.root";
    //if (!eff) fname4.ReplaceAll("efficiency","acceptance");
    TFile fin3(fname3,"read");
    TH2D* h2tmp=(TH2D*)fin3.Get(fieldName);
    printHisto(h2tmp);
    TH1D* h1tmp=createProfileX(h2tmp,1,"h1tmp");
    h2=removeLastBin(h1tmp,fieldName+TString("_wPU"));
    label3="regressed (20-500,500-800,800+); wPU";
    delete h1tmp;
    delete h2tmp;
    fin3.Close();
  }

  if (0 && !eff) {
    //TString fname4="../root_files_reg/constants/DY_j22_19712pb/acceptance_1D-PU.root";
    TString fname4="../root_files_reg/constants/DY_j22_19712pb_NoReweight/acceptance_1D___NoReweight.root";
    //if (!eff) fname4.ReplaceAll("efficiency","acceptance");
    TFile fin4(fname4,"read");
    TH2D* h2tmp=(TH2D*)fin4.Get(fieldName);
    printHisto(h2tmp);
    TH1D* h1tmp=createProfileX(h2tmp,1,"h1tmp");
    h3=removeLastBin(h1tmp,fieldName+TString("_noFEWZ"));
    label3="regressed (20-500,500-800,800+); noFEWZ";
    delete h1tmp;
    delete h2tmp;
    fin4.Close();
  }

  std::cout << "Alexey: "; printHisto(hAS);
  //std::cout << "OurRaw: "; printHisto(hOurRaw);
  std::cout << "Our   : "; printHisto(hOur);

  ComparisonPlot_t cp(ComparisonPlot_t::_ratioPlain,"cp","","#it{M}_{ee}",correctionName,"ratio");
  cp.SetLogx();
  cp.AddHist1D(hAS,"Alexey","LP",kBlack,1,0);
  cp.AddHist1D(hOur,label1,"LP",kBlue,1,0);
  if (h2) cp.AddHist1D(h2,label2,"LP",kGreen+1,1,0);
  if (h3) cp.AddHist1D(h3,label3,"LP",kRed+1,1,0);
  
  TCanvas *cx= new TCanvas("cx","cx",700,850);
  cp.Prepare2Pads(cx);
  cp.Draw(cx);
  cp.TransLegend(0,-0.6);
  if (1 || h2 || h3) {
    cp.TransLegend(-0.15,0.);
    cp.WidenLegend(0.15,0.);
  }
  cx->Update();
}
コード例 #5
0
ファイル: compareFinalCSRho_v2.C プロジェクト: andjuo/DYee
int compareFinalCSRho_v2(int analysisIs2D=0, int stopAt=0, int saveCS=0) {

  if (!DYTools::setup(analysisIs2D)) return retCodeError;

  // ----------------------------------------
  // Main part

  TH2D *h2UnfYield1_global=NULL;
  TH2D *h2UnfYield2_global=NULL;

  TH2D *hEffRho1_global=NULL;
  TH2D *hEffRho2_global=NULL;
  TH2D *hAcc1_global=NULL;
  TH2D *hAcc2_global=NULL;

  TMatrixD *fsrInvUnf1_global=NULL;
  TMatrixD *fsrInvUnf2_global=NULL;

  TString rhoCorrFName="../../Results-DYee/root_files_reg/constants/DY_j22_19712pb_egamma_Unregressed_energy/covRhoFileSF_nMB41_asymHLT_Unregressed_energy-allSyst_100_v2.root";
  TH2D* hRho=LoadMatrixFields(rhoCorrFName,1,"scaleFactor","scaleFactorErr",1);
  if (!hRho) return retCodeError;

  // ----------------------------------------------------------
  //  Unfolding
  // ============================================

  // ----------------------------------------
  // load signal yield and unfold it

  if (1) {
  TString path="../../Results-DYee/root_files_reg/";
  TString yieldFName="yield/DY_j22_19712pb_ApplyEscale/bg-subtracted_yield_1D__peak20140220.root";
  TString yieldField="signalYieldDDbkg";

  TH2D* h2SigYield=LoadHisto2D(yieldField,path+yieldFName,"",1);
  if (!h2SigYield) return retCodeError;
  TH2D *h2SigYield2=Clone(h2SigYield,"h2SigYield_divRho");

  if (!multiplyHisto(h2SigYield2,hRho,0)) return retCodeError;

  //h2SigYield_global= Clone(h2SigYield,"h2SigYield_ini");
  //if (!h2SigYield_global) return retCodeError;

  TMatrixD* UnfM1= loadMatrix(path+TString("constants/DY_j22_19712pb/detResponse_unfolding_constants1D.root"),"DetResponse",41,41,1);
  if (!UnfM1) return retCodeError;
  TH2D* detResp1= createHisto2D(*UnfM1,NULL,
				"detResponse_DYee","detResponse_DYee",
				_colrange_default,1,0.);
  if (!detResp1) return retCodeError;

  TMatrixD* UnfM2= new TMatrixD(*UnfM1);
  if (!UnfM2) return retCodeError;
  TH2D* detResp2= detResp1;
  if (!detResp2) return retCodeError;

  if (stopAt==1) {
    // compare the response matrices
    // ------------- begin inset
    TH2D* detRespDiff=Clone(detResp1,"detRespDiff");
    detRespDiff->Add(detResp2,-1);
    detRespDiff->SetTitle("difference");

    detResp1->GetZaxis()->SetRangeUser(0,1.);
    detResp2->GetZaxis()->SetRangeUser(0,1.);

    printProfileSums(detResp1);
    printProfileSums(detResp2);
    compareProfileSums(detResp1,detResp2);

    TCanvas *cdiff=new TCanvas("cdiff","cdiff",1200,400);
    cdiff->Divide(3,1);
    for (int i=1; i<3; ++i) {
      TPad *pad=(TPad*)cdiff->GetPad(i);
      pad->SetLogx();
      pad->SetLogy();
    }
    AdjustFor2DplotWithHeight(cdiff);
    cdiff->cd(1);
    detResp1->Draw("COLZ");
    cdiff->cd(2);
    detResp2->Draw("COLZ");
    cdiff->cd(3);
    //detRespDiff->GetZaxis()->SetRangeUser(-0.01,0.);
    detRespDiff->Draw("COLZ");
    //UnfM->Draw("COLZ");
    cdiff->Update();
    return retCodeStop;
    // ------------- end inset
  }

  TMatrixD invUnf1(*UnfM1);
  TMatrixD invUnf2(*UnfM2);

  double det;
  invUnf1.Invert(&det);
  invUnf2.Invert(&det);

  if (stopAt==2) {
    // compare the inverted response matrices
    // ------------- begin inset
    TH2D* detInvResp1= createHisto2D(invUnf1,NULL,
				   "detInvResponse_DYee","detInvResponse_DYee",
				   _colrange_default,0,0.);
    TH2D* detInvResp2= createHisto2D(invUnf2,NULL,
				   "detInvResponse","detInvResponse",
				   _colrange_default,0,0.);
    
    TH2D* detInvRespDiff=Clone(detInvResp1,"detInvRespDiff");
    detInvRespDiff->Add(detInvResp2,-1);
    detInvRespDiff->SetTitle("difference");

    TCanvas *cdiff=new TCanvas("cdiff","cdiff",1200,400);
    cdiff->Divide(3,1);
    for (int i=1; i<3; ++i) {
      TPad *pad=(TPad*)cdiff->GetPad(i);
      pad->SetLogx();
      pad->SetLogy();
    }
    AdjustFor2DplotWithHeight(cdiff);
    cdiff->cd(1);
    detInvResp1->Draw("COLZ");
    cdiff->cd(2);
    detInvResp2->Draw("COLZ");
    cdiff->cd(3);
    detInvRespDiff->Draw("COLZ");
    //UnfM->Draw("COLZ");
    cdiff->Update();
    return retCodeStop;
    // ------------- end inset
  }

  TH2D *h2Unf1=Clone(h2SigYield,"h2Unf_DYee");
  TH2D *h2Unf2=Clone(h2SigYield,"h2Unf2");

  if ( !unfold(h2Unf1, invUnf1, h2SigYield) ||
       !unfold(h2Unf2, invUnf2, h2SigYield2) ) return retCodeError;
  h2UnfYield1_global=Clone(h2Unf1, "h2UnfYield1");
  h2UnfYield2_global=Clone(h2Unf2, "h2UnfYield2");
  if (!h2UnfYield1_global || !h2UnfYield2_global) return retCodeError;

  if (stopAt==3) {
    // ------------ begin inset
    TString label1="DYee";
    TString label2="DYee (#rho corr)";

    std::vector<int> colors;
    colors.push_back(kRed+1);
    colors.push_back(kBlue);

    // ----------------------------------
    // Plot data

    TString yAxisLabel="unfolded yield";
    std::vector<TH2D*> hV;
    std::vector<TString> labelV;

    hV.push_back(h2Unf1); labelV.push_back(label1);
    hV.push_back(h2Unf2); labelV.push_back(label2);

    TCanvas *cx= plotProfiles("cxUnf",hV,labelV,&colors,1,
			      yAxisLabel);
    cx->Update();
      
    // ----------------------------------
    // Plot error

    std::vector<TH2D*> hErrV;

    for (unsigned int i=0; i<hV.size(); ++i) {
      TH2D* hErr=Clone(hV[i],hV[i]->GetName() + TString("_err"));
      swapContentAndError(hErr);
      removeError(hErr);
      hErrV.push_back(hErr);
    }

    TCanvas *cy= plotProfiles("cerrUnf",hErrV,labelV,&colors,1,
			      yAxisLabel + TString(" error"));
    cy->Update();
      // ------------------------ end inset
  }
  }

  // ----------------------------------------------------------
  //  Load corrective factors: EffRho, Acc
  // ============================================

  if (1) {

  for (int iter=0; iter<2; ++iter) {
  TCompareCase_t theCase=(iter==0) ? _cmp_EffRho : _cmp_Acc;

  TH2D* h1=NULL, *h2=NULL, *h3=NULL;
  TH2D *h1Syst=NULL;
  TString fname1,field1,field1err;
  TString fname2,field2,field2err;
  TString fname3,field3,field3err;
  TString label1="DYee", label2="DYeeRho", label3="unknown";

  TString path1="../../Results-DYee/root_files_reg/";
  TString path2=path1;
  TString path3=path1;
  int loadText2=0;
  int is1Dhisto2=0, is1Dhisto3=0;

  TString yAxisLabel="y";

  switch(theCase) {
  case _cmp_RawYield:
    fname1="yield/DY_j22_19712pb_ApplyEscale/bg-subtracted_yield_1D__peak20140220.root";
    field1="Input/observedYield";
    //fname2="raw_yield1D_EE.txt";
    //loadText2=1;
    yAxisLabel="raw yield";
    break;

  case _cmp_FakeBkg:
    fname1="yield/DY_j22_19712pb_ApplyEscale/bg-subtracted_yield_1D__peak20140220.root";
    field1="Input/fakeBackgroundFromData";
    field1err="Input/fakeBackgroundFromDataSyst";
    //fname2="fakeBkg1D_EE.txt";
    //loadText2=1;
    yAxisLabel="fake bkg";
    break;

  case _cmp_TrueBkg:
    fname1="yield/DY_j22_19712pb_ApplyEscale/bg-subtracted_yield_1D__peak20140220.root";
    field1="Input/true2eBackgroundFromData";
    field1err="Input/true2eBackgroundFromDataSyst";
    //fname2="trueBkg1D_EE.txt";
    //loadText2=1;
    yAxisLabel="true bkg";
    break;

  case _cmp_Eff:
    fname1="constants/DY_j22_19712pb/efficiency_1D.root";
    field1="hEfficiency";
    fname2=fname1;
    field2=field2;
    //fname2="efficiencyTotal1D_EE.txt";
    //loadText2=1;
    //fname3="acceff.root";
    //field3="eff_postFSRcorr";
    //is1Dhisto3=1;
    //label3="Alexey (acceff.root)";
    yAxisLabel="efficiency";
    break;

  case _cmp_MCeff:
    fname1="constants/DY_j22_19712pb/efficiency_1D.root";
    field1="hEfficiency";
    //fname2="acceff.root";
    //field2="eff_postFSRcorr";
    //is1Dhisto2=1;
    //label2="Alexey (acceff.root)";
    yAxisLabel="MC efficiency";
    break;

  case _cmp_EffRho:
    fname1="constants/DY_j22_19712pb/efficiency_1D.root";
    field1="hEfficiency";
    fname2=fname1;
    field2=field1;
    //fname2="efficiencyTotal1D_EE.txt";
    //loadText2=1;
    yAxisLabel="efficiency #times #rho";
    break;

  case _cmp_Acc:
    fname1="constants/DY_j22_19712pb/acceptance_1D.root";
    field1="hAcceptance";
    fname2=fname1;
    field2=field1;
    //fname2="acceptance1D_EE.txt";
    //loadText2=1;
    //fname3="acceff.root";
    //field3="acc_postFSRcorr";
    //is1Dhisto3=1;
    //label3="Alexey (acceff.root)";
    yAxisLabel="acceptance";
    break;

  default:
    std::cout << "Not ready for the case\n";
    return retCodeError;
  }

  // ----------------------------------
  // Load data

  h1=LoadHisto2D(field1,path1+fname1,"",1);
  if (!h1) return retCodeError;
  if (field1err.Length()) {
    h1Syst=LoadHisto2D(field1err,path1+fname1,"",1);
    if (!h1Syst) return retCodeError;
    h1->Add(h1Syst,1.);
  }
  
  if (loadText2) {
    h2=loadTextFile(path2+fname2,"h2");
    if (!h2) return retCodeError;
  }
  else if (is1Dhisto2) h2= loadHisto1D_convert_TH2D(path2+fname2,field2);
  else h2=LoadHisto2D(field2,path2+fname2,"",1);

  if (fname3.Length()) {
    if (is1Dhisto3) h3= loadHisto1D_convert_TH2D(path3+fname3,field3);
  }

  // ----------------------------------
  // Special adjustments

  if (theCase==_cmp_EffRho) {
    // load the scale factors
    int check=0;
    if (check) {
      printHisto(h1);
      printHisto(hRho);
    }
    if (!multiplyHisto(h1,hRho,1)) return retCodeError;
    if (check) printHisto(h1);

    if (0) {
    TH2D *rhoRelErr=getRelError(hRho,"rhoRelErr",0);
    printHisto(rhoRelErr);
    }
  }

  if (theCase==_cmp_EffRho) {
    hEffRho1_global=Clone(h1,"hEffRho_DYee");
    hEffRho2_global=Clone(h2,"hEffRho_DYeeRho");
    if (!hEffRho1_global || !hEffRho2_global) return retCodeError;
  }
  else if (theCase==_cmp_Acc) {
    hAcc1_global=Clone(h1,"hAcc_DYee");
    hAcc2_global=Clone(h2,"hAcc_DYeeRho");
  }

  if (stopAt==4) {
  // ----------------------------------
  // Plot data

  std::vector<TH2D*> hV;
  std::vector<TString> labelV;

  hV.push_back(h1); labelV.push_back(label1);
  hV.push_back(h2); labelV.push_back(label2);
  if (h3) { hV.push_back(h3); labelV.push_back(label3); }

  TString cxName=Form("cxCorr_%d",iter);
  TCanvas *cx= plotProfiles(cxName,hV,labelV,NULL,1,
			    yAxisLabel);
  cx->Update();

  // ----------------------------------
  // Plot error

  std::vector<TH2D*> hErrV;

  for (unsigned int i=0; i<hV.size(); ++i) {
    TH2D* hErr=Clone(hV[i],hV[i]->GetName() + TString("_err"));
    swapContentAndError(hErr);
    removeError(hErr);
    hErrV.push_back(hErr);
  }

  TString cyName=Form("cyCorr_%d",iter);
  TCanvas *cy= plotProfiles(cyName,hErrV,labelV,NULL,1,
			    yAxisLabel + TString(" error"));
  cy->Update();
  }
  }
  } // load corrective factors


  // ----------------------------------------------------------
  //  FSR unfolding
  // ============================================

  if (1) {
    TFsrUnfCompareCase_t theCase=_cmp_fsrGood;

  // ----------------------------------
  // Main part

  TString yAxisLabel="unfolded yield";

  TString path="../../Results-DYee/root_files_reg/";
  TString yieldFName="../../Results-DYee/root_files_reg/xsec/DY_j22_19712pb/xSec_preFsr_1DpostFsrFullSp.root";
  TString yieldField="hpPostFsrFullSp_divLumi";

  TH2D* h2PostFsrCS=LoadHisto2D(yieldField,path+yieldFName,"",1);
  if (!h2PostFsrCS) return retCodeError;

  TString fName=TString("constants/DY_j22_19712pb/detResponse_unfolding_constants1D.root");
  TString tag;
  switch(theCase) {
  case _cmp_fsrGood:
    fName.ReplaceAll("detResponse","fsrGood");
    tag="good";
    break;
  case _cmp_fsrExact:
    fName.ReplaceAll("detResponse","fsrExact");
    tag="exact";
    break;
  default:
    std::cout << "macro is not ready for this case\n";
    return retCodeError;
  }

  TMatrixD* UnfM1= loadMatrix(path+fName,"DetResponse",41,41,1);
  if (!UnfM1) return retCodeError;
  TString histoName1="detFSRResponse_DYee" + tag;
  TH2D* detResp1= createHisto2D(*UnfM1,NULL,histoName1,histoName1,
				_colrange_default,1,0.);
  detResp1->GetZaxis()->SetRangeUser(0,1);
  if (!detResp1) return retCodeError;

  TH2D* detResp2=detResp1;
  TMatrixD* UnfM2=UnfM1;

  if (stopAt==10) {
    // compare the response matrices
    // ------------- begin inset
    TH2D* detRespDiff=Clone(detResp1,"detRespDiff");
    detRespDiff->Add(detResp2,-1);
    detRespDiff->SetTitle("difference");

    TCanvas *cdiff=new TCanvas("cdiff","cdiff",1200,400);
    cdiff->Divide(3,1);
    AdjustFor2DplotWithHeight(cdiff);
    cdiff->cd(1);
    detResp1->Draw("COLZ");
    cdiff->cd(2);
    detResp2->Draw("COLZ");
    cdiff->cd(3);
    detRespDiff->Draw("COLZ");
    //UnfM->Draw("COLZ");
    cdiff->Update();
    printHisto(detRespDiff);
    return retCodeStop;
    // ------------- end inset
  }

  TMatrixD invUnf1(*UnfM1);
  TMatrixD invUnf2(*UnfM2);

  double det;
  invUnf1.Invert(&det);
  invUnf2.Invert(&det);

  if (stopAt==11) {
    // compare the inverted response matrices
    // ------------- begin inset
    TH2D* detInvResp1= createHisto2D(invUnf1,NULL,
				   "detInvResponse_DYee","detInvResponse_DYee",
				   _colrange_default,0,0.);
    TH2D* detInvResp2= createHisto2D(invUnf2,NULL,
				   "detInvResponse","detInvResponse",
				   _colrange_default,0,0.);

    TH2D* detInvRespDiff=Clone(detInvResp1,"detInvRespDiff");
    detInvRespDiff->Add(detInvResp2,-1);
    detInvRespDiff->SetTitle("difference");

    TCanvas *cdiff=new TCanvas("cdiff","cdiff",1200,400);
    cdiff->Divide(3,1);
    AdjustFor2DplotWithHeight(cdiff);
    cdiff->cd(1);
    detInvResp1->Draw("COLZ");
    cdiff->cd(2);
    detInvResp2->Draw("COLZ");
    cdiff->cd(3);
    detInvRespDiff->Draw("COLZ");
    //UnfM->Draw("COLZ");
    cdiff->Update();
    return retCodeStop;
    // ------------- end inset
  }

  TH2D *h2Unf1=Clone(h2PostFsrCS,"h2Unf_DYee");
  TH2D *h2Unf2=Clone(h2PostFsrCS,"h2Unf2");

  if ( !unfold(h2Unf1, invUnf1, h2PostFsrCS) ||
       !unfold(h2Unf2, invUnf2, h2PostFsrCS) ) return retCodeError;

  fsrInvUnf1_global= new TMatrixD(invUnf1);
  fsrInvUnf2_global= new TMatrixD(invUnf2);
  if (!fsrInvUnf1_global || !fsrInvUnf2_global) return retCodeError;

  if (0) { // plot unfolded cross section

  TString label1="DYee";
  TString label2="DYeeRho";

  // ----------------------------------
  // Plot data

  std::vector<TH2D*> hV;
  std::vector<TString> labelV;

  hV.push_back(h2Unf1); labelV.push_back(label1);
  hV.push_back(h2Unf2); labelV.push_back(label2);

  TCanvas *cx= plotProfiles("cxFsr",hV,labelV,NULL,1,
			    yAxisLabel);
  cx->Update();

  // ----------------------------------
  // Plot error

  std::vector<TH2D*> hErrV;

  for (unsigned int i=0; i<hV.size(); ++i) {
    TH2D* hErr=Clone(hV[i],hV[i]->GetName() + TString("_err"));
    swapContentAndError(hErr);
    removeError(hErr);
    hErrV.push_back(hErr);
  }

  TCanvas *cy= plotProfiles("cyFsr",hErrV,labelV,NULL,1,
			    yAxisLabel + TString(" error"));
  cy->Update();
  }
  } // load FSR unfolding

  // ----------------------------------------------------------------
  // ----------------------------------------------------------
  //  Final calculation
  // ============================================

  if (1) {
    TH2D* h2UnfEffRhoYield1= Clone(h2UnfYield1_global,"h2UnfEffRhoYield_DYee");
    TH2D* h2UnfEffRhoYield2= Clone(h2UnfYield2_global,"h2UnfEffRhoYield_DYeeRho");
    int multiply=0; // 1 - multiply, 0 - divide
    if (!multiplyHisto(h2UnfEffRhoYield1, hEffRho1_global, multiply) ||
	!multiplyHisto(h2UnfEffRhoYield2, hEffRho2_global, multiply)) {
      return retCodeError;
    }

    TH2D* h2PostFsrYield1= Clone(h2UnfEffRhoYield1, "h2PostFsrYield_DYee");
    TH2D* h2PostFsrYield2= Clone(h2UnfEffRhoYield2, "h2PostFsrYield_DYeeRho");
    if (!multiplyHisto(h2PostFsrYield1, hAcc1_global, multiply) ||
	!multiplyHisto(h2PostFsrYield2, hAcc2_global, multiply)) {
      return retCodeError;
    }

    TH2D* h2PostFsrCS1= Clone(h2PostFsrYield1, "h2PostFsrCS_DYee");
    TH2D* h2PostFsrCS2= Clone(h2PostFsrYield2, "h2PostFsrCS_DYeeRho");
    h2PostFsrCS1->Scale(1/DYTools::lumiAtECMS);
    h2PostFsrCS2->Scale(1/DYTools::lumiAtECMS);

    TH2D* h2PreFsrCS1= Clone(h2PostFsrCS1, "h2PreFsrCS_DYee");
    TH2D* h2PreFsrCS2= Clone(h2PostFsrCS2, "h2PreFsrCS_DYeeRho");
    if ( !unfold(h2PreFsrCS1, *fsrInvUnf1_global, h2PostFsrCS1) ||
	 !unfold(h2PreFsrCS2, *fsrInvUnf2_global, h2PostFsrCS2) ) {
      return retCodeError;
    }

    if (saveCS) {
      TString fname="cmp_UnfRho-20140604.root";
      TFile fout(fname,"recreate");
      if (!saveHisto(fout,h2PreFsrCS1,"","") ||
	  !saveHisto(fout,h2PreFsrCS2,"","")) {
	return retCodeError;
      }
      writeBinningArrays(fout,"compareAlexey/compareFinalCSRho.C");
      fout.Close();
      std::cout << "file <" << fout.GetName() << "> created\n";
    }

    const int plotSteps[4]= { 1, 1, 1, 1 };

    for (int iCase=0; iCase<4; ++iCase) {
      if (!plotSteps[iCase]) continue;

      TH2D *h1=NULL, *h2=NULL;
      TString canvTitle="cx";
      TString yAxisLabel="y";
      switch(iCase) {
      case 0:
	h1=h2UnfEffRhoYield1;
	h2=h2UnfEffRhoYield2;
	canvTitle.Append("_unfEffRhoYield");
	yAxisLabel="N_{u}/(#epsilon#rho)";
	break;
      case 1:
	h1=h2PostFsrYield1;
	h2=h2PostFsrYield2;
	canvTitle.Append("_postFsrYield");
	yAxisLabel="N_{u}/(A#epsilon#rho)";
	break;
      case 2:
	h1=h2PostFsrCS1;
	h2=h2PostFsrCS2;
	canvTitle.Append("_postFsrCS");
	yAxisLabel="#sigma_{postFsr}=N_{u}/(A#epsilon#rhoL) [pb]";
	break;
      case 3:
	h1=h2PreFsrCS1;
	h2=h2PreFsrCS2;
	canvTitle.Append("_preFsrCS");
	yAxisLabel="#sigma_{preFsr} [pb]";
	break;
      default:
	std::cout << "not ready for iCase=" << iCase << "\n";
	return retCodeError;
      }
      if (!h1 || !h2) {
	std::cout << "histos were not assigned\n";
	return retCodeError;
      }

      TString label1="DYee";
      TString label2="DYeeRho";

      // ----------------------------------
      // Plot data

      std::vector<TH2D*> hV;
      std::vector<TString> labelV;

      hV.push_back(h1); labelV.push_back(label1);
      hV.push_back(h2); labelV.push_back(label2);

      TCanvas *cx= plotProfiles(canvTitle,hV,labelV,NULL,1,
				yAxisLabel);
      cx->Update();

      // ----------------------------------
      // Plot error

      if (0) {
      std::vector<TH2D*> hErrV;

      for (unsigned int i=0; i<hV.size(); ++i) {
	TH2D* hErr=Clone(hV[i],hV[i]->GetName() + TString("_err"));
	swapContentAndError(hErr);
	removeError(hErr);
	hErrV.push_back(hErr);
      }

      TCanvas *cy= plotProfiles(canvTitle+TString("_err"),hErrV,labelV,NULL,1,
				yAxisLabel + TString(" error"));
      cy->Update();
      }
    }
  }

  return retCodeOk;
}
コード例 #6
0
ファイル: plotDYSignal.C プロジェクト: andjuo/DYee2015
void plotDYSignal(int print=0)
{
  TH1D *h1signalMM4p2= loadHisto("cs_DYmm_13TeVMuApproved_csA.root","h1Signal",
				 "h1signalMM4p2",1,h1dummy);
  TH1D *h1signalMM4p3= loadHisto("cs_DYmm_13TeVMuApproved_csB.root","h1Signal",
				 "h1signalMM4p2",1,h1dummy);
  TH1D *h1signalEE= loadHisto("cs_DYee_13TeV_El3.root","h1Signal",
			      "h1signalEE",1,h1dummy);

  double lumiMM4p2= 865.919;
  double lumiMM4p3= 2832.673 - lumiMM4p2;
  double lumiEE= 2316.969;

  h1signalMM4p2->Scale(1/lumiMM4p2);
  h1signalMM4p3->Scale(1/lumiMM4p3);
  h1signalEE->Scale(1/lumiEE);

  TString massStr= niceMassAxisLabel(2,"");
  //TString eemassStr= niceMassAxisLabel(0,"");
  //TString mmmassStr= niceMassAxisLabel(1,"");
  TString axisStr= ";" + massStr + ";signal yield/Lumi";

  TGraphErrors *grEE= createGraph(h1signalEE,"grEE", 0);
  TGraphErrors *grMM4p2= createGraph(h1signalMM4p2, "grMM4p2", -1);
  TGraphErrors *grMM4p3= createGraph(h1signalMM4p3, "grMM4p3",  1);

  printHisto(h1signalEE);
  printHisto(h1signalMM4p2);

  graphStyle(grEE, kGreen+1, 7, 1, 0.8);
  graphStyle(grMM4p2, 46, 24, 1, 0.8);
  graphStyle(grMM4p3, kBlack, 20, 1, 0.8);

  std::vector<TCanvas*> canvasV;
  for (int i=0; i<5; i++) {
    TString canvName="";
    TH2D *h2frame=NULL;
    TCanvas *cx= createMassFrame(i,"cSignal_",
				 "signal" + axisStr, 2,
				 &canvName,&h2frame);
    canvasV.push_back(cx);
    //plotHistoSame(h1csEE,canvName,"LPE1", "DY#rightarrowee");
    //plotHistoSame(h1csMM,canvName,"LPE1", "DY#rightarrow#mu#mu");
    plotGraphSame(grEE,canvName,"PE1", "DY#rightarrowee");
    plotGraphSame(grMM4p2,canvName,"PE1", "DY#rightarrow#mu#mu (HLT4.2)");
    plotGraphSame(grMM4p3,canvName,"PE1", "DY#rightarrow#mu#mu (HLT4.3)");
    if (i==1) {
      h2frame->GetYaxis()->SetTitleOffset(1.5);
      setLeftMargin(cx,0.11);
      moveLegend(cx,0.45,0.);
    }
    if (i==4) {
      h2frame->GetYaxis()->SetNoExponent(false);
      h2frame->GetYaxis()->SetTitleOffset(1.5);
      setLeftMargin(cx,0.15);
      moveLegend(cx,0.45,0.55);
    }
    if (i==5) moveLegend(cx,0.45,0.55);
  }


  if (print==1) {
    TFile fout("foutCanvas_DYSignal.root","RECREATE");
    SaveCanvases(canvasV,"dir-plot-DYSignal",&fout);
    writeTimeTag();
    fout.Close();
    std::cout << "file <" << fout.GetName() << "> created\n";
  }
}
コード例 #7
0
ファイル: plotDYAcceptance.C プロジェクト: andjuo/DYee
int plotDYAcceptance(int analysisIs2D,
		     const TString conf,
		     DYTools::TRunMode_t runMode=DYTools::NORMAL_RUN,
		     DYTools::TSystematicsStudy_t systMode=DYTools::NO_SYST,
		     TString rndStudyStr="")
{
  gBenchmark->Start("plotDYAcceptance");

  {
    DYTools::printExecMode(runMode,systMode);
    const int debug_print=1;
    if (!DYTools::checkSystMode(systMode,debug_print,9, DYTools::NO_SYST,
				DYTools::FSR_5plus, DYTools::FSR_5minus,
				DYTools::FSR_RND_STUDY, DYTools::PU_RND_STUDY,
				DYTools::PILEUP_5plus, DYTools::PILEUP_5minus,
				DYTools::NO_REWEIGHT, DYTools::NO_REWEIGHT_FEWZ))
      return retCodeError;
  }
  //
  // A note on systematics mode
  // - FSR_5plus, FSR_5minus should be taken care here
  // - PU_5plus, PU_5minus are taken care by the eventWeight, through
  //   the PUReweight class

  if (!DYTools::setup(analysisIs2D)) {
    std::cout << "failed to initialize the analysis\n";
    return retCodeError;
  }


  //--------------------------------------------------------------------------------------------------------------
  // Settings
  //==============================================================================================================

  InputFileMgr_t inpMgr;
  if (!inpMgr.Load(conf)) return retCodeError;
  // no energy correction for this evaluation
  inpMgr.clearEnergyScaleTag();

  // Construct eventSelector, update mgr and plot directory
  TString extraTag=rndStudyStr;
  EventSelector_t evtSelector(inpMgr,runMode,systMode,
			      extraTag,"",EventSelector::_selectDefault);
  evtSelector.setTriggerActsOnData(false);

  // Acceptance is generator-level quantity and should not
  // depend on the pile-up. The flag is disabled.
  // Correction on May 01, 2014: PU should be applied at all steps
  EventWeight_t evWeight;
  if (!evWeight.init(inpMgr.puReweightFlag(),inpMgr.fewzFlag(),
		     systMode,rndStudyStr)) {
    std::cout << "failed to prepare evWeight\n";
    return retCodeError;
  }
  std::cout << "evWeight: "; evWeight.PrintDetails();

  int useSpecWeight=1;
  double specWeight=1.0;
  const double FSRmassDiff=1.0;
  // FSR_RND_STUDY sets own special weights
  if (systMode==DYTools::FSR_5plus) { specWeight=1.05; useSpecWeight=1; }
  else if (systMode==DYTools::FSR_5minus) { specWeight=0.95; useSpecWeight=1; }

  // Prepare output directory
  inpMgr.constDir(systMode,1);
  TString outFileName=inpMgr.correctionFullFileName("acceptance",systMode,0);
  std::cout << "generated outFileName=<" << outFileName << ">\n";

#ifdef calculate_PreFsrAcc
  std::cout << dashline;
  std::cout << "\t\tcalculate_PreFsrAcc is defined\n";
  std::cout << dashline;
#endif

  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code
  //==============================================================================================================

  std::cout << mainpart;

  //
  // Set up histograms
  //

  // containers to accumulate the events
  std::vector<TH2D*> hvPass, hvTotal;
  std::vector<TH2D*> hvFail;

#ifdef calculate_PreFsrAcc
  std::vector<TH2D*> hvPassPreFsr, hvTotalPreFsr;
#endif

  // debug containters
  std::vector<TH1D*> hMassv;
  std::vector<TH1D*> hMassBinsv;
  std::vector<TH1D*> hZpeakv;
  TH1D *hSelEvents=NULL;

  // the main result of the macro
  createBaseH2Vec(hvPass ,"hvPass_" ,inpMgr.mcSampleNames());
  createBaseH2Vec(hvTotal,"hvTotal_",inpMgr.mcSampleNames());
  createBaseH2Vec(hvFail,"hvFail_",inpMgr.mcSampleNames());

#ifdef calculate_PreFsrAcc
  createBaseH2Vec(hvPassPreFsr ,"hvPassPreFsr_" ,inpMgr.mcSampleNames());
  createBaseH2Vec(hvTotalPreFsr,"hvTotalPreFsr_",inpMgr.mcSampleNames());
#endif

  // debug distributions: 1GeV bins
  createAnyH1Vec(hMassv,"hGenMass_",inpMgr.mcSampleNames(),1990,10.,2000.,"#it{M}_{ee} [GeV]","counts/1GeV");
  // debug distributions for current mass bin
  createBaseH1Vec(hMassBinsv,"hGenMassBins_",inpMgr.mcSampleNames());
  // debug: accumulate info about the selected events in the samples
  hSelEvents=createAnyTH1D("hSelEvents","hSelEvents",inpMgr.mcSampleCount(),0,inpMgr.mcSampleCount(),"sampleId","event count");
  // collect number of events in the Z-peak
  createAnyH1Vec(hZpeakv,"hZpeak_",inpMgr.mcSampleNames(),60,60.,120.,"#it{M}_{ee} [GeV]","counts/1GeV");

  //
  // Access samples and fill histograms
  //
  AccessOrigNtuples_t accessInfo;

  //
  // loop over samples
  //
  if (DYTools::processData(runMode)) {

  double extraWeightFactor=1.0;
  EventCounterExt_t ecTotal("total");
  for (unsigned int isample=0; isample<inpMgr.mcSampleCount(); ++isample) {
    const CSample_t *mcSample=inpMgr.mcSampleInfo(isample);
    std::cout << "Processing " << mcSample->getLabel() << "..." << std::endl;
    std::cout << " of size " << mcSample->size() << "\n";
    if (mcSample->size()!=1) {
      std::cout << "mcSample->size is expected to be 1\n";
      return retCodeError;
    }

    // accumulate info about processed files
    EventCounterExt_t ecSample(mcSample->name);

    for (unsigned int ifile=0; ifile<mcSample->size(); ++ifile) {
      // Read input file
      TFile *infile=new TFile(mcSample->getFName(ifile),"read");
      if (!infile || !infile->IsOpen()) {
	TString skimName=inpMgr.convertSkim2Ntuple(mcSample->getFName(ifile));
	std::cout <<  "  .. failed. Trying <" << skimName << ">" << std::endl;
	infile= new TFile(skimName,"read");
      }
      assert(infile->IsOpen());
      std::cout << " Reading file <" << mcSample->getFName(ifile) << ">\n";

      // Get the TTrees
      if (!accessInfo.setTree(*infile,"Events",true)) {
	return retCodeError;
      }

    // Find weight for events for this file
    // The first file in the list comes with weight 1*extraWeightFactor,
    // all subsequent ones are normalized to xsection and luminosity
      ULong_t maxEvents = accessInfo.getEntries();
      // to match old version package (DYee 7TeV paper),
      if ((inpMgr.userKeyValueAsInt("USE7TEVMCWEIGHT")==1) && (isample==0) && (ifile==0)) {
	extraWeightFactor=maxEvents / (inpMgr.totalLumi() * inpMgr.mcSampleInfo(0)->getXsec(ifile));
      }
      //std::cout << "extraWeightFactor=" << extraWeightFactor << ", chk=" << (maxEvents0/inpMgr.mcSampleInfo(0)->getXsec(ifile)) << "\n";
      //const double extraWeightFactor=1.0;
      if (! evWeight.setWeight_and_adjustMaxEvents(maxEvents, inpMgr.totalLumi(), mcSample->getXsec(ifile),
						   extraWeightFactor, inpMgr.selectEventsFlag())) {
	std::cout << "adjustMaxEvents failed\n";
	return retCodeError;
      }
      std::cout << "mcSample xsec=" << mcSample->getXsec(ifile) << ", nEntries=" << maxEvents << "\n";


      std::cout << "       -> sample base weight is " << evWeight.baseWeight() << "\n";

      // loop through events
      EventCounterExt_t ec(Form("%s_file%d",mcSample->name.Data(),ifile));
      ec.setIgnoreScale(0); // 1 - count events, 0 - take weight in account
      // adjust the scale in the counter
      // if FEWZ weight should be considered, use evWeight.totalWeight() after
      // the FEWZ weight has been identified (see a line below)
      ec.setScale(evWeight.baseWeight());

      std::cout << "numEntries = " << accessInfo.getEntriesFast()
		<< ", " << maxEvents << " events will be used" << std::endl;


      for(ULong_t ientry=0; ientry<maxEvents; ientry++) {
	if (DYTools::isDebugMode(runMode) && (ientry>1000000)) break; // debug option
	//if (DYTools::isDebugMode(runMode) && (ientry>100)) break; // debug option
	printProgress(250000," ientry=",ientry,maxEvents);
	ec.numEvents_inc();
	
	// Load generator level info
	accessInfo.GetGen(ientry);
	// If the Z->ll leptons are not electrons, discard this event.
	// This is needed for signal MC samples such as Madgraph Z->ll
	// where all 3 lepton flavors are possible
	if (!accessInfo.genLeptonsAreElectrons()) continue;

	// Load event info to get nPU
	accessInfo.GetInfoEntry(ientry);

	// FSR study correction for weight
	if (useSpecWeight) {
	  evWeight.setSpecWeightValue(accessInfo,FSRmassDiff,specWeight);
	  if ((systMode==DYTools::FSR_RND_STUDY) && (ientry<100)) {
	    std::cout << "ientry=" << ientry << ", ";
	    evWeight.PrintDetails();
	  }
	}

	// Adjust event weight
	// .. here "false" = "not data"
	evWeight.set_PU_and_FEWZ_weights(accessInfo,false);

	// adjust the scale in the counter to include FEWZ
	// (and possibly PU) weight
	//ec.setScale(evWeight.totalWeight());

	// accumulate denominator
	const mithep::TGenInfo *gen= accessInfo.genPtr();
	hMassv[isample]->Fill(gen->vmass, evWeight.totalWeight());
	hMassBinsv[isample]->Fill(gen->vmass, evWeight.totalWeight());

	hvTotal[isample]->Fill(gen->mass, fabs(gen->y), evWeight.totalWeight());

#ifdef calculate_PreFsrAcc
	hvTotalPreFsr[isample]->Fill(gen->vmass, fabs(gen->vy), evWeight.totalWeight());
#endif

	int failAcc=0;

	// check acceptance at Gen PostFSR level
	if (!evtSelector.inAcceptance(accessInfo)) {
	  hvFail[isample]->Fill(gen->mass, fabs(gen->y), evWeight.totalWeight());
	  failAcc=1;
	}

#ifdef calculate_PreFsrAcc
	if (evtSelector.inAcceptancePreFsr(accessInfo)) {
	  hvPassPreFsr[isample]->Fill(gen->vmass, fabs(gen->vy), evWeight.totalWeight());
	}
#endif

	if (failAcc) continue;

	ec.numEventsPassedAcceptance_inc();

	// accumulate denominator
	hvPass[isample]->Fill(gen->mass, fabs(gen->y), evWeight.totalWeight());
	hSelEvents->Fill(isample,evWeight.totalWeight());

      } // loop over events
      ec.print(2);  // print info about file
      ecSample.add(ec); // accumulate event counts
      ecTotal.add(ec);

      infile->Close();
      delete infile;
    }
    ecSample.print(2); // print info about sample
    evtSelector.printCounts();
  }
  ecTotal.print(2);
  } // if (processData)


  std::cout << "outFileName=<" << outFileName << ">\n";
  if (DYTools::processData(runMode)) {
    TFile file(outFileName,"recreate");
    int res=file.IsOpen();
    if (res) res=saveVec(file,hvPass,"accPassDir");
    if (res) res=saveVec(file,hvTotal,"accTotalDir");
    if (res) res=saveVec(file,hvFail,"accFailDir");
#ifdef calculate_PreFsrAcc
    if (res) res=saveVec(file,hvPassPreFsr,"accPassPreFsrDir");
    if (res) res=saveVec(file,hvTotalPreFsr,"accTotalPreFsrDir");
#endif
    if (res) res=saveVec(file,hMassv,"mass_1GeV_bins");
    if (res) res=saveVec(file,hMassBinsv,"mass_analysis_bins");
    if (res) res=saveVec(file,hZpeakv,"mass_Zpeak_1GeV");
    if (res) res=saveHisto(file,hSelEvents,"procFileInfo");
    if (res) writeBinningArrays(file);
    file.Close();
    if (!res) {
      std::cout << "error occurred during save to file <" << outFileName << ">\n";
      return retCodeError;
    }
  }
  else {
    TFile file(outFileName,"read");
    int res=file.IsOpen();
    if (res) res=checkBinningArrays(file);
    if (res) res=loadVec(file,hvPass,"accPassDir");
    if (res) res=loadVec(file,hvTotal,"accTotalDir");
    if (res) res=loadVec(file,hvFail,"accFailDir");
#ifdef calculate_PreFsrAcc
    if (res) res=saveVec(file,hvPassPreFsr,"accPassPreFsrDir");
    if (res) res=saveVec(file,hvTotalPreFsr,"accTotalPreFsrDir");
#endif
    if (res) res=loadVec(file,hMassv,"mass_1GeV_bins");
    if (res) res=loadVec(file,hMassBinsv,"mass_analysis_bins");
    if (res) res=loadVec(file,hZpeakv,"mass_Zpeak_1GeV");
    if (res) res=loadHisto(file,&hSelEvents,"procFileInfo");
    file.Close();
    if (!res) {
      std::cout << "error occurred during save to file <" << outFileName << ">\n";
      return retCodeError;
    }
  }

  TH2D *hSumPass_BaseH2=createBaseH2("hSumPass_baseH2","hSumPass_baseH2",1);
  TH2D *hSumTotal_BaseH2=createBaseH2("hSumTotal_baseH2","hSumTotal_baseH2",1);
  TH2D *hSumFail_BaseH2=createBaseH2("hSumFail_baseH2","hSumFail_baseH2",1);
  addHistos(hSumPass_BaseH2,hvPass);
  addHistos(hSumTotal_BaseH2,hvTotal);
  addHistos(hSumFail_BaseH2,hvFail);

  TH2D *hAcc_BaseH2 = createBaseH2("hAcceptance_baseH2","hAcc_baseH2",1);
  // We need the binomial error for the acceptance
  // eff=Pass/Tot,
  // (dEff)^2= (1-eff)^2/T^2 (dPass)^2 + eff^2/T^2 (dFail)^2
  // (dFail)^2 = (dTot)^2 - (dPass)^2
  hAcc_BaseH2->Divide(hSumPass_BaseH2,hSumTotal_BaseH2,1,1,"b");

  TH2D *hSumPass=convertBaseH2actual(hSumPass_BaseH2,"hSumPass",1);
  TH2D *hSumFail=convertBaseH2actual(hSumFail_BaseH2,"hSumFail",1);
  TH2D *hSumTotal=convertBaseH2actual(hSumTotal_BaseH2,"hSumTotal",1);
  TH2D *hAcc=Clone(hAcc_BaseH2,"hAcceptance","hAcc");
  hAcc->Divide(hSumPass,hSumTotal,1,1,"b");

#ifdef calculate_PreFsrAcc
  TH2D *hSumPassPreFsr_BaseH2=createBaseH2("hSumPassPreFsr_baseH2","hSumPassPreFsr_baseH2",1);
  TH2D *hSumTotalPreFsr_BaseH2=createBaseH2("hSumTotalPreFsr_baseH2","hSumTotalPreFsr_baseH2",1);
  addHistos(hSumPassPreFsr_BaseH2,hvPassPreFsr);
  addHistos(hSumTotalPreFsr_BaseH2,hvTotalPreFsr);

  TH2D *hSumPassPreFsr=convertBaseH2actual(hSumPassPreFsr_BaseH2,"hSumPassPreFsr",1);
  TH2D *hSumTotalPreFsr=convertBaseH2actual(hSumTotalPreFsr_BaseH2,"hSumTotalPreFsr",1);
  TH2D *hAccPreFsr=Clone(hSumPassPreFsr,"hAccPreFsr","hAccPreFsr");
  hAccPreFsr->Divide(hSumPassPreFsr,hSumTotalPreFsr,1,1,"b");
#endif

  std::cout << dashline;
  std::cout << dashline;

  printHisto(hSumPass_BaseH2);
  printHisto(hSumTotal_BaseH2);
  printHisto(hAcc_BaseH2);

  printHisto(hSumPass);
  printHisto(hSumTotal);
  printHisto(hAcc);

  //printHisto(hSumFail);
  std::cout << dashline;

#ifdef calculate_PreFsrAcc
  std::cout << dashline;
  printHisto(hSumPassPreFsr);
  printHisto(hSumTotalPreFsr);
  printHisto(hAccPreFsr);
  std::cout << dashline;
#endif

  if (DYTools::processData(runMode)) {
    TFile file(outFileName,"update");
    int res=file.IsOpen();
    std::cout << "res=" << res << "\n";
    if (res) res=saveHisto(file,hAcc,"");
    if (res) res=saveHisto(file,hSumPass,"");
    if (res) res=saveHisto(file,hSumTotal,"");
    if (res) res=saveHisto(file,hSumFail,"");
#ifdef calculate_PreFsrAcc
    if (res) res=saveHisto(file,hAccPreFsr,"");
    if (res) res=saveHisto(file,hSumPassPreFsr,"");
    if (res) res=saveHisto(file,hSumTotalPreFsr,"");
#endif
    file.Close();
    if (!res) {
      std::cout << "error occurred during additional save to file <" << outFileName << ">\n";
      return retCodeError;
    }
  }


  //--------------------------------------------------------------------------------------------------------------
  // Make plots
  //==============================================================================================================

  //--------------------------------------------------------------------------------------------------------------
  // Summary print out
  //==============================================================================================================
  /*
  cout << endl;
  cout << "*" << endl;
  cout << "* SUMMARY" << endl;
  cout << "*--------------------------------------------------" << endl;
  cout << endl;
    */

  //gBenchmark->Show("plotDYAcceptance");
  ShowBenchmarkTime("plotDYAcceptance");
  return retCodeOk;
}