All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GaudiTuples.icpp
Go to the documentation of this file.
1 // $Id: GaudiTuples.icpp,v 1.10 2008/05/15 08:50:32 marcocle Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.10 $
4 // ============================================================================
5 // Include files
6 // ============================================================================
7 // Gaudi
8 // ============================================================================
10 #include "GaudiKernel/ToStream.h"
11 // ============================================================================
12 // GaudiAlg
13 // ============================================================================
14 #include "GaudiAlg/GaudiTuples.h"
15 #include "GaudiAlg/Tuple.h"
16 #include "GaudiAlg/TupleObj.h"
17 #include "GaudiAlg/GaudiTupleAlg.h"
18 #include "GaudiAlg/TupleDetail.h"
19 #include "GaudiAlg/Print.h"
20 #include "GaudiAlg/HbookName.h"
21 // ============================================================================
22 /* @file GaudiTuples.cpp
23  *
24  * Implementation file for class : GaudiTuples
25  *
26  * @author Chris Jones Christopher.Rob.Jones@cern.ch
27  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
28  * @date 2005-08-08
29  */
30 // ============================================================================
31 // Disable warning on windows
32 #ifdef _WIN32
33 #pragma warning ( disable:4661 ) // incomplete explicit templates
34 #endif
35 // ============================================================================
36 // Destructor
37 //=============================================================================
38 template <class PBASE>
40 //=============================================================================
41 // Initialize ntupling
42 //=============================================================================
43 template <class PBASE>
45 #ifdef __ICC
46  i_gtInitialize
47 #else
49 #endif
50  ()
51 {
52  // initialize base class
54  if ( sc.isFailure() ) return sc;
55 
56  if ( produceNTuples() )
57  {
58  // check the existance of service
59  if ( this->ntupleSvc() == 0 )
60  { return this->Error( "INTupleSvc* points to NULL!" ); }
61  // Print ntuple path
62  this->Print( "The N-Tuple path is set to be '" + nTuplePath() + "'",
63  StatusCode(StatusCode::SUCCESS, true) , MSG::DEBUG).ignore();
64  }
65  else
66  { this->debug() << "Production of N-Tuples is switched OFF" << endmsg; }
67 
68  if ( produceEvtCols() )
69  {
70  // check the existance of service
71  if ( 0 == this->evtColSvc() )
72  { return this->Error( "INTupleSvc* points to NULL!" ); }
73  // Print EvtCol path
74  this->Print( "The EventCol path is set to be '" + evtColPath() + "'",
75  StatusCode(StatusCode::SUCCESS, true) , MSG::DEBUG ).ignore();
76  }
77  else
78  { this->debug() << "Production of Event Collections is switched OFF" << endmsg; }
79 
80  return sc;
81 }
82 
83 //=============================================================================
84 // finalize ntupling
85 //=============================================================================
86 template <class PBASE>
88 #ifdef __ICC
89  i_gtFinalize
90 #else
91  finalize
92 #endif
93  ()
94 {
95  if ( !( nTupleMapTitle () . empty () &&
96  nTupleMapID () . empty () &&
97  evtColMapTitle () . empty () &&
98  evtColMapID () . empty ()
99  ) )
100  {
101  const int nNtuples = nTupleMapID () . size () ;
102  const int nEvtCols = evtColMapID () . size ();
103  this->always()
104  << "Booked " << nNtuples << " N-Tuples and " << nEvtCols
105  << " Event Tag Collections" << endmsg ;
106  }
107 
108  if ( produceNTuples () && tuplesPrint () ) { printTuples () ; }
109  if ( produceEvtCols () && evtColsPrint () ) { printEvtCols () ; }
110 
111  { // release ntuples and clear the container
112  for( TupleMapTitle::iterator itup = m_nTupleMapTitle.begin() ;
113  m_nTupleMapTitle.end() != itup ; ++itup )
114  { if( 0 != itup->second ) { itup->second->release() ; } }
115  m_nTupleMapTitle.clear() ;
116  }
117  { // release ntuples and clear the container
118  for( TupleMapID::iterator itup = m_nTupleMapID.begin() ;
119  m_nTupleMapID.end() != itup ; ++itup )
120  { if( 0 != itup->second ) { itup->second->release() ; } }
121  m_nTupleMapID.clear();
122  }
123  { // release ntuples and clear the container
124  for( TupleMapTitle::iterator itup = m_evtColMapTitle.begin() ;
125  m_evtColMapTitle.end() != itup ; ++itup )
126  { if( 0 != itup->second ) { itup->second->release() ; } }
127  m_evtColMapTitle.clear();
128  }
129  { // release ntuples and clear the container
130  for( TupleMapID::iterator itup = m_evtColMapID.begin() ;
131  m_evtColMapID.end() != itup ; ++itup )
132  { if( 0 != itup->second ) { itup->second->release() ; } }
133  m_evtColMapID.clear() ;
134  }
135  // finalize base class
136  return PBASE::finalize();
137 }
138 
139 // ============================================================================
140 // get N-tuple object ( book on-demand ) with unique identidier
141 // ============================================================================
142 template <class PBASE>
144 ( const std::string& title ,
145  const CLID& clid ) const
146 {
147  // look up in the table
148  Tuples::TupleObj* tuple = m_nTupleMapTitle[ title ] ;
149  if ( 0 != tuple ) { return Tuple( tuple ) ; } // RETURN
150  // Create the tuple ID
151  TupleID ID;
152  if ( this->useNumericAutoIDs() || title.empty() )
153  {
154  if ( ! this->useNumericAutoIDs() )
155  {
156  this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for nTuple ID",
158  }
159  // propose the tuple ID
160  ID = TupleID ( m_nTupleMapID.size() + 1 + nTupleOffSet() );
161  // adjust the proposed ID
162  while ( nTupleExists(ID) || evtColExists(ID) )
163  { ID = TupleID ( ID.numeric() + 1 ) ; }
164  }
165  else
166  {
167  // use the title to create a unique literal ID
168  ID = TupleID( this->convertTitleToID(title) );
169  // Just in case ...
170  while ( nTupleExists(ID) || evtColExists(ID) )
171  { ID = TupleID(ID.idAsString()+"_"); }
172  }
173  // return
174  return nTuple( ID , title , clid ) ;
175 }
176 // ============================================================================
177 
178 // ============================================================================
179 // Access an Event Tag Collection object (book on-demand) with unique identifier
180 // ============================================================================
181 template <class PBASE>
183 ( const std::string& title ,
184  const CLID& clid ) const
185 {
186  // look up in the table
187  Tuples::TupleObj* tuple = m_evtColMapTitle[ title ] ;
188  if ( 0 != tuple ) { return Tuple( tuple ) ; } // RETURN
189  // Create the tuple ID
190  TupleID ID;
191  if ( this->useNumericAutoIDs() || title.empty() )
192  {
193  if ( ! this->useNumericAutoIDs() )
194  {
195  this -> Warning( "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for evtCol ID",
197  }
198  // proposed the tuple ID
199  ID = TupleID ( m_evtColMapID.size() + 1 + evtColOffSet() ) ;
200  // adjust the proposed ID
201  while ( nTupleExists(ID) || evtColExists(ID) )
202  { ID = TupleID ( ID.numeric() + 1 ) ; }
203  }
204  else
205  {
206  // use the title to create a unique literal ID
207  ID = TupleID( this->convertTitleToID(title) );
208  // Just in case ...
209  while ( nTupleExists(ID) || evtColExists(ID) )
210  { ID = TupleID ( ID.idAsString()+"_" ) ; }
211  }
212  // return
213  return evtCol( ID , title , clid ) ;
214 }
215 // ============================================================================
216 
217 // ============================================================================
218 // get N-tuple object ( book on-demand ) with forced ID
219 // ============================================================================
220 template <class PBASE>
222 ( const TupleID& ID ,
223  const std::string& title1 ,
224  const CLID& clid ) const
225 {
226  // Check ID
227  if ( ID.undefined() )
228  { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
229 
230  // look up in the table
231  Tuples::TupleObj * tuple = m_nTupleMapID[ ID ] ;
232  if( 0 != tuple ) { return Tuple( tuple ) ; } // RETURN
233 
234  // convert ID to the string
235  const std::string tID = ID.idAsString() ;
236 
237  // adjust the NTuple title
238  const std::string title = title1.empty() ? ( "NTuple #" + tID ) : title1 ;
239 
240  // book new ntuple
241  if( produceNTuples() )
242  {
243  // book NTupel
244  NTuple::Tuple * tup = 0;
245  if ( ID.numeric() )
246  {
247  tup = this->ntupleSvc() -> book ( nTuplePath() , ID.numericID() , clid , title );
248  }
249  else if ( ID.literal() )
250  {
251  tup = this->ntupleSvc() -> book ( nTuplePath() , ID.literalID() , clid , title );
252  }
253  else { this->Error( "Undefined NTuple ID" ); }
254 
255  // assertion
256  this->Assert( 0 != tup , "Could not book the N-Tuple='" + title + "'" ) ;
257  // some printout
258  if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
259  { this->debug() << "Booked NTuple '" << title << "' ID=" << tID
260  << "' Path='" << nTuplePath() << "' TS='"
261  << tup -> registry() -> identifier() << "'" << endmsg; }
262 
263  tuple = createNTuple ( title , tup , clid ) ;
264  }
265  else
266  {
267  tuple = createNTuple ( title , (NTuple::Tuple*) 0 , clid ) ;
268  }
269  // increment the reference
270  tuple -> addRef();
271  //
272  m_nTupleMapID [ ID ] = tuple ;
273  //
274  tuple -> addRef();
275  m_nTupleMapTitle[ title ] = tuple ;
276  // return
277  return Tuple( tuple ) ;
278 }
279 // ============================================================================
280 template <class PBASE>
282 ( const TupleID& ID ,
283  const std::string& title1 ,
284  const CLID& clid ) const
285 {
286  // Check ID
287  if ( ID.undefined() )
288  { this->Error("Undefined NTuple ID : Title='"+title1+"'"); return Tuple(0); }
289 
290  // look up in the table
291  Tuples::TupleObj* tuple = m_evtColMapID[ID] ;
292  if ( 0 != tuple ) { return Tuple( tuple ) ; } // RETURN
293 
294  // convert ID to the string
295  const std::string tID = ID.idAsString() ;
296 
297  // adjust the NTuple title
298  const std::string title = title1.empty() ? ( "EvtCol #" + tID ) : title1 ;
299 
300  // book new ntuple
301  if( produceEvtCols() )
302  {
303  // book NTuple
304  NTuple::Tuple* tup = 0;
305  if ( ID.numeric() )
306  {
307  tup = this->evtColSvc()->book ( evtColPath() , ID.numericID() , clid , title ) ;
308  }
309  else if ( ID.literal() )
310  {
311  tup = this->evtColSvc()->book ( evtColPath() , ID.literalID() , clid , title ) ;
312  }
313  else { this->Error( "Undefined NTuple ID" ); }
314 
315  // assertion
316  this->Assert( 0 != tup , "Could not book the EvtCol='" + title + "'" ) ;
317  // some printout
318  if( 0 != tup -> registry() && this->msgLevel(MSG::DEBUG) )
319  { this->debug() << "Booked EvtCol '" << title << "' ID=" << tID
320  << "' Path='" << evtColPath() << "' TS='"
321  << tup -> registry() -> identifier() << "'" << endmsg ; }
322 
323  tuple = createEvtCol ( title , tup , clid ) ;
324  }
325  else
326  {
327  tuple = createEvtCol ( title , (NTuple::Tuple*) 0 , clid ) ;
328  }
329  // increment the reference
330  tuple -> addRef();
331  //
332  m_evtColMapID [ ID ] = tuple ;
333  //
334  tuple -> addRef();
335  m_evtColMapTitle[ title ] = tuple ;
336  // return
337  return Tuple( tuple ) ;
338 }
339 // ============================================================================
340 // create TupleObj
341 // ============================================================================
342 template <class PBASE>
344 ( const std::string& name ,
345  NTuple::Tuple* tuple ,
346  const CLID& clid ) const
347 {
349  ( this , "Tuple '" + name + "'" , tuple , clid , Tuples::NTUPLE ) ;
350 }
351 // ============================================================================
352 
353 // ============================================================================
354 // create TupleObj for event tag collection
355 // ============================================================================
356 template <class PBASE>
358 ( const std::string& name ,
359  NTuple::Tuple* tuple ,
360  const CLID& clid ) const
361 {
363  ( this , "EvtCol '" + name + "'" , tuple , clid , Tuples::EVTCOL ) ;
364 }
365 // ============================================================================
366 // perform the actual printout of N-tuples
367 // ============================================================================
368 template <class PBASE>
370 {
371 
372  if ( nTupleMapTitle().empty() && nTupleMapID().empty() )
373  { if (this->msgLevel(MSG::DEBUG)) this->debug() << "No N-Tuples are booked" << endmsg ; }
374  else
375  { this->always() << "List of booked N-Tuples in directory "
376  << "\"" << nTuplePath() << "\"" << endmsg ; }
377 
378  // helper container to sort it:
379  typedef std::map<TupleID,const Tuples::TupleObj*> OrderedMapType;
380  OrderedMapType OrderedMap ( nTupleMapID().begin() , nTupleMapID().end() ) ;
381  //
382  for ( OrderedMapType::const_iterator entry = OrderedMap.begin() ;
383  OrderedMap.end() != entry ; ++entry )
384  {
385  if ( 0 == entry->second ) { continue ; }
386  const NTuple::Tuple* tuple = entry->second->tuple() ;
387  if ( 0 == tuple )
388  { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
389  this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first ) << endmsg ;
390  }
391  //
392  return this->nTupleMapID().size() ;
393 }
394 // ============================================================================
395 // perform the actual printout of Evt Tag Collections
396 // ============================================================================
397 template <class PBASE>
399 {
400  if ( evtColMapTitle().empty() && evtColMapID().empty() )
401  { this->always() << "No Event Tag Collections are booked" << endmsg ; }
402  else
403  { this->always() << "List of booked Event Tag Collections in directory "
404  << "\"" << evtColPath() << "\"" << endmsg ; }
405 
406  // helper container to sort it:
407  typedef std::map<TupleID,const Tuples::TupleObj*> OrderedMapType;
408  OrderedMapType OrderedMap ( evtColMapID().begin() , evtColMapID().end() ) ;
409  //
410  for ( OrderedMapType::const_iterator entry = OrderedMap.begin() ;
411  OrderedMap.end() != entry ; ++entry )
412  {
413  if ( 0 == entry->second ) { continue ; }
414  const NTuple::Tuple* tuple = entry->second->tuple() ;
415  if ( 0 == tuple )
416  { this->error() << " NTuple::Tuple* points to NULL" << endmsg ; continue ; }
417  this->always() << GaudiAlg::PrintTuple::print ( tuple , entry->first )
418  << " Items:"
419  << Gaudi::Utils::toString ( entry->second->items() ) << endmsg ;
420  }
421  //
422  return this->evtColMapID().size() ;
423 }
424 // ============================================================================
425 // check the existence AND validity of the N-Tuple with the given ID
426 // ============================================================================
427 template <class PBASE>
429 {
430  return m_nTupleMapID.end() != m_nTupleMapID.find ( ID ) ;
431 }
432 // ============================================================================
433 // check the existence AND validity of the Event Tag Collection with the given ID
434 // ============================================================================
435 template <class PBASE>
437 {
438  return m_evtColMapID.end() != m_evtColMapID.find ( ID ) ;
439 }
440 // ============================================================================
441 // Handle method for changes in the 'NTuplePrint' property
442 // ============================================================================
443 template <class PBASE>
445 {
446  // no action if not yet initialized
447  if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
448  if ( this -> tuplesPrint() ) { this -> printTuples () ; }
449 }
450 // ============================================================================
451 // Handle method for changes in the 'EvtColsPrint' property
452 // ============================================================================
453 template <class PBASE>
455 {
456  // no action if not yet initialized
457  if ( this -> FSMState() < Gaudi::StateMachine::INITIALIZED ) { return ; }
458  if ( this -> evtColsPrint() ) { this -> printEvtCols () ; }
459 }
460 // ============================================================================
461 // get the constructed N-Tuple path
462 // ============================================================================
463 template <class PBASE>
465 {
466  const std::string path = nTupleLUN() + "/" + nTupleTopDir() + nTupleDir();
467  return ( splitNTupleDir() ? dirHbookName( path ) : path ) ;
468 }
469 // ============================================================================
470 // get the constructed Event Tag Collection path
471 // ============================================================================
472 template <class PBASE>
474 {
475  std::string path = evtColLUN() + "/" + evtColTopDir() + evtColDir();
476  return ( splitEvtColDir() ? dirHbookName( path ) : path );
477 }
478 // ============================================================================
479 // The END
480 // ============================================================================
static std::string print(const INTuple *tuple, const GaudiAlg::TupleID &ID)
Definition: Print.cpp:177
GAUDI_API AIDA::IHistogram1D * book(IHistogramSvc *svc, const std::string &path, const Gaudi::Histo1DDef &hist)
helper function to book 1D-histogram
Definition: HistoDef.cpp:132
bool nTupleExists(const TupleID &ID) const
check the existence AND validity of the N-Tuple with the given ID
virtual Tuples::TupleObj * createEvtCol(const std::string &name, NTuple::Tuple *tuple, const CLID &clid) const
create TupleObj for event tag collection
Templated base class providing common 'ntupling' methods.
Definition: GaudiTuples.h:37
Header file for class TupleObj.
TupleObj * createTupleObj(const OWNER *owner, const std::string &name, NTuple::Tuple *tuple, const CLID &clid=CLID_ColumnWiseTuple, const Tuples::Type type=Tuples::NTUPLE)
Templated helper functions allow to avoid heavy semantics of dealing with explicit type of class Tupl...
Definition: TupleDetail.h:243
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:367
Tuple evtCol(const std::string &title, const CLID &clid=CLID_ColumnWiseTuple) const
Access an Event Tag Collection object (book on-demand) with unique identifier.
few useful function to construct names of Hbook histograms and directories functions are imported fro...
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:72
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: Tuple.h:115
void printEvtColHandler(Property &)
handler for "EvtColsPrint" property
NumericID numericID() const
Returns the numerical ID.
Definition: GaudiHistoID.h:74
def nTuple
Retrieve (book-on-demand) 'Smart'-N-tuple object.
Definition: TupleUtils.py:66
long printTuples() const
perform the actual printout of N-tuples
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:180
long printEvtCols() const
perform the actual printout of Event Tag Collections
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Tuple nTuple(const std::string &title, const CLID &clid=CLID_ColumnWiseTuple) const
Access an N-Tuple object (book on-demand) with unique identifier.
const LiteralID & literalID() const
Returns the ID as a LiteralID.
Definition: GaudiHistoID.h:72
GaudiAlg::ID TupleID
the actual type for N-Tuple identifier (HBOOK-style)
Definition: TupleID.h:25
collection of useful utilities to print certain objects (currently used for implementation in class G...
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
bool evtColExists(const TupleID &ID) const
check the existence AND validity of the Event Tag Collection with the given ID
Collection of few 'technical' methods for instantiation of tuples.
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:367
tuple end
Definition: IOTest.py:101
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
virtual Tuples::TupleObj * createNTuple(const std::string &name, NTuple::Tuple *tuple, const CLID &clid) const
create TupleObj
std::string evtColPath() const
get the constructed Event Tag Collection path
virtual ~GaudiTuples()
Destructor.
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
map_type::iterator iterator
Definition: Map.h:98
bool undefined() const
Is this ID undefined.
Definition: GaudiHistoID.h:70
std::string nTuplePath() const
get the constructed N-Tuple path
GAUDI_API LiteralID idAsString() const
Return ID as string, for both numeric and literal IDs.
bool literal() const
Is this ID numeric.
Definition: GaudiHistoID.h:68
void printNTupleHandler(Property &)
handler for "NTuplePrint" property
implemenattiono fvarioud functions for streaming.
ID class for Histogram and Ntuples.
Definition: GaudiHistoID.h:46
bool numeric() const
Is this ID numeric.
Definition: GaudiHistoID.h:66
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
Header file for class : Tuple.