void GaussianProfile::getTruncatedMeanRMS(TH1* hist, float& mean, float& mean_error, float& rms, float& rms_error) {
  int nBins = hist->GetNbinsX();
  double xMin = hist->GetXaxis()->GetXmin();
  double xMax = hist->GetXaxis()->GetXmax();
  //double binWidth = (xMax - xMin) / (double) nBins; //WARNING: this works only if bins are of the same size
  double integral = hist->Integral();

  int maxBin = 0;
  TF1* gaussian = new TF1("gaussian", "gaus");
  fitProjection(hist, gaussian, 1.5, "RQN");
  //maxBin = (int) ceil((gaussian->GetParameter(1) - xMin) / binWidth);
  maxBin = hist->FindBin(gaussian->GetParameter(1));
  delete gaussian;

  TH1D* newHisto = new TH1D("newHisto", "", nBins, xMin, xMax);
  newHisto->SetBinContent(maxBin, hist->GetBinContent(maxBin));
  newHisto->SetBinError(maxBin, hist->GetBinError(maxBin));
  int iBin = maxBin;
  int delta_iBin = 1;
  int sign  = 1;

  while (newHisto->Integral() < 0.99 * integral) {
    iBin += sign * delta_iBin;

    newHisto->SetBinContent(iBin, hist->GetBinContent(iBin));
    newHisto->SetBinError(iBin, hist->GetBinError(iBin));

    delta_iBin += 1;
    sign *= -1;
  }

  rms = newHisto->GetRMS();
  rms_error = newHisto->GetRMSError();

  while (newHisto->Integral() < 0.99 * integral) {
    iBin += sign * delta_iBin;

    newHisto->SetBinContent(iBin, hist->GetBinContent(iBin));
    newHisto->SetBinError(iBin, hist->GetBinError(iBin));

    delta_iBin += 1;
    sign *= -1;
  }

  mean = newHisto->GetMean();
  mean_error = newHisto->GetMeanError();

  delete newHisto;
}
Esempio n. 2
0
int logWeightingScanAll(){//main
  SetTdrStyle();

  //TString plotDir = "../PLOTS/gitV00-02-12/version12/gamma/200um/";
  TString plotDir = "/afs/cern.ch/work/a/amagnan/PFCalEEAna/PLOTS/gitV00-02-12/version12/gamma/200um/";

  bool useFit = true;

  //const unsigned nPu = 2;
  //unsigned pu[nPu] = {0,140};
  const unsigned nPu = 1;//2;
  unsigned pu[nPu] = {0};//,140};

  const unsigned nScans = 50;
  const double wStart = 1.;
  const double wStep = (6.-wStart)/nScans;

  const unsigned neta = 4;//7;
  const unsigned npt = 3;//13;

  const unsigned nLayers = 30;

  unsigned eta[neta] = {17,21,25,29};
  //unsigned pt[npt] = {20,30,40,50,60,70,80,90,100,125,150,175,200};
  unsigned pt[npt] = {20,50,100};

  //TF1 *cauchy = new TF1("cauchy","1/(TMath::Pi()*[2]*(1+pow((x-[0])/[2],2)))",-15,15);
  //cauchy->SetParameters(0,0,2.5);


  double wxminall[nPu][nLayers][neta];
  double wyminall[nPu][nLayers][neta];
  double etaval[neta];
       
  const unsigned nCanvas = 5;  
  TCanvas *mycx[nCanvas];
  TCanvas *mycy[nCanvas];
  for (unsigned iC(0);iC<nCanvas;++iC){
    std::ostringstream lName;
    lName << "mycx" << iC;
    mycx[iC] = new TCanvas(lName.str().c_str(),lName.str().c_str(),1500,1000);
    mycx[iC]->Divide(3,2);
    lName.str("");
    lName << "mycy" << iC;
    mycy[iC] = new TCanvas(lName.str().c_str(),lName.str().c_str(),1500,1000);
    mycy[iC]->Divide(3,2);
  }
  
  TCanvas *mycW = new TCanvas("mycW","mycW",1500,1000);
  TCanvas *mycFit = new TCanvas("mycFit","mycFit",1);

  for (unsigned ieta(0); ieta<neta;++ieta){
    etaval[ieta] = eta[ieta]/10.;
    bool savePoint = (eta[ieta] == 17);// &&  pt[ipt]==50);
    std::ostringstream pteta;
    pteta << "eta" << eta[ieta];// << "_et" << pt[ipt];
      
    TFile *fin[nPu];
      
    TFile *fout = 0;
    if (savePoint) {
      fout = TFile::Open(("PLOTS/LogWeightingStudy_"+pteta.str()+".root").c_str(),"RECREATE");
      fout->mkdir("scan");
      fout->mkdir("scan/xpos");
      fout->mkdir("scan/ypos");
      for (unsigned iS(0); iS<nScans;++iS){
	std::ostringstream lName;
	lName << "scan/xpos/scan_" << wStart+iS*wStep;
	fout->mkdir(lName.str().c_str());
	lName.str("");
	lName << "scan/ypos/scan_" << wStart+iS*wStep;
	fout->mkdir(lName.str().c_str());
      }
	
      fout->cd();
    }
    TH1F *p_xt;
    TH1F *p_yt; 
    TH1F *p_intercalibSigmaSquare[nPu];
    TH1F *p_chi2ndf; 
    if (savePoint) {
      p_xt = new TH1F("p_xt",";x truth (mm)",100,-5,5); 
      p_yt = new TH1F("p_yt",";y truth (mm)",100,-5,5);//200,1170,1370); 
	
      p_intercalibSigmaSquare[0] = new TH1F("p_intercalibSigmaSquare_0",";#sigma_{E}^{2} (2% intercalib) (GeV^2)",3000,0,30000);
      p_intercalibSigmaSquare[1] = new TH1F("p_intercalibSigmaSquare_140",";#sigma_{E}^{2} (2% intercalib) (GeV^2)",3000,0,30000);
      p_chi2ndf = new TH1F("p_chi2ndf",";#chi^{2}/N",100,0,20);
    }
      
    TH1F *p_posx[nPu][nLayers][nScans];
    TH1F *p_posy[nPu][nLayers][nScans];
      
    TH1F *p_wx[nPu][nLayers][3];
    TH1F *p_wy[nPu][nLayers][3];
      
    TH2F *p_Exy[nPu][nLayers];
    TH2F *p_deltavsreco_x[nPu][nLayers];
    TH2F *p_deltavsreco_y[nPu][nLayers];
    TH2F *p_recovstruth_x[nPu][nLayers];
    TH2F *p_recovstruth_y[nPu][nLayers];
      
    if (savePoint){
      mycFit->Print("PLOTS/fits_x.pdf[");
      mycFit->Print("PLOTS/fits_y.pdf[");
    }
      
    gStyle->SetOptStat("eMRuo");
      
    TGraphErrors *grX[nPu][nLayers];
    TGraphErrors *grY[nPu][nLayers];
    TGraphErrors *grXrms[nPu][nLayers];
    TGraphErrors *grYrms[nPu][nLayers];
      
    double resxmin[nPu][nLayers];
    double resymin[nPu][nLayers];
    double lay[nLayers];
    double wxmin[nPu][nLayers];
    double wymin[nPu][nLayers];

    for (unsigned ipu(0); ipu<nPu; ++ipu){//loop on pu


      std::vector<std::vector<double> > Exy;
      std::vector<double> init;
      init.resize(25,0);
      Exy.resize(nLayers,init);
      std::vector<double> truthPosX;
      truthPosX.resize(nLayers,0);
      std::vector<double> truthPosY;
      truthPosY.resize(nLayers,0);
	  
	  
      std::ostringstream label;
      label << "pu" << pu[ipu];
      if (savePoint){
	fout->mkdir(label.str().c_str());
	fout->cd(label.str().c_str());
      }

      for (unsigned iL(0);iL<nLayers;++iL){
	lay[iL] = iL;
	resxmin[ipu][iL] = 100;
	wxmin[ipu][iL] = 10;
	resymin[ipu][iL] = 100;
	wymin[ipu][iL] = 10;
	label.str("");   
	label << "pu" << pu[ipu];
	if (savePoint) fout->cd(label.str().c_str());
	label.str("");   
	label << "grX_pu" << pu[ipu] << "_" << iL;
	grX[ipu][iL] = new TGraphErrors();
	grX[ipu][iL]->SetName(label.str().c_str());
	label.str("");   
	label << "grY_pu" << pu[ipu] << "_" << iL;
	grY[ipu][iL] = new TGraphErrors();
	grY[ipu][iL]->SetName(label.str().c_str());
	label.str("");   
	label << "grXrms_pu" << pu[ipu] << "_" << iL;
	grXrms[ipu][iL] = new TGraphErrors();
	grXrms[ipu][iL]->SetName(label.str().c_str());
	label.str("");   
	label << "grYrms_pu" << pu[ipu] << "_" << iL;
	grYrms[ipu][iL] = new TGraphErrors();
	grYrms[ipu][iL]->SetName(label.str().c_str());
	if (savePoint) {
	  label.str("");   
	  label << "Exy_pu"<< pu[ipu] << "_" << iL;
	  p_Exy[ipu][iL] = new TH2F(label.str().c_str(),";x idx;y idx; E (mips)",
				    5,0,5,5,0,5);
	}
	label.str("");   
	label << "deltavsreco_x_pu"<< pu[ipu] << "_" << iL;
	p_deltavsreco_x[ipu][iL] = new TH2F(label.str().c_str(),";x reco (mm);x_{reco}-x_{truth} (mm);",
					    30,-15,15,100,-10,10);
	label.str("");
	label << "deltavsreco_y_pu"<< pu[ipu] << "_" << iL;
	p_deltavsreco_y[ipu][iL] = new TH2F(label.str().c_str(),";y reco (mm);y_{reco}-y_{truth} (mm);",
					    30,-15,15,100,-10,10);
	if (savePoint) {
	  label.str("");   
	  label << "recovstruth_x_pu"<< pu[ipu] << "_" << iL;
	  p_recovstruth_x[ipu][iL] = new TH2F(label.str().c_str(),";x_{truth} (mm);x reco (mm)",
					      30,-15,15,30,-15,15);
	  label.str("");
	  label << "recovstruth_y_pu"<< pu[ipu] << "_" << iL;
	  p_recovstruth_y[ipu][iL] = new TH2F(label.str().c_str(),";y_{truth} (mm);y reco (mm)",
					      30,-15,15,//200,1170,1370,
					      30,-15,15
					      ); 
	    
	}


	if (savePoint) {
	  if (savePoint) {
	    label.str("");   
	    label << "pu" << pu[ipu];
	    fout->cd(label.str().c_str());
	  }
	  for (unsigned i(0);i<5;++i){
	    label.str("");     
	    label << "wx_pu" << pu[ipu] << "_" << iL << "_" << i;
	    p_wx[ipu][iL][i] = new TH1F(label.str().c_str(),
					";wx;events",
					100,-10,0);
	    label.str("");     
	    label << "wy_pu" << pu[ipu] << "_" << iL << "_" << i;
	    p_wy[ipu][iL][i] = new TH1F(label.str().c_str(),
					";wy;events",
					100,-10,0);
	  }
	}
	for (unsigned iS(0); iS<nScans;++iS){
	  label.str("");
	  label << "scan/xpos/scan_" << wStart+iS*wStep;
	  if (savePoint) fout->cd(label.str().c_str());
	  label.str("");
	  label << "posx_pu" << pu[ipu] << "_" << iL << "_" << iS;
	  p_posx[ipu][iL][iS] = new TH1F(label.str().c_str(),
					 ";x-x_{truth} (mm);events",
					 100,-10,10);
	  label.str("");
	  label << "scan/ypos/scan_" << wStart+iS*wStep;
	  if (savePoint) fout->cd(label.str().c_str());
	  label.str("");
	  label << "posy_pu" << pu[ipu] << "_" << iL << "_" << iS;
	  p_posy[ipu][iL][iS] = new TH1F(label.str().c_str(),
					 ";y-y_{truth} (mm);events",
					 100,-10,10);
	}
      }//loop on layers
	  
      for (unsigned ipt(0); ipt<npt;++ipt){
	
	std::ostringstream linputStr;
	linputStr << plotDir << "/" << "eta" << eta[ieta] << "_et" << pt[ipt] << "_pu" << pu[ipu] ;
	//linputStr << "_logweight";
	linputStr << ".root";
	fin[ipu] = TFile::Open(linputStr.str().c_str());
	if (!fin[ipu]) {
	  std::cout << " -- Error, input file " << linputStr.str() << " cannot be opened. Skipping..." << std::endl;
	  continue;
	}
	else std::cout << " -- File " << linputStr.str() << " successfully opened." << std::endl;
	    
	TTree *tree = (TTree*)gDirectory->Get("EcellsSR2");
	if (!tree) {
	  std::cout << " Tree not found! " << std::endl;
	  continue;
	  //return 1;
	}

	for (unsigned iL(0);iL<nLayers;++iL){
	  label.str("");     
	  label << "TruthPosX_" << iL;
	  tree->SetBranchAddress(label.str().c_str(),&truthPosX[iL]);
	  label.str("");     
	  label << "TruthPosY_" << iL;
	  tree->SetBranchAddress(label.str().c_str(),&truthPosY[iL]);
	      
	  for (unsigned iy(0);iy<5;++iy){
	    for (unsigned ix(0);ix<5;++ix){
	      unsigned idx = 5*iy+ix;
	      label.str("");     
	      label << "E_" << iL << "_" << idx;
	      tree->SetBranchAddress(label.str().c_str(),&Exy[iL][idx]);
	    }
	  }
	}//loop on layers
	
	unsigned nEvts = tree->GetEntries();
	for (unsigned ievt(0); ievt<nEvts; ++ievt){//loop on entries
	      
	  if (ievt%50 == 0) std::cout << "... Processing entry: " << ievt << std::endl;
	      
	  tree->GetEntry(ievt);
	      
	  double Etotsq = 0;
	      
	  for (unsigned iL(0);iL<nLayers;++iL){
	    double Etot = 0;
	    double Ex[5] = {0,0,0,0,0};
	    double Ey[5] = {0,0,0,0,0};
	    for (unsigned idx(0);idx<25;++idx){
	      if (iL>22) Etot += Exy[iL][idx];
	      else if ((idx>5 && idx<9)||
		       (idx>10 && idx<14)||
		       (idx>15 && idx<19)) Etot += Exy[iL][idx];
	    }
	    Etotsq += pow(calibratedE(Etot*absWeight(iL,eta[ieta]/10.),eta[ieta]/10.),2);

	    if (iL>22){
	      Ex[0] = Exy[iL][0]+Exy[iL][5]+Exy[iL][10]+Exy[iL][15]+Exy[iL][20];
	      Ex[1] = Exy[iL][1]+Exy[iL][6]+Exy[iL][11]+Exy[iL][16]+Exy[iL][21];
	      Ex[2] = Exy[iL][2]+Exy[iL][7]+Exy[iL][12]+Exy[iL][17]+Exy[iL][22];
	      Ex[3] = Exy[iL][3]+Exy[iL][8]+Exy[iL][13]+Exy[iL][18]+Exy[iL][23];
	      Ex[4] = Exy[iL][4]+Exy[iL][9]+Exy[iL][14]+Exy[iL][19]+Exy[iL][24];
	      Ey[0] = Exy[iL][0]+Exy[iL][1]+Exy[iL][2]+Exy[iL][3]+Exy[iL][4];
	      Ey[1] = Exy[iL][5]+Exy[iL][6]+Exy[iL][7]+Exy[iL][8]+Exy[iL][9];
	      Ey[2] = Exy[iL][10]+Exy[iL][11]+Exy[iL][12]+Exy[iL][13]+Exy[iL][14];
	      Ey[3] = Exy[iL][15]+Exy[iL][16]+Exy[iL][17]+Exy[iL][18]+Exy[iL][19];
	      Ey[4] = Exy[iL][20]+Exy[iL][21]+Exy[iL][22]+Exy[iL][23]+Exy[iL][24];
	    }
	    else {
	      Ex[0] = Exy[iL][6]+Exy[iL][11]+Exy[iL][16];
	      Ex[1] = Exy[iL][7]+Exy[iL][12]+Exy[iL][17];
	      Ex[2] = Exy[iL][8]+Exy[iL][13]+Exy[iL][18];
	      Ey[0] = Exy[iL][6]+Exy[iL][7]+Exy[iL][8];
	      Ey[1] = Exy[iL][11]+Exy[iL][12]+Exy[iL][13];
	      Ey[2] = Exy[iL][16]+Exy[iL][17]+Exy[iL][18];
	    }
	    
	    double simplex = 0;
	    double simpley = 0;
	    if (Etot!=0) {
	      if (iL>22) {
		simplex = 10*(2*Ex[4]+Ex[3]-Ex[1]-2*Ex[0])/Etot;
		simpley = 10*(2*Ey[4]+Ey[3]-Ey[1]-2*Ey[0])/Etot;
	      }
	      else {
		simplex = 10*(Ex[2]-Ex[0])/Etot;
		simpley = 10*(Ey[2]-Ey[0])/Etot;
	      }
	    }
		
	    double xt = truthPosX[iL];
	    unsigned cellCenter = static_cast<unsigned>((truthPosY[iL]+5)/10.)*10;
	    double yt = 0;
	    //if (cellCenter>truthPosY[iL]) yt = cellCenter-truthPosY[iL];
	    yt=truthPosY[iL]-cellCenter;
	    p_deltavsreco_x[ipu][iL]->Fill(simplex,simplex-xt);
	    p_deltavsreco_y[ipu][iL]->Fill(simpley,simpley-yt);
	    if (savePoint) {
	      p_xt->Fill(xt);
	      p_yt->Fill(yt);
	      p_recovstruth_x[ipu][iL]->Fill(xt,simplex);
	      p_recovstruth_y[ipu][iL]->Fill(yt,simpley);
	
	      for (unsigned idx(0);idx<9;++idx){
		p_Exy[ipu][iL]->Fill(idx%5,idx/5,Exy[iL][idx]/Etot);
	      }
	    }
	    double wx[6][nScans];
	    double wy[6][nScans];
	  
	    for (unsigned i(0);i<6;++i){
	      for (unsigned iS(0); iS<nScans;++iS){
		wx[i][iS] = 0;
		wy[i][iS] = 0;
	      }
	    }
	    for (unsigned i(0);i<5;++i){
	      if (savePoint) p_wx[ipu][iL][i]->Fill(log(Ex[i]/Etot));
	      if (savePoint) p_wy[ipu][iL][i]->Fill(log(Ey[i]/Etot));
	      for (unsigned iS(0); iS<nScans;++iS){
		double w0 = wStart+iS*wStep;
		wx[i][iS] = std::max(0.,log(Ex[i]/Etot)+w0);
		wy[i][iS] = std::max(0.,log(Ey[i]/Etot)+w0);
		// if (log(Ex[i]/Etot)+w0<0)
		//   std::cout << " - iL= " << iL << " i=" << i << " w0=" << w0 
		// 		<< " logEx=" << log(Ex[i]/Etot)
		// 		<< " wx " << wx[i][iS] 
		// 		<< std::endl;
		// if (log(Ey[i]/Etot)+w0<0) 
		//   std::cout << " - iL= " << iL << " i=" << i << " w0=" << w0 
		// 		<< " logEy=" << log(Ey[i]/Etot)
		// 		<< " wy " << wy[i][iS] 
		// 		<< std::endl;
		wx[5][iS] += wx[i][iS];
		wy[5][iS] += wy[i][iS];
	      }
	    }
	    for (unsigned iS(0); iS<nScans;++iS){
	      double x = 0;//10*(wx[2][iS]-wx[0][iS])/wx[3][iS];
	      if (wx[5][iS]!=0) {
		if (iL>22) x = 10*(2*wx[4][iS]+wx[3][iS]-wx[1][iS]-2*wx[0][iS])/wx[5][iS];
		else x = 10*(wx[2][iS]-wx[0][iS])/wx[5][iS];
	      }
	      double y = 0;//10*(wy[2][iS]-wy[0][iS])/wy[3][iS];
	      if (wy[5][iS]!=0) {
		if (iL>22) y = 10*(2*wy[4][iS]+wy[3][iS]-wy[1][iS]-2*wy[0][iS])/wy[5][iS];
		else y = 10*(wy[2][iS]-wy[0][iS])/wy[5][iS];
	      }

	      //if (fabs(y-yt)>5) std::cout << " --- iL=" << iL << " iS=" << iS 
	      //<< " x=" << x << " xt=" << xt 
	      //<< " y=" << y << " yt=" << yt 
	      //<< std::endl;
	      p_posx[ipu][iL][iS]->Fill(x-xt);
	      p_posy[ipu][iL][iS]->Fill(y-yt);
	    }

	  }//loop on layers
	  if (savePoint) p_intercalibSigmaSquare[ipu]->Fill(Etotsq);
	}//loop on entries
    
      }//loop on pt

      //fill first point with linear weighting
      TLatex lat;
      char buf[500];

      for (unsigned iL(0);iL<nLayers;++iL){
	mycFit->cd();
	TH1D *projy = p_deltavsreco_x[ipu][iL]->ProjectionY();
	projy->Draw();
	projy->Fit("gaus","0+Q");
	TF1 *fitx = projy->GetFunction("gaus");
	fitx->SetLineColor(6);
	fitx->Draw("same");
	sprintf(buf,"Layer %d, linear weighting, pu=%d",iL,pu[ipu]);
	lat.DrawLatexNDC(0.1,0.96,buf);
	
	grX[ipu][iL]->SetPoint(0,0.5,fitx->GetParameter(2));
	grX[ipu][iL]->SetPointError(0,0,fitx->GetParError(2));
	grXrms[ipu][iL]->SetPoint(0,0.5,projy->GetRMS());
	grXrms[ipu][iL]->SetPointError(0,0,projy->GetRMSError());
	mycFit->Update();
	if (savePoint) mycFit->Print("PLOTS/fits_x.pdf");
	
	mycFit->cd();
	projy = p_deltavsreco_y[ipu][iL]->ProjectionY();
	projy->Draw();
	projy->Fit("gaus","0+Q");
	TF1 *fity = projy->GetFunction("gaus");
	fitx->SetLineColor(6);
	fitx->Draw("same");
	sprintf(buf,"Layer %d, linear weighting, pu=%d",iL,pu[ipu]);
	lat.DrawLatexNDC(0.1,0.96,buf);
	
	grY[ipu][iL]->SetPoint(0,0.5,fity->GetParameter(2));
	grY[ipu][iL]->SetPointError(0,0,fity->GetParError(2));
	grYrms[ipu][iL]->SetPoint(0,0.5,projy->GetRMS());
	grYrms[ipu][iL]->SetPointError(0,0,projy->GetRMSError());
	mycFit->Update();
	if (savePoint) mycFit->Print("PLOTS/fits_y.pdf");
      }

      //fit vs w0, get w0min
      for (unsigned iL(0);iL<nLayers;++iL){
	for (unsigned iS(0); iS<nScans;++iS){
	  mycFit->cd();
	  p_posx[ipu][iL][iS]->Draw();
	  double w0 = wStart+iS*wStep;
	  //myGaus->SetParameters();
	  p_posx[ipu][iL][iS]->Fit("gaus","0+Q","",-2.,2.);
	  TF1 *fitx = p_posx[ipu][iL][iS]->GetFunction("gaus");
	  fitx->SetLineColor(6);
	  fitx->Draw("same");
	  sprintf(buf,"Layer %d, w0=%3.1f, pu=%d",iL,w0,pu[ipu]);
	  lat.DrawLatexNDC(0.1,0.96,buf);

	  mycFit->Update();
	  if (savePoint) mycFit->Print("PLOTS/fits_x.pdf");

	  mycFit->cd();
	  p_posy[ipu][iL][iS]->Draw();
	  p_posy[ipu][iL][iS]->Fit("gaus","0+Q","",-2.,2.);
	  TF1 *fity = p_posy[ipu][iL][iS]->GetFunction("gaus");
	  fity->SetLineColor(6);
	  fity->Draw("same");
	  sprintf(buf,"Layer %d, w0=%3.1f, pu=%d",iL,w0,pu[ipu]);
	  lat.DrawLatexNDC(0.1,0.96,buf);
	  mycFit->Update();
	  if (savePoint) mycFit->Print("PLOTS/fits_y.pdf");
	  //grX[ipu][iL]->SetPoint(iS,w0,p_posx[ipu][iL][iS]->GetRMS());
	  //grX[ipu][iL]->SetPointError(iS,0,p_posx[ipu][iL][iS]->GetRMSError());
	  //grY[ipu][iL]->SetPoint(iS,w0,p_posy[ipu][iL][iS]->GetRMS());
	  //grY[ipu][iL]->SetPointError(iS,0,p_posy[ipu][iL][iS]->GetRMSError());
	  double xval = useFit? fitx->GetParameter(2) : p_posx[ipu][iL][iS]->GetRMS();
	  if (savePoint) p_chi2ndf->Fill(fitx->GetChisquare()/fitx->GetNDF());
	
	  grX[ipu][iL]->SetPoint(iS+1,w0,fitx->GetParameter(2));
	  grX[ipu][iL]->SetPointError(iS+1,0,fitx->GetParError(2));
	  grXrms[ipu][iL]->SetPoint(iS+1,w0,p_posx[ipu][iL][iS]->GetRMS());
	  grXrms[ipu][iL]->SetPointError(iS+1,0,p_posx[ipu][iL][iS]->GetRMSError());
	  if (xval < resxmin[ipu][iL]){
	    resxmin[ipu][iL] = xval;
	    wxminall[ipu][iL][ieta] = w0;
	    wxmin[ipu][iL] = w0;
	  }
	  double yval = useFit? fity->GetParameter(2) : p_posy[ipu][iL][iS]->GetRMS();
	  if (savePoint) p_chi2ndf->Fill(fity->GetChisquare()/fity->GetNDF());
	  grY[ipu][iL]->SetPoint(iS+1,w0,fity->GetParameter(2));
	  grY[ipu][iL]->SetPointError(iS+1,0,fity->GetParError(2));
	  grYrms[ipu][iL]->SetPoint(iS+1,w0,p_posy[ipu][iL][iS]->GetRMS());
	  grYrms[ipu][iL]->SetPointError(iS+1,0,p_posy[ipu][iL][iS]->GetRMSError());
	
	  if (yval < resymin[ipu][iL]){
	    resymin[ipu][iL] = yval;
	    wyminall[ipu][iL][ieta] = w0;
	    wymin[ipu][iL] = w0;
	  }
	}
	grX[ipu][iL]->SetTitle(";W0; #sigma(x-xt) (mm)");
	grY[ipu][iL]->SetTitle(";W0; #sigma(y-yt) (mm)");
	grX[ipu][iL]->SetLineColor(ipu+1);
	grX[ipu][iL]->SetMarkerColor(ipu+1);
	grX[ipu][iL]->SetMarkerStyle(ipu+21);
	grY[ipu][iL]->SetLineColor(ipu+1);
	grY[ipu][iL]->SetMarkerColor(ipu+1);
	grY[ipu][iL]->SetMarkerStyle(ipu+21);

	grXrms[ipu][iL]->SetLineColor(ipu+3);
	grXrms[ipu][iL]->SetMarkerColor(ipu+3);
	grXrms[ipu][iL]->SetMarkerStyle(ipu+23);
	grYrms[ipu][iL]->SetLineColor(ipu+3);
	grYrms[ipu][iL]->SetMarkerColor(ipu+3);
	grYrms[ipu][iL]->SetMarkerStyle(ipu+23);

	mycx[iL/6]->cd(iL%6+1);
	grX[ipu][iL]->Draw(ipu==0?"APL":"PLsame");
	grXrms[ipu][iL]->Draw("PLsame");
	//gStyle->SetStatX(0.4);
	//gStyle->SetStatY(1.0);
	//p_Exy[ipu][iL]->Draw("colztext");
	sprintf(buf,"Layer %d",iL);
	lat.DrawLatexNDC(0.4,0.85,buf);
	mycy[iL/6]->cd(iL%6+1);
	grY[ipu][iL]->Draw(ipu==0?"APL":"PLsame");
	grYrms[ipu][iL]->Draw("PLsame");
	lat.DrawLatexNDC(0.4,0.85,buf);

      }//loop on layers

      //return 1;
    }//loop on pu

    if (savePoint) {
      mycFit->Print("PLOTS/fits_x.pdf]");
      mycFit->Print("PLOTS/fits_y.pdf]");
    }

    return 1;

    TLegend *leg = new TLegend(0.6,0.6,0.94,0.94);
    leg->SetFillColor(0);
    if (grX[0][10]) leg->AddEntry(grX[0][10],"fit pu=0","P");
    if (grXrms[0][10]) leg->AddEntry(grXrms[0][10],"RMS pu=0","P");
    if (grX[1][10]) leg->AddEntry(grX[1][10],"fit pu=140","P");
    if (grXrms[1][10]) leg->AddEntry(grXrms[1][10],"RMS pu=140","P");
    for (unsigned iC(0);iC<nCanvas;++iC){
      mycx[iC]->cd(1);
      leg->Draw("same");
      mycx[iC]->Update();
      std::ostringstream lsave;
      lsave << "PLOTS/logWeighted_x_" << 6*iC << "_" << 6*iC+5 << "_" << pteta.str() ;
      lsave << ".pdf";
      mycx[iC]->Print(lsave.str().c_str());
      mycx[iC]->Update();
	
      mycy[iC]->cd(1);
      leg->Draw("same");
      lsave.str("");
      lsave << "PLOTS/logWeighted_y_" << 6*iC << "_" << 6*iC+5 << "_" << pteta.str();
      lsave << ".pdf";
      mycy[iC]->Print(lsave.str().c_str());
    }
      
    //plot w0min vs layer
    TGraph *grW[4] = {0,0,0,0};
    for (unsigned ipu(0); ipu<nPu; ++ipu){//loop on pu
      std::cout << " --Processing pu " << pu[ipu] << std::endl;
	
      grW[2*ipu] = new TGraph(nLayers,lay,wxmin[ipu]);
      grW[2*ipu+1] = new TGraph(nLayers,lay,wymin[ipu]);
      for (unsigned iL(0);iL<nLayers;++iL){
	std::cout << " if (layer==" << iL 
		  << ") return " << (wxmin[ipu][iL]+wymin[ipu][iL])/2. <<";"
	  //		<< " minimum x= " << grX[ipu][iL]->GetYaxis()->GetXmin()
	  //		<< " minimum y= " << grY[ipu][iL]->GetYaxis()->GetXmin()
		  << std::endl;
      }
      mycW->cd();
      grW[2*ipu]->SetTitle(";layer;W0");
      grW[2*ipu]->SetMaximum(6);
      grW[2*ipu]->SetMinimum(0);
      grW[2*ipu]->SetLineColor(2*ipu+1);
      grW[2*ipu]->SetMarkerColor(2*ipu+1);
      grW[2*ipu]->SetMarkerStyle(20+2*ipu+1);
      grW[2*ipu+1]->SetLineColor(2*ipu+2);
      grW[2*ipu+1]->SetMarkerColor(2*ipu+2);
      grW[2*ipu+1]->SetMarkerStyle(20+2*ipu+2);
      if (ipu==0) {
	grW[2*ipu]->Draw("APL");
	grW[2*ipu+1]->Draw("PLsame");
      }
      else {
	grW[2*ipu]->Draw("PLsame");
	grW[2*ipu+1]->Draw("PLsame");
      }
    }
    std::ostringstream lsave;
    lsave << "PLOTS/w0minvsLayers_" << pteta.str();
    if (useFit) lsave << "_fit";
    else lsave << "_rms";
    lsave << ".pdf";
    mycW->Update();
    mycW->Print(lsave.str().c_str());

    if (savePoint) fout->Write();
    else {
      for (unsigned i(0);i<4;++i){
	if (grW[i]) grW[i]->Delete();
      }
      for (unsigned ipu(0); ipu<nPu; ++ipu){//loop on pu
	for (unsigned iL(0); iL<nLayers;++iL){//loop on layers
	  grX[ipu][iL]->Delete();
	  grXrms[ipu][iL]->Delete();
	  grY[ipu][iL]->Delete();
	  grYrms[ipu][iL]->Delete();
	  p_deltavsreco_x[ipu][iL]->Delete();
	  p_deltavsreco_y[ipu][iL]->Delete();
	  for (unsigned iS(0); iS<nScans;++iS){
	    p_posx[ipu][iL][iS]->Delete();
	    p_posy[ipu][iL][iS]->Delete();
	  }
	}
      }
    }

    std::cout << " -- eta point finished successfully" << std::endl;

  }//loop on eta

  //plot w0min vs pt for all eta
  TGraph *grW[4] = {0,0,0,0};
  mycW->Clear();
  for (unsigned iL(0); iL<nLayers;++iL){//loop on layers
    for (unsigned ipu(0); ipu<nPu; ++ipu){//loop on pu
      std::cout << " --Processing pu " << pu[ipu] << std::endl;
      grW[2*ipu] = new TGraph(neta,etaval,wxminall[ipu][iL]);
      grW[2*ipu+1] = new TGraph(neta,etaval,wyminall[ipu][iL]);
      mycW->cd();//ieta+1);
      grW[2*ipu]->SetTitle(";#eta;W0");
      grW[2*ipu]->SetMaximum(6);
      grW[2*ipu]->SetMinimum(0);
      grW[2*ipu]->SetLineColor(2*ipu+1);
      grW[2*ipu]->SetMarkerColor(2*ipu+1);
      grW[2*ipu]->SetMarkerStyle(20+2*ipu+1);
      grW[2*ipu+1]->SetLineColor(2*ipu+2);
      grW[2*ipu+1]->SetMarkerColor(2*ipu+2);
      grW[2*ipu+1]->SetMarkerStyle(20+2*ipu+2);
      if (ipu==0) {
	grW[2*ipu]->Draw("APL");
	grW[2*ipu+1]->Draw("PLsame");
      }
      else {
	grW[2*ipu]->Draw("PLsame");
	grW[2*ipu+1]->Draw("PLsame");
      }
    }//loop on pu
    std::ostringstream lsave;
    lsave << "PLOTS/w0minvseta_layer" << iL;
    if (useFit) lsave << "_fit";
    else lsave << "_rms";
    lsave << ".pdf";
    
    char buf[500];
    TLatex lat;
    sprintf(buf,"Layer %d",iL);
    lat.DrawLatexNDC(0.1,0.96,buf);
    
    mycW->Update();
    mycW->Print(lsave.str().c_str());
  }//loop on layers
  
  
  return 0;
}//main
Esempio n. 3
0
void fitTools::getTruncatedMeanAndRMS(TH1D* h1_projection, Float_t& mean, Float_t& mean_err, Float_t& rms, Float_t& rms_err, Double_t percentIntegral_MEAN, Double_t percentIntegral_RMS) {
//TCanvas* getTruncatedMeanAndRMS(TH1D* h1_projection, Float_t& mean, Float_t& mean_err, Float_t& rms, Float_t& rms_err, Double_t percentIntegral_MEAN=0.9, Double_t percentIntegral_RMS=0.68) {

   bool useMode = false;


   if( percentIntegral_MEAN<0. || percentIntegral_MEAN>1. ) {
     std::cout << "WARNING! percentIntegral_MEAN is " << percentIntegral_MEAN << "!! Setting it to 90%." << std::endl;
     percentIntegral_MEAN = 0.9;
   }

   if( percentIntegral_RMS<0. || percentIntegral_RMS>1. ) {
     std::cout << "WARNING! percentIntegral_RMS is " << percentIntegral_RMS << "!! Setting it to 68%." << std::endl;
     percentIntegral_RMS = 0.68;
   }

   Int_t nBins = h1_projection->GetNbinsX();
   Double_t xMin = h1_projection->GetXaxis()->GetXmin();
   Double_t xMax = h1_projection->GetXaxis()->GetXmax();
   Double_t binWidth = (xMax-xMin)/(Double_t)nBins; //WARNING: this works only if bins are of the same size
   Double_t integral = h1_projection->Integral();
//  std::cout << "xmax: " << xMax << "\txMin: " << xMin << std::endl;
  
   //first: find maximum
//  std::cout << "N: " << gaussian->GetParameter(0) << "\tmu: " << gaussian->GetParameter(1) << "\tsigma: " << gaussian->GetParameter(2) << std::endl;
   Int_t maxBin;
   if( useMode ) {
     maxBin = h1_projection->GetMaximumBin();
   } else {
     TF1* gaussian = new TF1("gaussian", "gaus");
     gaussian->SetLineColor(kGreen);
     fitProjection(h1_projection, gaussian, 1.5, "RQN");
     maxBin = (Int_t)ceil((gaussian->GetParameter(1)-xMin)/binWidth);
     delete gaussian;
   }

//  std::cout << "maxBin: " << maxBin << "\tbin center: " << h1_projection->GetXaxis()->GetBinCenter(maxBin) << "\t gauss mu: " << gaussian->GetParameter(1) << std::endl;
   TH1D* newHisto = new TH1D("newHisto", "", nBins, xMin, xMax);
   newHisto->SetBinContent( maxBin, h1_projection->GetBinContent(maxBin) );
   newHisto->SetBinError( maxBin, h1_projection->GetBinError(maxBin) );
   Int_t iBin = maxBin;
   Int_t delta_iBin = 1;
   Int_t sign  = 1;
//  std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;

   while( newHisto->Integral() < percentIntegral_RMS*integral ) {

     iBin += sign*delta_iBin; 
     
//  std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;
     newHisto->SetBinContent( iBin, h1_projection->GetBinContent(iBin) );
     newHisto->SetBinError( iBin, h1_projection->GetBinError(iBin) );

     delta_iBin += 1;
     sign *= -1;

   }

//  std::cout << "done with rms." << std::endl;
//    TCanvas* c1 = new TCanvas("c1", "c1", 800, 600);
//    c1->cd();
//    h1_projection->Draw();
//    newHisto->SetFillColor(kRed);
//    newHisto->DrawClone("HISTO same");

   rms = newHisto->GetRMS();
   rms_err = newHisto->GetRMSError();

//std::cout << "rms: " << rms << std::endl;
   while( newHisto->Integral() < percentIntegral_MEAN*integral ) {
//  std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;

     iBin += sign*delta_iBin; 
     
     newHisto->SetBinContent( iBin, h1_projection->GetBinContent(iBin) );
     newHisto->SetBinError( iBin, h1_projection->GetBinError(iBin) );

     delta_iBin += 1;
     sign *= -1;

   }

//    newHisto->SetFillStyle(3004);
//    newHisto->SetFillColor(kBlue);
//    newHisto->DrawClone("HISTO same");

   mean = newHisto->GetMean();
   mean_err = newHisto->GetMeanError();

   delete newHisto;

//    return c1;
}
Esempio n. 4
0
// ============================================================================
/// finalize the algorithm
// ============================================================================
StatusCode Aida2Root::finalize()
{

  always() << "Get the native ROOT representation of histograms!" << endmsg ;

  {  // loop over all 1D-histograms
    for ( List::const_iterator ipath = m_1Ds.begin() ;
          m_1Ds.end() != ipath ; ++ipath )
    {
      /// retrieve the historam by full path:
      AIDA::IHistogram1D* aida = 0 ;
      StatusCode sc = histoSvc()->retrieveObject( *ipath , aida ) ;
      if ( sc.isFailure() || 0 == aida )
      { return Error ( "Unable to retrieve 1D-histogram '" + (*ipath) + "'"  ) ; }
      /// convert it to ROOT
      TH1D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
      if ( 0 == root )
      { return Error ( "Unable to convert to ROOT the 1D-histogram '"+(*ipath)+"'") ; }
      /// use the native printout from ROOT
      info() << "The native ROOT printout for 1D-histogram '" << (*ipath) << "':" << endmsg ;
      root->Print() ;

      info () << " |  Compare       | AIDA/HistoStats |     ROOT/TH1    |      Delta      | "  << endmsg ;
      const std::string format = " | %1$-14.14s | %2$ 15.8g | %3$- 15.8g | %4$= 15.8g | "  ;
      info () << print
        ( Gaudi::Utils::HistoStats::mean        ( aida ) ,
          root->GetMean      ()    , "'mean'"        , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::meanErr     ( aida ) ,
          root->GetMeanError ()    , "'meanErr'"     , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::rms         ( aida ) ,
          root->GetRMS       ()    , "'rms'"         , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::rmsErr      ( aida ) ,
          root->GetRMSError  ()    , "'rmsErr'"      , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::skewness    ( aida ) ,
          root->GetSkewness ()     , "'skewness'"    , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::skewnessErr ( aida ) ,
          root->GetSkewness ( 11 ) , "'skewnessErr'" , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::kurtosis  ( aida ) ,
          root->GetKurtosis ()     , "'kurtosis'"    , format  ) << endmsg ;
      info () << print
        ( Gaudi::Utils::HistoStats::kurtosisErr ( aida ) ,
          root->GetKurtosis ( 11 ) , "'kurtosisErr'" , format  ) << endmsg ;
    }
  }

  { // loop over all 2D-histograms
    for ( List::const_iterator ipath = m_2Ds.begin() ;
          m_2Ds.end() != ipath ; ++ipath )
    {
      /// retrieve the historam by full path:
      AIDA::IHistogram2D* aida = 0 ;
      StatusCode sc = histoSvc()->retrieveObject( *ipath , aida ) ;
      if ( sc.isFailure() || 0 == aida )
      { return Error ( "Unable to retrieve 2D-histogram '" + (*ipath) + "'"  ) ; }
      /// convert it to ROOT
      TH2D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
      if ( 0 == root )
      { return Error ( "Unable to convert to ROOT the 2D-histogram '"+(*ipath)+"'") ; }
      /// use the native printout from ROOT
      info() << "The native ROOT printout for 2D-histogram '" << (*ipath) << "':" << endmsg ;
      root->Print() ;
    }
  }

  { // loop over all 3D-histograms
    for ( List::const_iterator ipath = m_3Ds.begin() ;
          m_3Ds.end() != ipath ; ++ipath )
    {
      /// retrieve the historam by full path:
      AIDA::IHistogram3D* aida = 0 ;
      StatusCode sc = histoSvc()->retrieveObject( *ipath , aida ) ;
      if ( sc.isFailure() || 0 == aida )
      { return Error ( "Unable to retrieve 3D-histogram '" + (*ipath) + "'"  ) ; }
      /// convert it to ROOT
	TH3D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
      if ( 0 == root )
      { return Error ( "Unable to convert to ROOT the 3D-histogram '"+(*ipath)+"'") ; }
      /// use the native printout from ROOT
      info() << "The native ROOT printout for 3D-histogram '" << (*ipath) << "':" << endmsg ;
      root->Print() ;
    }
  }


  { // loop over all 1D-profiles
    for ( List::const_iterator ipath = m_1Ps.begin() ;
          m_1Ps.end() != ipath ; ++ipath )
    {
      /// retrieve the historam by full path:
      AIDA::IProfile1D* aida = 0 ;
      StatusCode sc = histoSvc()->retrieveObject( *ipath , aida ) ;
      if ( sc.isFailure() || 0 == aida )
      { return Error ( "Unable to retrieve 1D-profile '" + (*ipath) + "'"  ) ; }
      /// convert it to ROOT
	TProfile* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
      if ( 0 == root )
      { return Error ( "Unable to convert to ROOT the 1D-profile '"+(*ipath)+"'") ; }
      /// use the native printout from ROOT
      info() << "The native ROOT printout for 1D-profile '" << (*ipath) << "':" << endmsg ;
      root->Print() ;
    }
  }


  { // loop over all 2D-profiles
    for ( List::const_iterator ipath = m_2Ps.begin() ;
          m_2Ps.end() != ipath ; ++ipath )
    {
      /// retrieve the historam by full path:
      AIDA::IProfile2D* aida = 0 ;
      StatusCode sc = histoSvc()->retrieveObject( *ipath , aida ) ;
      if ( sc.isFailure() || 0 == aida )
      { Error ( "Unable to retrieve 2D-profile '" + (*ipath) + "'"  ) ; }
      /// convert it to ROOT
	TProfile2D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
      if ( 0 == root )
      { Error ( "Unable to convert to ROOT the 2D-profile '"+(*ipath)+"'") ; }
      /// use the native printout from ROOT
      info() << "The native ROOT printout for 2D-profile '" << (*ipath) << "':" << endmsg ;
      root->Print() ;
    }
  }

  return GaudiHistoAlg::finalize() ;
}