]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDinfoGen.cxx
Move track performance task selection to AddTask macros
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDinfoGen.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 /* $Id: AliTRDinfoGen.cxx 27496 2008-07-22 08:35:45Z cblume $ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //
20 //  Tender wagon for TRD performance/calibration train
21 //
22 // In this wagon the information from
23 //   - ESD
24 //   - Friends [if available]
25 //   - MC [if available]
26 // are grouped into AliTRDtrackInfo objects and fed to worker tasks
27 //
28 //  Authors:
29 //    Markus Fasel <M.Fasel@gsi.de>
30 //    Alexandru Bercuci <A.Bercuci@gsi.de>
31 //
32 ////////////////////////////////////////////////////////////////////////////
33
34 #include <TClonesArray.h>
35 #include <TObjArray.h>
36 #include <TObject.h>
37 #include <TString.h>
38 #include <TH1F.h>
39 #include <TFile.h>
40 #include <TTree.h>
41 #include <TROOT.h>
42 #include <TChain.h>
43 #include <TParticle.h>
44
45 #include "AliLog.h"
46 #include "AliAnalysisManager.h"
47 #include "AliESDEvent.h"
48 #include "AliMCEvent.h"
49 #include "AliESDInputHandler.h"
50 #include "AliMCEventHandler.h"
51
52 #include "AliESDfriend.h"
53 #include "AliESDfriendTrack.h"
54 #include "AliESDHeader.h"
55 #include "AliESDtrack.h"
56 #include "AliESDtrackCuts.h"
57 #include "AliMCParticle.h"
58 #include "AliPID.h"
59 #include "AliStack.h"
60 #include "AliTRDtrackV1.h"
61 #include "AliTrackReference.h"
62 #include "AliTRDgeometry.h"
63 #include "AliTRDcluster.h"
64 #include "AliTRDseedV1.h"
65 #include "TTreeStream.h"
66
67 #include <cstdio>
68 #include <climits>
69 #include <cstring>
70 #include <iostream>
71
72 #include "AliTRDinfoGen.h"
73 #include "info/AliTRDtrackInfo.h"
74 #include "info/AliTRDeventInfo.h"
75 #include "info/AliTRDv0Info.h"
76 #include "info/AliTRDeventCuts.h"
77 #include "macros/AliTRDperformanceTrain.h"
78
79 ClassImp(AliTRDinfoGen)
80
81 const Float_t AliTRDinfoGen::fgkITS = 100.; // to be checked
82 const Float_t AliTRDinfoGen::fgkTPC = 290.;
83 const Float_t AliTRDinfoGen::fgkTRD = 365.;
84
85 const Float_t AliTRDinfoGen::fgkEvVertexZ = 15.;
86 const Int_t   AliTRDinfoGen::fgkEvVertexN = 1;
87
88 const Float_t AliTRDinfoGen::fgkTrkDCAxy  = 40.;
89 const Float_t AliTRDinfoGen::fgkTrkDCAz   = 15.;
90 const Int_t   AliTRDinfoGen::fgkNclTPC    = 10;
91 const Float_t AliTRDinfoGen::fgkPt        = 0.2;
92 const Float_t AliTRDinfoGen::fgkEta       = 0.9;
93
94 //____________________________________________________________________
95 AliTRDinfoGen::AliTRDinfoGen()
96   :AliAnalysisTaskSE()
97   ,fEvTrigger(NULL)
98   ,fESDev(NULL)
99   ,fMCev(NULL)
100   ,fEventCut(NULL)
101   ,fTrackCut(NULL)
102   ,fTrackInfo(NULL)
103   ,fEventInfo(NULL)
104   ,fV0Info(NULL)
105   ,fTracksBarrel(NULL)
106   ,fTracksSA(NULL)
107   ,fTracksKink(NULL)
108   ,fV0List(NULL)
109   ,fDebugStream(NULL)
110 {
111   //
112   // Default constructor
113   //
114   SetNameTitle("infoGen", "MC-REC TRD-track list generator");
115 }
116
117 //____________________________________________________________________
118 AliTRDinfoGen::AliTRDinfoGen(char* name)
119   :AliAnalysisTaskSE(name)
120   ,fEvTrigger(NULL)
121   ,fESDev(NULL)
122   ,fMCev(NULL)
123   ,fEventCut(NULL)
124   ,fTrackCut(NULL)
125   ,fTrackInfo(NULL)
126   ,fEventInfo(NULL)
127   ,fV0Info(NULL)
128   ,fTracksBarrel(NULL)
129   ,fTracksSA(NULL)
130   ,fTracksKink(NULL)
131   ,fV0List(NULL)
132   ,fDebugStream(NULL)
133 {
134   //
135   // Default constructor
136   //
137   SetTitle("MC-REC TRD-track list generator");
138   DefineOutput(kTracksBarrel, TObjArray::Class());
139   DefineOutput(kTracksSA, TObjArray::Class());
140   DefineOutput(kTracksKink, TObjArray::Class());
141   DefineOutput(kEventInfo, AliTRDeventInfo::Class());
142   DefineOutput(kV0List, TObjArray::Class());
143 }
144
145 //____________________________________________________________________
146 AliTRDinfoGen::~AliTRDinfoGen()
147 {
148 // Destructor
149   
150   if(fDebugStream) delete fDebugStream;
151   if(fEvTrigger) delete fEvTrigger;
152   if(fTrackCut) delete fTrackCut;
153   if(fEventCut) delete fEventCut;
154   if(fTrackInfo) delete fTrackInfo; fTrackInfo = NULL;
155   if(fEventInfo) delete fEventInfo; fEventInfo = NULL;
156   if(fV0Info) delete fV0Info; fV0Info = NULL;
157   if(fTracksBarrel){ 
158     fTracksBarrel->Delete(); delete fTracksBarrel;
159     fTracksBarrel = NULL;
160   }
161   if(fTracksSA){ 
162     fTracksSA->Delete(); delete fTracksSA;
163     fTracksSA = NULL;
164   }
165   if(fTracksKink){ 
166     fTracksKink->Delete(); delete fTracksKink;
167     fTracksKink = NULL;
168   }
169   if(fV0List){ 
170     fV0List->Delete(); delete fV0List;
171     fV0List = NULL;
172   }
173 }
174
175 //____________________________________________________________________
176 void AliTRDinfoGen::UserCreateOutputObjects()
177 {       
178   //
179   // Create Output Containers (TObjectArray containing 1D histograms)
180   //
181  
182   fTrackInfo = new AliTRDtrackInfo();
183   fEventInfo = new AliTRDeventInfo();
184   fV0Info    = new AliTRDv0Info();
185   fTracksBarrel = new TObjArray(200); fTracksBarrel->SetOwner(kTRUE);
186   fTracksSA = new TObjArray(20); fTracksSA->SetOwner(kTRUE);
187   fTracksKink = new TObjArray(20); fTracksKink->SetOwner(kTRUE);
188   fV0List = new TObjArray(10); fV0List->SetOwner(kTRUE);
189 }
190
191 //____________________________________________________________________
192 void AliTRDinfoGen::UserExec(Option_t *){
193   //
194   // Run the Analysis
195   //
196
197   fESDev = dynamic_cast<AliESDEvent*>(InputEvent());
198   fMCev = MCEvent();
199
200   if(!fESDev){
201     AliError("Failed retrieving ESD event");
202     return;
203   }
204
205   // event selection : trigger cut
206   if(UseLocalEvSelection() && fEvTrigger){ 
207     Bool_t kTRIGGERED(kFALSE);
208     const TObjArray *trig = fEvTrigger->Tokenize(" ");
209     for(Int_t itrig=trig->GetEntriesFast(); itrig--;){
210       const Char_t *trigClass(((TObjString*)(*trig)[itrig])->GetName());
211       if(fESDev->IsTriggerClassFired(trigClass)) {
212         AliDebug(2, Form("Ev[%4d] Trigger[%s]", fESDev->GetEventNumberInFile(), trigClass));
213         kTRIGGERED = kTRUE;
214         break; 
215       }
216     }
217     if(!kTRIGGERED) return;
218
219     // select only physical events
220     if(fESDev->GetEventType() != 7) return;
221   }
222
223   // if the required trigger is a collision trigger then apply event vertex cut
224   if(UseLocalEvSelection() && IsCollision()){
225     const AliESDVertex *vertex = fESDev->GetPrimaryVertex();
226     if(TMath::Abs(vertex->GetZv())<1.e-10 || 
227        TMath::Abs(vertex->GetZv())>fgkEvVertexZ || 
228        vertex->GetNContributors()<fgkEvVertexN) {
229       //PostData(0, fOutStorage);
230       return;
231     }
232   }
233
234   if(fEventCut && !fEventCut->IsSelected(fESDev, IsCollision())) return;
235
236   if(!fESDfriend){
237     AliError("Failed retrieving ESD friend event");
238     return;
239   }
240   if(HasMCdata() && !fMCev){
241     AliError("Failed retrieving MC event");
242     return;
243   }
244
245   fTracksBarrel->Delete();
246   fTracksSA->Delete();
247   fTracksKink->Delete();
248   fV0List->Delete();
249   fEventInfo->Delete("");
250   new(fEventInfo)AliTRDeventInfo(fESDev->GetHeader(), const_cast<AliESDRun *>(fESDev->GetESDRun()));
251   
252   Bool_t *trackMap(NULL);
253   AliStack * mStack(NULL);
254   Int_t nTracksMC = HasMCdata() ? fMCev->GetNumberOfTracks() : 0, nTracksESD = fESDev->GetNumberOfTracks();
255   if(HasMCdata()){
256     mStack = fMCev->Stack();
257     if(!mStack){
258       AliError("Failed retrieving MC Stack");
259       return;
260     }
261     trackMap = new Bool_t[nTracksMC];
262     memset(trackMap, 0, sizeof(Bool_t) * nTracksMC);
263   }
264   
265   Double32_t dedx[100]; Int_t nSlices(0);
266   Int_t nTRDout(0), nTRDin(0), nTPC(0)
267        ,nclsTrklt
268        ,nBarrel(0), nSA(0), nKink(0)
269        ,nBarrelMC(0), nSAMC(0), nKinkMC(0);
270   AliESDtrack *esdTrack = NULL;
271   AliESDfriendTrack *esdFriendTrack = NULL;
272   TObject *calObject = NULL;
273   AliTRDtrackV1 *track = NULL;
274   AliTRDseedV1 *tracklet = NULL;
275   AliTRDcluster *cl = NULL;
276   for(Int_t itrk = 0; itrk < nTracksESD; itrk++){
277     esdTrack = fESDev->GetTrack(itrk);
278     AliDebug(3, Form("\n%3d ITS[%d] TPC[%d] TRD[%d]\n", itrk, esdTrack->GetNcls(0), esdTrack->GetNcls(1), esdTrack->GetNcls(2)));
279
280     if(esdTrack->GetStatus()&AliESDtrack::kTPCout) nTPC++;
281     if(esdTrack->GetStatus()&AliESDtrack::kTRDout) nTRDout++;
282     if(esdTrack->GetStatus()&AliESDtrack::kTRDin) nTRDin++;
283
284     // look at external track param
285     const AliExternalTrackParam *op = esdTrack->GetOuterParam();
286     Double_t xyz[3];
287     if(op){
288       op->GetXYZ(xyz);
289       op->Global2LocalPosition(xyz, op->GetAlpha());
290       AliDebug(3, Form("op @ X[%7.3f]\n", xyz[0]));
291     }
292
293     // read MC info
294     Int_t fPdg = -1;
295     Int_t label = -1; UInt_t alab=UINT_MAX;
296     if(HasMCdata()){
297       label = esdTrack->GetLabel(); 
298       alab = TMath::Abs(label);
299       // register the track
300       if(alab < UInt_t(nTracksMC)){ 
301         trackMap[alab] = kTRUE; 
302       } else { 
303         AliError(Form("MC label[%d] outside scope for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
304         continue; 
305       }
306       AliMCParticle *mcParticle = NULL; 
307       if(!(mcParticle = (AliMCParticle*) fMCev->GetTrack(alab))){
308         AliError(Form("MC particle label[%d] missing for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
309         continue;
310       }
311       fPdg = mcParticle->Particle()->GetPdgCode();
312       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
313       Int_t iref = 0; AliTrackReference *ref = NULL; 
314       while(iref<nRefs){
315         ref = mcParticle->GetTrackReference(iref);
316         if(ref->LocalX() > fgkTPC) break;
317         iref++;
318       }
319
320       new(fTrackInfo) AliTRDtrackInfo();
321       fTrackInfo->SetPDG(fPdg);
322       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
323       Int_t jref = iref;//, kref = 0;
324       while(jref<nRefs){
325         ref = mcParticle->GetTrackReference(jref);
326         if(ref->LocalX() > fgkTRD) break;
327         AliDebug(4, Form("  trackRef[%2d (%2d)] @ %7.3f OK", jref-iref, jref, ref->LocalX()));
328         fTrackInfo->AddTrackRef(ref);
329         jref++;
330       }
331       AliDebug(3, Form("NtrackRefs[%d(%d)]", fTrackInfo->GetNTrackRefs(), nRefs));
332     } else {
333       new (fTrackInfo) AliTRDtrackInfo();
334       fTrackInfo->SetPDG(fPdg);
335     }
336
337     // copy some relevant info to TRD track info
338     fTrackInfo->SetStatus(esdTrack->GetStatus());
339     fTrackInfo->SetTrackId(esdTrack->GetID());
340     Double_t p[AliPID::kSPECIES]; esdTrack->GetTRDpid(p);
341     fTrackInfo->SetESDpid(p);
342     fTrackInfo->SetESDpidQuality(esdTrack->GetTRDntrackletsPID());
343     if(!nSlices) nSlices = esdTrack->GetNumberOfTRDslices();
344     memset(dedx, 0, 100*sizeof(Double32_t));
345     Int_t in(0);
346     for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++)
347       for(Int_t is=0; is<nSlices; is++) 
348         dedx[in++]=esdTrack->GetTRDslice(il, is);
349     for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++) dedx[in++]=esdTrack->GetTRDmomentum(il);
350     fTrackInfo->SetSlices(in, dedx);
351     fTrackInfo->SetLabel(label);
352     fTrackInfo->SetNumberOfClustersRefit(esdTrack->GetNcls(2));
353     // some other Informations which we may wish to store in order to find problematic cases
354     fTrackInfo->SetKinkIndex(esdTrack->GetKinkIndex(0));
355     fTrackInfo->SetTPCncls(static_cast<UShort_t>(esdTrack->GetNcls(1)));
356     nclsTrklt = 0;
357   
358
359     // read REC info
360     esdFriendTrack = fESDfriend->GetTrack(itrk);
361     if(esdFriendTrack){
362       Int_t icalib = 0;
363       while((calObject = esdFriendTrack->GetCalibObject(icalib++))){
364         if(strcmp(calObject->IsA()->GetName(),"AliTRDtrackV1") != 0) continue; // Look for the TRDtrack
365         if(!(track = dynamic_cast<AliTRDtrackV1*>(calObject))) break;
366         AliDebug(4, Form("TRD track OK"));
367         // Set the clusters to unused
368         for(Int_t ipl = 0; ipl < AliTRDgeometry::kNlayer; ipl++){
369           if(!(tracklet = track->GetTracklet(ipl))) continue;
370           tracklet->ResetClusterIter();
371           while((cl = tracklet->NextCluster())) cl->Use(0);
372         }
373         fTrackInfo->SetTrack(track);
374         break;
375       }
376       AliDebug(3, Form("Ntracklets[%d]\n", fTrackInfo->GetNTracklets()));
377     } else AliDebug(3, "No ESD friends");
378     if(op) fTrackInfo->SetOuterParam(op);
379
380     if(DebugLevel() >= 1){
381       AliTRDtrackInfo info(*fTrackInfo);
382       (*DebugStream()) << "trackInfo"
383       << "TrackInfo.=" << &info
384       << "\n";
385       info.Delete("");
386     }
387
388     ULong_t status(esdTrack->GetStatus());
389     if((status&AliESDtrack::kTPCout)){
390       if(!esdTrack->GetKinkIndex(0)){ // Barrel  Track Selection
391         Bool_t selected(kTRUE);
392         if(UseLocalTrkSelection()){
393           if(esdTrack->Pt() < fgkPt) selected = kFALSE;
394           if(TMath::Abs(esdTrack->Eta()) > fgkEta) selected = kFALSE;
395           if(esdTrack->GetTPCNcls() < fgkNclTPC) selected = kFALSE;
396           Float_t par[2], cov[3];
397           esdTrack->GetImpactParameters(par, cov);
398           if(IsCollision()){ // cuts on DCA
399             if(TMath::Abs(par[0]) > fgkTrkDCAxy) selected = kFALSE;
400             if(TMath::Abs(par[1]) > fgkTrkDCAz) selected = kFALSE;
401           }
402         }
403         if(fTrackCut && !fTrackCut->IsSelected(esdTrack)) selected = kFALSE;
404         if(selected){ 
405           fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
406           nBarrel++;
407         }
408       } else {
409         fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
410         nKink++;
411       }
412     } else if((status&AliESDtrack::kTRDout) && !(status&AliESDtrack::kTRDin)){ 
413       fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
414       nSA++;
415     }
416     fTrackInfo->Delete("");
417   }
418
419 //   AliESDv0 *v0 = NULL;
420 //   for(Int_t iv0=0; iv0<fESD->GetNumberOfV0s(); iv0++){
421 //     if(!(v0 = fESD->GetV0(iv0))) continue;
422 //     fV0List->Add(new AliTRDv0Info(v0));
423 //   }
424
425   // Insert also MC tracks which are passing TRD where the track is not reconstructed
426   if(HasMCdata()){
427     AliDebug(10, "Output of the MC track map:");
428     for(Int_t itk = 0; itk < nTracksMC;  itk++) AliDebug(10, Form("trackMap[%d] = %s", itk, trackMap[itk] == kTRUE ? "TRUE" : "kFALSE"));
429   
430     for(Int_t itk = 0; itk < nTracksMC; itk++){
431       if(trackMap[itk]) continue;
432       AliMCParticle *mcParticle =  (AliMCParticle*) fMCev->GetTrack(TMath::Abs(itk));
433       Int_t fPdg = mcParticle->Particle()->GetPdgCode();
434       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
435       Int_t iref = 0; AliTrackReference *ref = NULL; 
436       Int_t nRefsTRD = 0;
437       new(fTrackInfo) AliTRDtrackInfo();
438       fTrackInfo->SetPDG(fPdg);
439       while(iref<nRefs){ // count TRD TR
440         Bool_t kIN(kFALSE);
441         ref = mcParticle->GetTrackReference(iref);
442         if(ref->LocalX() > fgkTPC && ref->LocalX() < fgkTRD){
443           fTrackInfo->AddTrackRef(ref);
444           nRefsTRD++;kIN=kTRUE;
445         }
446         AliDebug(4, Form("  trackRef[%2d] @ x[%7.3f] %s", iref, ref->LocalX(), kIN?"IN":"OUT"));
447         iref++;
448       }
449       if(!nRefsTRD){
450         // In this stage we at least require 1 hit inside TRD. What will be done with this tracks is a task for the 
451         // analysis job
452         fTrackInfo->Delete("");
453         continue;
454       }
455       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
456       fTrackInfo->SetLabel(itk);
457       if(DebugLevel() >= 1){
458         AliTRDtrackInfo info(*fTrackInfo);
459         (*DebugStream()) << "trackInfo"
460         << "TrackInfo.=" << &info
461         << "\n";
462         info.Delete("");
463       }
464       AliDebug(3, Form("Add MC track @ label[%d] nTRDrefs[%d].", itk, nRefsTRD));
465       // check where the track starts
466       ref = mcParticle->GetTrackReference(0);
467       if(ref->LocalX() < fgkITS){ 
468         fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
469         nBarrelMC++;
470       } else if(ref->LocalX() < fgkTPC) {
471         fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
472         nKinkMC++;
473       } else if(nRefsTRD>6){
474         fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
475         nSAMC++;
476       }
477       fTrackInfo->Delete("");
478     }
479     delete[] trackMap;
480   }
481   AliDebug(2, Form(
482     "\nEv[%3d] Tracks: ESD[%d] MC[%d]\n"
483     "        TPCout[%d] TRDin[%d] TRDout[%d]\n"
484     "        Barrel[%3d+%3d=%3d] SA[%2d+%2d=%2d] Kink[%2d+%2d=%2d]"
485     ,(Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTracksESD, nTracksMC
486     , nTPC, nTRDin, nTRDout
487     ,nBarrel, nBarrelMC, fTracksBarrel->GetEntries()
488     ,nSA, nSAMC, fTracksSA->GetEntries()
489     ,nKink, nKinkMC, fTracksKink->GetEntries()
490   ));
491
492   PostData(kTracksBarrel, fTracksBarrel);
493   PostData(kTracksSA, fTracksSA);
494   PostData(kTracksKink, fTracksKink);
495   PostData(kEventInfo, fEventInfo);
496   PostData(kV0List, fV0List);
497 }
498
499 //____________________________________________________________________
500 void AliTRDinfoGen::SetTrigger(const Char_t *trigger)
501 {
502   if(!fEvTrigger) fEvTrigger = new TString(trigger);
503   else (*fEvTrigger) = trigger;
504 }
505
506 //____________________________________________________________________
507 TTreeSRedirector* AliTRDinfoGen::DebugStream()
508 {
509   if(!fDebugStream){
510     TDirectory *savedir = gDirectory;
511     fDebugStream = new TTreeSRedirector("TRD.DebugInfoGen.root");
512     savedir->cd();
513   }
514   return fDebugStream;
515 }
516
517