Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GaudiTuples.icpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // Gaudi
5 // ============================================================================
7 #include "GaudiKernel/ToStream.h"
8 // ============================================================================
9 // GaudiAlg
10 // ============================================================================
11 #include "GaudiAlg/GaudiTupleAlg.h"
12 #include "GaudiAlg/GaudiTuples.h"
13 #include "GaudiAlg/HbookName.h"
14 #include "GaudiAlg/Print.h"
15 #include "GaudiAlg/Tuple.h"
16 #include "GaudiAlg/TupleDetail.h"
17 #include "GaudiAlg/TupleObj.h"
18 // ============================================================================
19 /* @file GaudiTuples.cpp
20  *
21  * Implementation file for class : GaudiTuples
22  *
23  * @author Chris Jones Christopher.Rob.Jones@cern.ch
24  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
25  * @date 2005-08-08
26  */
27 // ============================================================================
28 // Disable warning on windows
29 #ifdef _WIN32
30 # pragma warning( disable : 4661 ) // incomplete explicit templates
31 #endif
32 
33 //=============================================================================
34 // Initialize ntupling
35 //=============================================================================
36 template <class PBASE>
38 #ifdef __ICC
39  i_gtInitialize
40 #else
41  initialize
42 #endif
43  () {
44  // initialize base class
45  const StatusCode sc = PBASE::initialize();
46  if ( sc.isFailure() ) return sc;
47 
48  if ( produceNTuples() ) {
49  // check the existance of service
50  if ( this->ntupleSvc() == 0 ) { return this->Error( "INTupleSvc* points to NULL!" ); }
51  // Print ntuple path
52  this->Print( "The N-Tuple path is set to be '" + nTuplePath() + "'", StatusCode( StatusCode::SUCCESS, true ),
53  MSG::DEBUG )
54  .ignore();
55  } else {
56  this->debug() << "Production of N-Tuples is switched OFF" << endmsg;
57  }
58 
59  if ( produceEvtCols() ) {
60  // check the existance of service
61  if ( 0 == this->evtColSvc() ) { return this->Error( "INTupleSvc* points to NULL!" ); }
62  // Print EvtCol path
63  this->Print( "The EventCol path is set to be '" + evtColPath() + "'", StatusCode( StatusCode::SUCCESS, true ),
64  MSG::DEBUG )
65  .ignore();
66  } else {
67  this->debug() << "Production of Event Collections is switched OFF" << endmsg;
68  }
69 
70  return sc;
71 }
72 
73 //=============================================================================
74 // finalize ntupling
75 //=============================================================================
76 template <class PBASE>
78 #ifdef __ICC
79  i_gtFinalize
80 #else
81  finalize
82 #endif
83  () {
84  if ( !( m_nTupleMap.empty() && m_evtColMap.empty() ) ) {
85  const int nNtuples = m_nTupleMap.size();
86  const int nEvtCols = m_evtColMap.size();
87  this->always() << "Booked " << nNtuples << " N-Tuples and " << nEvtCols << " Event Tag Collections" << endmsg;
88  }
89 
90  if ( produceNTuples() && tuplesPrint() ) printTuples();
91  if ( produceEvtCols() && evtColsPrint() ) printEvtCols();
92 
93  // release ntuples and clear the container
94  m_nTupleMap.clear();
95 
96  // release ntuples and clear the container
97  m_evtColMap.clear();
98 
99  // finalize base class
100  return PBASE::finalize();
101 }
102 
103 // ============================================================================
104 // get N-tuple object ( book on-demand ) with unique identidier
105 // ============================================================================
106 template <class PBASE>
107 Tuples::Tuple GaudiTuples<PBASE>::nTuple( const std::string& title, const CLID& clid ) const {
108  // look up in the table
109  auto& m = nTupleByTitle();
110  auto tuple = m.find( title );
111  if ( tuple != m.end() ) return Tuple( tuple->tuple ); // RETURN
112  // Create the tuple ID
113  TupleID ID;
114  if ( this->useNumericAutoIDs() || title.empty() ) {
115  if ( !this->useNumericAutoIDs() ) {
116  this->Warning(
117  "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for nTuple ID",
119  .ignore();
120  }
121  // propose the tuple ID
122  ID = TupleID( m_nTupleMap.size() + 1 + nTupleOffSet() );
123  // adjust the proposed ID
124  while ( nTupleExists( ID ) || evtColExists( ID ) ) { ID = TupleID( ID.numeric() + 1 ); }
125  } else {
126  // use the title to create a unique literal ID
127  ID = TupleID( this->convertTitleToID( title ) );
128  // Just in case ...
129  while ( nTupleExists( ID ) || evtColExists( ID ) ) { ID = TupleID( ID.idAsString() + "_" ); }
130  }
131  // return
132  return nTuple( ID, title, clid );
133 }
134 // ============================================================================
135 
136 // ============================================================================
137 // Access an Event Tag Collection object (book on-demand) with unique identifier
138 // ============================================================================
139 template <class PBASE>
140 Tuples::Tuple GaudiTuples<PBASE>::evtCol( const std::string& title, const CLID& clid ) const {
141  // look up in the table
142  auto& m = evtColByTitle();
143  auto tuple = m.find( title );
144  if ( tuple != m.end() ) return Tuple( tuple->tuple ); // RETURN
145  // Create the tuple ID
146  TupleID ID;
147  if ( this->useNumericAutoIDs() || title.empty() ) {
148  if ( !this->useNumericAutoIDs() ) {
149  this->Warning(
150  "Cannot generate automatic literal ID from an empty title ! Using numeric ID instead for evtCol ID",
152  .ignore();
153  }
154  // proposed the tuple ID
155  ID = TupleID( m_evtColMap.size() + 1 + evtColOffSet() );
156  // adjust the proposed ID
157  while ( nTupleExists( ID ) || evtColExists( ID ) ) { ID = TupleID( ID.numeric() + 1 ); }
158  } else {
159  // use the title to create a unique literal ID
160  ID = TupleID( this->convertTitleToID( title ) );
161  // Just in case ...
162  while ( nTupleExists( ID ) || evtColExists( ID ) ) { ID = TupleID( ID.idAsString() + "_" ); }
163  }
164  // return
165  return evtCol( ID, title, clid );
166 }
167 // ============================================================================
168 
169 // ============================================================================
170 // get N-tuple object ( book on-demand ) with forced ID
171 // ============================================================================
172 template <class PBASE>
173 Tuples::Tuple GaudiTuples<PBASE>::nTuple( const TupleID& ID, const std::string& title1, const CLID& clid ) const {
174  // Check ID
175  if ( ID.undefined() ) {
176  this->Error( "Undefined NTuple ID : Title='" + title1 + "'" ).ignore();
177  return Tuple( 0 );
178  }
179 
180  // look up in the table
181  auto& m = nTupleByID();
182  auto tuple = m.find( ID );
183  if ( tuple != m.end() ) return Tuple( tuple->tuple ); // RETURN
184 
185  // convert ID to the string
186  const std::string tID = ID.idAsString();
187 
188  // adjust the NTuple title
189  const std::string title = title1.empty() ? ( "NTuple #" + tID ) : title1;
190 
191  // book new ntuple
192  if ( !produceNTuples() ) {
193  auto r = m_nTupleMap.insert( nTupleMapItem{title, ID, createNTuple( title, nullptr, clid )} );
194  if ( !r.second ) this->Error( "Failure to createNTuple" ).ignore();
195  return Tuple( r.first->tuple );
196  }
197  // book NTupel
198  NTuple::Tuple* tup = nullptr;
199  if ( ID.numeric() ) {
200  tup = this->ntupleSvc()->book( nTuplePath(), ID.numericID(), clid, title );
201  } else if ( ID.literal() ) {
202  tup = this->ntupleSvc()->book( nTuplePath(), ID.literalID(), clid, title );
203  } else {
204  this->Error( "Undefined NTuple ID" ).ignore();
205  }
206 
207  // assertion
208  this->Assert( tup, "Could not book the N-Tuple='" + title + "'" );
209  // some printout
210  if ( tup->registry() && this->msgLevel( MSG::DEBUG ) ) {
211  this->debug() << "Booked NTuple '" << title << "' ID=" << tID << "' Path='" << nTuplePath() << "' TS='"
212  << tup->registry()->identifier() << "'" << endmsg;
213  }
214 
215  auto r = m_nTupleMap.insert( nTupleMapItem{title, ID, createNTuple( title, tup, clid )} );
216  if ( !r.second ) this->Error( "Failure to createNTuple" ).ignore();
217  return Tuple( r.first->tuple );
218  //
219 }
220 // ============================================================================
221 template <class PBASE>
222 Tuples::Tuple GaudiTuples<PBASE>::evtCol( const TupleID& ID, const std::string& title1, const CLID& clid ) const {
223  // Check ID
224  if ( ID.undefined() ) {
225  this->Error( "Undefined NTuple ID : Title='" + title1 + "'" ).ignore();
226  return Tuple( 0 );
227  }
228 
229  // look up in the table
230  auto& tuple = evtColByID();
231  auto i = tuple.find( ID );
232  if ( i != tuple.end() ) return Tuple( i->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() ? ( "EvtCol #" + tID ) : title1;
239 
240  // book new ntuple
241  if ( !produceEvtCols() ) {
242  auto r = m_evtColMap.insert( nTupleMapItem{title, ID, createEvtCol( title, nullptr, clid )} );
243  if ( !r.second ) this->Error( "Failure to createEvtCol" ).ignore();
244  return Tuple( r.first->tuple );
245  }
246  // book NTuple
247  NTuple::Tuple* tup = nullptr;
248  if ( ID.numeric() ) {
249  tup = this->evtColSvc()->book( evtColPath(), ID.numericID(), clid, title );
250  } else if ( ID.literal() ) {
251  tup = this->evtColSvc()->book( evtColPath(), ID.literalID(), clid, title );
252  } else {
253  this->Error( "Undefined NTuple ID" ).ignore();
254  }
255 
256  // assertion
257  this->Assert( tup, "Could not book the EvtCol='" + title + "'" );
258  // some printout
259  if ( tup->registry() && this->msgLevel( MSG::DEBUG ) ) {
260  this->debug() << "Booked EvtCol '" << title << "' ID=" << tID << "' Path='" << evtColPath() << "' TS='"
261  << tup->registry()->identifier() << "'" << endmsg;
262  }
263  auto r = m_evtColMap.insert( nTupleMapItem{title, ID, createEvtCol( title, tup, clid )} );
264  if ( !r.second ) this->Error( "Failure to createEvtCol" ).ignore();
265  return Tuple( r.first->tuple );
266 }
267 // ============================================================================
268 // create TupleObj
269 // ============================================================================
270 template <class PBASE>
272  const CLID& clid ) const {
273  return Tuples::createTupleObj( this, "Tuple '" + name + "'", tuple, clid, Tuples::NTUPLE );
274 }
275 // ============================================================================
276 
277 // ============================================================================
278 // create TupleObj for event tag collection
279 // ============================================================================
280 template <class PBASE>
282  const CLID& clid ) const {
283  return Tuples::createTupleObj( this, "EvtCol '" + name + "'", tuple, clid, Tuples::EVTCOL );
284 }
285 // ============================================================================
286 // perform the actual printout of N-tuples
287 // ============================================================================
288 template <class PBASE>
290 
291  if ( m_nTupleMap.empty() ) {
292  if ( this->msgLevel( MSG::DEBUG ) ) this->debug() << "No N-Tuples are booked" << endmsg;
293  } else {
294  this->always() << "List of booked N-Tuples in directory "
295  << "\"" << nTuplePath() << "\"" << endmsg;
296  }
297 
298  for ( const auto& entry : this->nTupleOrdered() ) {
299  if ( !entry.tuple->tuple() ) {
300  this->error() << " NTuple::Tuple* points to NULL" << endmsg;
301  continue;
302  }
303  this->always() << GaudiAlg::PrintTuple::print( entry.tuple->tuple(), entry.id ) << endmsg;
304  }
305  //
306  return this->m_nTupleMap.size();
307 }
308 // ============================================================================
309 // perform the actual printout of Evt Tag Collections
310 // ============================================================================
311 template <class PBASE>
313  if ( m_evtColMap.empty() ) {
314  this->always() << "No Event Tag Collections are booked" << endmsg;
315  } else {
316  this->always() << "List of booked Event Tag Collections in directory "
317  << "\"" << evtColPath() << "\"" << endmsg;
318  }
319  for ( const auto& entry : this->evtColOrdered() ) {
320  if ( !entry.tuple->tuple() ) {
321  this->error() << " NTuple::Tuple* points to NULL" << endmsg;
322  continue;
323  }
324  this->always() << GaudiAlg::PrintTuple::print( entry.tuple->tuple(), entry.id )
325  << " Items:" << Gaudi::Utils::toString( entry.tuple->items() ) << endmsg;
326  }
327  //
328  return this->m_evtColMap.size();
329 }
330 // ============================================================================
331 // check the existence AND validity of the N-Tuple with the given ID
332 // ============================================================================
333 template <class PBASE>
334 bool GaudiTuples<PBASE>::nTupleExists( const TupleID& ID ) const {
335  auto& m = nTupleByID();
336  return m.find( ID ) != m.end();
337 }
338 // ============================================================================
339 // check the existence AND validity of the Event Tag Collection with the given ID
340 // ============================================================================
341 template <class PBASE>
342 bool GaudiTuples<PBASE>::evtColExists( const TupleID& ID ) const {
343  auto& m = evtColByID();
344  return m.find( ID ) != m.end();
345 }
346 // ============================================================================
347 // get the constructed N-Tuple path
348 // ============================================================================
349 template <class PBASE>
351  const std::string path = nTupleLUN() + "/" + nTupleTopDir() + nTupleDir();
352  return splitNTupleDir() ? dirHbookName( path ) : path;
353 }
354 // ============================================================================
355 // get the constructed Event Tag Collection path
356 // ============================================================================
357 template <class PBASE>
359  std::string path = evtColLUN() + "/" + evtColTopDir() + evtColDir();
360  return splitEvtColDir() ? dirHbookName( path ) : path;
361 }
362 // ============================================================================
363 // The END
364 // ============================================================================
static std::string print(const INTuple *tuple, const GaudiAlg::TupleID &ID)
Definition: Print.cpp:134
T empty(T...args)
bool nTupleExists(const TupleID &ID) const
check the existence AND validity of the N-Tuple with the given ID
Templated base class providing common &#39;ntupling&#39; methods.
Definition: GaudiTuples.h:43
Header file for class TupleObj.
NumericID numericID() const noexcept
Returns the numerical ID.
Definition: GaudiHistoID.h:72
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:334
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
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
Definition: StatusCode.h:130
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
STL class.
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: Tuple.h:116
long printTuples() const
perform the actual printout of N-tuples
virtual const id_type & identifier() const =0
Full identifier (or key)
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:50
constexpr double m
Definition: SystemOfUnits.h:92
Tuple nTuple(const std::string &title, const CLID &clid=CLID_ColumnWiseTuple) const
Access an N-Tuple object (book on-demand) with unique identifier.
bool literal() const noexcept
Is this ID numeric.
Definition: GaudiHistoID.h:66
GaudiAlg::ID TupleID
the actual type for N-Tuple identifier (HBOOK-style)
Definition: TupleID.h:22
collection of useful utilities to print certain objects (currently used for implementation in class G...
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
virtual std::unique_ptr< Tuples::TupleObj > createEvtCol(const std::string &name, NTuple::Tuple *tuple, const CLID &clid) const
create TupleObj for event tag collection
bool evtColExists(const TupleID &ID) const
check the existence AND validity of the Event Tag Collection with the given ID
Collection of few &#39;technical&#39; methods for instantiation of tuples.
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:375
auto 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:215
T insert(T...args)
std::string evtColPath() const
get the constructed Event Tag Collection path
STL class.
std::string nTuplePath() const
get the constructed N-Tuple path
const LiteralID & literalID() const noexcept
Returns the ID as a LiteralID.
Definition: GaudiHistoID.h:70
GAUDI_API LiteralID idAsString() const
Return ID as string, for both numeric and literal IDs.
bool numeric() const noexcept
Is this ID numeric.
Definition: GaudiHistoID.h:64
implementation of various functions for streaming.
bool undefined() const noexcept
Is this ID undefined.
Definition: GaudiHistoID.h:68
def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1')
Definition: TupleUtils.py:73
ID class for Histogram and Ntuples.
Definition: GaudiHistoID.h:43
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Header file for class : Tuple.
virtual std::unique_ptr< Tuples::TupleObj > createNTuple(const std::string &name, NTuple::Tuple *tuple, const CLID &clid) const
create TupleObj