All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
CollectionCloneAlg.cpp
Go to the documentation of this file.
1 // ====================================================================
2 // CollectionCloneAlg.cpp
3 // --------------------------------------------------------------------
4 //
5 // Author : Markus Frank
6 //
7 // ====================================================================
8 #include "GaudiKernel/IDataProviderSvc.h"
9 #include "GaudiKernel/ISelectStatement.h"
10 #include "GaudiKernel/IDataManagerSvc.h"
11 #include "GaudiKernel/GenericAddress.h"
12 #include "GaudiKernel/SmartDataPtr.h"
13 #include "GaudiKernel/AttribStringParser.h"
14 #include "GaudiKernel/IRegistry.h"
15 #include "GaudiKernel/Algorithm.h"
16 #include "GaudiKernel/MsgStream.h"
17 #include "GaudiKernel/SmartIF.h"
18 #include "GaudiKernel/NTuple.h"
19 #include "GaudiKernel/ObjectFactory.h"
20 #include <vector>
21 
22 namespace {
23  template <class T> static long upper(const INTupleItem* item) {
24  const NTuple::_Data<T>* it = dynamic_cast<const NTuple::_Data<T>*>(item);
25  return long(it->range().upper());
26  }
27 
28  template<class TYP> static
29  StatusCode createItem (MsgStream& log,
30  INTuple* tuple,
31  INTupleItem* src,
32  const TYP& null)
33  {
34  NTuple::_Data<TYP>* source = dynamic_cast<NTuple::_Data<TYP>*>(src);
35  TYP low = source->range().lower();
36  TYP high = source->range().upper();
37  long hasIdx = source->hasIndex();
38  long ndim = source->ndim();
39  const std::string& name = source->name();
40  std::string idxName;
41  long dim[4], idxLen = 0;
42  long dim1 = 1, dim2 = 1;
43  INTupleItem* it = 0;
44  for ( int i = 0; i < ndim; i++ )
45  dim[i] = source->dim(i);
47  if ( hasIdx ) {
48  const INTupleItem* index = source->indexItem();
49  idxName = index->name();
50  switch( index->type() ) {
52  idxLen = upper<unsigned char>(index);
53  break;
55  idxLen = upper<unsigned short>(index);
56  break;
57  case DataTypeInfo::UINT:
58  idxLen = upper<unsigned int>(index);
59  break;
61  idxLen = upper<unsigned long>(index);
62  break;
63  case DataTypeInfo::CHAR:
64  idxLen = upper<char>(index);
65  break;
67  idxLen = upper<short>(index);
68  break;
69  case DataTypeInfo::INT:
70  idxLen = upper<int>(index);
71  break;
72  case DataTypeInfo::LONG:
73  idxLen = upper<long>(index);
74  break;
75  default:
76  log << MSG::ERROR << "Column " << idxName
77  << " is not a valid index column!" << endmsg;
78  return StatusCode::FAILURE;
79  }
80  }
81  switch( ndim ) {
82  case 0:
83  it = NTuple::_Item<TYP>::create (tuple, name, typeid(TYP), low, high, null);
84  break;
85  case 1:
86  dim1 = (hasIdx) ? idxLen : dim[0];
87  it = NTuple::_Array<TYP>::create (tuple,
88  name,
89  typeid(TYP),
90  idxName,
91  dim1,
92  low,
93  high,
94  null);
95  break;
96  case 2:
97  dim1 = (hasIdx) ? idxLen : dim[0];
98  dim2 = (hasIdx) ? dim[0] : dim[1];
99  it = NTuple::_Matrix<TYP>::create ( tuple,
100  name,
101  typeid(TYP),
102  idxName,
103  dim1,
104  dim2,
105  low,
106  high,
107  null);
108  break;
109  default:
110  return StatusCode::FAILURE;
111  }
112  return tuple->add(it);
113  }
114 }
115 
130  std::string m_tupleSvc;
132  std::string m_output;
134  std::vector<std::string> m_inputs;
136  std::string m_rootName;
138  std::string m_outName;
140  std::string m_criteria;
142  std::string m_selectorName;
143 public:
144 
146  CollectionCloneAlg(const std::string& name, ISvcLocator* pSvcLocator)
147  : Algorithm(name, pSvcLocator)
148  {
149  declareProperty("EvtTupleSvc", m_tupleSvc="EvtTupleSvc");
150  declareProperty("Input", m_inputs);
151  declareProperty("Output", m_output);
152  }
155  }
156 
158  virtual StatusCode initialize() {
159  MsgStream log(msgSvc(), name());
160  m_rootName = "";
161  m_outName = "";
162  m_criteria = "";
163  m_selectorName = "";
164  m_dataSvc = service(m_tupleSvc, true);
165  if ( !m_dataSvc ) {
166  log << MSG::ERROR << "Failed to access service \""
167  << m_tupleSvc << "\"." << endmsg;
168  return StatusCode::FAILURE;
169  }
170  std::string fun;
171  using Parser = Gaudi::Utils::AttribStringParser;
172  for (auto attrib: Parser(m_output)) {
173  switch( ::toupper(attrib.tag[0]) ) {
174  case 'D':
175  m_outName = std::move(attrib.value);
176  break;
177  case 'S':
178  m_criteria = std::move(attrib.value);
179  break;
180  case 'F':
181  fun = std::move(attrib.value);
182  break ;
183  default:
184  break;
185  }
186  }
187  if ( m_outName.empty() ) {
188  log << MSG::ERROR << "Failed to analyze output specs:" << m_output << endmsg;
189  return StatusCode::FAILURE;
190  }
191  if ( !fun.empty() || !m_criteria.empty() ) {
192  if ( !m_criteria.empty() && fun.empty() ) fun = "NTuple::Selector";
193  m_selectorName = fun;
194  }
195  return StatusCode::SUCCESS;
196  }
197 
199  virtual StatusCode finalize() {
200  m_dataSvc.reset();
201  return StatusCode::SUCCESS;
202  }
203 
205  virtual StatusCode execute() {
206  StatusCode status = connect();
207  if ( status.isSuccess() ) {
208  status = mergeInputTuples();
209  }
210  return status;
211  }
212 
214  virtual StatusCode book(const NTuple::Tuple* nt) {
215  MsgStream log(msgSvc(), name());
216  const INTuple::ItemContainer& items = nt->items();
218  INTuple::ItemContainer::const_iterator i;
219  NTuple::Tuple* tuple = m_dataSvc->book(m_outName, nt->clID(), nt->title());
220  for (i = items.begin(); i != items.end(); ++i) {
221  long type = (*i)->type();
222  switch(type) {
223  case DataTypeInfo::UCHAR:
224  status = createItem(log, tuple, *i, (unsigned char)0);
225  break;
227  status = createItem(log, tuple, *i, (unsigned short)0);
228  break;
229  case DataTypeInfo::UINT:
230  status = createItem(log, tuple, *i, (unsigned int)0);
231  break;
232  case DataTypeInfo::ULONG:
233  status = createItem(log, tuple, *i, (unsigned long)0);
234  break;
235  case DataTypeInfo::CHAR:
236  status = createItem(log, tuple, *i, char(0));
237  break;
238  case DataTypeInfo::SHORT:
239  status = createItem(log, tuple, *i, short(0));
240  break;
241  case DataTypeInfo::INT:
242  status = createItem(log, tuple, *i, int(0));
243  break;
244  case DataTypeInfo::LONG:
245  status = createItem(log, tuple, *i, long(0));
246  break;
247  case DataTypeInfo::BOOL:
248  status = createItem(log, tuple, *i, false);
249  break;
250  case DataTypeInfo::FLOAT:
251  status = createItem(log, tuple, *i, float(0.0));
252  break;
254  status = createItem(log, tuple, *i, double(0.0));
255  break;
257  status = createItem(log, tuple, *i, (IOpaqueAddress*)0);
258  break;
260  status = createItem(log, tuple, *i, (void*)0);
261  break;
263 // status = createItem(log, tuple, *i, (std::string*)0);
264 // break;
266 // status = createItem(log, tuple, *i, (char*)0);
267 // break;
269  default:
270  status = StatusCode::FAILURE;
271  break;
272  }
273  }
274  return status;
275  }
276 
277  // Perform some basic checks
278  virtual StatusCode checkInput(const NTuple::Tuple* clone, const NTuple::Tuple* src) {
279  MsgStream log(msgSvc(), name());
280  if ( 0 != clone && 0 != src ) {
281  const INTuple::ItemContainer& clone_items = clone->items();
282  const std::string clone_id = clone->registry()->identifier();
283  const std::string src_id = src->registry()->identifier();
284 
285  INTuple::ItemContainer::const_iterator i;
286  log << MSG::ERROR;
287  for (i = clone_items.begin(); i != clone_items.end(); ++i) {
288  const INTupleItem* itm = *i;
289  const std::string& nam = itm->name();
290  const INTupleItem* src_itm = src->find(nam);
291  if ( !src_itm ) {
292  log << "Tuple item " << nam << " not present in " << src_id << endmsg;
293  return StatusCode::FAILURE;
294  }
295  if ( itm->type() != src_itm->type() ) {
296  log << "Tuple item " << nam << " are of different types in "
297  << src_id << ":" << src_itm->typeName() << " <-> "
298  << clone_id << ":" << itm->typeName() << endmsg;
299  return StatusCode::FAILURE;
300  }
301  if ( itm->ndim() != src_itm->ndim() ) {
302  log << "Tuple item " << nam << " have different dimensions in "
303  << src_id << ":" << src_itm->ndim() << " <-> "
304  << clone_id << ":" << itm->ndim() << endmsg;
305  return StatusCode::FAILURE;
306  }
307  for (int j=0; j<itm->ndim(); ++j) {
308  if ( src_itm->dim(j) != itm->dim(j) ) {
309  log << "Tuple item " << nam << " have different dimensions in "
310  << src_id << "[" << j << "]:" << src_itm->dim(j) << " <-> "
311  << clone_id << "[" << j << "]:" << itm->dim(j) << endmsg;
312  return StatusCode::FAILURE;
313  }
314  }
315  if ( itm->hasIndex() != src_itm->hasIndex() ) {
316  log << "Tuple item " << nam << " has different index colums "
317  << src_id << ":" << src_itm->hasIndex() << " <-> "
318  << clone_id << ":" << itm->hasIndex() << endmsg;
319  return StatusCode::FAILURE;
320  }
321  if ( itm->hasIndex() ) {
322  if ( itm->index() != src_itm->index() ) {
323  log << "Tuple item " << nam << " has different index colums "
324  << src_id << ":" << src_itm->index() << " <-> "
325  << clone_id << ":" << itm->index() << endmsg;
326  return StatusCode::FAILURE;
327  }
328  }
329  }
330  return StatusCode::SUCCESS;
331  }
332  return StatusCode::FAILURE;
333  }
334 
336  StatusCode mergeEntries(const std::string& input) {
337  MsgStream log(msgSvc(), name());
338  NTuplePtr out(m_dataSvc.get(), m_outName);
339  if ( 0 != out ) {
340  const INTuple::ItemContainer& clone_items = out->items();
341  std::vector<GenericAddress> addrVector(clone_items.size());
343  NTuplePtr nt(m_dataSvc.get(), input);
344  size_t k = 0, nentry = 0;
345  if ( 0 != nt ) {
346  const INTuple::ItemContainer& source_items = nt->items();
347  for (k=0; k < source_items.size(); ++k ) {
348  if ( source_items[k]->type() == DataTypeInfo::OBJECT_ADDR ) {
349  *(IOpaqueAddress**)source_items[k]->buffer() = &addrVector[k];
350  }
351  }
352  while ( status.isSuccess() ) {
353  status = m_dataSvc->readRecord(nt.ptr());
354  if ( status.isSuccess() ) {
355  INTuple::ItemContainer::const_iterator i;
356  nentry++;
357  for (k=0,i = source_items.begin(); i != source_items.end(); ++i,++k) {
358  const INTupleItem* src_itm = *i;
359  const INTupleItem* out_itm = out->find(src_itm->name());
360  size_t size = 0;
361  switch((*i)->type()) {
362  case DataTypeInfo::UCHAR:
363  size = sizeof(unsigned char);
364  break;
366  size = sizeof(unsigned short);
367  break;
368  case DataTypeInfo::UINT:
369  size = sizeof(unsigned int);
370  break;
371  case DataTypeInfo::ULONG:
372  size = sizeof(unsigned long);
373  break;
374  case DataTypeInfo::CHAR:
375  size = sizeof(char);
376  break;
377  case DataTypeInfo::SHORT:
378  size = sizeof(short);
379  break;
380  case DataTypeInfo::INT:
381  size = sizeof(int);
382  break;
383  case DataTypeInfo::LONG:
384  size = sizeof(long);
385  break;
386  case DataTypeInfo::BOOL:
387  size = sizeof(bool);
388  break;
389  case DataTypeInfo::FLOAT:
390  size = sizeof(float);
391  break;
393  size = sizeof(double);
394  break;
396  *(std::string*)out_itm->buffer() = *(std::string*)src_itm->buffer();
397  size = 0;
398  break;
400  size = ::strlen((const char*)src_itm->buffer())+1;
401  break;
403  {
404  *(void**)out_itm->buffer() = *(void**)src_itm->buffer();
405  size = 0;
406  }
407  break;
409  {
410  IOpaqueAddress* ppA1 = &addrVector[k];
411  IOpaqueAddress** ppA2 = (IOpaqueAddress**)out_itm->buffer();
412  *ppA2 = ppA1;
413  size = 0;
414  }
415  break;
417  default:
418  size = 0;
419  break;
420  }
421  if ( size > 0 ) {
422  ::memcpy((void*)out_itm->buffer(), src_itm->buffer(), size*src_itm->length());
423  }
424  }
425  status = m_dataSvc->writeRecord(out.ptr());
426  if ( !status.isSuccess() ) {
427  log << MSG::ERROR << "Failed to write record " << nentry
428  << " from " << input << " to " << m_outName << endmsg;
429  }
430  }
431  }
432  log << MSG::INFO << "End of reading tuple " << input
433  << " after " << nentry << " entries." << endmsg;
434 
435  if ( nentry > 0 || m_selectorName != "" ) {
436  return StatusCode::SUCCESS;
437  }
438  return StatusCode::FAILURE;
439  }
440  log << MSG::ERROR << "Failed to access input: " << input << endmsg;
441  }
442  return StatusCode::FAILURE;
443  }
444 
448  for (size_t i=0; i < m_inputs.size(); ++i) {
449  NTuplePtr nt(m_dataSvc.get(), m_inputs[i]);
450  if ( !(0 == nt) ) {
451  NTuplePtr out(m_dataSvc.get(), m_outName);
452  if ( 0 == out ) {
453  status = book(nt);
454  } else {
455  status = checkInput(out, nt);
456  }
457  if ( !status.isSuccess() ) {
458  return status;
459  }
460  else if ( m_selectorName != "" ) {
461  SmartIF<ISelectStatement> stmt(ObjFactory::create(m_selectorName, serviceLocator()));
462  if ( stmt ) {
463  if ( !m_criteria.empty() ) stmt->setCriteria(m_criteria);
464  nt->attachSelector(stmt);
465  } else {
466  MsgStream log(msgSvc(), name());
467  log << MSG::ERROR << "Failed to attach tuple selector to " << m_inputs[i] << endmsg;
468  return StatusCode::FAILURE;
469  }
470  }
471  } else {
472  MsgStream log(msgSvc(), name());
473  log << MSG::ERROR << "Failed to access tuple: " << m_inputs[i] << endmsg;
474  return StatusCode::FAILURE;
475  }
476  }
477  return StatusCode::SUCCESS;
478  }
479 
482  MsgStream log(msgSvc(), name());
483  for( const auto& input : m_inputs ) {
484  StatusCode sc = mergeEntries(input);
485  if ( !sc.isSuccess() ) {
486  log << MSG::ERROR << "Failed to merge tuple:" << input << endmsg;
487  return sc;
488  }
489  }
490  return StatusCode::SUCCESS;
491  }
492 };
std::string m_tupleSvc
Name of the data provider service.
std::string m_criteria
Selection criteria (if any)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual long type() const =0
Type information of the item.
SmartIF< ISvcLocator > & serviceLocator() const
The standard service locator.
Definition: Algorithm.cpp:1045
tuple itm
Definition: ana.py:57
virtual StatusCode book(const NTuple::Tuple *nt)
Book the N-tuple according to the specification.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
CollectionCloneAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard algorithm constructor.
virtual StatusCode add(INTupleItem *item)=0
Add an item row to the N tuple.
std::vector< INTupleItem * > ItemContainer
Definition: INTuple.h:85
virtual const std::string & title() const =0
Object title.
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Algorithm.h:435
virtual const ItemRange & range() const =0
Access the range if specified.
virtual ~CollectionCloneAlg()
Standard Destructor.
const std::string & type() const override
The type of the algorithm object.
Definition: Algorithm.h:167
virtual bool hasIndex() const =0
Is the tuple have an index item?
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
static _Array * create(INTuple *tup, const std::string &name, const std::type_info &info, const std::string &index, long len, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:32
virtual const std::string & name() const =0
Access _Item name.
SmartIF< INTupleSvc > m_dataSvc
Reference to data provider service.
StatusCode mergeEntries(const std::string &input)
Merge the entries of a single input tuple into the output.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:919
NTuple interface class definition.
Definition: INTuple.h:79
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
std::string m_output
Output specification.
std::vector< std::string > m_inputs
input specifications
NTuple interface class definition.
Definition: INTuple.h:26
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:68
virtual INTupleItem * indexItem()=0
Pointer to index column (if present, 0 else)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual StatusCode writeRecord(NTuple::Tuple *tuple)=0
Write single record to N tuple.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::string m_outName
Output tuple name.
StatusCode mergeInputTuples()
Merge all N-tuple entries.
virtual long dim(long i) const =0
Access individual dimensions.
Abstract class describing basic data in an Ntuple.
Definition: NTuple.h:37
virtual StatusCode checkInput(const NTuple::Tuple *clone, const NTuple::Tuple *src)
std::string m_rootName
Name of the root leaf (obtained at initialize)
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:370
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:77
virtual StatusCode initialize()
Initialize.
virtual long ndim() const =0
Dimension.
StatusCode connect()
Connect input and output N-tuples.
virtual StatusCode execute()
Execute procedure.
virtual StatusCode finalize()
Finalize.
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
Small algorithm, which allows to merge N-tuples in a generic way.
virtual StatusCode readRecord(NTuple::Tuple *tuple)=0
Read single record from N tuple.
tuple item
print s1,s2
Definition: ana.py:146
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:46
virtual const id_type & identifier() const =0
Full identifier (or key)
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
virtual ItemContainer & items()=0
Access item container.
static _Item * create(INTuple *tup, const std::string &name, const std::type_info &info, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:26
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:231
virtual std::string typeName() const =0
Proper type name of the object.
Opaque address interface definition.
std::string m_selectorName
Selector factory.
virtual void setCriteria(const std::string &crit)=0
Set the type.
list i
Definition: ana.py:128
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: Algorithm.cpp:1001
void toupper(std::string &s)
virtual const INTupleItem * find(const std::string &name) const =0
Find an item row of the Ntuple (CONST)
virtual NTuple::Tuple * book(const std::string &fullPath, const CLID &type, const std::string &title)=0
Book Ntuple and register it with the data store.
virtual const std::string & index() const =0
Access the index _Item.
static _Matrix * create(INTuple *tup, const std::string &name, const std::type_info &info, const std::string &index, long ncol, long nrow, TYP min, TYP max, TYP def)
Create instance.
Definition: NTupleItems.cpp:38
virtual long length() const =0
Access the buffer length.
virtual const void * buffer() const =0
Access data buffer (CONST)