The Gaudi Framework  master (01b473db)
RHistogramCnv.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 // Include files
14 #include "RConverter.h"
15 #include "RootObjAddress.h"
16 #include <GaudiKernel/DataObject.h>
19 #include <GaudiKernel/IRegistry.h>
21 #include <GaudiKernel/SmartIF.h>
22 #include <TArray.h>
23 #include <memory>
24 
25 namespace RootHistCnv {
26 
39  template <typename T, typename S, typename Q>
40  class RHistogramCnv : public RConverter {
41  template <typename CLASS>
42  class TTH {
43  public:
44  template <typename INPUT>
45  TTH( INPUT* i ) : m_c( dynamic_cast<CLASS*>( i ) ) {}
46  template <typename INPUT>
47  bool CopyH( INPUT& i ) {
48  if ( m_c ) { m_c->Copy( i ); }
49  return m_c != nullptr;
50  }
51 
52  private:
53  CLASS* m_c = nullptr;
54  };
55 
56  public:
58  StatusCode createObj( IOpaqueAddress* pAddr, DataObject*& refpObj ) override {
59  refpObj = DataObjFactory::create( objType() ).release();
60  RootObjAddress* r = dynamic_cast<RootObjAddress*>( pAddr );
61  Q* h = dynamic_cast<Q*>( refpObj );
62  if ( r && h ) {
63  // Need to flip representation .... clumsy for the time being, because
64  // THXY constructor has no "generic" copy constructor
65  auto s = dynamic_cast<S*>( r->tObj() );
66  if ( s ) {
67  auto a = dynamic_cast<TArray*>( s );
68  if ( a ) {
69  auto p = std::make_unique<T>();
70  auto ok = TTH<S>( s ).CopyH( *p );
71  if ( ok ) {
72  p->Set( a->GetSize() );
73  p->Reset();
74  p->Add( s );
75  h->adoptRepresentation( p.release() );
76  return StatusCode::SUCCESS;
77  }
78  }
79  }
80  }
81  return error( "Cannot create histogram - invalid address." );
82  }
84  StatusCode updateObj( IOpaqueAddress* /* pAddr */, DataObject* /* pObj */ ) override { return StatusCode::SUCCESS; }
86  TObject* createPersistent( DataObject* pObj ) override {
87  auto h = dynamic_cast<Q*>( pObj );
88  if ( h ) {
89  auto r = dynamic_cast<T*>( h->representation() );
90  if ( r ) {
91  auto a = dynamic_cast<TArray*>( r );
92  if ( a ) {
93  auto c = std::make_unique<T>();
94  auto ok = TTH<S>( r ).CopyH( *c );
95  if ( ok ) {
96  c->Set( a->GetSize() );
97  c->Reset();
98  c->Add( r );
99  c->SetName( pObj->registry()->name().c_str() + 1 );
100  return c.release();
101  }
102  }
103  }
104  }
105  error( "Histogram object is invalid!" ).ignore();
106  return nullptr;
107  }
109  static const CLID& classID();
112  };
113 } // namespace RootHistCnv
RConverter.h
RootHistCnv::RHistogramCnv::updateObj
StatusCode updateObj(IOpaqueAddress *, DataObject *) override
Update the transient object from the other representation.
Definition: RHistogramCnv.h:84
RootHistCnv::RootObjAddress::tObj
virtual TObject * tObj() const
Retrieve TObject* ptr.
Definition: RootObjAddress.h:90
RootHistCnv::RHistogramCnv::TTH::m_c
CLASS * m_c
Definition: RHistogramCnv.h:53
gaudirun.s
string s
Definition: gaudirun.py:346
IOpaqueAddress
Definition: IOpaqueAddress.h:28
DataObject::release
virtual unsigned long release()
release reference to object
Definition: DataObject.cpp:56
ISvcLocator
Definition: ISvcLocator.h:42
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:20
RootHistCnv::RHistogramCnv::TTH::TTH
TTH(INPUT *i)
Definition: RHistogramCnv.h:45
SmartIF.h
RootHistCnv::RHistogramCnv
Definition: RHistogramCnv.h:40
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
RootHistCnv
Definition: DirectoryCnv.h:26
StatusCode
Definition: StatusCode.h:64
IOpaqueAddress.h
AlgSequencer.h
h
Definition: AlgSequencer.py:31
HistogramBase.h
RootHistCnv::RConverter
Definition: RConverter.h:40
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:16
Converter::objType
const CLID & objType() const override
Retrieve the class type of objects the converter produces.
Definition: Converter.cpp:22
RootObjAddress.h
IRegistry.h
RootHistCnv::RHistogramCnv::TTH::CopyH
bool CopyH(INPUT &i)
Definition: RHistogramCnv.h:47
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:99
DataObject.h
DataObject
Definition: DataObject.h:37
RootHistCnv::RHistogramCnv::createObj
StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&refpObj) override
Create the transient representation of an object.
Definition: RHistogramCnv.h:58
RootHistCnv::RHistogramCnv::TTH
Definition: RHistogramCnv.h:42
RootHistCnv::RHistogramCnv::RHistogramCnv
RHistogramCnv(ISvcLocator *svc)
Standard constructor.
Definition: RHistogramCnv.h:111
RootHistCnv::RHistogramCnv::createPersistent
TObject * createPersistent(DataObject *pObj) override
Create the persistent representation of the histogram object.
Definition: RHistogramCnv.h:86
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:79