TH1* getWeightedAverage(TH1* h1, TH1* h2, char* histName) {


  TH1* h = h1->Clone(histName);

  int NBinsMax = h1->GetNbinsX() +2;

  for(int i=0; i < NBinsMax; ++i) {

    double x1 = h1->GetBinContent(i);
    double e1 = h1->GetBinError(i);
    double x2 = h2->GetBinContent(i);
    double e2 = h2->GetBinError(i);

    double x = 0.0;
    double e = 0.0;

    if(x1 < 0.1) {x = x2; e = e2; }
    else if(x2 < 0.1) {x = x1; e = e1; }
    else {
      x = (x1/(e1*e1) + x2/(e2*e2)) / (1.0/(e1*e1) + 1.0/(e2*e2));
      e = 1.0 / sqrt(1.0/(e1*e1) + 1.0/(e2*e2));
    }

    h->SetBinContent(i, x);
    h->SetBinError(i, e);
  }

  return h;
}
Пример #2
0
// Test the global style settings for a generic histogram.  
void styles::testGlobalStyle(bool fixY, float scale) {
  
  readGroupStyle(); setGlobalStyle(); setDefaultStyle();
  
  TH1* h = new TH1F("h", "h", 50, 0, 50);
  TH1* hc[6];
  for (int i=1; i<=50; i++) {
    double value = scale*exp(-0.5*pow(((i-25.)/5.),2));  // Gaussian shape
    h->SetBinContent(i, value);
  }

  TCanvas c;
  if(nPads == 2) c.Divide(2);
  if(nPads == 3) c.Divide(3);
  if(nPads == 4) c.Divide(2,2);
  if(nPads == 6) c.Divide(3,2);
  c.cd(1);
  h->Draw();
  if(fixY) moveYAxisLabel(h,100);
  setTitles(h, "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(10 MeV^{2})");
  float scales[] = {0.1, 10, 0.01};
  for(int pads = 2; pads<=4; pads++){
    if(nPads>=pads){
      c.cd(pads);
      hc[pads-2] = static_cast<TH1F*>(h->Clone());
      hc[pads-2]->Scale(scales[pads-2]); 
      if(fixY) moveYAxisLabel(hc[pads-2],hc[pads-2]->GetMaximum());
      hc[pads-2]->Draw();
      setTitles(hc[pads-2], "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(1000 MeV^{2})");
    }
  }
  TString epsName = "babar_code/styles/Plot_"; epsName += nPads; epsName += "Pads.eps";
  c.Print(epsName);
  
}
Пример #3
0
TH1* GenerateConsistentHist(TH1* hbg, TH1* hprod)
{
    TH1* hpprod = 0;
    if (hbg!=NULL && hprod!=NULL)
    {
        hpprod = dynamic_cast<TH1*>(hbg->Clone(hprod->GetName()));
        if (hpprod!=NULL)
	{
            Reset(hpprod);
            Double_t binctr=0, x=0, ex=0;
//            cout << hpprod->GetName() << endl;
            for(Int_t i=1; i<=hpprod->GetNbinsX(); i++)
	    {
                binctr = hbg->GetBinCenter(i);
                x  = hprod->GetBinContent(hprod->GetXaxis()->FindBin(binctr));
                ex = hprod->GetBinError(hprod->GetXaxis()->FindBin(binctr));
                if (x!=0 && ex/x > 1.0001)
                    cout << "bin:" << i << "\tcontent:" << x << "\terr:" << ex
                            << " \terr/content:" << ex/x << endl;
                hpprod->SetBinContent(i,x);
                hpprod->SetBinError(i,ex);
	    }

	}
    }
    return hpprod;
}
Пример #4
0
void dominik()
{
  TH1* matHistogramRoman = static_cast<TH1*>(extractObjectFromFile("lyRoman.root", "lightYieldProjectionY")->At(0));
  TList* objects = extractObjectFromFile("c.root", "chargeBins");
  TH1* matHistogramDominik = new TH1D("matHistogramDominik", ";channel;light yield / pixels", 512, -0.5, 512-0.5);
  int sipmIt = 0;
  for (int i = 0; i < objects->GetSize(); ++i) {
    TH1* h = static_cast<TH1*>(objects->At(i));
    if (h->GetLineColor() == 8) {
      for (int bin = 1; bin <= 128; ++bin) {
        matHistogramDominik->SetBinContent(512 - (sipmIt * 128 + bin - 1), h->GetBinContent(bin));
        if (h->GetBinError(bin) > 0)
          matHistogramDominik->SetBinError(512 - (sipmIt * 128 + bin - 1), h->GetBinError(bin));
      }
      ++sipmIt;
    }
  }
  TCanvas* c = new TCanvas;
  c->Divide(1, 2);
  c->cd(1);
  matHistogramDominik->Draw();
  matHistogramRoman->Draw("SAME");
  c->cd(2);
  TH1* h = static_cast<TH1*>(matHistogramDominik->Clone());
  h->Add(matHistogramRoman, -1);
  h->Draw();
}
Пример #5
0
TH1* subtractHistograms(const std::string& newHistogramName, const TH1* histogram_central, const TH1* histogram_shift, int mode)
{
  const TH1* histogramMinuend = 0;
  const TH1* histogramSubtrahend = 0;
  if ( compIntegral(histogram_central) >= compIntegral(histogram_shift) ) {
    histogramMinuend    = histogram_central;
    histogramSubtrahend = histogram_shift;
  } else {
    histogramMinuend    = histogram_shift;
    histogramSubtrahend = histogram_central;
  }
  if ( !(histogramMinuend && histogramSubtrahend) ) return 0;
  TH1* newHistogram = (TH1*)histogramMinuend->Clone(newHistogramName.data());
  newHistogram->Reset();
  if ( !newHistogram->GetSumw2N() ) newHistogram->Sumw2();
  int numBins = newHistogram->GetNbinsX();
  for ( int iBin = 0; iBin <= (numBins + 1); ++iBin ) {
    double newBinContent = histogramMinuend->GetBinContent(iBin) - histogramSubtrahend->GetBinContent(iBin);
    double newBinError2 = square(histogramMinuend->GetBinError(iBin)) + square(histogramSubtrahend->GetBinError(iBin));
    if ( mode == kRelative ) {
      if ( histogram_central->GetBinContent(iBin) > 0. ) {
	newBinContent /= histogram_central->GetBinContent(iBin);
	newBinError2 /= square(histogram_central->GetBinContent(iBin));
      } else {
	newBinContent = 0.;
	newBinError2 = 0.;
      }
    }
    newHistogram->SetBinContent(iBin, newBinContent);
    assert(newBinError2 >= 0.);
    newHistogram->SetBinError(iBin, TMath::Sqrt(newBinError2));
  }
  return newHistogram;
}
Пример #6
0
// ===  FUNCTION  ============================================================
//         Name:  TPlot::GetEffHist
//  Description:  
// ===========================================================================
TH1* TPlot::GetEffHist(std::string Eff, std::string det, std::string algo)
{

  TH1* hNum = GetHist1D(listEff[Eff].first, det, algo);

  TH1* hDem = GetHist1D(listEff[Eff].second, det, algo);

  TH1* temp = (TH1*)hNum->Clone(Eff.c_str());

  assert(hNum->GetNbinsX() == hDem->GetNbinsX());
  for (int i = 0; i < hNum->GetNbinsX(); ++i)
  {
    double val = hNum->GetBinContent(i) / hDem->GetBinContent(i);
    double valerr = val * sqrt( pow(hNum->GetBinError(i)/hNum->GetBinContent(i), 2) +
        pow(hDem->GetBinError(i)/hDem->GetBinContent(i), 2) );
    if (isnan(val)) { val = 0; valerr = 0; }
    temp->SetBinContent(i, val);
    temp->SetBinError(i, valerr);
    std::cout << " bin " <<i <<" val " << val << std::endl;
  }

  //temp->Divide(hDem);
  temp->GetYaxis()->SetTitle("Efficiency");

  return temp;
}       // -----  end of function TPlot::GetEffHist  -----
Пример #7
0
TH1 *
YieldMean_HighExtrapolationHisto(TH1 *h, TF1 *f, Double_t max, Double_t binwidth)
{
  /* find highest edge in histo */
  Int_t binhi;
  Double_t hi;
  for (Int_t ibin = h->GetNbinsX(); ibin > 0; ibin--) {
    if (h->GetBinContent(ibin) != 0.) {
      binhi = ibin + 1;
      hi = h->GetBinLowEdge(ibin + 1);
      break;
    }
  }
  if(max<hi) {
	Printf("Warning! You should probably set a higher max value (Max = %f, hi = %f)", max, hi);
    return 0x0;
  }
  Int_t nbins = (max - hi) / binwidth;
 if(nbins<1)
	return 0x0;	 
  TH1 *hhi = new TH1F("hhi", "", nbins, hi, max);
  
  /* integrate function in histogram bins */
  Double_t cont, err, width;
  for (Int_t ibin = 0; ibin < hhi->GetNbinsX(); ibin++) {
    width = hhi->GetBinWidth(ibin + 1);
    cont = f->Integral(hhi->GetBinLowEdge(ibin + 1), hhi->GetBinLowEdge(ibin + 2), (Double_t *)0, 1.e-6);
    err = f->IntegralError(hhi->GetBinLowEdge(ibin + 1), hhi->GetBinLowEdge(ibin + 2), (Double_t *)0, (Double_t *)0, 1.e-6);
    hhi->SetBinContent(ibin + 1, cont / width);
    hhi->SetBinError(ibin + 1, err / width);
  }

  return hhi;
}
Пример #8
0
TH1 *
YieldMean_LowExtrapolationHisto(TH1 *h, TF1 *f, Double_t min, Double_t binwidth)
{
  /* find lowest edge in histo */
  Int_t binlo;
  Double_t lo;
  for (Int_t ibin = 1; ibin < h->GetNbinsX() + 1; ibin++) {
    if (h->GetBinContent(ibin) != 0.) {
      binlo = ibin;
      lo = h->GetBinLowEdge(ibin);
      break;
    }
  }
  
  Int_t nbins = (lo - min) / binwidth;
  if(nbins<1)
	return 0x0;		
  TH1 *hlo = new TH1F("hlo", "", nbins, min, lo);
  
  /* integrate function in histogram bins */
  Double_t cont, err, width;
  for (Int_t ibin = 0; ibin < hlo->GetNbinsX(); ibin++) {
    width = hlo->GetBinWidth(ibin + 1);
    cont = f->Integral(hlo->GetBinLowEdge(ibin + 1), hlo->GetBinLowEdge(ibin + 2), (Double_t *)0, 1.e-6);
    err = f->IntegralError(hlo->GetBinLowEdge(ibin + 1), hlo->GetBinLowEdge(ibin + 2), (Double_t *)0, (Double_t *)0, 1.e-6);
    hlo->SetBinContent(ibin + 1, cont / width);
    hlo->SetBinError(ibin + 1, err / width);
  }

  return hlo;
}
Пример #9
0
  //____________________________________________________________________
  void Run(const char* newName,        const char* oldName,
	   const char* newTitle="New", const char* oldTitle="Old")
  {
    TFile* newFile = TFile::Open(newName,"READ");
    TFile* oldFile = TFile::Open(oldName,"READ");
    if (!newFile || !oldFile) return;

    TH1* newCent = GetH1(newFile, "realCent");
    TH1* oldCent = GetH1(oldFile, "realCent");
    if (!newCent || !oldCent) return;

    TString  t; t.Form("#it{R}=#frac{%s}{%s}", newTitle, oldTitle);
    TCanvas* c     = new TCanvas("c", t, 1200, 800);
    c->SetTopMargin(0.01);
    c->SetRightMargin(0.20);
    fLegend = new TLegend(1-c->GetRightMargin(),
			  c->GetBottomMargin(),
			  1, 1-c->GetTopMargin(),
			  t);
    fLegend->SetFillStyle(0);
    fLegend->SetBorderSize(0);
    THStack* stack = new THStack("ratios","");
			       
    fMin = +1e6;
    fMax = -1e6;
    TH1* one = 0;
    for (Int_t i = newCent->GetNbinsX(); i--;) {
      Double_t c1 = newCent->GetXaxis()->GetBinLowEdge(i+1);
      Double_t c2 = newCent->GetXaxis()->GetBinUpEdge(i+1);
      Info("", "c1=%f c2=%f", c1, c2);
      TH1*     r  = One(newFile, oldFile, c1, c2);    
      if (!r) continue;
      if (!one) {
	one = static_cast<TH1*>(r->Clone("one"));
	one->SetDirectory(0);
	one->Reset();
	for (Int_t j = 1; j <= one->GetNbinsX(); j++) {
	  one->SetBinContent(j,1);
	  one->SetBinError  (j,0);
	}
      }
      // r->Add(one, i-1);
      // r->Scale(TMath::Power(10,i));
      stack->Add(r);
    }
    stack->Draw("nostack");
    stack->SetMinimum(0.95*fMin);
    stack->SetMaximum(1.05*fMax);
    stack->GetHistogram()->SetXTitle("#eta");
    stack->GetHistogram()->SetYTitle("#it{R}");
    fLegend->Draw();
    c->Modified();
    c->Update();
    c->cd();
    c->SaveAs(Form("%sover%s.png", newTitle, oldTitle));
  }  
