File indexing completed on 2025-02-21 10:00:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDISVC_GENERIC1D_H
0012 #define GAUDISVC_GENERIC1D_H 1
0013
0014 #include "AIDA/IProfile1D.h"
0015 #include "Annotation.h"
0016 #include "Axis.h"
0017 #include "GaudiKernel/HistogramBase.h"
0018 #include "TFile.h"
0019 #include <memory>
0020 #include <stdexcept>
0021
0022
0023
0024 #ifdef __clang__
0025 # pragma clang diagnostic push
0026 # pragma clang diagnostic ignored "-Wsuggest-override"
0027 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
0028 #elif defined( __GNUC__ ) && __GNUC__ >= 5
0029 # pragma GCC diagnostic push
0030 # pragma GCC diagnostic ignored "-Wsuggest-override"
0031 #endif
0032
0033 namespace Gaudi {
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 template <class INTERFACE, class IMPLEMENTATION>
0046 class GAUDI_API Generic1D : virtual public INTERFACE, virtual public HistogramBase {
0047 public:
0048 typedef Generic1D<INTERFACE, IMPLEMENTATION> Base;
0049
0050 Generic1D() = default;
0051
0052 protected:
0053
0054 Generic1D( IMPLEMENTATION* p ) : m_rep( p ) {}
0055
0056 public:
0057
0058 virtual const std::string& userLevelClassType() const { return m_classType; }
0059
0060 void* cast( const std::string& cl ) const override;
0061
0062 TObject* representation() const override { return m_rep.get(); }
0063
0064 void adoptRepresentation( TObject* rep ) override;
0065
0066 std::string title() const override { return m_annotation.value( "Title" ); }
0067
0068 bool setTitle( const std::string& title ) override;
0069
0070 std::string name() const { return m_annotation.value( "Name" ); }
0071
0072 bool setName( const std::string& newName );
0073
0074 AIDA::IAnnotation& annotation() override { return m_annotation; }
0075
0076 const AIDA::IAnnotation& annotation() const override { return m_annotation; }
0077
0078 Axis& axis() { return m_axis; }
0079
0080 const Axis& axis() const override { return m_axis; }
0081
0082
0083 int entries() const override { return m_rep->GetEntries(); }
0084
0085 int allEntries() const override { return m_rep->GetEntries(); }
0086
0087 int extraEntries() const override;
0088
0089 int binEntries( int index ) const override;
0090
0091 virtual double binRms( int index ) const;
0092
0093 double sumBinHeights() const override { return m_rep->GetSumOfWeights(); }
0094
0095 double sumAllBinHeights() const override { return m_rep->GetSum(); }
0096
0097 double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
0098
0099 double minBinHeight() const override { return m_rep->GetMinimum(); }
0100
0101 double maxBinHeight() const override { return m_rep->GetMaximum(); }
0102
0103
0104 virtual double equivalentBinEntries() const;
0105
0106
0107 virtual bool scale( double scaleFactor );
0108
0109 bool reset() override;
0110
0111 bool add( const INTERFACE& profile ) override;
0112
0113 virtual int rIndex( int index ) const { return m_axis.rIndex( index ); }
0114
0115 double binMean( int index ) const override;
0116
0117 double binHeight( int index ) const override;
0118
0119 double binError( int index ) const override;
0120
0121 double mean() const override { return m_rep->GetMean(); }
0122
0123 double rms() const override { return m_rep->GetRMS(); }
0124
0125 int coordToIndex( double coord ) const override { return axis().coordToIndex( coord ); }
0126
0127 int dimension() const override { return 1; }
0128
0129 std::ostream& print( std::ostream& s ) const override;
0130
0131 std::ostream& write( std::ostream& s ) const override;
0132
0133 int write( const char* file_name ) const override;
0134
0135 protected:
0136
0137 Axis m_axis;
0138
0139 mutable AIDA::Annotation m_annotation;
0140
0141 std::unique_ptr<IMPLEMENTATION> m_rep;
0142
0143 std::string m_classType;
0144
0145 int m_sumEntries{ 0 };
0146 };
0147
0148 template <class INTERFACE, class IMPLEMENTATION>
0149 bool Generic1D<INTERFACE, IMPLEMENTATION>::setTitle( const std::string& title ) {
0150 m_rep->SetTitle( title.c_str() );
0151 if ( !annotation().addItem( "Title", title ) ) m_annotation.setValue( "Title", title );
0152 if ( !annotation().addItem( "title", title ) ) annotation().setValue( "title", title );
0153 return true;
0154 }
0155
0156 template <class INTERFACE, class IMPLEMENTATION>
0157 bool Generic1D<INTERFACE, IMPLEMENTATION>::setName( const std::string& newName ) {
0158 m_rep->SetName( newName.c_str() );
0159 m_annotation.setValue( "Name", newName );
0160 return true;
0161 }
0162
0163 template <class INTERFACE, class IMPLEMENTATION>
0164 double Generic1D<INTERFACE, IMPLEMENTATION>::binRms( int index ) const {
0165 return m_rep->GetBinError( rIndex( index ) );
0166 }
0167
0168 template <class INTERFACE, class IMPLEMENTATION>
0169 double Generic1D<INTERFACE, IMPLEMENTATION>::binMean( int index ) const {
0170 return m_rep->GetBinCenter( rIndex( index ) );
0171 }
0172
0173 template <class INTERFACE, class IMPLEMENTATION>
0174 double Generic1D<INTERFACE, IMPLEMENTATION>::binHeight( int index ) const {
0175 return m_rep->GetBinContent( rIndex( index ) );
0176 }
0177
0178 template <class INTERFACE, class IMPLEMENTATION>
0179 double Generic1D<INTERFACE, IMPLEMENTATION>::binError( int index ) const {
0180 return m_rep->GetBinError( rIndex( index ) );
0181 }
0182
0183 template <class INTERFACE, class IMPLEMENTATION>
0184 int Generic1D<INTERFACE, IMPLEMENTATION>::extraEntries() const {
0185 return binEntries( AIDA::IAxis::UNDERFLOW_BIN ) + binEntries( AIDA::IAxis::OVERFLOW_BIN );
0186 }
0187 template <class INTERFACE, class IMPLEMENTATION>
0188 bool Generic1D<INTERFACE, IMPLEMENTATION>::reset() {
0189 m_sumEntries = 0;
0190 m_rep->Reset();
0191 return true;
0192 }
0193
0194 template <class INTERFACE, class IMPLEMENTATION>
0195 double Generic1D<INTERFACE, IMPLEMENTATION>::equivalentBinEntries() const {
0196 if ( sumBinHeights() <= 0 ) return 0;
0197 Stat_t stats[11];
0198 m_rep->GetStats( stats );
0199 return stats[0] * stats[0] / stats[1];
0200 }
0201
0202 template <class INTERFACE, class IMPLEMENTATION>
0203 bool Generic1D<INTERFACE, IMPLEMENTATION>::scale( double scaleFactor ) {
0204 m_rep->Scale( scaleFactor );
0205 return true;
0206 }
0207
0208 template <class INTERFACE, class IMPLEMENTATION>
0209 bool Generic1D<INTERFACE, IMPLEMENTATION>::add( const INTERFACE& h ) {
0210 const Generic1D<INTERFACE, IMPLEMENTATION>* p = dynamic_cast<const Generic1D<INTERFACE, IMPLEMENTATION>*>( &h );
0211 if ( p ) {
0212 m_rep->Add( p->m_rep.get() );
0213 return true;
0214 }
0215 throw std::runtime_error( "Cannot add profile histograms of different implementations." );
0216 }
0217
0218 template <class INTERFACE, class IMPLEMENTATION>
0219 std::ostream& Generic1D<INTERFACE, IMPLEMENTATION>::print( std::ostream& s ) const {
0220
0221 m_rep->Print( "all" );
0222 return s;
0223 }
0224
0225
0226 template <class INTERFACE, class IMPLEMENTATION>
0227 std::ostream& Generic1D<INTERFACE, IMPLEMENTATION>::write( std::ostream& s ) const {
0228 s << "\n1D Histogram Table: " << std::endl;
0229 s << "Bin, Height, Error " << std::endl;
0230 for ( int i = 0; i < axis().bins(); ++i )
0231 s << binMean( i ) << ", " << binHeight( i ) << ", " << binError( i ) << std::endl;
0232 s << std::endl;
0233 return s;
0234 }
0235
0236
0237 template <class INTERFACE, class IMPLEMENTATION>
0238 int Generic1D<INTERFACE, IMPLEMENTATION>::write( const char* file_name ) const {
0239 TFile* f = TFile::Open( file_name, "RECREATE" );
0240 Int_t nbytes = m_rep->Write();
0241 f->Close();
0242 return nbytes;
0243 }
0244 }
0245
0246 #ifdef __clang__
0247 # pragma clang diagnostic pop
0248 #elif defined( __GNUC__ ) && __GNUC__ >= 5
0249 # pragma GCC diagnostic pop
0250 #endif
0251
0252 #endif