SFrame 3.6
core/include/SCycleBaseHist.icc
Go to the documentation of this file.
00001 // Dear emacs, this is -*- c++ -*-
00002 // $Id: SCycleBaseHist.icc 335 2012-11-21 14:11:47Z krasznaa $
00003 /***************************************************************************
00004  * @Project: SFrame - ROOT-based analysis framework for ATLAS
00005  * @Package: Core
00006  *
00007  * @author Stefan Ask       <Stefan.Ask@cern.ch>           - Manchester
00008  * @author David Berge      <David.Berge@cern.ch>          - CERN
00009  * @author Johannes Haller  <Johannes.Haller@cern.ch>      - Hamburg
00010  * @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen
00011  *
00012  ***************************************************************************/
00013 
00014 #ifndef SFRAME_CORE_SCycleBaseHist_ICC
00015 #define SFRAME_CORE_SCycleBaseHist_ICC
00016 
00017 // ROOT include(s):
00018 #include <TROOT.h>
00019 #include <TH1.h>
00020 #include <TList.h>
00021 #include <TDirectory.h>
00022 #include <TKey.h>
00023 
00024 // Local include(s):
00025 #include "SCycleOutput.h"
00026 
00049 template< class T >
00050 T* SCycleBaseHist::Book( const T& histo,
00051                          const char* directory,
00052                          Bool_t inFile ) throw( SError ) {
00053 
00054    // Put the object into our temporary directory in memory:
00055    GetTempDir()->cd();
00056 
00057    // Construct a full path name for the object:
00058    TString path = ( directory ? directory + TString( "/" ) : "" ) +
00059       TString( histo.GetName() );
00060 
00061    // Decide which TList to store the object in:
00062    TList* output = ( inFile ? &m_fileOutput : m_proofOutput );
00063 
00064    // Check if the object was already added:
00065    SCycleOutput* out =
00066       dynamic_cast< SCycleOutput* >( output->FindObject( path ) );
00067 
00068    // If not, add it now:
00069    if( ! out ) {
00070       out = new SCycleOutput( histo.Clone(), path, directory );
00071       output->TList::AddLast( out );
00072       REPORT_VERBOSE( "Added new object with name \"" << histo.GetName()
00073                       << "\" in directory \"" << ( directory ? directory : "" )
00074                       << "\"" );
00075    }
00076 
00077    // Get the pointer to the created object:
00078    T* ret = dynamic_cast< T* >( out->GetObject() );
00079 
00080    gROOT->cd(); // So that the temporary objects would be
00081                 // created in a general memory space.
00082 
00083    // Calculate the statistical uncertainties correctly for
00084    // weighted histograms:
00085    TH1* hist = dynamic_cast< TH1* >( ret );
00086    if( hist ) {
00087       if( ( ! TH1::GetDefaultSumw2() ) && ( ! hist->GetSumw2N() ) ) {
00088          hist->Sumw2();
00089       }
00090    }
00091 
00092    // Return the object pointer:
00093    return ret;
00094 }
00095 
00119 template< class T >
00120 T* SCycleBaseHist::Retrieve( const char* name,
00121                              const char* directory,
00122                              Bool_t outputOnly ) throw( SError ) {
00123 
00124    // Just to make sure that we're in the default directory in
00125    // memory:
00126    gROOT->cd();
00127 
00128    // Construct a path name from the specified parameters:
00129    const TString path = ( directory ? directory + TString( "/" ) : "" ) +
00130       TString( name );
00131 
00132    // Pointer to the requested object:
00133    T* result = 0;
00134 
00135    //
00136    // Try to find this object in the output PROOF list:
00137    //
00138    SCycleOutput* out =
00139       dynamic_cast< SCycleOutput* >( m_proofOutput->FindObject( path ) );
00140    if( out ) {
00141       result = dynamic_cast< T* >( out->GetObject() );
00142       if( ! result ) {
00143          REPORT_ERROR( "Output object with name \"" << name << "\" found in "
00144                        << "directory \"" << ( directory ? directory : "" )
00145                        << "\" but is not of the requested type" );
00146          SError error( SError::SkipCycle );
00147          error << "No object found in the holder with name: " << name
00148                << " in directory: " << directory;
00149          throw error;
00150       }
00151       return result;
00152    }
00153 
00154    //
00155    // Try to find this object in our private object list:
00156    //
00157    out = dynamic_cast< SCycleOutput* >( m_fileOutput.FindObject( path ) );
00158    if( out ) {
00159       result = dynamic_cast< T* >( out->GetObject() );
00160       if( ! result ) {
00161          REPORT_ERROR( "Output object with name \"" << name << "\" found in "
00162                        << "directory \"" << ( directory ? directory : "" )
00163                        << "\" but is not of the requested type" );
00164          SError error( SError::SkipCycle );
00165          error << "No object found in the holder with name: " << name
00166                << " in directory: " << directory;
00167          throw error;
00168       }
00169       return result;
00170    }
00171 
00172    // Return gracefully if the input should not be checked:
00173    if( outputOnly ) {
00174       REPORT_VERBOSE( "Object not found in output: " << path );
00175       return 0;
00176    }
00177 
00178    // Check that we have an input file already specified:
00179    if( ! m_inputFile ) {
00180       REPORT_ERROR( "No input file defined, and requested object "
00181                     "not found in output list" );
00182       throw SError( "No input file defined, and requested object "
00183                     "not found in output list", SError::SkipCycle );
00184    }
00185 
00186    //
00187    // Try to find this object inside the input file:
00188    //
00189    result = dynamic_cast< T* >( m_inputFile->Get( path ) );
00190    if( ! result ) {
00191       REPORT_ERROR( "Couldn't access object with path: " << path );
00192       SError error( SError::SkipCycle );
00193       error << "Couldn't access object with name: " << name
00194             << " in directory: " << directory;
00195       throw error;
00196    }
00197 
00198    return result;
00199 }
00200 
00220 template< class T >
00221 std::vector< T* >
00222 SCycleBaseHist::RetrieveAll( const char* name,
00223                              const char* directory ) throw( SError ) {
00224 
00225    // The result object:
00226    std::vector< T* > result;
00227 
00228    // Decide which directory to search in:
00229    TDirectory* dir = m_inputFile;
00230    if( directory ) {
00231       dir = dynamic_cast< TDirectory* >( m_inputFile->Get( directory ) );
00232       // If the requestested directory doesn't exist that's not necessariy an
00233       // error condition:
00234       if( ! dir ) {
00235          m_logger << DEBUG << "Directory \"" << directory
00236                   << "\" doesn't exist in the input file" << SLogger::endmsg;
00237          return result;
00238       }
00239    }
00240 
00241    // Get a list of all keys from the directory:
00242    TList* keyList = dir->GetListOfKeys();
00243    // Search for objects of the specified name:
00244    for( Int_t i = 0; i < keyList->GetEntries(); ++i ) {
00245       // Convert object to a TKey:
00246       TKey* key = dynamic_cast< TKey* >( keyList->At( i ) );
00247       if( ! key ) {
00248          REPORT_ERROR( "Couldn't cast to TKey. "
00249                        "There is some problem in the code" );
00250          throw SError( "Couldn't cast to TKey. "
00251                        "There is some problem in the code",
00252                        SError::StopExecution );
00253       }
00254       // Check if the object has the right name:
00255       if( strcmp( key->GetName(), name ) ) continue;
00256       // Check if this object is of the right type:
00257       T* obj =
00258          dynamic_cast< T* >( dir->Get( TString( key->GetName() ) + ";" +
00259                                        TString::Format( "%hi",
00260                                                         key->GetCycle() ) ) );
00261       if( ! obj ) continue;
00262       // Add it to our output:
00263       result.push_back( obj );
00264    }
00265 
00266    // Return the full list:
00267    return result;
00268 }
00269 
00270 #endif // SFRAME_CORE_SCycleBaseHist_ICC