Back to home page

EIC code displayed by LXR

 
 

    


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

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 __GAUDI_CHRONOSTATSVC_STAT_H__
0012 #define __GAUDI_CHRONOSTATSVC_STAT_H__
0013 // ============================================================================
0014 // Iinclude files
0015 // ============================================================================
0016 // STD & STL
0017 // ============================================================================
0018 #include <iostream>
0019 #include <string>
0020 // ============================================================================
0021 // GaudiKernel
0022 // ============================================================================
0023 #include "GaudiKernel/IStatSvc.h"
0024 #include "GaudiKernel/SmartIF.h"
0025 #include "GaudiKernel/StatEntity.h"
0026 // ============================================================================
0027 /** @class Stat  Stat.h GaudiKernel/Stat.h
0028  *
0029  *  Small wrapper class for easy manipulation with generic counters
0030  *   and IStatSvc interface
0031  *
0032  *  It acts as "smart pointer" fro StatEntity objects, and allows
0033  *  manipulation with StatEntity objects, owned by
0034  *  GaudiCommon<TYPE> base class and/or IStatSvc
0035  *
0036  *   @code
0037  *
0038  *   long nTracks = ... ;
0039  *   Stat stat( chronoSvc() , "#tracks" , nTracks ) ;
0040  *
0041  *   @endcode
0042  *
0043  *  Alternatively one can use operator methods:
0044  *
0045  *   @code
0046  *
0047  *   long nTracks = ... ;
0048  *   Stat stat( chronoSvc() , "#tracks" ) ;
0049  *   stat += nTracks ;
0050  *
0051  *   @endcode
0052  *
0053  *   @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
0054  *   @date 2007-08-02
0055  */
0056 class GAUDI_API Stat {
0057 public:
0058   /**  constructor from StatEntity, name and group :
0059    *
0060    *   @code
0061    *
0062    *    StatEntity* entity = ... ;
0063    *
0064    *    // make helper object:
0065    *    Stat stat ( entity ) ;
0066    *
0067    *   @endcode
0068    *
0069    *   @see StatEntity
0070    *   @param entity pointer to entity object
0071    *   @param name  (optional) name of the object, for printout
0072    *   @param group (optional) group of the object, for printout
0073    */
0074   Stat( StatEntity* entity = 0, const std::string& name = "", const std::string& group = "" )
0075       : m_entity( entity ), m_tag( name ), m_group( group ) {}
0076   /**  constructor from StatEntity, name and group :
0077    *
0078    *   @code
0079    *
0080    *    // make helper object:
0081    *    Stat stat = Stat( countter("Name") , "Name" ) ;
0082    *
0083    *   @endcode
0084    *   @see StatEntity
0085    *   @see GaudiCommon::counter
0086    *   @param entity reference to entity object
0087    *   @param name  (optional) name of the object, for printout
0088    *   @param group (optional) group of the object, for printout
0089    */
0090   Stat( StatEntity& entity, const std::string& name = "", const std::string& group = "" )
0091       : m_entity( &entity ), m_tag( name ), m_group( group ) {}
0092   /**  constructor from IStatSvc, tag and value
0093    *
0094    *   @code
0095    *
0096    *   IStatSvc* svc = ... ;
0097    *   double eTotal = .... ;
0098    *
0099    *   // get/create the counter from Stat Service
0100    *   Stat eTot ( svc , "total energy" ) ;
0101    *
0102    *   eTot += eTotal ;
0103    *
0104    *   @endcode
0105    *
0106    *   @see IStatSvc
0107    *   @param svc pointer to Chrono&Stat Service
0108    *   @paran tag unique tag for the entry
0109    */
0110   Stat( IStatSvc* svc, const std::string& tag );
0111   /**  constructor from IStatSvc, tag and value
0112    *
0113    *   @code
0114    *
0115    *   IStatSvc* svc = ... ;
0116    *   double eTotal = .... ;
0117    *
0118    *   // get/create the counter from Stat Service
0119    *   Stat stat( svc , "total energy" , eTotal ) ;
0120    *
0121    *   @endcode
0122    *
0123    *   @see IStatSvc
0124    *   @param svc pointer to Chrono&Stat Service
0125    *   @paran tag unique tag for the entry
0126    *   @param flag    "flag"(additive quantity) to be used
0127    */
0128   Stat( IStatSvc* svc, const std::string& tag, const double flag );
0129   /// copy constructor
0130   Stat( const Stat& ) = default;
0131   /// Assignement operator
0132   Stat& operator=( const Stat& ) = default;
0133   /// destructor
0134   ~Stat() = default;
0135   // ==========================================================================
0136   /// get the entity
0137   const StatEntity* entity() const { return m_entity; }
0138   /// dereference operaqtor
0139   const StatEntity* operator->() const { return entity(); }
0140   /// cast to StatEntity
0141   operator const StatEntity&() const { return *entity(); }
0142   /// check validity
0143   bool operator!() const { return 0 == m_entity; }
0144   // ==========================================================================
0145   /** General increment for the counter
0146    *
0147    *  @code
0148    *
0149    *    Stat stat = ... ;
0150    *
0151    *    const long nTracks = ... ;
0152    *
0153    *    stat += nTracks ;
0154    *
0155    *  @endcode
0156    *
0157    *  @see StatEntity
0158    *  @param f value to be added to the counter
0159    *  @return selfreference
0160    */
0161   Stat& operator+=( const double f ) {
0162     if ( m_entity ) { ( *m_entity ) += f; }
0163     return *this;
0164   }
0165   /** Pre-increment operator for the counter
0166    *
0167    *  @code
0168    *
0169    *    Stat stat = ... ;
0170    *
0171    *    ++stat ;
0172    *
0173    *  @endcode
0174    *
0175    *  @see StatEntity
0176    *  @return selfreference
0177    */
0178   Stat& operator++() {
0179     if ( m_entity ) { ++( *m_entity ); }
0180     return *this;
0181   }
0182   /** Post-increment operator for the counter
0183    *
0184    *  @code
0185    *
0186    *    Stat stat = ... ;
0187    *
0188    *    stat++ ;
0189    *
0190    *  @endcode
0191    *  @see StatEntity
0192    *  @return self-reference
0193    */
0194   Stat& operator++( int ) {
0195     if ( m_entity ) { ( *m_entity )++; }
0196     return *this;
0197   }
0198   /** General decrement operator for the counter
0199    *  @see StatEntity
0200    *  @return self-reference
0201    *  @param f counter decrement
0202    */
0203   Stat& operator-=( const double f ) {
0204     if ( m_entity ) { ( *m_entity ) -= f; }
0205     return *this;
0206   }
0207   /// Pre-decrement operator for the flag
0208   Stat& operator--() {
0209     if ( m_entity ) { --( *m_entity ); }
0210     return *this;
0211   }
0212   /// Post-decrement operator for the flag
0213   Stat& operator--( int ) {
0214     if ( m_entity ) { ( *m_entity )--; }
0215     return *this;
0216   }
0217   /// increment with StatEntity object
0218   Stat& operator+=( const StatEntity& right ) {
0219     if ( m_entity ) { ( *m_entity ) += right; }
0220     return *this;
0221   }
0222   /// increment with  other stat objects
0223   Stat& operator+=( const Stat& right ) {
0224     if ( 0 != right.entity() ) { ( *this ) += *right.entity(); }
0225     return *this;
0226   }
0227   // ==========================================================================
0228   /// representation as string
0229   std::string toString() const;
0230   /** printout to std::ostream
0231    *  @param s the reference to the output stream
0232    *  @return the reference to the output stream
0233    */
0234   std::ostream& print( std::ostream& o = std::cout ) const;
0235   /** printout  to std::ostream
0236    *  @param s the reference to the output stream
0237    *  @return the reference to the output stream
0238    */
0239   std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
0240   // ==========================================================================
0241   /// alternative access to underlying counter
0242   StatEntity* counter() const { return m_entity; }
0243   /// counter name
0244   const std::string& name() const { return m_tag; }
0245   /// counter group
0246   const std::string& group() const { return m_group; }
0247   // ==========================================================================
0248 private:
0249   // underlying counter
0250   StatEntity* m_entity = nullptr; ///< underlying counter
0251   // unique stat tag(name)
0252   std::string m_tag; ///< unique stat tag(name)
0253   // group
0254   std::string m_group;
0255   // Stat  service
0256   SmartIF<IStatSvc> m_stat; ///< Stat  service
0257 };
0258 // ============================================================================
0259 /// external operator for addition of Stat and a number
0260 GAUDI_API Stat operator+( const Stat& stat, const double value );
0261 // ============================================================================
0262 /// external operator for subtraction of Stat and a number
0263 GAUDI_API Stat operator-( const Stat& stat, const double value );
0264 // ============================================================================
0265 /// external operator for addition of Stat and a number
0266 GAUDI_API Stat operator+( const double value, const Stat& stat );
0267 // ============================================================================
0268 /// external operator for addition of Stat and Stat
0269 GAUDI_API Stat operator+( const Stat& stat, const Stat& value );
0270 // ============================================================================
0271 /// external printout operator to std::ostream
0272 GAUDI_API std::ostream& operator<<( std::ostream& stream, const Stat& stat );
0273 // ============================================================================
0274 
0275 // ============================================================================
0276 // The END
0277 // ============================================================================
0278 #endif // __GAUDI_CHRONOSTATSVC_STAT_H__