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 "P1D.h"
16 
18 ( const std::string& title ,
19  int nBins , double xlow, double xup,
20  double ylow, double yup, const std::string& opt )
21 {
22  auto _p = new TProfile(title.c_str(),title.c_str(),nBins,xlow,xup,ylow,yup,opt.c_str() ) ;
23  auto p = new Profile1D(_p);
24  return {p,p};
25 }
26 
28 ( const std::string& title,
29  const Edges& e, double ylow, double yup ,
30  const std::string& opt )
31 {
32  auto p = new Profile1D(new TProfile(title.c_str(),title.c_str(),e.size()-1,&e.front(),ylow,yup,opt.c_str()));
33  return {p,p};
34 }
35 
37  TProfile *h = getRepresentation<AIDA::IProfile1D,TProfile>(hist);
38  auto n = ( h ? new Profile1D(new TProfile(*h)) : nullptr );
39  return {n,n};
40 }
41 
42 namespace Gaudi {
43  template <>
45  return int(m_rep->GetBinEntries( rIndex(index) )+0.5);
46  }
47 
48  template <> void* Generic1D<AIDA::IProfile1D,TProfile>::cast(const std::string& className) const {
49  if (className == "AIDA::IProfile1D")
50  return const_cast<AIDA::IProfile1D*>((AIDA::IProfile1D*)this);
51  else if (className == "AIDA::IProfile")
52  return const_cast<AIDA::IProfile*>((AIDA::IProfile*)this);
53  else if (className == "AIDA::IBaseHistogram")
54  return const_cast<AIDA::IBaseHistogram*>((AIDA::IBaseHistogram*)this);
55  return nullptr;
56  }
57 
58  template <>
60  TProfile* imp = dynamic_cast<TProfile*>(rep);
61  if ( !imp ) throw std::runtime_error("Cannot adopt native histogram representation.");
62  m_rep.reset( imp );
63  m_axis.initialize(m_rep->GetXaxis(),true);
64  const TArrayD* a = m_rep->GetSumw2();
65  if ( !a || (a && a->GetSize()==0) ) m_rep->Sumw2();
66  setTitle(m_rep->GetTitle());
67  }
68 }
69 
71  : Base( new TProfile() )
72 {
73  init("",false);
74 }
75 
77  : Base( rep)
78 {
79  init(m_rep->GetTitle());
80 }
81 
82 void Gaudi::Profile1D::init(const std::string& title, bool initialize_axis) {
83  m_classType = "IProfile1D";
84  setTitle(title);
85  setName(title);
86  if ( initialize_axis ) {
87  axis().initialize(m_rep->GetXaxis(),false);
88  }
89  //m_rep->SetErrorOption("s");
90  m_rep->SetDirectory(nullptr);
91  m_sumEntries = 0;
92 }
93 
94 bool Gaudi::Profile1D::setBinContents(int i, int entries,double height,double /*error*/, double spread, double /* centre */ ) {
95  m_rep->SetBinEntries(rIndex(i), entries );
96  // set content takes in root height * entries
97  m_rep->SetBinContent(rIndex(i), height*entries );
98  // set error takes sqrt of bin sum(w*y**2)
99  double sumwy2Bin = ( spread*spread + height*height )*entries;
100  m_rep->SetBinError(rIndex(i), sqrt(sumwy2Bin) );
102  // not very efficient (but do evey bin since root cannot figure out by himself)
103  m_rep->SetEntries(m_sumEntries);
104  return true;
105 }
106 
107 #ifdef __ICC
108 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
109 // The comparison is correct
110 #pragma warning(disable:1572)
111 #endif
112 bool Gaudi::Profile1D::fill ( double x, double y, double weight ) {
113  (weight == 1.) ? m_rep->Fill(x,y) : m_rep->Fill(x,y,weight);
114  return true;
115 }
116 
Gaudi::Profile1D P1D
Definition: P1D.cpp: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:74
AIDA implementation for 1 D profiles using ROOT TProfile.
Definition: P1D.h:20
std::string m_classType
Definition: Generic1D.h:133
std::vector< double > Edges
Definition: GaudiPI.h:19
#define DECLARE_DATAOBJECT_FACTORY(x)
Definition: ObjectFactory.h:21
virtual bool setBinContents(int i, int entries, double height, double error, double spread, double centre)
Definition: P1D.cpp:94
void adoptRepresentation(TObject *rep) override
Adopt ROOT histogram representation.
STL class.
Axis & axis()
Access to axis object.
Definition: Generic1D.h:69
std::unique_ptr< IMPLEMENTATION > m_rep
Reference to underlying implementation.
Definition: Generic1D.h:131
bool setTitle(const std::string &title) override
Set the title of the object.
Definition: Generic1D.h:139
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:70
void initialize(TAxis *itaxi, bool)
Definition: Axis.h:71
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:112
std::pair< DataObject *, AIDA::IProfile1D * > createProf1D(const AIDA::IProfile1D &hist)
Copy constructor.
virtual int rIndex(int index) const
operator methods
Definition: Generic1D.h:103
void init(const std::string &title, bool initialize_axis=true)
Definition: P1D.cpp:82
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:149
Common AIDA implementation stuff for histograms and profiles using ROOT implementations.
Definition: Generic1D.h:37