The Gaudi Framework  master (37c0b60a)
RHistogramCnv.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef ROOTHISTCNV_RHISTOGRAMCNV_H
12 #define ROOTHISTCNV_RHISTOGRAMCNV_H 1
13 
14 // Include files
15 #include "RConverter.h"
16 #include "RootObjAddress.h"
17 #include <GaudiKernel/DataObject.h>
20 #include <GaudiKernel/IRegistry.h>
22 #include <GaudiKernel/SmartIF.h>
23 #include <TArray.h>
24 #include <memory>
25 
26 namespace RootHistCnv {
27 
40  template <typename T, typename S, typename Q>
41  class RHistogramCnv : public RConverter {
42  template <typename CLASS>
43  class TTH {
44  public:
45  template <typename INPUT>
46  TTH( INPUT* i ) : m_c( dynamic_cast<CLASS*>( i ) ) {}
47  template <typename INPUT>
48  bool CopyH( INPUT& i ) {
49  if ( m_c ) { m_c->Copy( i ); }
50  return m_c != nullptr;
51  }
52 
53  private:
54  CLASS* m_c = nullptr;
55  };
56 
57  public:
59  StatusCode createObj( IOpaqueAddress* pAddr, DataObject*& refpObj ) override {
60  refpObj = DataObjFactory::create( objType() ).release();
61  RootObjAddress* r = dynamic_cast<RootObjAddress*>( pAddr );
62  Q* h = dynamic_cast<Q*>( refpObj );
63  if ( r && h ) {
64  // Need to flip representation .... clumsy for the time being, because
65  // THXY constructor has no "generic" copy constructor
66  auto s = dynamic_cast<S*>( r->tObj() );
67  if ( s ) {
68  auto a = dynamic_cast<TArray*>( s );
69  if ( a ) {
70  auto p = std::make_unique<T>();
71  auto ok = TTH<S>( s ).CopyH( *p );
72  if ( ok ) {
73  p->Set( a->GetSize() );
74  p->Reset();
75  p->Add( s );
76  h->adoptRepresentation( p.release() );
77  return StatusCode::SUCCESS;
78  }
79  }
80  }
81  }
82  return error( "Cannot create histogram - invalid address." );
83  }
85  StatusCode updateObj( IOpaqueAddress* /* pAddr */, DataObject* /* pObj */ ) override { return StatusCode::SUCCESS; }
87  TObject* createPersistent( DataObject* pObj ) override {
88  auto h = dynamic_cast<Q*>( pObj );
89  if ( h ) {
90  auto r = dynamic_cast<T*>( h->representation() );
91  if ( r ) {
92  auto a = dynamic_cast<TArray*>( r );
93  if ( a ) {
94  auto c = std::make_unique<T>();
95  auto ok = TTH<S>( r ).CopyH( *c );
96  if ( ok ) {
97  c->Set( a->GetSize() );
98  c->Reset();
99  c->Add( r );
100  c->SetName( pObj->registry()->name().c_str() + 1 );
101  return c.release();
102  }
103  }
104  }
105  }
106  error( "Histogram object is invalid!" ).ignore();
107  return nullptr;
108  }
110  static const CLID& classID();
113  };
114 } // namespace RootHistCnv
115 #endif // ROOTHISTCNV_RHISTOGRAMCNV_H
RConverter.h
RootHistCnv::RHistogramCnv::updateObj
StatusCode updateObj(IOpaqueAddress *, DataObject *) override
Update the transient object from the other representation.
Definition: RHistogramCnv.h:85
RootHistCnv::RootObjAddress::tObj
virtual TObject * tObj() const
Retrieve TObject* ptr.
Definition: RootObjAddress.h:91
RootHistCnv::RHistogramCnv::TTH::m_c
CLASS * m_c
Definition: RHistogramCnv.h:54
gaudirun.s
string s
Definition: gaudirun.py:346
IOpaqueAddress
Definition: IOpaqueAddress.h:33
DataObject::release
virtual unsigned long release()
release reference to object
Definition: DataObject.cpp:56
ISvcLocator
Definition: ISvcLocator.h:46
gaudirun.c
c
Definition: gaudirun.py:525
ObjectFactory.h
RootHistCnv::RHistogramCnv::classID
static const CLID & classID()
Inquire class type.
Definition: RHistogramCnv.cpp:18
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:21
RootHistCnv::RHistogramCnv::TTH::TTH
TTH(INPUT *i)
Definition: RHistogramCnv.h:46
SmartIF.h
RootHistCnv::RHistogramCnv
Definition: RHistogramCnv.h:41
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
RootHistCnv
Definition: DirectoryCnv.h:27
StatusCode
Definition: StatusCode.h:65
IOpaqueAddress.h
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:31
HistogramBase.h
RootHistCnv::RConverter
Definition: RConverter.h:41
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
Converter::objType
const CLID & objType() const override
Retrieve the class type of objects the converter produces.
Definition: Converter.cpp:23
RootObjAddress.h
IRegistry.h
RootHistCnv::RHistogramCnv::TTH::CopyH
bool CopyH(INPUT &i)
Definition: RHistogramCnv.h:48
RootHistCnv::RConverter::error
StatusCode error(const std::string &msg)
Definition: RConverter.cpp:339
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataObject.h
DataObject
Definition: DataObject.h:36
RootHistCnv::RHistogramCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RHistogramCnv.h:59
RootHistCnv::RHistogramCnv::TTH
Definition: RHistogramCnv.h:43
RootHistCnv::RHistogramCnv::RHistogramCnv
RHistogramCnv(ISvcLocator *svc)
Standard constructor.
Definition: RHistogramCnv.h:112
RootHistCnv::RHistogramCnv::createPersistent
TObject * createPersistent(DataObject *pObj) override
Create the persistent representation of the histogram object.
Definition: RHistogramCnv.h:87
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78