Gaudi Framework, version v23r0

Home   Generated: Mon Jan 30 2012

Histo2String.cpp

Go to the documentation of this file.
00001 // ============================================================================
00002 // Include files
00003 // ============================================================================
00004 // ROOT
00005 // ============================================================================
00006 #include "TH1D.h"
00007 #include "TH2D.h"
00008 #include "TH1F.h"
00009 #include "TH2F.h"
00010 // ============================================================================
00011 // AIDA
00012 // ============================================================================
00013 #include "AIDA/IHistogram1D.h"
00014 #include "AIDA/IHistogram2D.h"
00015 // ============================================================================
00016 // GaudiKernel
00017 // ============================================================================
00018 #include "GaudiKernel/ToStream.h"
00019 // ============================================================================
00020 // local
00021 // ============================================================================
00022 #include "GaudiUtils/Aida2ROOT.h"
00023 #include "GaudiUtils/Histo2String.h"
00024 #include "GaudiUtils/HistoXML.h"
00025 // ============================================================================
00031 // ============================================================================
00032 namespace
00033 {
00034   // ==========================================================================
00035   template <class HISTO>
00036   std::ostream& _toStream_1D_
00037   ( const HISTO&   histo  ,
00038     std::ostream& stream ,
00039     const bool    asXML  )
00040   {
00041     if ( asXML ) { return Gaudi::Utils::Histos::toXml ( histo , stream ) ; }
00042     //
00043     stream << "{ " ;
00044     //
00045     stream << "'name'  : "  ;
00046     Gaudi::Utils::toStream ( std::string ( histo.GetName () ) , stream ) << " , " ;
00047     stream << "'title' : "  ;
00048     Gaudi::Utils::toStream ( std::string ( histo.GetTitle() ) , stream ) << " , " ;
00049     //
00050     const TAxis*       axis  = histo.GetXaxis() ;
00051     const unsigned int nBins = axis->GetNbins() ;
00052     if ( axis->IsVariableBinSize() )
00053     {
00054       const TArrayD*     xbins = axis->GetXbins() ;
00055       const unsigned int xsize = xbins->GetSize() ;
00056       std::vector<double> edges ;
00057       for  ( unsigned int iBin = 0 ; iBin < xsize ; ++iBin )
00058       { edges.push_back ( xbins->At( iBin ) ) ; }
00059       // the edges
00060       stream << std::endl ;
00061       stream << "'edges' : "  ;
00062       Gaudi::Utils::toStream (  edges , stream ) << " ," << std::endl;
00063     }
00064     else
00065     {
00066       stream << "'nbins' : "  ;
00067       Gaudi::Utils::toStream (  nBins            , stream ) << " , " ;
00068       stream << "'low'   : "  ;
00069       Gaudi::Utils::toStream (  axis->GetXmin () , stream ) << " , " ;
00070       stream << "'high'  : "  ;
00071       Gaudi::Utils::toStream (  axis->GetXmax () , stream ) << " , " << std::endl ;
00072     }
00073     // finally: the content
00074     std::vector<std::pair<double,double> > bins ;
00075     for ( unsigned int iBin = 0 ; iBin <= nBins + 1 ; ++iBin )
00076     {
00077       bins.push_back ( std::make_pair( histo.GetBinContent( iBin ) ,
00078                                        histo.GetBinError  ( iBin ) ) ) ;
00079     }
00080     stream << "'bins'  : "  ;
00081     Gaudi::Utils::toStream ( bins , stream ) ;
00082     //
00083     stream << " }" ;
00084     //
00085     return stream ;
00086   }
00087   // ==========================================================================
00088   template <class HISTO>
00089   std::ostream& _toStream_2D_
00090   ( const HISTO&   histo ,
00091     std::ostream& stream ,
00092     const bool    asXML  )
00093   {
00094     if ( asXML ) { return Gaudi::Utils::Histos::toXml ( histo , stream ) ; }
00095     //
00096     stream << "{ " ;
00097     //
00098     stream << "'name'  : "  ;
00099     Gaudi::Utils::toStream ( std::string( histo.GetName () ) , stream ) << " , " ;
00100     stream << "'title' : "  ;
00101     Gaudi::Utils::toStream ( std::string( histo.GetTitle() ) , stream ) << " , " ;
00102     //
00103     const TAxis* xaxis = histo.GetXaxis() ;
00104     const int    xBins = xaxis->GetNbins() ;
00105     //
00106     stream << std::endl << "'X' : { " ;
00107     if ( xaxis->IsVariableBinSize() )
00108     {
00109       const TArrayD*     xbins = xaxis->GetXbins() ;
00110       const unsigned int xsize = xbins->GetSize() ;
00111       std::vector<double> edges ;
00112       for  ( unsigned int iBin = 0 ; iBin < xsize ; ++iBin )
00113       { edges.push_back ( xbins->At( iBin ) ) ; }
00114       // the edges
00115       stream << "'edges' : "  ;
00116       Gaudi::Utils::toStream (  edges , stream ) << " }," << std::endl;
00117     }
00118     else
00119     {
00120       stream << "'nbins' : "  ;
00121       Gaudi::Utils::toStream (  xBins            , stream ) << " , " ;
00122       stream << "'low'   : "  ;
00123       Gaudi::Utils::toStream (  xaxis->GetXmin () , stream ) << " , " ;
00124       stream << "'high'  : "  ;
00125       Gaudi::Utils::toStream (  xaxis->GetXmax () , stream ) << " }, " << std::endl ;
00126     }
00127     //
00128     const TAxis* yaxis = histo.GetYaxis() ;
00129     const int    yBins = yaxis->GetNbins() ;
00130     //
00131     stream << std::endl << "'Y' : { " ;
00132     if ( yaxis->IsVariableBinSize() )
00133     {
00134       const TArrayD*     ybins = yaxis->GetXbins() ;
00135       const unsigned int ysize = ybins->GetSize() ;
00136       std::vector<double> edges ;
00137       for  ( unsigned int iBin = 0 ; iBin < ysize ; ++iBin )
00138       { edges.push_back ( ybins->At( iBin ) ) ; }
00139       // the edges
00140       stream << " 'edges' : "  ;
00141       Gaudi::Utils::toStream (  edges , stream ) << " }," << std::endl;
00142     }
00143     else
00144     {
00145       stream << "'nbins' : "  ;
00146       Gaudi::Utils::toStream (  yBins            , stream ) << " , " ;
00147       stream << "'low'   : "  ;
00148       Gaudi::Utils::toStream (  yaxis->GetXmin () , stream ) << " , " ;
00149       stream << "'high'  : "  ;
00150       Gaudi::Utils::toStream (  yaxis->GetXmax () , stream ) << " }, " << std::endl ;
00151     }
00152     //
00153     // finally: the content
00154     stream << "'bins' : " << std::endl << " [ " ;
00155     for ( int jBin = yBins + 1  ; jBin >= 0 ; --jBin )
00156     {
00157       if ( yBins + 1 != jBin ) { stream << std::endl ; }
00158       for ( int iBin = 0 ; iBin <= xBins + 1 ; ++iBin )
00159       {
00160         //
00161         Gaudi::Utils::toStream ( std::make_pair
00162                                  ( histo.GetBinContent( iBin , jBin ) ,
00163                                    histo.GetBinError  ( iBin , jBin ) ) , stream ) ;
00164         //
00165         if ( xBins + 1 != iBin || 0 != jBin ) { stream << " , " ; }
00166       }
00167     }
00168     stream << " ]" ;
00169     //
00170     stream << " }" ;
00171     //
00172     return stream ;
00173   }
00174   // ==========================================================================
00175 } //                                                 end of anonymous namespace
00176 // ============================================================================
00177 /*  stream the ROOT histogram into output stream
00178  *  @param histo  (INPUT)  the histogram to be streamed
00179  *  @param stream (OUTPUT) the stream
00180  *  @param asXML  (INPUT)  use XML-format
00181  */
00182 // ============================================================================
00183 std::ostream& Gaudi::Utils::toStream
00184 ( const TH1D&   histo  ,
00185   std::ostream& stream ,
00186   const bool    asXML  ) { return _toStream_1D_ ( histo , stream , asXML ) ; }
00187 // ============================================================================
00188 /*  stream the ROOT histogram into output stream
00189  *  @param histo  (INPUT)  the histogram to be streamed
00190  *  @param stream (OUTPUT) the stream
00191  *  @param asXML  (INPUT)  use XML-format
00192  */
00193 // ============================================================================
00194 std::ostream& Gaudi::Utils::toStream
00195 ( const TH1F&   histo  ,
00196   std::ostream& stream ,
00197   const bool    asXML  ) { return _toStream_1D_ ( histo , stream , asXML ) ; }
00198 // ============================================================================
00199 /*  stream the ROOT histogram into output stream
00200  *  @param histo  (INPUT)  the histogram to be streamed
00201  *  @param stream (OUTPUT) the stream
00202  *  @param asXML  (INPUT)  use XML-format
00203  */
00204 // ============================================================================
00205 std::ostream& Gaudi::Utils::toStream
00206 ( const TH2D&   histo  ,
00207   std::ostream& stream ,
00208   const bool    asXML  ) { return _toStream_2D_ ( histo , stream , asXML ) ; }
00209 // ============================================================================
00210 /*  stream the ROOT histogram into output stream
00211  *  @param histo  (INPUT)  the histogram to be streamed
00212  *  @param stream (OUTPUT) the stream
00213  *  @param asXML  (INPUT)  use XML-format
00214  */
00215 // ============================================================================
00216 std::ostream& Gaudi::Utils::toStream
00217 ( const TH2F&   histo  ,
00218   std::ostream& stream ,
00219   const bool    asXML  ) { return _toStream_2D_ ( histo , stream , asXML ) ; }
00220 // ============================================================================
00221 /*  stream the AIDA histogram into output stream
00222  *  @param histo  (INPUT)  the histogram to be streamed
00223  *  @param stream (OUTPUT) the stream
00224  *  @param asXML  (INPUT)  use XML-format
00225  */
00226 // ============================================================================
00227 std::ostream& Gaudi::Utils::toStream
00228 ( const AIDA::IHistogram1D& histo  ,
00229   std::ostream&             stream ,
00230   const bool                asXML  )
00231 {
00232   //
00233   AIDA::IHistogram1D* aida = const_cast<AIDA::IHistogram1D*> ( &histo ) ;
00234   //
00235   const TH1D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
00236   if ( 0 == root ) { return stream ; }                                // RETURN
00237   //
00238   return toStream  ( *root , stream , asXML ) ;
00239 }
00240 // ============================================================================
00241 /*  stream the AIDA histogram into output stream
00242  *  @param histo  (INPUT)  the histogram to be streamed
00243  *  @param stream (OUTPUT) the stream
00244  *  @param asXML  (INPUT)  use XML-format
00245  */
00246 // ============================================================================
00247 std::ostream& Gaudi::Utils::toStream
00248 ( const AIDA::IHistogram2D& histo  ,
00249   std::ostream&             stream ,
00250   const bool                asXML  )
00251 {
00252   //
00253   AIDA::IHistogram2D* aida = const_cast<AIDA::IHistogram2D*> ( &histo ) ;
00254   //
00255   const TH2D* root = Gaudi::Utils::Aida2ROOT::aida2root ( aida ) ;
00256   if ( 0 == root ) { return stream ; }                                // RETURN
00257   //
00258   return toStream  ( *root , stream , asXML ) ;
00259 }
00260 // ============================================================================
00261 /*  convert the histogram into the string
00262  *  @param histo  (INPUT)  the histogram to be streamed
00263  *  @param asXML  (INPUT)  use XML-format
00264  *  @return the string representation of the histogram
00265  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00266  *  @date 2009-09-26
00267  */
00268 // ============================================================================
00269 std::string Gaudi::Utils::toString
00270 ( const TH1D&               histo ,
00271   const bool                asXML )
00272 {
00273   std::ostringstream o ;
00274   toStream ( histo , o , asXML ) ;
00275   return o.str() ;
00276 }
00277 // ============================================================================
00278 /*  convert the histogram into the string
00279  *  @param histo  (INPUT)  the histogram to be streamed
00280  *  @param asXML  (INPUT)  use XML-format
00281  *  @return the string representation of the histogram
00282  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00283  *  @date 2009-09-26
00284  */
00285 // ============================================================================
00286 std::string Gaudi::Utils::toString
00287 ( const TH1F&               histo ,
00288   const bool                asXML )
00289 {
00290   std::ostringstream o ;
00291   toStream ( histo , o , asXML ) ;
00292   return o.str() ;
00293 }
00294 // ============================================================================
00295 /*  convert the histogram into the string
00296  *  @param histo  (INPUT)  the histogram to be streamed
00297  *  @param asXML  (INPUT)  use XML-format
00298  *  @return the string representation of the histogram
00299  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00300  *  @date 2009-09-26
00301  */
00302 // ============================================================================
00303 std::string Gaudi::Utils::toString
00304 ( const TH2D&               histo ,
00305   const bool                asXML )
00306 {
00307   std::ostringstream o ;
00308   toStream ( histo , o , asXML ) ;
00309   return o.str() ;
00310 }
00311 // ============================================================================
00312 /*  convert the histogram into the string
00313  *  @param histo  (INPUT)  the histogram to be streamed
00314  *  @param asXML  (INPUT)  use XML-format
00315  *  @return the string representation of the histogram
00316  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00317  *  @date 2009-09-26
00318  */
00319 // ============================================================================
00320 std::string Gaudi::Utils::toString
00321 ( const TH2F&               histo ,
00322   const bool                asXML )
00323 {
00324   std::ostringstream o ;
00325   toStream ( histo , o , asXML ) ;
00326   return o.str() ;
00327 }
00328 // ============================================================================
00329 /*  convert the histogram into the string
00330  *  @param histo  (INPUT)  the histogram to be streamed
00331  *  @param asXML  (INPUT)  use XML-format
00332  *  @return the string representation of the histogram
00333  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00334  *  @date 2009-09-26
00335  */
00336 // ============================================================================
00337 std::string Gaudi::Utils::toString
00338 ( const AIDA::IHistogram1D& histo ,
00339   const bool                asXML )
00340 {
00341   std::ostringstream o ;
00342   toStream ( histo , o , asXML ) ;
00343   return o.str() ;
00344 }
00345 // =============================================================================
00346 /*  convert the histogram into the string
00347  *  @param histo  (INPUT)  the histogram to be streamed
00348  *  @param asXML  (INPUT)  use XML-format
00349  *  @return the string representation of the histogram
00350  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00351  *  @date 2009-09-26
00352  */
00353 // ============================================================================
00354 std::string Gaudi::Utils::toString
00355 ( const AIDA::IHistogram2D& histo ,
00356   const bool                asXML )
00357 {
00358   std::ostringstream o ;
00359   toStream ( histo , o , asXML ) ;
00360   return o.str() ;
00361 }
00362 // =============================================================================
00363 /*  convert the histogram into the string
00364  *  @param histo  (INPUT)  the histogram to be streamed
00365  *  @param asXML  (INPUT)  use XML-format
00366  *  @return the string representation of the histogram
00367  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00368  *  @date 2009-09-26
00369  */
00370 // =============================================================================
00371 std::string Gaudi::Utils::toString ( const AIDA::IHistogram1D* histo )
00372 {
00373   if ( 0 == histo ) { return std::string ("{}") ; }
00374   return toString ( *histo ) ;
00375 }
00376 // ============================================================================
00377 /*  convert the histogram into the string
00378  *  @param histo  (INPUT)  the histogram to be streamed
00379  *  @param asXML  (INPUT)  use XML-format
00380  *  @return the string representation of the histogram
00381  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00382  *  @date 2009-09-26
00383  */
00384 // =============================================================================
00385 std::string Gaudi::Utils::toString ( AIDA::IHistogram1D* histo )
00386 {
00387   if ( 0 == histo ) { return std::string ("{}") ; }
00388   return toString ( *histo ) ;
00389 }
00390 // ============================================================================
00391 /*  convert the histogram into the string
00392  *  @param histo  (INPUT)  the histogram to be streamed
00393  *  @param asXML  (INPUT)  use XML-format
00394  *  @return the string representation of the histogram
00395  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00396  *  @date 2009-09-26
00397  */
00398 // =============================================================================
00399 std::string Gaudi::Utils::toString ( const TH1D* histo )
00400 {
00401   if ( 0 == histo ) { return std::string ("{}") ; }
00402   return toString ( *histo ) ;
00403 }
00404 // ============================================================================
00405 /*  convert the histogram into the string
00406  *  @param histo  (INPUT)  the histogram to be streamed
00407  *  @param asXML  (INPUT)  use XML-format
00408  *  @return the string representation of the histogram
00409  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00410  *  @date 2009-09-26
00411  */
00412 // =============================================================================
00413 std::string Gaudi::Utils::toString ( const TH2D* histo )
00414 {
00415   if ( 0 == histo ) { return std::string ("{}") ; }
00416   return toString ( *histo ) ;
00417 }
00418 // ============================================================================
00419 /*  convert the histogram into the string
00420  *  @param histo  (INPUT)  the histogram to be streamed
00421  *  @param asXML  (INPUT)  use XML-format
00422  *  @return the string representation of the histogram
00423  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00424  *  @date 2009-09-26
00425  */
00426 // =============================================================================
00427 std::string Gaudi::Utils::toString ( TH1D* histo )
00428 {
00429   if ( 0 == histo ) { return std::string ("{}") ; }
00430   return toString ( *histo ) ;
00431 }
00432 // ============================================================================
00433 /*  convert the histogram into the string
00434  *  @param histo  (INPUT)  the histogram to be streamed
00435  *  @param asXML  (INPUT)  use XML-format
00436  *  @return the string representation of the histogram
00437  *  @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
00438  *  @date 2009-09-26
00439  */
00440 // =============================================================================
00441 std::string Gaudi::Utils::toString ( TH2D* histo )
00442 {
00443   if ( 0 == histo ) { return std::string ("{}") ; }
00444   return toString ( *histo ) ;
00445 }
00446 // ============================================================================
00447 // The END
00448 // ============================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Jan 30 2012 13:53:04 for Gaudi Framework, version v23r0 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004