HistogramPersistencySvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // HistogramPersistencySvc.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : System ( The LHCb Offline System)
6 //
7 // Description: implementation of the Event data persistency service
8 // This specialized service only deals with event related
9 // data
10 //
11 // Author : M.Frank
12 // History :
13 // +---------+----------------------------------------------+---------
14 // | Date | Comment | Who
15 // +---------+----------------------------------------------+---------
16 // | 29/10/98| Initial version | MF
17 // +---------+----------------------------------------------+---------
18 //
19 //====================================================================
20 #define PERSISTENCYSVC_HISTOGRAMPERSISTENCYSVC_CPP
21 // ============================================================================
22 // Include files
23 // ============================================================================
24 // GaudiKernel
25 // ============================================================================
26 #include "GaudiKernel/SmartIF.h"
27 #include "GaudiKernel/MsgStream.h"
30 #include "GaudiKernel/DataObject.h"
31 #include "GaudiKernel/IRegistry.h"
33 // ============================================================================
34 // local
35 // ============================================================================
37 // ============================================================================
38 // AIDA
39 // ============================================================================
41 #ifdef __clang__
42 #pragma clang diagnostic push
43 #pragma clang diagnostic ignored "-Wkeyword-macro"
44 #endif
45 #define class class GAUDI_API
46 #ifdef __clang__
47 #pragma clang diagnostic pop
48 #endif
49 #include "AIDA/IBaseHistogram.h"
50 #undef class
51 // ============================================================================
52 // Instantiation of a static factory class used by clients to create
53 // instances of this service
55 
56 // ============================================================================
57 // Finalize the service.
59 {
60  //
61  if ( !(m_convert.empty() && m_exclude.empty()) )
62  { // print message if any of the two properties is used
63  info() << "Histograms Converted/Excluded: "
64  << m_converted.size() << "/" << m_excluded.size() << endmsg ;
65  }
66  if (msgLevel(MSG::DEBUG)) {
67  if ( !m_excluded.empty() )
68  {
69  auto& log = debug();
70  log << "Excluded Histos : #" << m_excluded.size() ;
71  for ( const auto& item : m_excluded )
72  { log << std::endl << " '" << item << "'" ; }
73  log << endmsg ;
74  }
75  //
76  if ( !m_converted.empty() )
77  {
78  auto& log = debug();
79  log << "Converted Histos : #" << m_converted.size() ;
80  for ( const auto& item : m_converted )
81  { log << std::endl << " '" << item << "'" ; }
82  log << endmsg ;
83  }
84  }
85  return PersistencySvc::finalize();
86 }
87 // ============================================================================
88 // Initialize the service.
89 // ============================================================================
92  return status.isSuccess() ? reinitialize() : status;
93 }
94 // ============================================================================
95 // Reinitialize the service.
96 // ============================================================================
98 {
99  // Obtain the IProperty of the ApplicationMgr
100  auto prpMgr = serviceLocator()->as<IProperty>();
101  if ( !prpMgr ) {
102  fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
103  return StatusCode::FAILURE;
104  }
105  else {
106  setProperty(prpMgr->getProperty("HistogramPersistency")).ignore();
107  }
108 
109  // To keep backward compatibility, we set the property of conversion service
110  // into JobOptions catalogue
111  if( !m_outputFile.empty() ) {
112  auto joptsvc = serviceLocator()->service<IJobOptionsSvc> ("JobOptionsSvc");
113  if( joptsvc ) {
114  StringProperty p("OutputFile", m_outputFile);
115  if ( m_histPersName == "ROOT" ) {
116  joptsvc->addPropertyToCatalogue("RootHistSvc", p).ignore();
117  } else if (m_histPersName == "HBOOK" ) {
118  joptsvc->addPropertyToCatalogue("HbookHistSvc", p).ignore();
119  }
120  }
121  }
122 
123  // Load the Histogram persistency service that's required as default
124  setConversionSvc(nullptr).ignore();
125  if ( m_histPersName == "ROOT" ) {
126  setConversionSvc(service("RootHistSvc")).ignore();
127  if ( !conversionSvc() ) {
128  return StatusCode::FAILURE;
129  }
130  enable(true);
131  }
132  else if ( m_histPersName == "HBOOK" ) {
133  setConversionSvc(service("HbookHistSvc")).ignore();
134  if ( !conversionSvc() ) {
135  return StatusCode::FAILURE;
136  }
137  enable(true);
138  }
139  else if ( m_histPersName == "NONE" ) {
140  enable(false);
141  if ( m_warnings ) {
142  warning() << "Histograms saving not required." << endmsg;
143  }
144  }
145  else {
147  if ( !conversionSvc() ) {
148  return StatusCode::FAILURE;
149  }
150  enable(true);
151  if ( m_warnings ) {
152  warning() << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
153  }
154  }
155  return StatusCode::SUCCESS;
156 }
157 // ============================================================================
158 namespace
159 {
160  // ==========================================================================
162  const std::string s_NULL = "<NULL>" ;
163  // ==========================================================================
169  // ==========================================================================
170  inline bool match
171  ( const std::string& name ,
172  const std::string& pat )
173  {
174  // the most primitive match
175  return std::string::npos != name.find ( pat );
176  }
177  // ==========================================================================
182  inline const std::string& oname ( const DataObject* obj )
183  {
184  if ( !obj ) { return s_NULL ; }
185  auto reg = obj->registry() ;
186  return reg ? reg -> identifier() : obj -> name () ;
187  }
188  // ==========================================================================
194  inline bool match ( const DataObject* obj ,
195  const std::string& pat )
196  {
197  return obj && match ( oname ( obj ) , pat ) ;
198  }
199  // ==========================================================================
200 }
201 // ============================================================================
202 // Convert the transient object to the requested representation.
203 // ============================================================================
205 ( DataObject* pObj ,
206  IOpaqueAddress*& refpAddr )
207 {
208  // enable the conversion
209  enable ( true ) ;
210  // conversion is possible ?
211  if ( "NONE" == m_histPersName )
212  {
213  enable ( false ) ;
214  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
215  }
216  // histogram ?
217  if ( dynamic_cast<AIDA::IBaseHistogram*> ( pObj ) )
218  {
219 
220  auto match_pObj = [&](const std::string& s) { return match( pObj , s ); };
221  // Empty ConvertHistos property means convert all
222  bool select = ( m_convert.empty()
223  || std::any_of( m_convert.begin(), m_convert.end(), match_pObj )
224  ) && std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
225  //
226  enable ( select ) ;
227  //
228  const auto& path = oname ( pObj ) ;
229  //
230  if ( !select ) { m_excluded.insert ( path ) ; }
231  else { m_converted.insert ( path ) ; }
232  }
233  //
234  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
235 }
236 // ============================================================================
237 // Standard Constructor
238 // ============================================================================
240 ( const std::string& name ,
241  ISvcLocator* svc )
242  : PersistencySvc(name, svc)
243 {
244  std::vector<std::string> defServices;
245  defServices.push_back("RootHistSvc");
246  m_svcNames.set(defServices);
247  declareProperty ("HistogramPersistency", m_histPersName = "");
248  declareProperty ("OutputFile", m_outputFile = "");
249  //
250  declareProperty
251  ("ConvertHistos" , m_convert ,
252  "The list of patterns to be accepted for conversion" ) ;
253  //
254  declareProperty
255  ("ExcludeHistos" , m_exclude ,
256  "The list of patterns to be excluded for conversion" ) ;
257  declareProperty("Warnings",m_warnings=true,
258  "Set this property to false to suppress warning messages");
259 }
260 
261 // ============================================================================
262 // The END
263 // ============================================================================
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode initialize() override
Initialize the service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
T endl(T...args)
StatusCode setProperty(const Property &p) override
Definition: Service.cpp:330
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
SmartIF< IFace > as()
Definition: ISvcLocator.h:106
StatusCode reinitialize() override
Reinitialize the service.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
bool m_warnings
Flag to disable warning messages when using external input.
STL class.
bool enable(bool value)
Set enabled flag.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
T push_back(T...args)
Main interface for the JobOptions service.
StatusCode initialize() override
Initialize the service.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
StatusCode finalize() override
stop the service.
PersistencySvc class implementation definition.
HistogramPersistencySvc class implementation definition.
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
T find(T...args)
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
T any_of(T...args)
string s
Definition: gaudirun.py:245
tuple item
print s1,s2
Definition: ana.py:146
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:108
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
std::string m_outputFile
Name of the outputFile.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
def select(list, sel)
Definition: AnalysisInit.py:29
std::string m_histPersName
Name of the Hist Pers type.