ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/ObjectService/interface/ObjectService.h
Revision: 1.5
Committed: Wed Sep 10 03:31:02 2008 UTC (16 years, 8 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre1, Mit_006b, Mit_006a, Mit_006, Mit_005, Mit_004
Changes since 1.4: +3 -3 lines
Log Message:
Cleanup

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.5 // $Id: ObjectService.h,v 1.4 2008/07/31 12:08:32 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.2 template<class T> const T *get(const char *name) const;
46     template<class T> const T *getObjEvt(const char *name) const;
47 loizides 1.1
48     private:
49     void preEventProcessing(const edm::EventID &id, const edm::Timestamp &t);
50 loizides 1.2 void postEventProcessing(const edm::Event &e, const edm::EventSetup &es);
51 loizides 1.1 void postBeginJob();
52     void postEndJob();
53    
54 loizides 1.2 THashTable obs_; //hash table holding the objects
55     THashTable obsEvt_; //hash table holding the objects per event
56 loizides 1.1 };
57     }
58 loizides 1.2
59     //--------------------------------------------------------------------------------------------------
60     template<class T>
61     bool mithep::ObjectService::add(const T *obj, const char *name)
62     {
63     // Add object with given name.
64    
65     if (obs_.FindObject(name)) {
66     edm::LogError("ObjectService") << "Cannot add object with name " << name
67 loizides 1.4 << " since name is already used."
68 loizides 1.2 << std::endl;
69 loizides 1.3 throw edm::Exception(edm::errors::Configuration, "ObjectService::add()\n")
70     << "Cannot add object with name " << name
71 loizides 1.4 << " since name is already used.\n";
72 loizides 1.3
73 loizides 1.2 return 0;
74     }
75    
76     NamedObject<T> *no = new NamedObject<T>(obj, name);
77     obs_.Add(no);
78     return 1;
79     }
80    
81     //--------------------------------------------------------------------------------------------------
82     template<class T>
83 loizides 1.3 bool mithep::ObjectService::addObjEvt(T *obj, const char *name)
84 loizides 1.2 {
85     // Add object with given name for the current event.
86    
87     if (obsEvt_.FindObject(name)) {
88     edm::LogError("ObjectService") << "Cannot add object with name " << name
89 loizides 1.4 << " to event since name is already used."
90 loizides 1.2 << std::endl;
91 loizides 1.3 throw edm::Exception(edm::errors::Configuration, "ObjectService::addObjEvt()\n")
92     << "Cannot add object with name " << name
93 loizides 1.4 << " to event since name is already used.\n";
94 loizides 1.3
95 loizides 1.2 return 0;
96     }
97    
98 loizides 1.3 NamedObjectOwned<T> *no = new NamedObjectOwned<T>(obj, name);
99 loizides 1.2 obsEvt_.Add(no);
100     return 1;
101     }
102    
103     //--------------------------------------------------------------------------------------------------
104     template<class T>
105     const T *mithep::ObjectService::get(const char *name) const
106     {
107     // Retrieve object with given name.
108    
109     TObject *o = obs_.FindObject(name);
110     if (!o)
111     return 0;
112    
113 loizides 1.3 const NamedObject<T> *no = static_cast<const NamedObject<T>* >(o);
114 loizides 1.2 if (!no)
115     return 0;
116    
117     return no->Get();
118     }
119    
120     //--------------------------------------------------------------------------------------------------
121     template<class T>
122     const T *mithep::ObjectService::getObjEvt(const char *name) const
123     {
124     // Retrieve object for the current event with given name.
125    
126     TObject *o = obsEvt_.FindObject(name);
127     if (!o)
128     return 0;
129    
130 loizides 1.3 const NamedObjectOwned<T> *no = static_cast<const NamedObjectOwned<T>* >(o);
131 loizides 1.2 if (!no)
132     return 0;
133    
134     return no->Get();
135     }
136 loizides 1.1 #endif