Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:26

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
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 // Hide warning message:
0023 // warning: 'XYZ' overrides a member function but is not marked 'override'
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   /** @class Generic1D Generic1D.h GaudiPI/Generic1D.h
0036    *
0037    * Common AIDA implementation stuff for histograms and profiles
0038    * using ROOT implementations
0039    *
0040    * Credits: This code is the result of some stripdown implementation
0041    * of LCG/PI. Credits to them!
0042    *
0043    *  @author M.Frank
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     /// Default constructor
0050     Generic1D() = default;
0051 
0052   protected:
0053     /// constructor
0054     Generic1D( IMPLEMENTATION* p ) : m_rep( p ) {}
0055 
0056   public:
0057     /// The AIDA user-level unterface leaf class type
0058     virtual const std::string& userLevelClassType() const { return m_classType; }
0059     /// Manual cast by class name
0060     void* cast( const std::string& cl ) const override;
0061     /// ROOT object implementation
0062     TObject* representation() const override { return m_rep.get(); }
0063     /// Adopt ROOT histogram representation
0064     void adoptRepresentation( TObject* rep ) override;
0065     /// Get the title of the object
0066     std::string title() const override { return m_annotation.value( "Title" ); }
0067     /// Set the title of the object
0068     bool setTitle( const std::string& title ) override;
0069     /// object name
0070     std::string name() const { return m_annotation.value( "Name" ); }
0071     /// Set the name of the object
0072     bool setName( const std::string& newName );
0073     /// Access annotation object
0074     AIDA::IAnnotation& annotation() override { return m_annotation; }
0075     /// Access annotation object (cons)
0076     const AIDA::IAnnotation& annotation() const override { return m_annotation; }
0077     /// Access to axis object
0078     Axis& axis() { return m_axis; }
0079     /// Get the x axis of the IHistogram1D.
0080     const Axis& axis() const override { return m_axis; }
0081 
0082     /// Get the number or all the entries
0083     int entries() const override { return m_rep->GetEntries(); }
0084     /// Get the number or all the entries, both in range and underflow/overflow bins of the IProfile.
0085     int allEntries() const override { return m_rep->GetEntries(); }
0086     /// Get the number of entries in the underflow and overflow bins.
0087     int extraEntries() const override;
0088     /// Number of entries in the corresponding bin (ie the number of times fill was called for this bin).
0089     int binEntries( int index ) const override;
0090     // spread
0091     virtual double binRms( int index ) const;
0092     /// Get the sum of in range bin heights in the IProfile.
0093     double sumBinHeights() const override { return m_rep->GetSumOfWeights(); }
0094     /// Get the sum of all the bins heights (including underflow and overflow bin).
0095     double sumAllBinHeights() const override { return m_rep->GetSum(); }
0096     /// Get the sum of the underflow and overflow bin height.
0097     double sumExtraBinHeights() const override { return sumAllBinHeights() - sumBinHeights(); }
0098     /// Get the minimum height of the in-range bins.
0099     double minBinHeight() const override { return m_rep->GetMinimum(); }
0100     /// Get the maximum height of the in-range bins.
0101     double maxBinHeight() const override { return m_rep->GetMaximum(); }
0102 
0103     /// Number of equivalent entries, i.e. <tt>SUM[ weight ] ^ 2 / SUM[ weight^2 ]</tt>
0104     virtual double equivalentBinEntries() const;
0105     /// Scale the weights and the errors of all the IHistogram's bins (in-range and out-of-range ones) by a given scale
0106     /// factor.
0107     virtual bool scale( double scaleFactor );
0108     /// Reset the Histogram; as if just created.
0109     bool reset() override;
0110     /// Modifies this IProfile1D by adding the contents of profile to it.
0111     bool add( const INTERFACE& profile ) override;
0112     /// operator methods
0113     virtual int rIndex( int index ) const { return m_axis.rIndex( index ); }
0114     /// The weighted mean of a bin.
0115     double binMean( int index ) const override;
0116     /// Total height of the corresponding bin (ie the sum of the weights in this bin).
0117     double binHeight( int index ) const override;
0118     /// The error of a given bin.
0119     double binError( int index ) const override;
0120     /// The mean of the whole IHistogram1D.
0121     double mean() const override { return m_rep->GetMean(); }
0122     /// The RMS of the whole IHistogram1D.
0123     double rms() const override { return m_rep->GetRMS(); }
0124     /// Get the bin number corresponding to a given coordinate along the x axis.
0125     int coordToIndex( double coord ) const override { return axis().coordToIndex( coord ); }
0126     /// Get the Histogram's dimension.
0127     int dimension() const override { return 1; }
0128     /// Print (ASCII) the histogram into the output stream
0129     std::ostream& print( std::ostream& s ) const override;
0130     /// Write (ASCII) the histogram table into the output stream
0131     std::ostream& write( std::ostream& s ) const override;
0132     /// Write (ASCII) the histogram table into a file
0133     int write( const char* file_name ) const override;
0134 
0135   protected:
0136     /// Axis member
0137     Axis m_axis;
0138     /// Object annotations
0139     mutable AIDA::Annotation m_annotation;
0140     /// Reference to underlying implementation
0141     std::unique_ptr<IMPLEMENTATION> m_rep;
0142     // class type
0143     std::string m_classType;
0144     // cache sumEntries (allEntries)   when setting contents since Root can't compute by himself
0145     int m_sumEntries{ 0 };
0146   }; // end class Generic1D
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]; // cover up to 3D...
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     /// bin contents and errors are printed for all bins including under and overflows
0221     m_rep->Print( "all" );
0222     return s;
0223   }
0224 
0225   /// Write (ASCII) the histogram table into the output stream
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   /// Write (ASCII) the histogram table into a file
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 } // namespace Gaudi
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 // AIDAROOT_GENERIC1D_H