201db8a021d65c263122cca1a1b23d2969045937
[u/mrichter/AliRoot.git] / PWG1 / TRD / info / AliTRDeventCuts.cxx
1 /*************************************************************************
2 * Copyright(c) 1998-2008, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  * **************************************************************************/
14
15 ////////////////////////////////////////////////////////////////////////////
16 //                                                                        //
17 // Event cut class for the TRD Performance Train                          //
18 //                                                                        //
19 // Encapsulation of events cuts for usage with TRD performance train      //
20 // - reconstructed vertex                                                 //
21 // - vertex Z position                                                    //
22 // - vertex number of contributors                                        //
23 // - trigger selection list                                               //
24 //                                                                        //
25 // *) see constructor for default values                                  //
26 //                                                                        //
27 // author                                                                 //
28 // Markus Fasel <m.fasel@gsi.de>                                          //
29 //                                                                        //
30 ////////////////////////////////////////////////////////////////////////////
31
32
33 #include <TIterator.h>
34 #include <TMath.h>
35 #include <TObjArray.h>
36 #include <TObjString.h>
37 #include <TString.h>
38
39 #include "AliLog.h"
40 #include "AliESDEvent.h"
41 #include "AliESDVertex.h"
42
43 #include "AliTRDeventInfo.h"
44 #include "AliTRDeventCuts.h"
45
46 ClassImp(AliTRDeventCuts)
47
48 //______________________________________________________________
49 AliTRDeventCuts::AliTRDeventCuts()
50   :TNamed("trdEventCuts", "")
51   ,fTriggerNames(NULL)
52   ,fBunches(NULL)
53   ,fVertexN(1)
54   ,fVertexZ(15.)
55 {
56   //
57   // Dummy Constructor
58   //
59   
60 }
61
62 //______________________________________________________________
63 AliTRDeventCuts::AliTRDeventCuts(const Char_t *name)
64   :TNamed(name, "")
65   ,fTriggerNames(NULL)
66   ,fBunches(NULL)
67   ,fVertexN(1)
68   ,fVertexZ(15.)
69 {
70   //
71   // Default Constructor
72   //
73 }
74
75 //______________________________________________________________
76 AliTRDeventCuts::AliTRDeventCuts(const AliTRDeventCuts &ref)
77   :TNamed((TNamed&)ref)
78   ,fTriggerNames(NULL)
79   ,fBunches(NULL)
80   ,fVertexN(ref.fVertexN)
81   ,fVertexZ(ref.fVertexZ)
82 {
83 // Copy constructor
84   if(ref.fTriggerNames){
85     for(Int_t it(0); it<ref.fTriggerNames->GetEntriesFast(); it++) AddTrigger(((TObjString*)(*ref.fTriggerNames)[it])->GetName());
86   }
87   if(ref.fBunches) SetBunchSelection(AliTRDeventInfo::kLHCbunches, ref.fBunches);
88 }
89
90 //______________________________________________________________
91 AliTRDeventCuts::~AliTRDeventCuts()
92 {
93 // Destructor
94
95   if(fTriggerNames) fTriggerNames->Delete();
96   delete fTriggerNames;
97   if(fBunches) delete [] fBunches;
98 }
99
100 //______________________________________________________________
101 Bool_t AliTRDeventCuts::IsSelected(AliESDEvent *ev, Bool_t col)
102 {
103 // Apply cuts
104
105   Bool_t select = kTRUE;
106   if(fTriggerNames){
107     Bool_t passTrigger = kFALSE; 
108     TString trgname = ev->GetFiredTriggerClasses();
109     TObjArray *triggers = trgname.Tokenize(" ");
110     TIterator *trgIter = triggers->MakeIterator(); 
111     TObjString *trg(NULL);
112     while((trg = dynamic_cast<TObjString *>(trgIter->Next())))
113       passTrigger = passTrigger || CheckTrigger(trg->String().Data());
114     delete triggers; delete trgIter;
115     select = select && passTrigger;
116   }
117   if(!select){
118     AliDebug(1, Form("Reject Ev[%d] for trigger[%s]", ev->GetEventNumberInFile(), ev->GetFiredTriggerClasses().Data()));
119     return select;
120   }
121   // select only physical events
122   select = select && (ev->GetEventType() == 7);
123   if(!select){
124     AliDebug(1, Form("Reject Ev[%d] for EvType[%d]", ev->GetEventNumberInFile(), ev->GetEventType()));
125     return select;
126   }
127
128   if(!col) return select;
129
130   // vertex selection
131   const AliESDVertex *primVtx = ev->GetPrimaryVertex();
132   if(fVertexN > 0)
133     select = select && (primVtx && primVtx->GetNContributors() >= fVertexN);
134   if(fVertexZ >= 0.)
135     select = select && (primVtx && TMath::Abs(primVtx->GetZ()) <= fVertexZ);
136   if(!select){
137     AliDebug(1, Form("Reject Ev[%d] for Vertex[%p][%d %6.2f]", ev->GetEventNumberInFile(), (void*)primVtx, primVtx?primVtx->GetNContributors():0, primVtx?TMath::Abs(primVtx->GetZ()):0));
138     return select;
139   }
140
141   // bunch cross selection
142   if(fBunches){
143     Int_t evBC(ev->GetBunchCrossNumber()), ibc(0);
144     Bool_t kFOUND(kFALSE);
145     while(fBunches[ibc]>0){
146       if(evBC==fBunches[ibc]){
147         kFOUND = kTRUE;
148         break;
149       }
150       ibc++;
151     }
152     select = select && kFOUND;
153   }
154   if(!select){
155     AliDebug(1, Form("Reject Ev[%d] for BunchCross[%d]", ev->GetEventNumberInFile(), ev->GetBunchCrossNumber()));
156     return select;
157   }
158   return select;
159 }
160
161 //______________________________________________________________
162 void AliTRDeventCuts::AddTrigger(const Char_t *name)
163 {
164 // Add trigger name according to the logbook
165
166   if(!fTriggerNames) fTriggerNames = new TObjArray;
167   if(CheckTrigger(name)) return;
168   fTriggerNames->Add(new TObjString(name));
169 }
170
171 //______________________________________________________________
172 Bool_t AliTRDeventCuts::CheckTrigger(const Char_t *name)
173 {
174 // check if trigger with id "name" is on the accepted trigger list
175
176   if(!fTriggerNames) return kFALSE;
177   Bool_t kExists(kFALSE);
178   for(Int_t it(0); it<fTriggerNames->GetEntriesFast(); it++){
179     if(((TObjString*)(*fTriggerNames)[it])->String().CompareTo(name)==0){
180       kExists = kTRUE;
181       break;
182     }
183   }
184   return kExists;
185 }
186
187
188 //______________________________________________________________
189 void AliTRDeventCuts::Print(Option_t */*opt*/) const
190 {
191 // Print content of event cuts
192   printf("Vertex  selection: N[%2d] Z[cm]=%6.2f\n", fVertexN, fVertexZ);
193   if(fTriggerNames){
194     printf("Trigger selection: ");
195     for(Int_t it(0); it<fTriggerNames->GetEntriesFast(); it++) printf("\"%s\" ", ((TObjString*)(*fTriggerNames)[it])->GetName());
196     printf("\n");
197   }
198   if(fBunches){
199     printf("Bunches selection: ");
200     for(Int_t ibc(0); ibc<AliTRDeventInfo::kLHCbunches; ibc++){
201       if(fBunches[ibc]<0) break;
202       printf("%4d ", fBunches[ibc]);
203     }
204     printf("\n");
205   }
206 }
207
208 //______________________________________________________________
209 void AliTRDeventCuts::SetBunchSelection(Int_t n, Int_t bunches[])
210 {
211 // Set Bunch selection for run
212   if(!fBunches) fBunches = new Int_t[AliTRDeventInfo::kLHCbunches];
213   for(Int_t ibc(0); ibc<AliTRDeventInfo::kLHCbunches; ibc++) fBunches[ibc] = ibc<n?bunches[ibc]:-1;
214 }
215