ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/ObjectService/interface/ObjectService.h
Revision: 1.7
Committed: Sun Mar 15 11:17:47 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_025c_branch2, Mit_025c_branch1, Mit_025c_branch0, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008, Mit_008pre2
Branch point for: Mit_025c_branch
Changes since 1.6: +27 -11 lines
Log Message:
Added std::string setters

File Contents

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