1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: ObjectService.h,v 1.7 2009/03/15 11:17:47 loizides Exp $
|
3 |
//
|
4 |
// ObjectService
|
5 |
//
|
6 |
// This service can be used in the config files to provide a simple interface to exchange objects
|
7 |
// accross fillers and in principle also across modules. There are two types of lifetimes:
|
8 |
// a) over the full run time
|
9 |
// b) per event
|
10 |
//
|
11 |
// See functions add/addObjEvt and get/getObjEvt.
|
12 |
//
|
13 |
// Usage in config files is:
|
14 |
// service = ObjectService { }
|
15 |
//
|
16 |
// Authors: C.Loizides
|
17 |
//--------------------------------------------------------------------------------------------------
|
18 |
|
19 |
#ifndef MITPROD_OBJECTSERVICE_OBJECTSERVICE_H
|
20 |
#define MITPROD_OBJECTSERVICE_OBJECTSERVICE_H
|
21 |
|
22 |
#include <string>
|
23 |
#include "DataFormats/Provenance/interface/EventID.h"
|
24 |
#include "DataFormats/Provenance/interface/Timestamp.h"
|
25 |
#include "FWCore/Framework/interface/Event.h"
|
26 |
#include "FWCore/Framework/interface/EventSetup.h"
|
27 |
#include "FWCore/MessageLogger/interface/MessageLogger.h"
|
28 |
#include "MitProd/ObjectService/interface/NamedObject.h"
|
29 |
#include "MitProd/ObjectService/interface/NamedObjectOwned.h"
|
30 |
#include <THashTable.h>
|
31 |
|
32 |
namespace edm
|
33 |
{
|
34 |
class ActivityRegistry;
|
35 |
class ParameterSet;
|
36 |
class ModuleDescription;
|
37 |
}
|
38 |
|
39 |
namespace mithep
|
40 |
{
|
41 |
using std::string;
|
42 |
|
43 |
class ObjectService {
|
44 |
public:
|
45 |
ObjectService(const edm::ParameterSet &cfg, edm::ActivityRegistry &ar);
|
46 |
~ObjectService();
|
47 |
|
48 |
template<class T> bool add(const T *obj, const char *name);
|
49 |
template<class T> bool addObjEvt(T *obj, const char *name);
|
50 |
template<class T> const T *get(const char *name) const;
|
51 |
template<class T> const T *getObjEvt(const char *name) const;
|
52 |
template<class T> T *mod(const char *name) const;
|
53 |
template<class T> T *modObjEvt(const char *name) const;
|
54 |
|
55 |
template<class T> bool add(const T *obj, const string &name)
|
56 |
{ return add<T>(obj,name.c_str()); }
|
57 |
template<class T> bool addObjEvt(T *obj, const string &name)
|
58 |
{ return addObjEvt<T>(obj,name.c_str()); }
|
59 |
template<class T> const T *get(const string &name) const
|
60 |
{ return get<T>(name.c_str()); }
|
61 |
template<class T> const T *getObjEvt(const string &name) const
|
62 |
{ return getObjEvt<T>(name.c_str()); }
|
63 |
template<class T> T *mod(const string &name) const
|
64 |
{ return mod<T>(name.c_str()); }
|
65 |
template<class T> T *modObjEvt(const string &name) const
|
66 |
{ return modObjEvt<T>(name.c_str()); }
|
67 |
|
68 |
private:
|
69 |
void preEventProcessing(const edm::EventID &id, const edm::Timestamp &t);
|
70 |
void postEventProcessing(const edm::Event &e, const edm::EventSetup &es);
|
71 |
void postBeginJob();
|
72 |
void postEndJob();
|
73 |
|
74 |
THashTable obs_; //hash table holding the objects
|
75 |
THashTable obsEvt_; //hash table holding the objects per event
|
76 |
};
|
77 |
}
|
78 |
|
79 |
//--------------------------------------------------------------------------------------------------
|
80 |
template<class T>
|
81 |
bool mithep::ObjectService::add(const T *obj, const char *name)
|
82 |
{
|
83 |
// Add object with given name.
|
84 |
|
85 |
if (obs_.FindObject(name)) {
|
86 |
edm::LogError("ObjectService") << "Cannot add object with name " << name
|
87 |
<< " since name is already used."
|
88 |
<< std::endl;
|
89 |
throw edm::Exception(edm::errors::Configuration, "ObjectService::add()\n")
|
90 |
<< "Cannot add object with name " << name
|
91 |
<< " since name is already used.\n";
|
92 |
|
93 |
return 0;
|
94 |
}
|
95 |
|
96 |
NamedObject<T> *no = new NamedObject<T>(obj, name);
|
97 |
obs_.Add(no);
|
98 |
return 1;
|
99 |
}
|
100 |
|
101 |
//--------------------------------------------------------------------------------------------------
|
102 |
template<class T>
|
103 |
bool mithep::ObjectService::addObjEvt(T *obj, const char *name)
|
104 |
{
|
105 |
// Add object with given name for the current event.
|
106 |
|
107 |
if (obsEvt_.FindObject(name)) {
|
108 |
edm::LogError("ObjectService") << "Cannot add object with name " << name
|
109 |
<< " to event since name is already used."
|
110 |
<< std::endl;
|
111 |
throw edm::Exception(edm::errors::Configuration, "ObjectService::addObjEvt()\n")
|
112 |
<< "Cannot add object with name " << name
|
113 |
<< " to event since name is already used.\n";
|
114 |
|
115 |
return 0;
|
116 |
}
|
117 |
|
118 |
NamedObjectOwned<T> *no = new NamedObjectOwned<T>(obj, name);
|
119 |
obsEvt_.Add(no);
|
120 |
return 1;
|
121 |
}
|
122 |
|
123 |
//--------------------------------------------------------------------------------------------------
|
124 |
template<class T>
|
125 |
const T *mithep::ObjectService::get(const char *name) const
|
126 |
{
|
127 |
// Retrieve object with given name.
|
128 |
|
129 |
TObject *o = obs_.FindObject(name);
|
130 |
if (!o)
|
131 |
return 0;
|
132 |
|
133 |
const NamedObject<T> *no = static_cast<const NamedObject<T>* >(o);
|
134 |
if (!no)
|
135 |
return 0;
|
136 |
|
137 |
return no->Get();
|
138 |
}
|
139 |
|
140 |
//--------------------------------------------------------------------------------------------------
|
141 |
template<class T>
|
142 |
const T *mithep::ObjectService::getObjEvt(const char *name) const
|
143 |
{
|
144 |
// Retrieve object for the current event with given name.
|
145 |
|
146 |
TObject *o = obsEvt_.FindObject(name);
|
147 |
if (!o)
|
148 |
return 0;
|
149 |
|
150 |
const NamedObjectOwned<T> *no = static_cast<const NamedObjectOwned<T>* >(o);
|
151 |
if (!no)
|
152 |
return 0;
|
153 |
|
154 |
return no->Get();
|
155 |
}
|
156 |
|
157 |
//--------------------------------------------------------------------------------------------------
|
158 |
template<class T>
|
159 |
T *mithep::ObjectService::mod(const char *name) const
|
160 |
{
|
161 |
// Retrieve object with given name for write access.
|
162 |
|
163 |
return const_cast<T*>(this->get<T>(name));
|
164 |
}
|
165 |
|
166 |
//--------------------------------------------------------------------------------------------------
|
167 |
template<class T>
|
168 |
T *mithep::ObjectService::modObjEvt(const char *name) const
|
169 |
{
|
170 |
// Retrieve object for the current event with given name for write access.
|
171 |
|
172 |
return const_cast<T*>(this->getObjEvt<T>(name));
|
173 |
}
|
174 |
#endif
|