ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/interface/BaseMod.h
Revision: 1.29
Committed: Tue Nov 24 14:27:32 2009 UTC (15 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.28: +11 -9 lines
Log Message:
Added L1Mod

File Contents

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