Gaudi Framework, version v21r6

Home   Generated: 11 Nov 2009

GaudiTuples.icpp

Go to the documentation of this file.
00001 // $Id: GaudiTuples.icpp,v 1.10 2008/05/15 08:50:32 marcocle Exp $
00002 // ============================================================================
00003 // CVS tag $Name:  $, version $Revision: 1.10 $
00004 // ============================================================================
00005 // Include files
00006 // ============================================================================
00007 // Gaudi
00008 // ============================================================================
00009 #include "GaudiKernel/IRegistry.h"
00010 #include "GaudiKernel/ToStream.h"
00011 // ============================================================================
00012 // GaudiAlg
00013 // ============================================================================
00014 #include "GaudiAlg/GaudiTuples.h"
00015 #include "GaudiAlg/Tuple.h"
00016 #include "GaudiAlg/TupleObj.h"
00017 #include "GaudiAlg/GaudiTupleAlg.h"
00018 #include "GaudiAlg/TupleDetail.h"
00019 #include "GaudiAlg/Print.h"
00020 // ============================================================================
00021 /* @file GaudiTuples.cpp
00022  *
00023  *  Implementation file for class : GaudiTuples
00024  *
00025  *  @author Chris Jones   Christopher.Rob.Jones@cern.ch
00026  *  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
00027  *  @date   2005-08-08
00028  */
00029 // ============================================================================
00030 // Disable warning on windows
00031 #ifdef _WIN32
00032 #pragma warning ( disable:4661 ) // incomplete explicit templates
00033 #endif
00034 // ============================================================================
00035 // Destructor
00036 //=============================================================================
00037 template <class PBASE>
00038 GaudiTuples<PBASE>::~GaudiTuples() {}
00039 //=============================================================================
00040 // Initialize ntupling
00041 //=============================================================================
00042 template <class PBASE>
00043 StatusCode GaudiTuples<PBASE>::initialize()
00044 {
00045   // initialize base class
00046   const StatusCode sc = PBASE::initialize();
00047   if ( sc.isFailure() ) return sc;
00048 
00049   if ( produceNTuples() )
00050   {
00051     // check the existance of service
00052     if ( this->ntupleSvc() == 0 )
00053     { return  this->Error( "INTupleSvc* points to NULL!" ); }
00054     // Print ntuple path
00055     Print( "The N-Tuple   path is set to be '" + nTuplePath() + "'",
00056            StatusCode(StatusCode::SUCCESS, true) , MSG::DEBUG);
00057   }
00058   else
00059   { this->debug() << "Production of N-Tuples is switched OFF" << endmsg; }
00060 
00061   if ( produceEvtCols() )
00062   {
00063     // check the existance of service
00064     if ( 0 == this->evtColSvc() )
00065     { return  this->Error( "INTupleSvc* points to NULL!" ); }
00066     // Print EvtCol path
00067     Print( "The EventCol  path is set to be '" + evtColPath() + "'",
00068            StatusCode(StatusCode::SUCCESS, true) , MSG::DEBUG );
00069   }
00070   else
00071   { this->debug() << "Production of Event Collections is switched OFF" << endmsg; }
00072 
00073   return sc;
00074 }
00075 
00076 //=============================================================================
00077 // finalize ntupling
00078 //=============================================================================
00079 template <class PBASE>
00080 StatusCode GaudiTuples<PBASE>::finalize()
00081 {
00082   if ( !( nTupleMapTitle () . empty () &&
00083           nTupleMapID    () . empty () &&
00084           evtColMapTitle () . empty () &&
00085           evtColMapID    () . empty ()
00086           ) )
00087   {
00088     const int nNtuples = nTupleMapID () . size () ;
00089     const int nEvtCols = evtColMapID () . size ();
00090     this->always()
00091       << "Booked " << nNtuples << " N-Tuples and " << nEvtCols
00092       << " Event Tag Collections" << endmsg ;
00093   }
00094 
00095   if ( produceNTuples () && tuplesPrint  () ) { printTuples  () ; }
00096   if ( produceEvtCols () && evtColsPrint () ) { printEvtCols () ; }
00097 
00098   { // release ntuples and clear the container
00099     for( TupleMapTitle::iterator itup = m_nTupleMapTitle.begin() ;
00100          m_nTupleMapTitle.end() != itup ; ++itup )
00101     { if( 0 != itup->second ) { itup->second->release() ; } }
00102     m_nTupleMapTitle.clear() ;
00103   }
00104   { // release ntuples and clear the container
00105     for( TupleMapID::iterator itup = m_nTupleMapID.begin() ;
00106          m_nTupleMapID.end() != itup ; ++itup )
00107     { if( 0 != itup->second ) { itup->second->release() ; } }
00108     m_nTupleMapID.clear();
00109   }
00110   { // release ntuples and clear the container
00111     for( TupleMapTitle::iterator itup = m_evtColMapTitle.begin() ;
00112          m_evtColMapTitle.end() != itup ; ++itup )
00113     { if( 0 != itup->second ) { itup->second->release() ; } }
00114     m_evtColMapTitle.clear();
00115   }
00116   { // release ntuples and clear the container
00117     for( TupleMapID::iterator itup = m_evtColMapID.begin() ;
00118          m_evtColMapID.end() != itup ; ++itup )
00119     { if( 0 != itup->second ) { itup->second->release() ; } }
00120     m_evtColMapID.clear() ;
00121   }
00122   // finalize base class
00123   return PBASE::finalize();
00124 }
00125 
00126 // ============================================================================
00127 // get N-tuple object ( book on-demand ) with unique identidier
00128 // ============================================================================
00129 template <class PBASE>
00130 Tuples::Tuple GaudiTuples<PBASE>::nTuple
00131 ( const std::string& title ,
00132   const CLID&        clid  ) const
00133 {
00134   // look up in the table
00135   Tuples::TupleObj* tuple = m_nTupleMapTitle[ title ]  ;
00136   if ( 0 != tuple ) { return Tuple( tuple ) ; }                  // RETURN
00137   // Create the tuple ID
00138   TupleID ID;
00139   if ( this->useNumericAutoIDs() || title.empty() )
00140   {
00141     if ( ! this->useNumericAutoIDs() )
00142     {
00143       this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for nTuple ID",
00144                        StatusCode::SUCCESS );
00145     }
00146     // propose the tuple ID
00147     ID = TupleID ( m_nTupleMapID.size() + 1 + nTupleOffSet() );
00148     // adjust the proposed ID
00149     while ( nTupleExists(ID) || evtColExists(ID) )
00150     { ID = TupleID ( ID.numeric() + 1 ) ; }
00151   }
00152   else
00153   {
00154     // use the title to create a unique literal ID
00155     ID = TupleID( this->convertTitleToID(title) );
00156     // Just in case ...
00157     while ( nTupleExists(ID) || evtColExists(ID) )
00158     { ID = TupleID(ID.idAsString()+"_"); }
00159   }
00160   // return
00161   return nTuple( ID , title , clid ) ;
00162 }
00163 // ============================================================================
00164 
00165 // ============================================================================
00166 // Access an Event Tag Collection object (book on-demand) with unique identifier
00167 // ============================================================================
00168 template <class PBASE>
00169 Tuples::Tuple GaudiTuples<PBASE>::evtCol
00170 ( const std::string& title ,
00171   const CLID&        clid  ) const
00172 {
00173   // look up in the table
00174   Tuples::TupleObj* tuple = m_evtColMapTitle[ title ]  ;
00175   if ( 0 != tuple ) { return Tuple( tuple ) ; }                   // RETURN
00176   // Create the tuple ID
00177   TupleID ID;
00178   if ( this->useNumericAutoIDs() || title.empty() )
00179   {
00180     if ( ! this->useNumericAutoIDs() )
00181     {
00182       this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for evtCol ID",
00183                        StatusCode::SUCCESS );
00184     }
00185     // proposed the tuple ID
00186     ID = TupleID ( m_evtColMapID.size() + 1 + evtColOffSet() ) ;
00187     // adjust the proposed ID
00188     while ( nTupleExists(ID) || evtColExists(ID) )
00189     { ID = TupleID ( ID.numeric() + 1 ) ; }
00190   }
00191   else
00192   {
00193     // use the title to create a unique literal ID
00194     ID = TupleID( this->convertTitleToID(title) );
00195     // Just in case ...
00196     while ( nTupleExists(ID) || evtColExists(ID) )
00197     { ID = TupleID ( ID.idAsString()+"_" ) ; }
00198   }
00199   // return
00200   return evtCol( ID , title , clid ) ;
00201 }
00202 // ============================================================================
00203 
00204 // ============================================================================
00205 // get N-tuple object ( book on-demand ) with forced ID
00206 // ============================================================================
00207 template <class PBASE>
00208 Tuples::Tuple GaudiTuples<PBASE>::nTuple
00209 ( const TupleID&     ID     ,
00210   const std::string& title1 ,
00211   const CLID&        clid   ) const
00212 {
00213   // Check ID
00214   if ( ID.undefined() )
00215   { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
00216   
00217   // look up in the table
00218   Tuples::TupleObj * tuple = m_nTupleMapID[ ID ] ;
00219   if( 0 != tuple ) { return Tuple( tuple ) ; }                   // RETURN
00220   
00221   // convert ID to the string
00222   const std::string tID = ID.idAsString() ;
00223 
00224   // adjust the NTuple title
00225   const std::string title = title1.empty() ? ( "NTuple #" + tID ) : title1 ;
00226 
00227   // book new ntuple
00228   if( produceNTuples() )
00229   {
00230     // book NTupel
00231     NTuple::Tuple * tup = 0;
00232     if ( ID.numeric() )
00233     {
00234       tup = this->ntupleSvc() -> book ( nTuplePath() , ID.numericID() , clid , title );
00235     }
00236     else if ( ID.literal() )
00237     {
00238       tup = this->ntupleSvc() -> book ( nTuplePath() , ID.literalID() , clid , title );
00239     }
00240     else { this->Error( "Undefined NTuple ID" ); }
00241 
00242     // assertion
00243     this->Assert( 0 != tup , "Could not book the N-Tuple='" + title + "'" ) ;
00244     // some printout
00245     if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
00246     { this->debug() << "Booked NTuple '" << title << "' ID=" << tID
00247                     << "' Path='"  << nTuplePath() << "' TS='"
00248                     << tup -> registry() -> identifier() << "'" << endmsg; }
00249 
00250     tuple = createNTuple ( title , tup                , clid ) ;
00251   }
00252   else
00253   {
00254     tuple = createNTuple ( title , (NTuple::Tuple*) 0 , clid ) ;
00255   }
00256   // increment the reference
00257   tuple -> addRef();
00258   //
00259   m_nTupleMapID [ ID ] = tuple ;
00260   //
00261   tuple -> addRef();
00262   m_nTupleMapTitle[ title ] = tuple ;
00263   // return
00264   return Tuple( tuple ) ;
00265 }
00266 // ============================================================================
00267 template <class PBASE>
00268 Tuples::Tuple GaudiTuples<PBASE>::evtCol
00269 ( const TupleID&     ID     ,
00270   const std::string& title1 ,
00271   const CLID&        clid   ) const
00272 {
00273   // Check ID
00274   if ( ID.undefined() )
00275   { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
00276 
00277   // look up in the table
00278   Tuples::TupleObj* tuple = m_evtColMapID[ID] ;
00279   if ( 0 != tuple ) { return Tuple( tuple ) ; }                   // RETURN
00280   
00281   // convert ID to the string
00282   const std::string tID = ID.idAsString() ;
00283 
00284   // adjust the NTuple title
00285   const std::string title = title1.empty() ? ( "EvtCol #" + tID ) : title1 ;
00286 
00287   // book new ntuple
00288   if( produceEvtCols() )
00289   {
00290     // book NTuple
00291     NTuple::Tuple* tup = 0;
00292     if (  ID.numeric() )
00293     {
00294       tup = this->evtColSvc()->book ( evtColPath() , ID.numericID() , clid , title ) ;
00295     }
00296     else if ( ID.literal() )
00297     {
00298       tup = this->evtColSvc()->book ( evtColPath() , ID.literalID() , clid , title ) ;
00299     }
00300     else { this->Error( "Undefined NTuple ID" ); }
00301 
00302     // assertion
00303     this->Assert( 0 != tup , "Could not book the EvtCol='" + title + "'" ) ;
00304     // some printout
00305     if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
00306     { this->debug() << "Booked EvtCol '" << title << "' ID=" << tID
00307                     << "' Path='" << evtColPath() << "' TS='"
00308                     << tup -> registry() -> identifier() << "'" << endmsg ; }
00309 
00310     tuple = createEvtCol ( title , tup                , clid ) ;
00311   }
00312   else
00313   {
00314     tuple = createEvtCol ( title , (NTuple::Tuple*) 0 , clid ) ;
00315   }
00316   // increment the reference
00317   tuple -> addRef();
00318   //
00319   m_evtColMapID [ ID ] = tuple ;
00320   //
00321   tuple -> addRef();
00322   m_evtColMapTitle[ title ] = tuple ;
00323   // return
00324   return Tuple( tuple ) ;
00325 }
00326 // ============================================================================
00327 // create TupleObj
00328 // ============================================================================
00329 template <class PBASE>
00330 Tuples::TupleObj* GaudiTuples<PBASE>::createNTuple
00331 ( const std::string& name  ,
00332   NTuple::Tuple*     tuple ,
00333   const CLID&        clid  ) const
00334 {
00335   return Tuples::createTupleObj
00336     ( this , "Tuple '" + name  + "'" , tuple , clid , Tuples::NTUPLE ) ;
00337 }
00338 // ============================================================================
00339 
00340 // ============================================================================
00341 // create TupleObj for event tag collection
00342 // ============================================================================
00343 template <class PBASE>
00344 Tuples::TupleObj* GaudiTuples<PBASE>::createEvtCol
00345 ( const std::string& name  ,
00346   NTuple::Tuple*     tuple ,
00347   const CLID&        clid  ) const
00348 {
00349   return Tuples::createTupleObj
00350     ( this , "EvtCol '" + name  + "'" , tuple , clid , Tuples::EVTCOL ) ;
00351 }
00352 // ============================================================================
00353 // perform the actual printout of N-tuples
00354 // ============================================================================
00355 template <class PBASE>
00356 long GaudiTuples<PBASE>::printTuples  () const
00357 {
00358 
00359   if ( nTupleMapTitle().empty() && nTupleMapID().empty() )
00360   { if (this->msgLevel(MSG::DEBUG)) this->debug() << "No N-Tuples   are booked" << endmsg ; }
00361   else
00362   { this->always() << "List of booked N-Tuples in directory "
00363                    << "\"" << nTuplePath() << "\"" << endmsg ; }
00364   
00365   // helper container to sort it:
00366   typedef std::map<TupleID,const Tuples::TupleObj*> OrderedMapType;
00367   OrderedMapType OrderedMap ( nTupleMapID().begin() , nTupleMapID().end() ) ;
00368   //
00369   for ( OrderedMapType::const_iterator entry = OrderedMap.begin() ;
00370         OrderedMap.end() != entry ; ++entry )
00371   {
00372     if ( 0 == entry->second ) { continue ; }
00373     const NTuple::Tuple* tuple = entry->second->tuple() ;
00374     if ( 0 == tuple )
00375     { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00376     this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first ) << endmsg ;
00377   }
00378   //
00379   return this->nTupleMapID().size() ;
00380 }
00381 // ============================================================================
00382 // perform the actual printout of Evt Tag Collections
00383 // ============================================================================
00384 template <class PBASE>
00385 long GaudiTuples<PBASE>::printEvtCols () const
00386 {
00387   if ( evtColMapTitle().empty() && evtColMapID().empty() )
00388   { this->always() << "No Event Tag Collections are booked" << endmsg ; }
00389   else
00390   { this->always() << "List of booked Event Tag Collections in directory "
00391                    << "\"" << evtColPath() << "\"" << endmsg ; }
00392   
00393   // helper container to sort it:
00394   typedef std::map<TupleID,const Tuples::TupleObj*> OrderedMapType;
00395   OrderedMapType OrderedMap ( evtColMapID().begin() , evtColMapID().end() ) ;
00396   //
00397   for ( OrderedMapType::const_iterator entry = OrderedMap.begin() ;
00398         OrderedMap.end() != entry ; ++entry )
00399   {
00400     if ( 0 == entry->second ) { continue ; }
00401     const NTuple::Tuple* tuple = entry->second->tuple() ;
00402     if ( 0 == tuple )
00403     { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00404     this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first )
00405                    << " Items:"
00406                    << Gaudi::Utils::toString ( entry->second->items() ) << endmsg ;
00407   }
00408   //
00409   return this->evtColMapID().size() ;
00410 }
00411 // ============================================================================
00412 // check the existence AND validity of the N-Tuple with the given ID
00413 // ============================================================================
00414 template <class PBASE>
00415 bool GaudiTuples<PBASE>::nTupleExists ( const TupleID& ID ) const
00416 { return m_nTupleMapID.end() != m_nTupleMapID.find ( ID ) ; }
00417 // ============================================================================
00418 // check the existence AND validity of the Event Tag Collection with the given ID
00419 // ============================================================================
00420 template <class PBASE>
00421 bool GaudiTuples<PBASE>::evtColExists ( const TupleID& ID ) const
00422 { return m_evtColMapID.end() != m_evtColMapID.find ( ID ) ; }
00423 // ============================================================================
00424 // Handle method for changes in the 'NTuplePrint'      property
00425 // ============================================================================
00426 template <class PBASE>
00427 void GaudiTuples<PBASE>::printNTupleHandler  ( Property& )
00428 {
00429   // no action if not yet initialized
00430   if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
00431   if ( this -> tuplesPrint() ) { this -> printTuples () ; }
00432 }
00433 // ============================================================================
00434 // Handle method for changes in the 'EvtColsPrint'      property
00435 // ============================================================================
00436 template <class PBASE>
00437 void GaudiTuples<PBASE>::printEvtColHandler  ( Property& )
00438 {
00439   // no action if not yet initialized
00440   if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
00441   if ( this -> evtColsPrint() ) { this -> printEvtCols () ; }
00442 }
00443 // ============================================================================
00444 // The END
00445 // ============================================================================

Generated at Wed Nov 11 16:22:57 2009 for Gaudi Framework, version v21r6 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004