ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/ObjectService/interface/ObjectService.h
Revision: 1.6
Committed: Fri Mar 13 20:32:25 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.5: +22 -2 lines
Log Message:
Added mod == get with write access.

File Contents

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