00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "GaudiKernel/IRegistry.h"
00010 #include "GaudiKernel/ToStream.h"
00011
00012
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
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef _WIN32
00032 #pragma warning ( disable:4661 ) // incomplete explicit templates
00033 #endif
00034
00035
00036
00037 template <class PBASE>
00038 GaudiTuples<PBASE>::~GaudiTuples() {}
00039
00040
00041
00042 template <class PBASE>
00043 StatusCode GaudiTuples<PBASE>::initialize()
00044 {
00045
00046 const StatusCode sc = PBASE::initialize();
00047 if ( sc.isFailure() ) return sc;
00048
00049 if ( produceNTuples() )
00050 {
00051
00052 if ( this->ntupleSvc() == 0 )
00053 { return this->Error( "INTupleSvc* points to NULL!" ); }
00054
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
00064 if ( 0 == this->evtColSvc() )
00065 { return this->Error( "INTupleSvc* points to NULL!" ); }
00066
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
00078
00079 template <class PBASE>
00080 StatusCode GaudiTuples<PBASE>::finalize()
00081 {
00082 if ( !( nTupleMapTitle() .empty() &&
00083 nTupleMapNumID() .empty() &&
00084 nTupleMapLitID() .empty() &&
00085 evtColMapTitle() .empty() &&
00086 evtColMapNumID() .empty() &&
00087 evtColMapLitID() .empty()
00088 ) )
00089 {
00090 const int nNtuples = nTupleMapNumID().size() + nTupleMapLitID().size();
00091 const int nEvtCols = evtColMapNumID().size() + evtColMapLitID().size();
00092 this->always()
00093 << "Booked " << nNtuples << " N-Tuples and " << nEvtCols
00094 << " Event Tag Collections" << endmsg ;
00095 }
00096
00097 if ( produceNTuples () && tuplesPrint () ) { printTuples () ; }
00098 if ( produceEvtCols () && evtColsPrint () ) { printEvtCols () ; }
00099
00100 {
00101 for( TupleMapTitle::iterator itup = m_nTupleMapTitle.begin() ;
00102 m_nTupleMapTitle.end() != itup ; ++itup )
00103 { if( 0 != itup->second ) { itup->second->release() ; } }
00104 m_nTupleMapTitle.clear() ;
00105 }
00106 {
00107 for( TupleMapNumID::iterator itup = m_nTupleMapNumID.begin() ;
00108 m_nTupleMapNumID.end() != itup ; ++itup )
00109 { if( 0 != itup->second ) { itup->second->release() ; } }
00110 m_nTupleMapNumID.clear();
00111 }
00112 {
00113 for( TupleMapLitID::iterator itup = m_nTupleMapLitID.begin() ;
00114 m_nTupleMapLitID.end() != itup ; ++itup )
00115 { if( 0 != itup->second ) { itup->second->release() ; } }
00116 m_nTupleMapLitID.clear();
00117 }
00118 {
00119 for( TupleMapTitle::iterator itup = m_evtColMapTitle.begin() ;
00120 m_evtColMapTitle.end() != itup ; ++itup )
00121 { if( 0 != itup->second ) { itup->second->release() ; } }
00122 m_evtColMapTitle.clear();
00123 }
00124 {
00125 for( TupleMapNumID::iterator itup = m_evtColMapNumID.begin() ;
00126 m_evtColMapNumID.end() != itup ; ++itup )
00127 { if( 0 != itup->second ) { itup->second->release() ; } }
00128 m_evtColMapNumID.clear() ;
00129 }
00130 {
00131 for( TupleMapLitID::iterator itup = m_evtColMapLitID.begin() ;
00132 m_evtColMapLitID.end() != itup ; ++itup )
00133 { if( 0 != itup->second ) { itup->second->release() ; } }
00134 m_evtColMapLitID.clear() ;
00135 }
00136
00137
00138 return PBASE::finalize();
00139 }
00140
00141
00142
00143
00144 template <class PBASE>
00145 Tuples::Tuple GaudiTuples<PBASE>::nTuple
00146 ( const std::string& title ,
00147 const CLID& clid ) const
00148 {
00149
00150 Tuples::TupleObj* tuple = m_nTupleMapTitle[ title ] ;
00151 if ( 0 != tuple ) { return Tuple( tuple ) ; }
00152
00153 TupleID ID;
00154 if ( this->useNumericAutoIDs() || title.empty() )
00155 {
00156 if ( ! this->useNumericAutoIDs() )
00157 {
00158 this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for nTuple ID",
00159 StatusCode::SUCCESS );
00160 }
00161
00162 ID = TupleID ( m_nTupleMapNumID.size() + 1 + nTupleOffSet() );
00163
00164 while ( nTupleExists(ID) || evtColExists(ID) )
00165 { ID = TupleID ( ID.numeric() + 1 ) ; }
00166 }
00167 else
00168 {
00169
00170 ID = TupleID( this->convertTitleToID(title) );
00171
00172 while ( nTupleExists(ID) || evtColExists(ID) )
00173 { ID = TupleID(ID.idAsString()+"_"); }
00174 }
00175
00176 return nTuple( ID , title , clid ) ;
00177 }
00178
00179
00180
00181
00182
00183 template <class PBASE>
00184 Tuples::Tuple GaudiTuples<PBASE>::evtCol
00185 ( const std::string& title ,
00186 const CLID& clid ) const
00187 {
00188
00189 Tuples::TupleObj* tuple = m_evtColMapTitle[ title ] ;
00190 if ( 0 != tuple ) { return Tuple( tuple ) ; }
00191
00192 TupleID ID;
00193 if ( this->useNumericAutoIDs() || title.empty() )
00194 {
00195 if ( ! this->useNumericAutoIDs() )
00196 {
00197 this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for evtCol ID",
00198 StatusCode::SUCCESS );
00199 }
00200
00201 ID = m_evtColMapNumID.size() + 1 + evtColOffSet() ;
00202
00203 while ( nTupleExists(ID) || evtColExists(ID) )
00204 { ID = TupleID ( ID.numeric() + 1 ) ; }
00205 }
00206 else
00207 {
00208
00209 ID = TupleID( this->convertTitleToID(title) );
00210
00211 while ( nTupleExists(ID) || evtColExists(ID) )
00212 { ID = TupleID ( ID.idAsString()+"_" ) ; }
00213 }
00214
00215 return evtCol( ID , title , clid ) ;
00216 }
00217
00218
00219
00220
00221
00222 template <class PBASE>
00223 Tuples::Tuple GaudiTuples<PBASE>::nTuple
00224 ( const TupleID& ID ,
00225 const std::string& title1 ,
00226 const CLID& clid ) const
00227 {
00228
00229 if ( ID.undefined() )
00230 { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
00231
00232
00233 Tuples::TupleObj * tuple =
00234 ( ID.numeric() ? m_nTupleMapNumID[ID.numericID()] : m_nTupleMapLitID[ID.literalID()] ) ;
00235 if( 0 != tuple ) { return Tuple( tuple ) ; }
00236
00237
00238 const std::string tID = ID.idAsString() ;
00239
00240
00241 const std::string title = title1.empty() ? ( "NTuple #" + tID ) : title1 ;
00242
00243
00244 if( produceNTuples() )
00245 {
00246
00247 NTuple::Tuple * tup = 0;
00248 if ( ID.numeric() )
00249 {
00250 tup = this->ntupleSvc() -> book ( nTuplePath() , ID.numericID() , clid , title );
00251 }
00252 else if ( ID.literal() )
00253 {
00254 tup = this->ntupleSvc() -> book ( nTuplePath() , ID.literalID() , clid , title );
00255 }
00256 else { this->Error( "Undefined NTuple ID" ); }
00257
00258
00259 this->Assert( 0 != tup , "Could not book the N-Tuple='" + title + "'" ) ;
00260
00261 if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
00262 { this->debug() << "Booked NTuple '" << title << "' ID=" << tID
00263 << "' Path='" << nTuplePath() << "' TS='"
00264 << tup -> registry() -> identifier() << "'" << endmsg; }
00265
00266 tuple = createNTuple ( title , tup , clid ) ;
00267 }
00268 else
00269 {
00270 tuple = createNTuple ( title , (NTuple::Tuple*) 0 , clid ) ;
00271 }
00272
00273 tuple -> addRef();
00274 if ( ID.numeric() ) { m_nTupleMapNumID [ ID.numericID() ] = tuple ; }
00275 else { m_nTupleMapLitID [ ID.literalID() ] = tuple ; }
00276 tuple -> addRef();
00277 m_nTupleMapTitle[ title ] = tuple ;
00278
00279 return Tuple( tuple ) ;
00280 };
00281
00282
00283 template <class PBASE>
00284 Tuples::Tuple GaudiTuples<PBASE>::evtCol
00285 ( const TupleID& ID ,
00286 const std::string& title1 ,
00287 const CLID& clid ) const
00288 {
00289
00290 if ( ID.undefined() )
00291 { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
00292
00293
00294 Tuples::TupleObj* tuple =
00295 ( ID.numeric() ? m_evtColMapNumID[ID.numericID()] : m_evtColMapLitID[ID.literalID()] ) ;
00296 if ( 0 != tuple ) { return Tuple( tuple ) ; }
00297
00298
00299 const std::string tID = ID.idAsString() ;
00300
00301
00302 const std::string title = title1.empty() ? ( "EvtCol #" + tID ) : title1 ;
00303
00304
00305 if( produceEvtCols() )
00306 {
00307
00308 NTuple::Tuple* tup = 0;
00309 if ( ID.numeric() )
00310 {
00311 tup = this->evtColSvc()->book ( evtColPath() , ID.numericID() , clid , title ) ;
00312 }
00313 else if ( ID.literal() )
00314 {
00315 tup = this->evtColSvc()->book ( evtColPath() , ID.literalID() , clid , title ) ;
00316 }
00317 else { this->Error( "Undefined NTuple ID" ); }
00318
00319
00320 this->Assert( 0 != tup , "Could not book the EvtCol='" + title + "'" ) ;
00321
00322 if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
00323 { this->debug() << "Booked EvtCol '" << title << "' ID=" << tID
00324 << "' Path='" << evtColPath() << "' TS='"
00325 << tup -> registry() -> identifier() << "'" << endmsg ; }
00326
00327 tuple = createEvtCol ( title , tup , clid ) ;
00328 }
00329 else
00330 {
00331 tuple = createEvtCol ( title , (NTuple::Tuple*) 0 , clid ) ;
00332 }
00333
00334 tuple -> addRef();
00335 if ( ID.numeric() ) { m_evtColMapNumID [ ID.numericID() ] = tuple ; }
00336 else { m_evtColMapLitID [ ID.literalID() ] = tuple ; }
00337 tuple -> addRef();
00338 m_evtColMapTitle[ title ] = tuple ;
00339
00340 return Tuple( tuple ) ;
00341 }
00342
00343
00344
00345
00346
00347 template <class PBASE>
00348 Tuples::TupleObj* GaudiTuples<PBASE>::createNTuple
00349 ( const std::string& name ,
00350 NTuple::Tuple* tuple ,
00351 const CLID& clid ) const
00352 {
00353 return Tuples::createTupleObj
00354 ( this , "Tuple '" + name + "'" , tuple , clid , Tuples::NTUPLE ) ;
00355 }
00356
00357
00358
00359
00360
00361 template <class PBASE>
00362 Tuples::TupleObj* GaudiTuples<PBASE>::createEvtCol
00363 ( const std::string& name ,
00364 NTuple::Tuple* tuple ,
00365 const CLID& clid ) const
00366 {
00367 return Tuples::createTupleObj
00368 ( this , "EvtCol '" + name + "'" , tuple , clid , Tuples::EVTCOL ) ;
00369 }
00370
00371
00372
00373
00374
00375 template <class PBASE>
00376 long GaudiTuples<PBASE>::printTuples () const
00377 {
00378
00379 typedef std::set<const NTuple::Tuple*> _Tuples ;
00380
00381 if ( nTupleMapTitle().empty() &&
00382 nTupleMapNumID().empty() &&
00383 nTupleMapLitID().empty() )
00384 { if (this->msgLevel(MSG::DEBUG)) this->debug() << "No N-Tuples are booked" << endmsg ; }
00385 else
00386 { this->always() << "List of booked N-Tuples in directory "
00387 << "\"" << nTuplePath() << "\"" << endmsg ; }
00388
00389 _Tuples tuples ;
00390 {
00391 typedef std::map<TupleMapNumID::key_type, TupleMapNumID::mapped_type> ordered_map_t;
00392 ordered_map_t ordered_map(nTupleMapNumID().begin(), nTupleMapNumID().end());
00393 for ( ordered_map_t::const_iterator entry = ordered_map.begin() ;
00394 ordered_map.end() != entry ; ++entry )
00395 {
00396 if ( 0 == entry->second ) { continue ; }
00397 const NTuple::Tuple* tuple = entry->second->tuple() ;
00398 if ( 0 == tuple )
00399 { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00400 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00401 tuples.insert ( tuple ) ;
00402 this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first ) << endmsg ;
00403 }
00404 }
00405 {
00406 typedef std::map<TupleMapLitID::key_type, TupleMapLitID::mapped_type> ordered_map_t;
00407 ordered_map_t ordered_map(nTupleMapLitID().begin(), nTupleMapLitID().end());
00408 for ( ordered_map_t::const_iterator entry = ordered_map.begin() ;
00409 ordered_map.end() != entry ; ++entry )
00410 {
00411 if ( 0 == entry->second ) { continue ; }
00412 const NTuple::Tuple* tuple = entry->second->tuple() ;
00413 if ( 0 == tuple )
00414 { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00415 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00416 tuples.insert ( tuple ) ;
00417 this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first ) << endmsg ;
00418 }
00419 }
00420 {
00421 typedef std::map<TupleMapTitle::key_type, TupleMapTitle::mapped_type> ordered_map_t;
00422 ordered_map_t ordered_map(nTupleMapTitle().begin(), nTupleMapTitle().end());
00423 for ( ordered_map_t::const_iterator entry = ordered_map.begin() ;
00424 ordered_map.end() != entry ; ++entry )
00425 {
00426 if ( 0 == entry->second ) { continue ; }
00427 const NTuple::Tuple* tuple = entry->second->tuple() ;
00428 if ( 0 == tuple )
00429 { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00430 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00431 tuples.insert ( tuple ) ;
00432 this->always() << GaudiAlg::PrintTuple::print ( tuple ) << endmsg ;
00433 }
00434 }
00435
00436 return tuples.size() ;
00437 };
00438
00439
00440
00441
00442
00443 template <class PBASE>
00444 long GaudiTuples<PBASE>::printEvtCols () const
00445 {
00446 typedef std::set<const NTuple::Tuple*> _Tuples ;
00447
00448 if ( evtColMapTitle().empty() &&
00449 evtColMapNumID().empty() &&
00450 evtColMapLitID().empty() )
00451 { this->always() << "No Event Tag Collections are booked" << endmsg ; }
00452 else
00453 { this->always() << "List of booked Event Tag Collections in directory "
00454 << "\"" << evtColPath() << "\"" << endmsg ; }
00455
00456 _Tuples tuples ;
00457 {
00458 for ( TupleMapNumID::const_iterator entry = evtColMapNumID().begin() ;
00459 evtColMapNumID().end() != entry ; ++entry )
00460 {
00461 if ( 0 == entry->second ) { continue ; }
00462 const NTuple::Tuple* tuple = entry->second->tuple() ;
00463 if ( 0 == tuple )
00464 { this->error() << "NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00465 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00466 tuples.insert ( tuple ) ;
00467 this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first )
00468 << " Items:"
00469 << Gaudi::Utils::toString ( entry->second->items() ) << endmsg ;
00470 }
00471 }
00472 {
00473 for ( TupleMapLitID::const_iterator entry = evtColMapLitID().begin() ;
00474 evtColMapLitID().end() != entry ; ++entry )
00475 {
00476 if ( 0 == entry->second ) { continue ; }
00477 const NTuple::Tuple* tuple = entry->second->tuple() ;
00478 if ( 0 == tuple )
00479 { this->error() << "NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00480 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00481 tuples.insert ( tuple ) ;
00482 this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first )
00483 << " Items:"
00484 << Gaudi::Utils::toString ( entry->second->items() ) << endmsg ;
00485 }
00486 }
00487 {
00488 for ( TupleMapTitle::const_iterator entry = evtColMapTitle().begin() ;
00489 evtColMapTitle().end() != entry ; ++entry )
00490 {
00491 if ( 0 == entry->second ) { continue ; }
00492 const NTuple::Tuple* tuple = entry->second->tuple() ;
00493 if ( 0 == tuple )
00494 { this->error() << "NTuple::Tuple* points to NULL" << endmsg ; continue ; }
00495 if ( tuples.end() != tuples.find( tuple ) ) { continue ; }
00496 tuples.insert ( tuple ) ;
00497 this->always() << GaudiAlg::PrintTuple::print ( tuple )
00498 << " Items:"
00499 << Gaudi::Utils::toString ( entry->second->items() ) << endmsg ;
00500 }
00501 }
00502
00503 return tuples.size() ;
00504 }
00505
00506
00507
00508
00509 template <class PBASE>
00510 bool GaudiTuples<PBASE>::nTupleExists ( const TupleID& ID ) const
00511 {
00512 bool OK(false);
00513 if ( ID.numeric() )
00514 {
00515 TupleMapNumID::iterator found = m_nTupleMapNumID.find( ID.numericID() ) ;
00516 OK = ( m_nTupleMapNumID.end() != found );
00517 }
00518 else
00519 {
00520 TupleMapLitID::iterator found = m_nTupleMapLitID.find( ID.literalID() ) ;
00521 OK = ( m_nTupleMapLitID.end() != found );
00522 }
00523 return OK;
00524 }
00525
00526
00527
00528
00529 template <class PBASE>
00530 bool GaudiTuples<PBASE>::evtColExists ( const TupleID& ID ) const
00531 {
00532 bool OK(false);
00533 if ( ID.numeric() )
00534 {
00535 TupleMapNumID::iterator found = m_evtColMapNumID.find( ID.numericID() ) ;
00536 OK = ( m_evtColMapNumID.end() != found );
00537 }
00538 else
00539 {
00540 TupleMapLitID::iterator found = m_evtColMapLitID.find( ID.literalID() ) ;
00541 OK = ( m_evtColMapLitID.end() != found );
00542 }
00543 return OK;
00544 }
00545
00546
00547
00548 template <class PBASE>
00549 void GaudiTuples<PBASE>::printNTupleHandler ( Property& )
00550 {
00551
00552 if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
00553 if ( this -> tuplesPrint() ) { this -> printTuples () ; }
00554 }
00555
00556
00557
00558 template <class PBASE>
00559 void GaudiTuples<PBASE>::printEvtColHandler ( Property& )
00560 {
00561
00562 if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
00563 if ( this -> evtColsPrint() ) { this -> printEvtCols () ; }
00564 }
00565
00566
00567
00568
00569