Пример #10
0
void scaleToBinWidth(TH1 &h1){
  double nBins = h1.GetXaxis()->GetNbins();
  for (int iBin = h1.GetXaxis()->GetFirst(); iBin <= h1.GetXaxis()->GetLast(); iBin++){
    double iW = h1.GetBinWidth(iBin);
    double old = h1.GetBinContent(iBin);
    h1.SetBinContent(iBin,old/iW);
  }


}
Пример #11
0
TH1 *invertHisto(TH1 *h)
{
	unsigned int n = h->GetNbinsX();
	TH1 *inv = new TH1F(Form("%s_inverted", h->GetName()), "inverted",
	                    n, -h->GetXaxis()->GetXmax(), -h->GetXaxis()->GetXmin());
	for(unsigned int i = 0; i <= n + 1; i++)
		inv->SetBinContent(n + 1 - i, h->GetBinContent(i));

	return inv;
}
Пример #12
0
TH1 *
YieldMean_ReturnExtremeLowHisto(TH1 *hin)
{
  TH1 *hout = (TH1 *)hin->Clone(Form("%s_extremelow", hin->GetName()));
  for (Int_t ibin = 0; ibin < hin->GetNbinsX(); ibin++) {
    if (hin->GetBinError(ibin + 1) <= 0.) continue;
    Double_t val = hin->GetBinContent(ibin + 1);
    Double_t err = hin->GetBinError(ibin + 1);
    hout->SetBinContent(ibin + 1, val - err);
  }
  return hout;
}
makeMuonTriggerEfficiencyLUT()
{
  TH1* histoLUT = new TH2F("muonTriggerEfficiencyCorrection", "muonTriggerEfficiencyCorrection", 
			   1, -2.5, +2.5, 1, 0., 10000.);

  histoLUT->SetBinContent(1, 1, 0.98);
  histoLUT->SetBinError(1, 1, 0.02);

  TFile* outputFile = new TFile("../data/muonTriggerEfficiencyCorrection.root", "RECREATE");
  histoLUT->Write();
  delete outputFile;
}
Пример #14
0
TH1* compHistogramErr(const TH1* histogram)
{
  if ( !histogram ) return 0;
  std::string histogramErrName = Form("%sErr", histogram->GetName());
  TH1* histogramErr = (TH1*)histogram->Clone();
  int numBins = histogram->GetNbinsX();
  for ( int iBin = 1; iBin <= numBins; ++iBin ) {
    double binError = histogram->GetBinError(iBin);
    histogramErr->SetBinContent(iBin, binError);
    histogramErr->SetBinError(iBin, 0.);
  }
  return histogramErr;
}
TH1* makeCDF(TH1* h) {
	TString sName(TString(h->GetName())+TString("_CDF"));
	TString sTitle(TString(h->GetTitle())+TString(" CDF"));
	TH1* hOut = (TH1*) h->Clone(sName);
	hOut->SetTitle(sTitle);
	hOut->Reset();
	double cdf = 0;
	for (int ibin=0; ibin < h->GetNbinsX()+2; ++ibin) {
		cdf += h->GetBinContent(ibin);
		hOut->SetBinContent(ibin,cdf);
	}
	hOut->Scale(1.0/(h->Integral(0,h->GetNbinsX()+1)));
	return hOut;
}
Пример #16
0
TH1 *
YieldMean_ReturnRandom(TH1 *hin)
{
  TH1 *hout = (TH1 *)hin->Clone("hout");
  hout->Reset();
  Double_t cont, err;
  for (Int_t ibin = 0; ibin < hin->GetNbinsX(); ibin++) {
    if (hin->GetBinError(ibin + 1) <= 0.) continue;
    cont = hin->GetBinContent(ibin + 1);
    err = hin->GetBinError(ibin + 1);
    hout->SetBinContent(ibin + 1, gRandom->Gaus(cont, err));
    hout->SetBinError(ibin + 1, err);
  }
  return hout;
}
TH1* makeIntHist(const TH1* hist,bool intIsGreatThan)
{
  TH1* cHist = (TH1*) hist->Clone("cHist");
  cHist->SetDirectory(0);
  cHist->SetName(hist->GetName());
  int maxBin = hist->GetNbinsX()+1;

  for(int binNr=0;binNr<=hist->GetNbinsX();binNr++){
    //if(hist->GetBinContent(binNr) == 0) continue;
    float nrEntries = intIsGreatThan ? hist->Integral(binNr,maxBin) : hist->Integral(0,binNr);
    cHist->SetBinContent(binNr,nrEntries);
  }
  return cHist;
    
}
TH1* divideHistogramByBinWidth(TH1* histogram)
{
  std::string histogramDensityName = Form("%s_density", histogram->GetName());
  TH1* histogramDensity = (TH1*)histogram->Clone(histogramDensityName.data());
  TAxis* xAxis = histogram->GetXaxis();
  int numBins = xAxis->GetNbins();
  for ( int iBin = 1; iBin <= numBins; ++iBin ) {
    double binContent = histogram->GetBinContent(iBin);
    double binError = histogram->GetBinError(iBin);
    double binWidth = xAxis->GetBinWidth(iBin);
    histogramDensity->SetBinContent(iBin, binContent/binWidth);
    histogramDensity->SetBinError(iBin, binError/binWidth);
  }
  return histogramDensity;
}
Пример #19
0
TH1 *
YieldMean_ReturnExtremeHisto(TH1 *hin, Float_t sign)
{
  Double_t ptlow, pthigh;
  for (Int_t ibin = 0; ibin < hin->GetNbinsX(); ibin++) {
    if (hin->GetBinError(ibin + 1) <= 0.) continue;
    ptlow = hin->GetBinLowEdge(ibin + 1);
    break;
  }
  for (Int_t ibin = hin->GetNbinsX(); ibin >= 0; ibin--) {
    if (hin->GetBinError(ibin + 1) <= 0.) continue;
    pthigh = hin->GetBinLowEdge(ibin + 2);
    break;
  }

  Double_t mean = hin->GetMean();
  Double_t maxdiff = 0.;
  TH1 *hmax = NULL;
  for (Int_t inode = 0; inode < hin->GetNbinsX(); inode++) {

    Double_t ptnode = hin->GetBinCenter(inode + 1);
    TH1 *hout = (TH1 *)hin->Clone(Form("%s_extremehard", hin->GetName()));
    
    for (Int_t ibin = 0; ibin < hin->GetNbinsX(); ibin++) {
      if (hin->GetBinError(ibin + 1) <= 0.) continue;
      Double_t val = hin->GetBinContent(ibin + 1);
      Double_t err = hin->GetBinError(ibin + 1);
      Double_t cen = hin->GetBinCenter(ibin + 1);
      if (cen < ptnode)
        err *= -1. + (cen - ptlow) / (ptnode - ptlow);
      else
        err *= (cen - ptnode) / (pthigh - ptnode);

      hout->SetBinContent(ibin + 1, val + sign * err);
    }

    Double_t diff = TMath::Abs(mean - hout->GetMean());
    if (diff > maxdiff) {
      //      printf("found max at %f\n", ptnode);
      if (hmax) delete hmax;
      hmax = (TH1 *)hout->Clone("hmax");
      maxdiff = diff;
    }
    delete hout;
  }
  return hmax;
}
Пример #20
0
TH1 *computeEffVsCut(TH1 *discrShape, double total)
{
	TH1 *h = discrShape->Clone(Form("%s_effVsCut", discrShape->GetName()));
	h->Sumw2();
	h->SetMaximum(1.5);
	h->SetMinimum(1e-3);

	unsigned int n = h->GetNbinsX();
	for(unsigned int bin = 1; bin <= n; bin++) {
		double efficiency = h->Integral(bin, n + 1) / total;
		double error = sqrt(efficiency * (1 - efficiency) / total);
		h->SetBinContent(bin, efficiency);
		h->SetBinError(bin, error);
	}

	return h;
}
Пример #21
0
TH1 *
YieldMean_ReturnCoherentRandom(TH1 *hin)
{
 if(!hin)
	return 0x0;		
  TH1 *hout = (TH1 *)hin->Clone("hout");
  hout->Reset();
  Double_t cont, err, cohe;
  cohe = gRandom->Gaus(0., 1.);
  for (Int_t ibin = 0; ibin < hin->GetNbinsX(); ibin++) {
    if (hin->GetBinError(ibin + 1) <= 0.) continue;
    cont = hin->GetBinContent(ibin + 1);
    err = hin->GetBinError(ibin + 1);
    hout->SetBinContent(ibin + 1, cont + cohe * err);
    hout->SetBinError(ibin + 1, err);
  }
  return hout;
}
Пример #22
0
// A function that get histogram and sets contents to 0 
// if entries are too small
TH1 * getHisto(TFile * file, const char * name, unsigned int rebin) {
  TObject * h = file->Get(name);
  if(h == 0)
    throw edm::Exception(edm::errors::Configuration) 
      << "Can't find object " << name << "\n";
  TH1 * histo = dynamic_cast<TH1*>(h);
  if(histo == 0)
    throw edm::Exception(edm::errors::Configuration) 
      << "Object " << name << " is of type " << h->ClassName() << ", not TH1\n";
  histo->Rebin(rebin);  
  for(int i = 1; i <= histo->GetNbinsX(); ++i) {
    if(histo->GetBinContent(i) < 0.1) {
      histo->SetBinContent(i, 0.0);
      histo->SetBinError(i, 0.0);
    }
  }
  return histo;
}
Пример #23
0
//======================================================================
// takes the ID of a graph to fill into a pre-booked histo
//
void fill1DHistoFromGraph(std::string& gid,
			  wTH1 *&wth1)
{
  map<string, wGraph_t *>::const_iterator it=glmap_id2graph.find(gid);
  if (it==glmap_id2graph.end()) {
    cerr<<"Couldn't find graph with id "<<gid<<", define first"<<endl;
    exit(-1);
  }

  if (gl_verbose)
    cout << "Loading histo from graph " << gid << endl;

  TH1 *h = wth1->histo();

  for (int ibin=1; ibin <= h->GetNbinsX(); ibin++)
    h->SetBinContent(ibin,it->second->gr->Eval(h->GetBinCenter(ibin)));

}                                               //  fill1DHistoFromGraph
Пример #24
0
void makefinal(TFile* fp, TFile *fm,  TFile* of=0) {
  setTDRStyle();
  
  TH1 *hp = (TH1*) fp->Get("MYHA");
  TH1 *hm = (TH1*) fm->Get("MYHA");
  
  TH1 *h = hp->Clone("MYNA");
  for (int k=1; k<=h->GetNbinsX(); k++) {
    std::cout << hp->GetBinContent(k) <<  " " << hm->GetBinContent(k) << std::endl;
    h->SetBinContent(k,hp->GetBinContent(k)*.5 +hm->GetBinContent(k)*.5);
    std::cout << " NEW " << h->GetBinContent(k) << std::endl;
    //h->SetBinEntries(k,1);
  }
  
  if (of!=0) { of->cd(); h->Write();}

  
}
Пример #25
0
TH1 *computeEffVsBEff(TH1 *effVsCut, TH1 *effVsCutB)
{
	TH1 *h = new TH1F(Form("%s_effVsBEff", effVsCut->GetName()), "effVsBEff",
	                  100, 0, 1);
	h->SetMaximum(1.5);
	h->SetMinimum(1e-3);

	unsigned int n = effVsCut->GetNbinsX();
	for(unsigned int bin = 1; bin <= n; bin++) {
		double eff = effVsCut->GetBinContent(bin);
		double error = effVsCut->GetBinError(bin);
		double effB = effVsCutB->GetBinContent(bin);

		h->SetBinContent(h->FindBin(effB), eff);
		h->SetBinError(h->FindBin(effB), error);
 		// FIXME: The error in effB is not propagated
	}

	return h;
}
Пример #26
0
TH1p RootWriter::CreateTH1(Histogram1D* h)
{
    const Axis& xax = h->GetAxisX();
    const int channels = xax.GetBinCount();
    TH1* r = new TH1I( h->GetName().c_str(), h->GetTitle().c_str(),
                       channels, xax.GetLeft(), xax.GetRight() );

    TAxis* rxax = r->GetXaxis();
    rxax->SetTitle(xax.GetTitle().c_str());
    rxax->SetTitleSize(0.03);
    rxax->SetLabelSize(0.03);

    TAxis* ryax = r->GetYaxis();
    ryax->SetLabelSize(0.03);

    for(int i=0; i<channels+2; ++i)
        r->SetBinContent(i, h->GetBinContent(i));
    r->SetEntries( h->GetEntries() );

    return r;
}
Пример #27
0
TH1* Cumulate(TH1* histo, Bool_t doErr, const char* copyName)
{
  // create cumulative histo
  TString nname = copyName;
  if (nname.IsNull()) {
    nname = histo->GetName();
    nname += "_cml";
  }
  TH1* cml = (TH1*) histo->Clone(nname.Data());
  int nb = histo->GetNbinsX();
  double sm = 0;
  double sme = 0;
  //
  for (int i=1;i<=nb;i++) {
    sm += histo->GetBinContent(i);
    cml->SetBinContent(i,sm);
    if (!doErr) continue;
    double ee = histo->GetBinError(i);
    sme += ee*ee;
    cml->SetBinError(i, sme>0 ? TMath::Sqrt(sme):0.);
  }
  return cml;
}
Пример #28
0
TH1* compRatioHistogram(const std::string& ratioHistogramName, const TH1* numerator, const TH1* denominator)
{
  TH1* histogramRatio = 0;
  
  if ( numerator->GetDimension() == denominator->GetDimension() &&
       numerator->GetNbinsX() == denominator->GetNbinsX() ) {
    histogramRatio = (TH1*)numerator->Clone(ratioHistogramName.data());
    histogramRatio->Divide(denominator);
    
    int nBins = histogramRatio->GetNbinsX();
    for ( int iBin = 1; iBin <= nBins; ++iBin ){
      double binContent = histogramRatio->GetBinContent(iBin);
      histogramRatio->SetBinContent(iBin, binContent - 1.);
    }
    
    histogramRatio->SetLineColor(numerator->GetLineColor());
    histogramRatio->SetLineWidth(numerator->GetLineWidth());
    histogramRatio->SetMarkerColor(numerator->GetMarkerColor());
    histogramRatio->SetMarkerStyle(numerator->GetMarkerStyle());
    histogramRatio->SetMarkerSize(numerator->GetMarkerSize());
  }

  return histogramRatio;
}
Пример #29
0
TH1* VariableSizeRebin(TH1* inhisto, unsigned int nbinsx,
		       double *xbins, TString axisname="x",
		       TString newhistoname="newhist")
{
  if ( nbinsx == 0 ) {
    cout << "Error! nbinsx must be non-zero." << endl; return 0; }

  if ( inhisto == 0 ) {
    cout << "Error! Input histogram pointer is null." << endl; return 0; }

  if ( axisname == "y"  &&  !inhisto->InheritsFrom("TH2") ) {
    cout << "No y-axis defined for " << inhisto->GetName() << endl; return 0; }

  if ( newhistoname == "" ) {
    cout << "Error! Output histogram name is null."<< endl; return 0; }

  double *edgeArr = new double[nbinsx+2]; // an extra bin for safety

  TAxis *axis = (axisname=="y" ? inhisto->GetYaxis() : inhisto->GetXaxis());

  unsigned int nbins = 0; // number of bins for the new histogram
  unsigned int j = 0; // dummy bin index (to be used as a pointer)

  for ( unsigned int i=0; i<=axis->GetNbins()+1; ++i ) {

    if ( j > nbinsx ) break;

    double ble = axis->GetBinLowEdge(i);

    if ( xbins[j] > ble ) continue;

    edgeArr[nbins] = ble; j++; nbins++;

    if ( xbins[j-1] < ble ) {
      cout << "Warning! Bin edge at " << xbins[j-1] << " does not align with"
	   << " input histo. Realigning at " << ble << ".\n";
      // check if the upcoming bin edges become obsolete after realigning.
      while ( j<=nbinsx && xbins[j] <= ble ) j++;
    }

  }

  // if we finished the loop normally, ie. not 'break'ing out, it must be
  // that the input histogram xrange is shorter than what the new binning
  // tried to get. So handle that.
  if ( j <= nbinsx ) {
    double xmax = axis->GetBinLowEdge(axis->GetNbins()+1);
    if ( xmax>edgeArr[nbins-1] ) {
      edgeArr[nbins]=xmax;
      cout << "Warning! Input histo reached max value of its x-range. "
	   << "Last bin to be closed at " << edgeArr[nbins] << "." << endl;
      nbins++; }
  }

  // we go out of the loop when index j overshoots. So our nbins is
  // always one more than actual number of bins. Fix that.
  nbins--;

  if ( nbinsx != nbins )
    cout << "Warning! nbinsx set to " << nbins
	 << " instead of " << nbinsx << "." << endl;

  //for ( unsigned int i=0; i<=nbins; i++ )
  //  cout << "For bin " << i+1 << "\tlowedge= " << edgeArr[i] << endl;

  // Now generate the new histogram
  TH1 *newhist = 0;

  if ( !inhisto->InheritsFrom("TH2") )
    newhist = inhisto->Rebin(nbins,newhistoname.Data(),edgeArr);

  else {

    // Copy the perpendicular axis as it is.
    TAxis *axisp = (axisname=="y" ? inhisto->GetXaxis() : inhisto->GetYaxis());
    unsigned int nbinsp = axisp->GetNbins();
    double *edgeArrp = new double[nbinsp+1];
    for ( unsigned int i=1; i<=nbinsp+1; ++i )
      edgeArrp[i] = axisp->GetBinLowEdge(i);

    if ( axisname == "y" ) {

      if ( axisp->IsVariableBinSize() )
	newhist = new TH2D(newhistoname, inhisto->GetTitle(),
			   nbinsp, edgeArrp, nbins, edgeArr);
      else
	newhist = new TH2D(newhistoname, inhisto->GetTitle(),
			   nbinsp, edgeArrp[0], edgeArrp[nbinsp+1],
			   nbins, edgeArr);

      if ( axisp->GetLabels() )
	for ( unsigned int i=1; i<=nbinsp; ++i )
	newhist->GetXaxis()->SetBinLabel(i, axisp->GetBinLabel(i));

    }
    else
      // ToDo: Have not yet implemented the above nice stuff for axisname=="x"
      newhist = new TH2D(newhistoname, inhisto->GetTitle(),
			 nbins, edgeArr, nbinsp, edgeArrp);

    newhist->GetYaxis()->SetTitle(inhisto->GetYaxis()->GetTitle());
    newhist->GetXaxis()->SetTitle(inhisto->GetXaxis()->GetTitle());
    bool sw2 = ( inhisto->GetSumw2N() != 0 );

    // Fill the new histogram from the input histogram
    j=0; // reset the dummy bin index
    for ( unsigned int i=0; i<=axis->GetNbins()+1; ++i ) {

      double ble = axis->GetBinLowEdge(i);
      if ( edgeArr[j] == ble ) j++; 

      for ( unsigned int k=0; k<=nbinsp+1; ++k ) {

	int newbin(0), oldbin(0);
	// Equivalent 1D bin number = binx + (fXaxis.GetNbins()+2)*biny
	if ( axisname == "y" ) {
	  newbin = k+j*(nbinsp+2); oldbin = k+i*(nbinsp+2); }
	else {
	  newbin = j+k*(nbins+2); oldbin = i+k*(axis->GetNbins()+2); }

	newhist->SetBinContent( newbin, newhist->GetBinContent(newbin)
				+ inhisto->GetBinContent(oldbin) );
	if ( sw2 )
	  newhist->SetBinError( newbin,
			        sqrt(pow(newhist->GetBinError(newbin),2) +
				     pow(inhisto->GetBinError(oldbin),2)) );
      }
    }

    newhist->SetEntries(inhisto->GetEntries());

  }

  //newhist->Draw();
  delete [] edgeArr;
  return newhist;
}
Пример #30
0
TH1 *
UnfoldMe_MB2(const Char_t *data, const Char_t *mc, const Char_t *anatag, Int_t bin, Bool_t useMBcorr , Bool_t usecorrfit , Bool_t ismc , Float_t smooth , Int_t iter , Int_t regul , Float_t weight , Bool_t bayesian , Int_t nloop )
{

  // MF comments:
  // usedMBcorr: changes the matrix used for unfonding, from effMatrix to bin matrix (I think this is just to use mult dependent v s mb correction_)
  // usecorrfit: if I understand correctly, fits the response matrix and uses fit to extrapolate it

  
  TFile *fdt =0;
  if (ismc)
    fdt =  TFile::Open(data);
  else
    fdt = TFile::Open(data);
  TFile *fmc = TFile::Open(mc);

  TList *ldt = (TList *)fdt->Get(Form("%s", anatag));
  TList *lmc = (TList *)fmc->Get(Form("%s", anatag));
  
  TH2 *hmatdt = (TH2 *)ldt->FindObject(Form(responseMatrix, bin));
  TH2 *hmatmc = 0;
  if (useMBcorr){
     hmatmc = (TH2 *)lmc->FindObject("effMatrix");
     std::cout << "USING MB" << std::endl;
     
  }
  else {
    hmatmc = (TH2 *)lmc->FindObject(Form(responseMatrix, bin));
  }

  TH1 *hdata = hmatdt->ProjectionY("hdata");
//  TH1 *hdata = hmatdt->ProjectionY("htrue");  // For truth Only Calculations

  hdata->Sumw2();
  hdata->SetBinContent(1, 0.);
  hdata->SetBinError(1, 0.);
  //  hdata->Scale(1. / hdata->Integral());
  hdata->SetMarkerStyle(25);
  TH1 *htrue = hmatdt->ProjectionX("htrue");
  htrue->Sumw2();
  //  htrue->Scale(1. / htrue->Integral());
  htrue->SetMarkerStyle(7);
  htrue->SetMarkerColor(2);
  htrue->SetBinContent(1, 0.);
  htrue->SetBinError(1, 0.);

  TH2 *hcorr = (TH2 *)hmatmc->Clone("hcorr");
  TH1 *hinit = (TH1 *)hdata->Clone("hinit");
  TH1 *hresu = (TH1 *)hdata->Clone("hresu");
  TH1 *hbias = (TH1 *)hdata->Clone("hbias");
  hresu->SetMarkerStyle(20);
  hresu->SetMarkerColor(4);
  hresu->Reset();

  TH1 *hnum = hcorr->ProjectionY("hnum");
  TH1 *hden = hcorr->ProjectionY("hden");
  TH1 *heff = hcorr->ProjectionY("heff");
  hnum->Reset();
  hnum->Sumw2();
  hden->Reset();
  hden->Sumw2();
  heff->Reset();
  for (Int_t i = 0; i < heff->GetNbinsX(); i++) {
    Float_t int1 = hcorr->Integral(i + 1, i + 1, 0, -1);
    if (int1 <= 0.) continue;
    Float_t int2 = hcorr->Integral(i + 1, i + 1, 2, -1);
    hnum->SetBinContent(i + 1, int2);
    hnum->SetBinError(i + 1, TMath::Sqrt(int2));
    hden->SetBinContent(i + 1, int1);
    hden->SetBinError(i + 1, TMath::Sqrt(int1));
  }
  TCanvas *cEfficiency = new TCanvas("cEfficiency", "cEfficiency");
  cEfficiency->SetLogx();
  cEfficiency->SetLogy();

  heff->Divide(hnum, hden, 1., 1., "B");
  heff->Draw();
#if 0
  for (Int_t ii = 0; ii < heff->GetNbinsX(); ii++) {
    heff->SetBinContent(ii + 1, 1.);
    heff->SetBinError(ii + 1, 0.);
  }
#endif

  for (Int_t i = 0; i < hcorr->GetNbinsX(); i++) {
    hcorr->SetBinContent(i + 1, 1, 0.);
    hcorr->SetBinError(i + 1, 1, 0.);
  }
  for (Int_t i = 0; i < hcorr->GetNbinsY(); i++) {
    hcorr->SetBinContent(1, i + 1, 0.);
    hcorr->SetBinError(1, i + 1, 0.);
  }
  TH2 *hcorrfit = ReturnCorrFromFit(hcorr);
  // Docs from AliUnfolding
  //Int_t AliUnfolding::Unfold(TH2* correlation, TH1* efficiency, TH1* measured, TH1* initialConditions, TH1* result, Bool_t check)
  // unfolds with unfolding method fgMethodType
  //
  // parameters:
  //  correlation: response matrix as measured vs. generated
  //  efficiency:  (optional) efficiency that is applied on the unfolded spectrum, i.e. it has to be in unfolded variables. If 0 no efficiency is applied.
  //  measured:    the measured spectrum
  //  initialConditions: (optional) initial conditions for the unfolding. if 0 the measured spectrum is used as initial conditions.
  //  result:      target for the unfolded result
  //  check:       depends on the unfolding method, see comments in specific functions

  for (Int_t iloop = 0; iloop < nloop; iloop++) {
    if (bayesian) {
      AliUnfolding::SetUnfoldingMethod(AliUnfolding::kBayesian);
      AliUnfolding::SetBayesianParameters(smooth, iter);
    } else {
      AliUnfolding::SetUnfoldingMethod(AliUnfolding::kChi2Minimization);
      AliUnfolding::SetChi2Regularization(AliUnfolding::RegularizationType(regul), weight);
    }
    AliUnfolding::SetSkip0BinInChi2(kTRUE);
    AliUnfolding::SetSkipBinsBegin(1);
    AliUnfolding::SetNbins(150, 150);
    AliUnfolding::Unfold(usecorrfit ? hcorrfit : hcorr, heff, hdata, hinit, hresu);
    hinit = (TH1 *)hresu->Clone(Form("hinit_%d", iloop));
  }

  printf("hdata->Integral(2, -1) = %f\n", hdata->Integral(2, -1));
  printf("hresu->Integral(2, -1) = %f\n", hresu->Integral(2, -1));


  TCanvas *cUnfolded = new TCanvas ("cUnfolded", "cUnfolded", 400, 800);
  cUnfolded->Divide(1, 2);
  cUnfolded->cd(1)->SetLogx();
  cUnfolded->cd(1)->SetLogy();
  hdata->Draw();
  hresu->Draw("same");
  htrue->Draw("same");
  cUnfolded->cd(2)->SetLogx();
  cUnfolded->cd(2)->DrawFrame(1., 0, 300., 10);
  TH1 *hrat = (TH1 *)hresu->Clone("hrat");
  hrat->Divide(htrue);
  hrat->Draw("same");

  TH1 *htrig = (TH1 *)hresu->Clone("htrig");
  htrig->Multiply(heff); 
  

  Float_t dndeta_resu = 0.;
  Float_t integr_resu = 0.;
  Float_t dndeta_trig = 0.;
  Float_t integr_trig = 0.;
  for (Int_t i = 1; i < hresu->GetNbinsX(); i++) {
    dndeta_resu += hresu->GetBinContent(i + 1) * hresu->GetBinLowEdge(i + 1);
    integr_resu += hresu->GetBinContent(i + 1);
    dndeta_trig += htrig->GetBinContent(i + 1) * htrig->GetBinLowEdge(i + 1);
    integr_trig += htrig->GetBinContent(i + 1);
  }

  cUnfolded->SaveAs("unfold_efficiency.pdf");

  integr_eff = integr_trig / integr_resu;
  integr_eff_err = TMath::Sqrt(integr_eff * (1. - integr_eff) / integr_resu);
  dndeta_eff = dndeta_trig / dndeta_resu;
  dndeta_eff_err = TMath::Sqrt(dndeta_eff * (1. - dndeta_eff) / dndeta_resu);

  printf("INEL > 0 efficiency: %.3f +- %.3f\n", integr_eff, integr_eff_err);
  printf("dN/dEta correction:  %.3f +- %.3f\n", dndeta_eff, dndeta_eff_err);

  return hresu;
}