ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/interface/BaseMod.h
Revision: 1.26
Committed: Wed Sep 9 03:47:49 2009 UTC (15 years, 7 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.25: +13 -1 lines
Log Message:
Comments

File Contents

# User Rev Content
1 loizides 1.6 //--------------------------------------------------------------------------------------------------
2 bendavid 1.25 // $Id: BaseMod.h,v 1.24 2009/07/13 11:08:10 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.19 Int_t GetNEventsProcessed() const { return fNEventsProc; }
61 bendavid 1.25 template <class T> const T *GetColThisEvt(const char *name, Bool_t warn=1);
62 loizides 1.18 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 loizides 1.19 const RunInfo *GetRunInfo() const { return GetSel()->GetRunInfo(); }
67 loizides 1.9 const Selector *GetSel() const;
68 loizides 1.12 Bool_t HasHLTInfo() const;
69 loizides 1.19 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 loizides 1.9 void SaveNEventsProcessed(const char *name="hDEvents");
75 loizides 1.19 void SetFillHist(Bool_t b) { fFillHist = b; }
76 loizides 1.22 Bool_t ValidRunInfo() const { return GetSel()->ValidRunInfo(); }
77 loizides 1.9
78     private:
79 loizides 1.14 Bool_t fFillHist; //=true then fill histos (def=0)
80 loizides 1.19 THashTable fEvtObjBrNames; //names of per-event objects
81 loizides 1.12 mutable const HLTFwkMod *fHltFwkMod; //!pointer to HLTFwdMod
82     const TString fHltFwkModName; //!name of HLTFwkMod
83 loizides 1.14 Int_t fNEventsProc; //!number of events
84 loizides 1.1
85 loizides 1.15 ClassDef(BaseMod, 1) // Base TAM module
86 loizides 1.1 };
87 loizides 1.6 }
88 loizides 1.4
89 loizides 1.6 //--------------------------------------------------------------------------------------------------
90 loizides 1.19 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 loizides 1.23 SendError(kWarning, Form("AddEventObject (\"%s\")",GetName()),
97     "Can not add event object with name \"\"%s\"\" and type %d",
98 loizides 1.19 name, fromBr);
99     return;
100     }
101    
102     fEvtObjBrNames.Add(new ObjType(name,fromBr));
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106 loizides 1.14 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 phedex 1.17 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 loizides 1.20 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 loizides 1.8 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 loizides 1.10 template <class T>
163 bendavid 1.25 const T *mithep::BaseMod::GetColThisEvt(const char *name, Bool_t warn)
164     {
165    
166 bendavid 1.26 //Returns a collection of the specified type with the given name from the event.
167     //If a collection is present in the event with the given name but a different type, attempt
168     //to create an ObjArray of the appropriate type and fill it with the applicable objects
169     //using the dynamic type information. This ObjArray will then be cached in the event for
170     //repeated calls to this function with the same type and name.
171    
172 bendavid 1.25 // Get published object for the current event.
173     TObject *inObj = FindObjThisEvt(name);
174 bendavid 1.26
175     //check if type is already correct
176 bendavid 1.25 T *ret = dynamic_cast<T*>(inObj);
177     if (ret)
178     return ret;
179    
180 bendavid 1.26 //construct modified name for new collection
181 bendavid 1.25 TString outName(name);
182     outName.Append("_GetColThisEvt_");
183     outName.Append(T::Class_Name());
184    
185 bendavid 1.26 //check if new collection was already added to the event
186 bendavid 1.25 ret = dynamic_cast<T*>(FindObjThisEvt(outName));
187     if (ret)
188     return ret;
189    
190 bendavid 1.26 //cast to base collection interface
191 bendavid 1.25 mithep::BaseCollection *inCol = dynamic_cast<mithep::BaseCollection*>(inObj);
192    
193     if (!inCol && warn) {
194     SendError(kWarning, Form("GetColThisEvt (\"%s\")",GetName()),
195     "Could not obtain collection with name \"%s\" and type \"%s\" for current event!",
196     name, T::Class_Name());
197     }
198    
199 bendavid 1.26 //create and fill output ObjArray with the requested type
200 bendavid 1.25 mithep::ObjArray<typename T::element_type> *newRet = new mithep::ObjArray<typename T::element_type>;
201     newRet->SetName(outName);
202     for (UInt_t i=0; i<inCol->GetEntries(); ++i) {
203     const typename T::element_type *outElement = dynamic_cast<typename T::element_type*>(inCol->ObjAt(i));
204     if (outElement)
205     newRet->Add(outElement);
206     }
207    
208 bendavid 1.26 //add new obj array to the event and return
209 bendavid 1.25 AddObjThisEvt(newRet);
210     return newRet;
211     }
212    
213     //--------------------------------------------------------------------------------------------------
214     template <class T>
215 loizides 1.18 inline const T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn) const
216 loizides 1.13 {
217     // Get published object for the current event.
218    
219     T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
220 loizides 1.18 if (!ret && warn) {
221 loizides 1.23 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
222     "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
223 loizides 1.13 name, T::Class_Name());
224     }
225     return ret;
226     }
227    
228     //--------------------------------------------------------------------------------------------------
229     template <class T>
230 loizides 1.18 inline T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn)
231 loizides 1.10 {
232     // Get published object for the current event.
233    
234     T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
235 loizides 1.18 if (!ret && warn) {
236 loizides 1.23 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
237     "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
238 loizides 1.10 name, T::Class_Name());
239     }
240     return ret;
241     }
242    
243     //--------------------------------------------------------------------------------------------------
244     template <class T>
245 loizides 1.18 inline const T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn) const
246 loizides 1.13 {
247     // Get public object.
248    
249    
250     T *ret = dynamic_cast<T*>(FindPublicObj(name));
251 loizides 1.18 if (!ret && warn) {
252 loizides 1.23 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
253     "Could not obtain public object with name \"%s\" and type \"%s\"!",
254 loizides 1.13 name, T::Class_Name());
255     }
256     return ret;
257     }
258    
259     //--------------------------------------------------------------------------------------------------
260     template <class T>
261 loizides 1.18 inline T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn)
262 loizides 1.10 {
263     // Get public object.
264    
265     T *ret = dynamic_cast<T*>(FindPublicObj(name));
266 loizides 1.18 if (!ret && warn) {
267 loizides 1.23 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
268     "Could not obtain public object with name \"%s\" and type \"%s\"!",
269 loizides 1.10 name, T::Class_Name());
270     }
271     return ret;
272     }
273    
274     //--------------------------------------------------------------------------------------------------
275 loizides 1.6 inline const mithep::Selector *mithep::BaseMod::GetSel() const
276     {
277 loizides 1.8 // Get pointer to selector.
278    
279 loizides 1.6 return static_cast<const Selector*>(GetSelector());
280 loizides 1.1 }
281 loizides 1.13
282     //--------------------------------------------------------------------------------------------------
283     template <class T>
284 loizides 1.19 inline Bool_t mithep::BaseMod::LoadEventObject(const char *name, const T *&addr, Bool_t warn)
285 loizides 1.13 {
286 loizides 1.19 // Obtain the object with the specified name that has been requested during processing.
287     // In case name is found in fEvtObjBrNames it will be read from a branch to the
288     // address specified in ReqEventObject.
289    
290     TString type("event");
291     ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
292     if (o && o->IsBranch()) {
293     type = "branch";
294     LoadBranch(name);
295     } else {
296     addr = GetObjThisEvt<T>(name);
297     }
298    
299     Bool_t ret = (addr!=0);
300     if (!ret && warn) {
301 loizides 1.23 SendError(kWarning, Form("LoadEventObject (\"%s\")",GetName()),
302     "Could not obtain object with name \"%s\" and type \"%s\" from \"%s\"!",
303 loizides 1.19 name, T::Class_Name(), type.Data());
304     }
305    
306     return ret;
307     }
308    
309     //--------------------------------------------------------------------------------------------------
310     template <class T>
311     inline void mithep::BaseMod::ReqBranch(const char *bname, const T *&addr)
312     {
313     // Requests that the branch with the specified name is made available
314 loizides 1.13 // during processing and that it be read in to the address specified.
315    
316 loizides 1.19 TAModule::ReqBranch(bname, const_cast<T*&>(addr));
317     }
318    
319     //--------------------------------------------------------------------------------------------------
320     template <class T>
321     inline void mithep::BaseMod::ReqEventObject(const char *name, const T *&addr)
322     {
323     // Requests that the object with the specified name is made available
324     // during processing. In case name is found in fEvtObjBrNames it
325     // will be read from a branch to the address specified.
326    
327     ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
328     if (!o || !o->IsBranch())
329     return;
330    
331     ReqBranch(name, addr);
332     }
333    
334     //--------------------------------------------------------------------------------------------------
335     template <class T>
336     inline void mithep::BaseMod::ReqEventObject(const char *name, const T *&addr, Bool_t fromBr)
337     {
338     // Requests that the object with the specified name is made available
339     // during processing. In case name is found in fEvtObjBrNames it
340     // will be read from a branch to the address specified.
341    
342     ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
343     if (!o && fromBr)
344     AddEventObject(name, fromBr);
345    
346     ReqEventObject(name, addr);
347 loizides 1.13 }
348 loizides 1.1 #endif