ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/interface/BaseMod.h
Revision: 1.31
Committed: Wed Mar 28 12:15:37 2012 UTC (13 years, 1 month ago) by paus
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, HEAD
Changes since 1.30: +3 -3 lines
Log Message:
Enable skimming.

File Contents

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