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

# User Rev Content
1 loizides 1.6 //--------------------------------------------------------------------------------------------------
2 loizides 1.28 // $Id: BaseMod.h,v 1.27 2009/09/25 08:39:26 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 bendavid 1.26 //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 bendavid 1.25 // Get published object for the current event.
172     TObject *inObj = FindObjThisEvt(name);
173 bendavid 1.26
174     //check if type is already correct
175 bendavid 1.25 T *ret = dynamic_cast<T*>(inObj);
176     if (ret)
177     return ret;
178    
179 bendavid 1.26 //construct modified name for new collection
180 bendavid 1.25 TString outName(name);
181     outName.Append("_GetColThisEvt_");
182     outName.Append(T::Class_Name());
183    
184 bendavid 1.26 //check if new collection was already added to the event
185 bendavid 1.25 ret = dynamic_cast<T*>(FindObjThisEvt(outName));
186     if (ret)
187     return ret;
188    
189 bendavid 1.26 //cast to base collection interface
190 bendavid 1.25 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 bendavid 1.26 //create and fill output ObjArray with the requested type
199 bendavid 1.25 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 bendavid 1.26 //add new obj array to the event and return
208 bendavid 1.25 AddObjThisEvt(newRet);
209     return newRet;
210     }
211    
212     //--------------------------------------------------------------------------------------------------
213     template <class T>
214 loizides 1.18 inline const T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn) const
215 loizides 1.13 {
216     // Get published object for the current event.
217    
218     T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
219 loizides 1.18 if (!ret && warn) {
220 loizides 1.23 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
221     "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
222 loizides 1.13 name, T::Class_Name());
223     }
224     return ret;
225     }
226    
227     //--------------------------------------------------------------------------------------------------
228     template <class T>
229 loizides 1.18 inline T *mithep::BaseMod::GetObjThisEvt(const char *name, Bool_t warn)
230 loizides 1.10 {
231     // Get published object for the current event.
232    
233     T *ret = dynamic_cast<T*>(FindObjThisEvt(name));
234 loizides 1.18 if (!ret && warn) {
235 loizides 1.23 SendError(kWarning, Form("GetObjThisEvt (\"%s\")",GetName()),
236     "Could not obtain object with name \"%s\" and type \"%s\" for current event!",
237 loizides 1.10 name, T::Class_Name());
238     }
239     return ret;
240     }
241    
242     //--------------------------------------------------------------------------------------------------
243     template <class T>
244 loizides 1.18 inline const T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn) const
245 loizides 1.13 {
246     // Get public object.
247    
248    
249     T *ret = dynamic_cast<T*>(FindPublicObj(name));
250 loizides 1.18 if (!ret && warn) {
251 loizides 1.23 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
252     "Could not obtain public object with name \"%s\" and type \"%s\"!",
253 loizides 1.13 name, T::Class_Name());
254     }
255     return ret;
256     }
257    
258     //--------------------------------------------------------------------------------------------------
259     template <class T>
260 loizides 1.18 inline T *mithep::BaseMod::GetPublicObj(const char *name, Bool_t warn)
261 loizides 1.10 {
262     // Get public object.
263    
264     T *ret = dynamic_cast<T*>(FindPublicObj(name));
265 loizides 1.18 if (!ret && warn) {
266 loizides 1.23 SendError(kWarning, Form("GetPublicObject (\"%s\")",GetName()),
267     "Could not obtain public object with name \"%s\" and type \"%s\"!",
268 loizides 1.10 name, T::Class_Name());
269     }
270     return ret;
271     }
272    
273     //--------------------------------------------------------------------------------------------------
274 loizides 1.6 inline const mithep::Selector *mithep::BaseMod::GetSel() const
275     {
276 loizides 1.8 // Get pointer to selector.
277    
278 loizides 1.6 return static_cast<const Selector*>(GetSelector());
279 loizides 1.1 }
280 loizides 1.13
281     //--------------------------------------------------------------------------------------------------
282     template <class T>
283 loizides 1.19 inline Bool_t mithep::BaseMod::LoadEventObject(const char *name, const T *&addr, Bool_t warn)
284 loizides 1.13 {
285 loizides 1.19 // 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 loizides 1.23 SendError(kWarning, Form("LoadEventObject (\"%s\")",GetName()),
301     "Could not obtain object with name \"%s\" and type \"%s\" from \"%s\"!",
302 loizides 1.19 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 loizides 1.13 // during processing and that it be read in to the address specified.
314    
315 loizides 1.19 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 loizides 1.28 // 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 loizides 1.19
343     ObjType *o = static_cast<ObjType*>(fEvtObjBrNames.FindObject(name));
344     if (!o && fromBr)
345     AddEventObject(name, fromBr);
346    
347     ReqEventObject(name, addr);
348 loizides 1.13 }
349 loizides 1.1 #endif