00001
00002
00003
00004 #include <cmath>
00005 #include <cassert>
00006 #include <iostream>
00007
00008
00009 #include "TH1.h"
00010
00011
00012 #include "PhysicsNtuple/Hist1d.h"
00013 #include "CountHist.h"
00014
00015 using namespace std;
00016
00017
00018 Anp::CountHist::CountHist(int iactn_, int reson_, Particle::Particle_t particle_)
00019 :hall(0),
00020 hsel(0),
00021 hrat(0),
00022 nmin(1000.0),
00023 particle(particle_),
00024 iactn(iactn_),
00025 reson(reson_),
00026 verbose(false)
00027 {
00028 }
00029
00030
00031 Anp::CountHist::~CountHist()
00032 {
00033 }
00034
00035
00036 void Anp::CountHist::Fill(const double value, const double weight_all, const double weight_sel)
00037 {
00038 if(!CountHist::Valid())
00039 {
00040 return;
00041 }
00042
00043 if(weight_all < 0.0 || weight_sel < 0.0)
00044 {
00045 cerr << "CountHist::Fill - event with negative weight" << endl;
00046 }
00047
00048 hall -> Fill(value, weight_all);
00049 hsel -> Fill(value, weight_sel);
00050 }
00051
00052
00053 TH1* Anp::CountHist::ComputeRatio(const string &option)
00054 {
00055 if(!Valid())
00056 {
00057 return 0;
00058 }
00059
00060 CountHist::SetRatio(hsel, hall, hrat);
00061
00062 if(option.find("merge") != string::npos && nmin > 0.0)
00063 {
00064 Anp::Hist1d<double> hist(*hsel);
00065 hist.Merge(nmin);
00066
00067 TH1 *HAll = CountHist::Refill(hist, hall);
00068 TH1 *HSel = CountHist::Refill(hist, hsel);
00069 TH1 *HRat = CountHist::Refill(hist, hrat);
00070
00071 const string name = std::string(hrat -> GetName()) + "_merge";
00072
00073 HRat -> Reset();
00074 Anp::SetDir(HRat, hrat -> GetDirectory(), name);
00075
00076 SetRatio(HSel, HAll, HRat);
00077
00078 delete HAll;
00079 delete HSel;
00080 }
00081
00082 return hrat;
00083 }
00084
00085
00086 bool Anp::CountHist::SetRatio(TH1 *hN, TH1 *hD, TH1 *hR)
00087 {
00088 if(!hN || !hD || !hR)
00089 {
00090 return false;
00091 }
00092
00093 const int nbin = hN -> GetNbinsX();
00094
00095 if(hD -> GetNbinsX() != nbin)
00096 {
00097 cerr << "CountHist::SetRatio - mismatched number of bins" << endl;
00098 return false;
00099 }
00100
00101 for(int ibin = 1; ibin <= nbin; ++ibin)
00102 {
00103 const double valueN = hN -> GetBinContent(ibin);
00104 const double valueD = hD -> GetBinContent(ibin);
00105
00106 if(!(valueN > 0.0) || !(valueD > 0.0))
00107 {
00108 continue;
00109 }
00110
00111 if(valueD < valueN)
00112 {
00113 if(verbose)
00114 {
00115 cerr << "CountHist::SetRatio - denominator is smaller than nominator "
00116 << valueN << "/" << valueD << " = " << valueN/valueD << endl;
00117 }
00118
00119 continue;
00120 }
00121
00122 const double ratio = valueN/valueD;
00123 const double error = std::sqrt(valueN*(1.0 - valueN/valueD))/valueD;
00124
00125 hR -> SetBinContent(ibin, ratio);
00126 hR -> SetBinError(ibin, error);
00127 }
00128
00129 return true;
00130 }
00131
00132
00133 TH1* Anp::CountHist::Refill(Hist1d<double> &hist, TH1 *hO)
00134 {
00135 if(!hO)
00136 {
00137 return 0;
00138 }
00139
00140 hist.Reset();
00141 hist.Fill(*hO);
00142
00143 TH1 *hN = Anp::CreateTH1<double>(hist, hO -> GetName());
00144 Anp::SetDir(hN, 0, hO -> GetName());
00145
00146 hN -> SetTitle(hO -> GetTitle());
00147 hN -> GetXaxis() -> SetTitle(hO -> GetXaxis() -> GetTitle());
00148 hN -> GetYaxis() -> SetTitle(hO -> GetYaxis() -> GetTitle());
00149 hN -> GetXaxis() -> CenterTitle();
00150 hN -> GetYaxis() -> CenterTitle();
00151
00152 return hN;
00153 }
00154
00155
00156 bool Anp::CountHist::Valid() const
00157 {
00158 if(hall && hsel && hrat)
00159 {
00160 return true;
00161 }
00162
00163 return false;
00164 }
00165
00166
00167 Particle::Particle_t Anp::CountHist::GetParticle() const
00168 {
00169 return particle;
00170 }
00171
00172
00173 int Anp::CountHist::GetInteraction() const
00174 {
00175 return iactn;
00176 }
00177
00178
00179 int Anp::CountHist::GetReson() const
00180 {
00181 return reson;
00182 }
00183
00184
00185 bool Anp::operator==(const Handle<CountHist> &lhs, const Handle<CountHist> &rhs)
00186 {
00187 if(!lhs.valid() || !rhs.valid())
00188 {
00189 return false;
00190 }
00191
00192 if(lhs -> GetInteraction() != rhs -> GetInteraction())
00193 {
00194 return false;
00195 }
00196
00197 if(lhs -> GetReson() != rhs -> GetReson())
00198 {
00199 return false;
00200 }
00201
00202 if(lhs -> GetParticle() != rhs -> GetParticle())
00203 {
00204 return false;
00205 }
00206
00207 return true;
00208 }