ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/interface/BaseMod.h
Revision: 1.28
Committed: Thu Oct 1 12:43:53 2009 UTC (15 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_012b, Mit_012a, Mit_012, Mit_011a
Changes since 1.27: +4 -2 lines
Log Message:
cosmetics.

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: BaseMod.h,v 1.27 2009/09/25 08:39:26 loizides Exp $
3 //
4 // BaseMod
5 //
6 // This TAM module is the base module for all our TAM modules. It defines a couple of useful
7 // getters to retrieve information from the underlying framework code, such as the EventHeader,
8 // RunInfo or Trigger information.
9 //
10 // Authors: C.Loizides
11 //--------------------------------------------------------------------------------------------------
12
13 #ifndef MITANA_TREEMOD_BASEMOD_H
14 #define MITANA_TREEMOD_BASEMOD_H
15
16 #include <TObjString.h>
17 #include "MitAna/TAM/interface/TAModule.h"
18 #include "MitAna/TreeMod/interface/Selector.h"
19 #include "MitAna/DataTree/interface/TriggerObjectCol.h"
20 #include "MitAna/DataCont/interface/ObjArray.h"
21
22 namespace mithep
23 {
24 class HLTFwkMod;
25 class TriggerObjectsTable;
26 class TriggerTable;
27
28 class BaseMod : public TAModule {
29 public:
30 BaseMod(const char *name="BaseMod", const char *title="Base analysis module");
31
32 private:
33 class ObjType : public TObjString {
34 public:
35 ObjType(const char *name="", Bool_t br=kTRUE) : TObjString(name), fBr(br) {}
36 ~ObjType() {}
37 Bool_t IsBranch() const { return fBr; }
38 protected:
39 Bool_t fBr; //=true then object is from branch
40 };
41
42 protected:
43 void AddEventObject(const char *name, Bool_t fromBr=kTRUE);
44 template <class T> void AddTH1(T *&ptr, const char *name, const char *title,
45 Int_t nbins, Double_t xmin, Double_t xmax);
46 template <class T> void AddTH2(T *&ptr, const char *name, const char *title,
47 Int_t nbinsx, Double_t xmin, Double_t xmax,
48 Int_t nbinsy, Double_t ymin, Double_t ymax);
49 template <class T> void AddTH3(T *&ptr, const char *name, const char *title,
50 Int_t nbinsx, Double_t xmin, Double_t xmax,
51 Int_t nbinsy, Double_t ymin, Double_t ymax,
52 Int_t nbinsz, Double_t zmin, Double_t zmax);
53 void AddToTrash(TObject *obj);
54 const EventHeader *GetEventHeader() const { return GetSel()->GetEventHeader(); }
55 Bool_t GetFillHist() const { return fFillHist; }
56 const HLTFwkMod *GetHltFwkMod() const { return fHltFwkMod; }
57 const TriggerObjectCol *GetHLTObjects(const char *name) const;
58 const TriggerObjectsTable *GetHLTObjectsTable() const;
59 const TriggerTable *GetHLTTable() const;
60 Int_t GetNEventsProcessed() const { return fNEventsProc; }
61 template <class T> const T *GetColThisEvt(const char *name, Bool_t warn=1);
62 template <class T> const T *GetObjThisEvt(const char *name, Bool_t warn=1) const;
63 template <class T> T *GetObjThisEvt(const char *name, Bool_t warn=1);
64 template <class T> const T *GetPublicObj(const char *name, Bool_t warn=1) const;
65 template <class T> T *GetPublicObj(const char *name, Bool_t warn=1);
66 const RunInfo *GetRunInfo() const { return GetSel()->GetRunInfo(); }
67 const Selector *GetSel() const;
68 Bool_t HasHLTInfo() const;
69 void IncNEventsProcessed() { ++fNEventsProc; }
70 template <class T> Bool_t LoadEventObject(const char *name, const T *&addr, Bool_t warn=1);
71 template <class T> void ReqBranch(const char *bname, const T *&addr);
72 template <class T> void ReqEventObject(const char *name, const T *&addr);
73 template <class T> void ReqEventObject(const char *name, const T *&addr, Bool_t fromBr);
74 void SaveNEventsProcessed(const char *name="hDEvents");
75 void SetFillHist(Bool_t b) { fFillHist = b; }
76 Bool_t ValidRunInfo() const { return GetSel()->ValidRunInfo(); }
77
78 private:
79 Bool_t fFillHist; //=true then fill histos (def=0)
80 THashTable fEvtObjBrNames; //names of per-event objects
81 mutable const HLTFwkMod *fHltFwkMod; //!pointer to HLTFwdMod
82 const TString fHltFwkModName; //!name of HLTFwkMod
83 Int_t fNEventsProc; //!number of events
84
85 ClassDef(BaseMod, 1) // Base TAM module
86 };
87 }
88
89 //--------------------------------------------------------------------------------------------------
90 inline void mithep::BaseMod::AddEventObject(const char *name, Bool_t fromBr)
91 {
92 // Add event object to list of objects that should be retrieved from a branch rather
93 // than from a pointer given by the event.
94
95 if (fEvtObjBrNames.FindObject(name)) {
96 SendError(kWarning, Form("AddEventObject (\"%s\")",GetName()),
97 "Can not add event object with name \"\"%s\"\" and type %d",
98 name, fromBr);
99 return;
100 }
101
102 fEvtObjBrNames.Add(new ObjType(name,fromBr));
103 }
104
105 //--------------------------------------------------------------------------------------------------
106 template <class T>
107 inline void mithep::BaseMod::AddTH1(T *&ptr, const char *name, const char *title,
108 Int_t nbins, Double_t xmin, Double_t xmax)
109 {
110 // Create ROOT histogram and add it to the output list.
111
112 ptr = new T(name, title, nbins, xmin, xmax);
113 ptr->Sumw2();
114 AddOutput(ptr);
115 }
116
117 //--------------------------------------------------------------------------------------------------
118 template <class T>
119 inline void mithep::BaseMod::AddTH2(T *&ptr, const char *name, const char *title,
120 Int_t nbinsx, Double_t xmin, Double_t xmax,
121 Int_t nbinsy, Double_t ymin, Double_t ymax)
122 {
123 // Create ROOT histogram and add it to the output list.
124
125 ptr = new T(name, title, nbinsx, xmin, xmax, nbinsy, ymin, ymax);
126 ptr->Sumw2();
127 AddOutput(ptr);
128 }
129
130 //--------------------------------------------------------------------------------------------------
131 template <class T>
132 inline void mithep::BaseMod::AddTH3(T *&ptr, const char *name, const char *title,
133 Int_t nbinsx, Double_t xmin, Double_t xmax,
134 Int_t nbinsy, Double_t ymin, Double_t ymax,
135 Int_t nbinsz, Double_t zmin, Double_t zmax)
136 {
137 // Create ROOT histogram and add it to the output list.
138
139 ptr = new T(name, title, nbinsx, xmin, xmax, nbinsy, ymin, ymax, nbinsz, zmin, zmax);
140 ptr->Sumw2();
141 AddOutput(ptr);
142 }
143
144 //--------------------------------------------------------------------------------------------------
145 inline void mithep::BaseMod::AddToTrash(TObject *obj)
146 {
147 // Add object to trash. Trash will be emptied after an entry of the tree was processed.
148
149 Selector *sel = const_cast<Selector*>(GetSel());
150 sel->AddToTrash(obj);
151 }
152
153 //--------------------------------------------------------------------------------------------------
154 inline const mithep::TriggerObjectCol *mithep::BaseMod::GetHLTObjects(const char *name) const
155 {
156 // Get pointer to HLT TriggerObjects collection with given name for the current event.
157
158 return (dynamic_cast<const TriggerObjectCol *>(FindObjThisEvt(name)));
159 }
160
161 //--------------------------------------------------------------------------------------------------
162 template <class T>
163 const T *mithep::BaseMod::GetColThisEvt(const char *name, Bool_t warn)
164 {
165 //Returns a collection of the specified type with the given name from the event.
166 //If a collection is present in the event with the given name but a different type, attempt
167 //to create an ObjArray of the appropriate type and fill it with the applicable objects
168 //using the dynamic type information. This ObjArray will then be cached in the event for
169 //repeated calls to this function with the same type and name.
170
171 // Get published object for the current event.
172 TObject *inObj = FindObjThisEvt(name);
173
174 //check if type is already correct
175 T *ret = dynamic_cast<T*>(inObj);
176 if (ret)
177 return ret;
178
179 //construct modified name for new collection
180 TString outName(name);
181 outName.Append("_GetColThisEvt_");
182 outName.Append(T::Class_Name());
183
184 //check if new collection was already added to the event
185 ret = dynamic_cast<T*>(FindObjThisEvt(outName));
186 if (ret)
187 return ret;
188
189 //cast to base collection interface
190 mithep::BaseCollection *inCol = dynamic_cast<mithep::BaseCollection*>(inObj);
191
192 if (!inCol && warn) {
193 SendError(kWarning, Form("GetColThisEvt (\"%s\")",GetName()),
194 "Could not obtain collection with name \"%s\" and type \"%s\" for current event!",
195 name, T::Class_Name());
196 }
197
198 //create and fill output ObjArray with the requested type
199 mithep::ObjArray<typename T::element_type> *newRet = new mithep::ObjArray<typename T::element_type>;
200 newRet->SetName(outName);
201 for (UInt_t i=0; i<inCol->GetEntries(); ++i) {
202 const typename T::element_type *outElement = dynamic_cast<typename T::element_type*>(inCol->ObjAt(i));
203 if (outElement)
204 newRet->Add(outElement);
205 }
206
207 //add new obj array to the event and return
208 AddObjThisEvt(newRet);
209 return newRet;
210 }
211
212 //--------------------------------------------------------------------------------------------------
213 template <class T>
214 inline const T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn) const
215 {
216 // Get published object for the current event.
217
218 T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
219 if (!ret && warn) {
220 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
221 "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
222 name, T::Class_Name());
223 }
224 return ret;
225 }
226
227 //--------------------------------------------------------------------------------------------------
228 template <class T>
229 inline T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn)
230 {
231 // Get published object for the current event.
232
233 T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
234 if (!ret && warn) {
235 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
236 "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
237 name, T::Class_Name());
238 }
239 return ret;
240 }
241
242 //--------------------------------------------------------------------------------------------------
243 template <class T>
244 inline const T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn) const
245 {
246 // Get public object.
247
248
249 T *ret = dynamic_cast<T*>(FindPublicObj(name));
250 if (!ret && warn) {
251 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
252 "Could not obtain public object with name \"%s\" and type \"%s\"!",
253 name, T::Class_Name());
254 }
255 return ret;
256 }
257
258 //--------------------------------------------------------------------------------------------------
259 template <class T>
260 inline T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn)
261 {
262 // Get public object.
263
264 T *ret = dynamic_cast<T*>(FindPublicObj(name));
265 if (!ret && warn) {
266 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
267 "Could not obtain public object with name \"%s\" and type \"%s\"!",
268 name, T::Class_Name());
269 }
270 return ret;
271 }
272
273 //--------------------------------------------------------------------------------------------------
274 inline const mithep::Selector *mithep::BaseMod::GetSel() const
275 {
276 // Get pointer to selector.
277
278 return static_cast<const Selector*>(GetSelector());
279 }
280
281 //--------------------------------------------------------------------------------------------------
282 template <class T>
283 inline Bool_t mithep::BaseMod::LoadEventObject(const char *name, const T *&addr, Bool_t warn)
284 {
285 // Obtain the object with the specified name that has been requested during processing.
286 // In case name is found in fEvtObjBrNames it will be read from a branch to the
287 // address specified in ReqEventObject.
288
289 TString type("event");
290 ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
291 if (o && o->IsBranch()) {
292 type = "branch";
293 LoadBranch(name);
294 } else {
295 addr = GetObjThisEvt<T>(name);
296 }
297
298 Bool_t ret = (addr!=0);
299 if (!ret && warn) {
300 SendError(kWarning, Form("LoadEventObject (\"%s\")",GetName()),
301 "Could not obtain object with name \"%s\" and type \"%s\" from \"%s\"!",
302 name, T::Class_Name(), type.Data());
303 }
304
305 return ret;
306 }
307
308 //--------------------------------------------------------------------------------------------------
309 template <class T>
310 inline void mithep::BaseMod::ReqBranch(const char *bname, const T *&addr)
311 {
312 // Requests that the branch with the specified name is made available
313 // during processing and that it be read in to the address specified.
314
315 TAModule::ReqBranch(bname, const_cast<T*&>(addr));
316 }
317
318 //--------------------------------------------------------------------------------------------------
319 template <class T>
320 inline void mithep::BaseMod::ReqEventObject(const char *name, const T *&addr)
321 {
322 // Requests that the object with the specified name is made available
323 // during processing. In case name is found in fEvtObjBrNames it
324 // will be read from a branch to the address specified.
325
326 ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
327 if (!o || !o->IsBranch())
328 return;
329
330 ReqBranch(name, addr);
331 }
332
333 //--------------------------------------------------------------------------------------------------
334 template <class T>
335 inline void mithep::BaseMod::ReqEventObject(const char *name, const T *&addr, Bool_t fromBr)
336 {
337 // Requests that the object with the specified name is made available
338 // during processing. In case name is found in fEvtObjBrNames it
339 // will be read from a branch to the address specified. If "fromBr" is
340 // kTRUE then add the event to the list of events needed to be retrieved
341 // from a branch.
342
343 ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
344 if (!o && fromBr)
345 AddEventObject(name, fromBr);
346
347 ReqEventObject(name, addr);
348 }
349 #endif