ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/ObjectService/interface/ObjectService.h
Revision: 1.8
Committed: Thu Mar 29 23:41:58 2012 UTC (13 years, 1 month ago) by paus
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, HEAD
Changes since 1.7: +7 -5 lines
Log Message:
Version with working skimming and last 4.4 tag.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 paus 1.8 // $Id: ObjectService.h,v 1.7 2009/03/15 11:17:47 loizides Exp $
3 loizides 1.1 //
4     // ObjectService
5     //
6 paus 1.8 // 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 loizides 1.3 //
13 loizides 1.2 // Usage in config files is:
14     // service = ObjectService { }
15 loizides 1.1 //
16     // Authors: C.Loizides
17     //--------------------------------------------------------------------------------------------------
18    
19 loizides 1.5 #ifndef MITPROD_OBJECTSERVICE_OBJECTSERVICE_H
20     #define MITPROD_OBJECTSERVICE_OBJECTSERVICE_H
21 loizides 1.1
22 loizides 1.7 #include <string>
23 loizides 1.1 #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 loizides 1.2 #include "FWCore/MessageLogger/interface/MessageLogger.h"
28     #include "MitProd/ObjectService/interface/NamedObject.h"
29 loizides 1.3 #include "MitProd/ObjectService/interface/NamedObjectOwned.h"
30 loizides 1.2 #include <THashTable.h>
31 loizides 1.1
32     namespace edm
33     {
34     class ActivityRegistry;
35     class ParameterSet;
36     class ModuleDescription;
37     }
38    
39     namespace mithep
40     {
41 loizides 1.7 using std::string;
42    
43 loizides 1.1 class ObjectService {
44     public:
45     ObjectService(const edm::ParameterSet &cfg, edm::ActivityRegistry &ar);
46     ~ObjectService();
47    
48 loizides 1.7 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 loizides 1.1
68     private:
69 loizides 1.7 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 loizides 1.1
74 loizides 1.2 THashTable obs_; //hash table holding the objects
75     THashTable obsEvt_; //hash table holding the objects per event
76 loizides 1.1 };
77     }
78 loizides 1.2
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 loizides 1.4 << " since name is already used."
88 loizides 1.2 << std::endl;
89 loizides 1.3 throw edm::Exception(edm::errors::Configuration, "ObjectService::add()\n")
90     << "Cannot add object with name " << name
91 loizides 1.4 << " since name is already used.\n";
92 loizides 1.3
93 loizides 1.2 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 loizides 1.3 bool mithep::ObjectService::addObjEvt(T *obj, const char *name)
104 loizides 1.2 {
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 loizides 1.4 << " to event since name is already used."
110 loizides 1.2 << std::endl;
111 loizides 1.3 throw edm::Exception(edm::errors::Configuration, "ObjectService::addObjEvt()\n")
112     << "Cannot add object with name " << name
113 loizides 1.4 << " to event since name is already used.\n";
114 loizides 1.3
115 loizides 1.2 return 0;
116     }
117    
118 loizides 1.3 NamedObjectOwned<T> *no = new NamedObjectOwned<T>(obj, name);
119 loizides 1.2 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 loizides 1.3 const NamedObject<T> *no = static_cast<const NamedObject<T>* >(o);
134 loizides 1.2 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 loizides 1.3 const NamedObjectOwned<T> *no = static_cast<const NamedObjectOwned<T>* >(o);
151 loizides 1.2 if (!no)
152     return 0;
153    
154     return no->Get();
155     }
156 loizides 1.6
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 loizides 1.1 #endif