P1D.cpp
Go to the documentation of this file.
1 #ifdef __ICC
2 // disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
3 // TODO: To be removed, since it comes from ROOT TMathBase.h
4 #pragma warning(disable:2259)
5 #endif
6 #ifdef WIN32
7 // Disable warning
8 // warning C4996: 'sprintf': This function or variable may be unsafe.
9 // coming from TString.h
10 #pragma warning(disable:4996)
11 #endif
12 
13 #include <cmath>
14 #include <GaudiCommonSvc/P1D.h>
17 #include "GaudiPI.h"
18 
20 ( const std::string& title ,
21  int nBins , double xlow, double xup,
22  double ylow, double yup, const std::string& opt )
23 {
24  auto _p = new TProfile(title.c_str(),title.c_str(),nBins,xlow,xup,ylow,yup,opt.c_str() ) ;
25  auto p = new Profile1D(_p);
26  return {p,p};
27 }
28 
30 ( const std::string& title,
31  const Edges& e, double ylow, double yup ,
32  const std::string& opt )
33 {
34  auto p = new Profile1D(new TProfile(title.c_str(),title.c_str(),e.size()-1,&e.front(),ylow,yup,opt.c_str()));
35  return {p,p};
36 }
37 
39  TProfile *h = getRepresentation<AIDA::IProfile1D,TProfile>(hist);
40  auto n = ( h ? new Profile1D(new TProfile(*h)) : nullptr );
41  return {n,n};
42 }
43 
44 namespace Gaudi {
45  template <>
47  return int(m_rep->GetBinEntries( rIndex(index) )+0.5);
48  }
49 
50  template <> void* Generic1D<AIDA::IProfile1D,TProfile>::cast(const std::string& className) const {
51  if (className == "AIDA::IProfile1D")
52  return const_cast<AIDA::IProfile1D*>((AIDA::IProfile1D*)this);
53  else if (className == "AIDA::IProfile")
54  return const_cast<AIDA::IProfile*>((AIDA::IProfile*)this);
55  else if (className == "AIDA::IBaseHistogram")
56  return const_cast<AIDA::IBaseHistogram*>((AIDA::IBaseHistogram*)this);
57  return nullptr;
58  }
59 
60  template <>
62  TProfile* imp = dynamic_cast<TProfile*>(rep);
63  if ( !imp ) throw std::runtime_error("Cannot adopt native histogram representation.");
64  m_rep.reset( imp );
65  m_axis.initialize(m_rep->GetXaxis(),true);
66  const TArrayD* a = m_rep->GetSumw2();
67  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
68  setTitle(m_rep->GetTitle());
69  }
70 }
71 
73  : Base( new TProfile() )
74 {
75  init("",false);
76 }
77 
79  : Base( rep)
80 {
81  init(m_rep->GetTitle());
82 }
83 
84 void Gaudi::Profile1D::init(const std::string& title, bool initialize_axis) {
85  m_classType = "IProfile1D";
86  setTitle(title);
87  setName(title);
88  if ( initialize_axis ) {
89  axis().initialize(m_rep->GetXaxis(),false);
90  }
91  //m_rep->SetErrorOption("s");
92  m_rep->SetDirectory(nullptr);
93  m_sumEntries = 0;
94 }
95 
96 bool Gaudi::Profile1D::setBinContents(int i, int entries,double height,double /*error*/, double spread, double /* centre */ ) {
97  m_rep->SetBinEntries(rIndex(i), entries );
98  // set content takes in root height * entries
99  m_rep->SetBinContent(rIndex(i), height*entries );
100  // set error takes sqrt of bin sum(w*y**2)
101  double sumwy2Bin = ( spread*spread + height*height )*entries;
102  m_rep->SetBinError(rIndex(i), sqrt(sumwy2Bin) );
104  // not very efficient (but do evey bin since root cannot figure out by himself)
105  m_rep->SetEntries(m_sumEntries);
106  return true;
107 }
108 
109 #ifdef __ICC
110 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
111 // The comparison is correct
112 #pragma warning(disable:1572)
113 #endif
114 bool Gaudi::Profile1D::fill ( double x, double y, double weight ) {
115  (weight == 1.) ? m_rep->Fill(x,y) : m_rep->Fill(x,y,weight);
116  return true;
117 }
void * cast(const std::string &cl) const override
Manual cast by class name.
int entries() const override
Get the number or all the entries.
Definition: Generic1D.h:72
std::string m_classType
Definition: Generic1D.h:131
std::vector< double > Edges
Definition: GaudiPI.h:17
virtual bool setBinContents(int i, int entries, double height, double error, double spread, double centre)
Definition: P1D.cpp:96
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
STL class.
Axis & axis()
Access to axis object.
Definition: Generic1D.h:67
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:129
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:137
int binEntries(int index) const override
Number of entries in the corresponding bin (ie the number of times fill was called for this bin)...
Profile1D()
Default Constructor.
Definition: P1D.cpp:72
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:66
T c_str(T...args)
bool fill(double x, double y, double weight=1.) override
Fill the Profile1D with a value and the corresponding weight.
Definition: P1D.cpp:114
std::pair< DataObject *, AIDA::IProfile1D * > createProf1D(const AIDA::IProfile1D &hist)
Copy constructor.
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:101
void init(const std::string &title, bool initialize_axis=true)
Definition: P1D.cpp:84
Helper functions to set/get the application return code.
Definition: __init__.py:1
bool setName(const std::string &newName)
Set the name of the object.
Definition: Generic1D.h:147
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:35