SFrame 3.6
core/src/SCycleBaseHist.cxx
Go to the documentation of this file.
00001 // $Id: SCycleBaseHist.cxx 335 2012-11-21 14:11:47Z krasznaa $
00002 /***************************************************************************
00003  * @Project: SFrame - ROOT-based analysis framework for ATLAS
00004  * @Package: Core
00005  *
00006  * @author Stefan Ask       <Stefan.Ask@cern.ch>           - Manchester
00007  * @author David Berge      <David.Berge@cern.ch>          - CERN
00008  * @author Johannes Haller  <Johannes.Haller@cern.ch>      - Hamburg
00009  * @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen
00010  *
00011  ***************************************************************************/
00012 
00013 // STL include(s):
00014 #include <vector>
00015 #include <string>
00016 
00017 // ROOT include(s):
00018 #include <TDirectory.h>
00019 #include <TH1.h>
00020 #include <TList.h>
00021 
00022 // Local inlcude(s):
00023 #include "../include/SCycleBaseHist.h"
00024 #include "../include/SCycleOutput.h"
00025 
00026 #ifndef DOXYGEN_IGNORE
00027 ClassImp( SCycleBaseHist )
00028 #endif // DOXYGEN_IGNORE
00029 
00033 SCycleBaseHist::SCycleBaseHist()
00034    : SCycleBaseBase(), m_histoMap(), m_fileOutput(),
00035      m_proofOutput( 0 ), m_inputFile( 0 ) {
00036 
00037    REPORT_VERBOSE( "SCycleBaseHist constructed" );
00038 }
00039 
00040 void SCycleBaseHist::SetHistOutput( TList* output ) {
00041 
00042    m_proofOutput = output;
00043    m_histoMap.clear();
00044    return;
00045 }
00046 
00047 TList* SCycleBaseHist::GetHistOutput() const {
00048 
00049    return m_proofOutput;
00050 }
00051 
00071 void SCycleBaseHist::WriteObj( const TObject& obj,
00072                                const char* directory,
00073                                Bool_t inFile ) throw( SError ) {
00074 
00075    // Put the object into our temporary directory in memory:
00076    GetTempDir()->cd();
00077 
00078    // Construct a full path name for the object:
00079    const TString path = ( directory ? directory + TString( "/" ) : "" ) +
00080       TString( obj.GetName() );
00081 
00082    // Decide which TList to store the object in:
00083    TList* output = ( inFile ? &m_fileOutput : m_proofOutput );
00084 
00085    // Check if the object was already added:
00086    SCycleOutput* out =
00087       dynamic_cast< SCycleOutput* >( output->FindObject( path ) );
00088 
00089    // If not, add it now:
00090    if( ! out ) {
00091       out = new SCycleOutput( obj.Clone(), path, directory );
00092       output->TList::AddLast( out );
00093    }
00094 
00095    gROOT->cd(); // So that the temporary objects would be created
00096                 // in a general memory space.
00097 
00098    return;
00099 }
00100 
00129 TH1* SCycleBaseHist::Hist( const char* name, const char* dir ) throw( SError ) {
00130 
00131    TH1* result;
00132 
00133    std::pair< std::string, std::string > this_pair( name, ( dir ? dir : "" ) );
00134    std::map< std::pair< std::string, std::string >, TH1* >::const_iterator it;
00135    if( ( it = m_histoMap.find( this_pair ) ) != m_histoMap.end() ) {
00136       result = it->second;
00137    } else {
00138       REPORT_VERBOSE( "Hist(): Using Retrieve for name \""
00139                       << name << "\" and dir \"" << ( dir ? dir : "" ) << "\"" );
00140       result = Retrieve< TH1 >( name, dir ); // This line can throw an exception...
00141       m_histoMap[ this_pair ] = result;
00142    }
00143 
00144    return result;
00145 }
00146 
00147 void SCycleBaseHist::SetHistInputFile( TDirectory* file ) {
00148 
00149    m_inputFile = file;
00150    return;
00151 }
00152 
00153 TDirectory* SCycleBaseHist::GetHistInputFile() const {
00154 
00155    return m_inputFile;
00156 }
00157 
00168 void SCycleBaseHist::WriteHistObjects( TDirectory* output ) {
00169 
00170    // Return right away if we don't have objects designated for in-file
00171    // merging:
00172    if( ! m_fileOutput.GetSize() ) return;
00173 
00174    // If the objects are added to the output file:
00175    if( output ) {
00176 
00177       // Remember which directory we were in:
00178       TDirectory* currDir = gDirectory;
00179       // Go to the output file's directory:
00180       output->cd();
00181 
00182       // Write out each object to the file:
00183       for( Int_t i = 0; i < m_fileOutput.GetSize(); ++i ) {
00184          m_fileOutput.At( i )->Write();
00185       }
00186 
00187       // Remove the in-memory objects:
00188       m_fileOutput.SetOwner( kTRUE );
00189       m_fileOutput.Clear();
00190 
00191       // Change back to the old directory:
00192       currDir->cd();
00193    }
00194    // If the objects have to be merged in memory after all:
00195    else {
00196 
00197       // Print a WARNING message, as this is probably not what the user
00198       // wanted:
00199       m_logger << WARNING << "Objects designated to be merged in-file will be"
00200                << SLogger::endmsg;
00201       m_logger << WARNING << "merged in-memory instead!" << SLogger::endmsg;
00202 
00203       // Add each object to the PROOF output list instead:
00204       for( Int_t i = 0; i < m_fileOutput.GetSize(); ++i ) {
00205          m_proofOutput->TList::AddLast( m_fileOutput.At( i ) );
00206       }
00207 
00208       // Make out private list forget about the objects:
00209       m_fileOutput.SetOwner( kFALSE );
00210       m_fileOutput.Clear();
00211    }
00212 
00213    return;
00214 }
00215 
00223 TDirectory* SCycleBaseHist::GetTempDir() const {
00224 
00225    static TDirectory* tempdir = 0;
00226 
00227    if( ! tempdir ) {
00228       gROOT->cd();
00229       tempdir = gROOT->mkdir( "SFrameTempDir" );
00230       if( ! tempdir ) {
00231          REPORT_ERROR( "Temporary directory could not be created" );
00232       }
00233    }
00234 
00235    return tempdir;
00236 }