]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/qaRec/AliTRDtrackInfoGen.cxx
d36a4db445da6480f41814f9dbbaa13e456cbf3b
[u/mrichter/AliRoot.git] / TRD / qaRec / AliTRDtrackInfoGen.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: AliTRDtrackInfoGen.cxx 27496 2008-07-22 08:35:45Z cblume $ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //                                                                        //
20 //  Reconstruction QA                                                     //
21 //                                                                        //
22 //  Authors:                                                              //
23 //    Markus Fasel <M.Fasel@gsi.de>                                       //
24 //                                                                        //
25 ////////////////////////////////////////////////////////////////////////////
26
27 #include <TClonesArray.h>
28 #include <TObjArray.h>
29 #include <TObject.h>
30 #include <TH1F.h>
31 #include <TFile.h>
32 #include <TTree.h>
33 #include <TROOT.h>
34 #include <TChain.h>
35 #include <TParticle.h>
36
37 #include "AliLog.h"
38 #include "AliAnalysisManager.h"
39 #include "AliESDEvent.h"
40 #include "AliMCEvent.h"
41 #include "AliESDInputHandler.h"
42 #include "AliMCEventHandler.h"
43
44 #include "AliESDfriend.h"
45 #include "AliESDfriendTrack.h"
46 #include "AliESDHeader.h"
47 #include "AliESDtrack.h"
48 #include "AliMCParticle.h"
49 #include "AliStack.h"
50 #include "AliTRDtrackV1.h"
51 #include "AliTrackReference.h"
52 #include "AliTRDgeometry.h"
53 #include "TTreeStream.h"
54
55 #include <cstdio>
56 #include <cstring>
57 #include <iostream>
58
59 #include "AliTRDtrackInfoGen.h"
60 #include "AliTRDtrackInfo/AliTRDtrackInfo.h"
61 #include "AliTRDtrackInfo/AliTRDeventInfo.h"
62
63 ClassImp(AliTRDtrackInfoGen)
64
65 const Float_t AliTRDtrackInfoGen::xTPC = 290.;
66 const Float_t AliTRDtrackInfoGen::xTOF = 365.;
67
68 //____________________________________________________________________
69 AliTRDtrackInfoGen::AliTRDtrackInfoGen():
70   AliTRDrecoTask("InfoGen", "Track List Generator")
71   ,fESD(0x0)
72   ,fMC(0x0)
73   ,fESDfriend(0x0)
74   ,fTrackInfo(0x0)
75   ,fEventInfo(0x0)
76 {
77   //
78   // Default constructor
79   //
80
81   DefineInput(0, TChain::Class());
82   DefineOutput(0, TObjArray::Class());
83   DefineOutput(1, AliTRDeventInfo::Class());
84   //DefineOutput(1, TTree::Class());
85 }
86
87 //____________________________________________________________________
88 AliTRDtrackInfoGen::~AliTRDtrackInfoGen()
89 {
90   if(fTrackInfo) delete fTrackInfo;
91   //if(fEventInfo) delete fEventInfo;
92   //fTrackInfo = 0x0; fEventInfo = 0x0;
93 }
94
95 //____________________________________________________________________
96 void AliTRDtrackInfoGen::ConnectInputData(Option_t *)
97 {
98   //
99   // Link the Input Data
100   //
101   TTree *tree = dynamic_cast<TChain*>(GetInputData(0));
102   if(!tree){
103     printf("ERROR - ESD event not found");
104   } else {
105     tree->SetBranchStatus("Tracks", 1);
106     tree->SetBranchStatus("ESDfriend*",1);
107   }
108
109   AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*>(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
110   if(!esdH){
111     printf("ERROR - ESD input handler not found");
112   } else {
113     fESD = esdH->GetEvent();
114     if(!fESD){
115       printf("ERROR - ESD event not found");
116     } else {
117       esdH->SetActiveBranches("ESDfriend*");
118       fESDfriend = (AliESDfriend *)fESD->FindListObject("AliESDfriend");
119     }
120   }
121   if(HasMCdata()){
122     AliMCEventHandler *mcH = dynamic_cast<AliMCEventHandler*>(AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
123     if(!mcH){ 
124       AliError("MC input handler not found");
125     } else {
126       fMC = mcH->MCEvent();
127     }
128   }
129 }
130
131 //____________________________________________________________________
132 void AliTRDtrackInfoGen::CreateOutputObjects()
133 {       
134   //
135   // Create Output Containers (TObjectArray containing 1D histograms)
136   //
137   fTrackInfo = new AliTRDtrackInfo();
138   fEventInfo = new AliTRDeventInfo();
139   fContainer = new TObjArray(1000);
140
141 /*  OpenFile(1, "RECREATE");
142   fTree = new TTree("trd", "extract of the TRD detector");
143   fTree->Branch("info",  &fTrackInfo);
144   printf("output tree build in %s\n", fTree->GetDirectory()->GetName());*/
145 }
146
147 //____________________________________________________________________
148 void AliTRDtrackInfoGen::Exec(Option_t *){
149   //
150   // Run the Analysis
151   //
152   if(!fESD){
153     puts("Error: ESD not found");
154     return;
155   }
156   if(!fESDfriend){
157     puts("Error: ESD friend not found");
158     return;
159   }
160   if(HasMCdata() && !fMC){
161     puts("Error: Monte Carlo Event not available");
162     return;
163   }
164   fContainer->Delete();
165   fEventInfo->Delete("");
166   fESD->SetESDfriend(fESDfriend);
167   new(fEventInfo)AliTRDeventInfo(fESD->GetHeader(), const_cast<AliESDRun *>(fESD->GetESDRun()));
168   
169   Bool_t *trackMap = 0x0;
170   AliStack * mStack = 0x0;
171   if(HasMCdata()){
172     mStack = fMC->Stack();
173     if(!mStack){
174       puts("Error: Cannot get the Monte Carlo Stack");
175       return;
176     }
177     trackMap = new Bool_t[fMC->GetNumberOfTracks()];
178     memset(trackMap, 0, sizeof(Bool_t) * fMC->GetNumberOfTracks());
179   }
180   
181   Int_t nTRD = 0, nTPC = 0, nclsTrklt;
182   Int_t nTracks = fESD->GetNumberOfTracks();
183   if(fDebugLevel>=1){ 
184     printf("%3d Tracks: ESD[%d] MC[%d]\n", (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTracks, HasMCdata() ? mStack->GetNtrack() : 0);
185   }
186   AliESDtrack *esdTrack = 0x0;
187   AliESDfriendTrack *esdFriendTrack = 0x0;
188   TObject *calObject = 0x0;
189   AliTRDtrackV1 *track = 0x0;
190   for(Int_t itrk = 0; itrk < nTracks; itrk++){
191     esdTrack = fESD->GetTrack(itrk);
192     if(fDebugLevel>=2) printf("\n%3d ITS[%d] TPC[%d] TRD[%d]\n", itrk, esdTrack->GetNcls(0), esdTrack->GetNcls(1), esdTrack->GetNcls(2));
193     if(esdTrack->GetNcls(1)) nTPC++;
194     if(esdTrack->GetNcls(2)) nTRD++;
195
196     // look at external track param
197     const AliExternalTrackParam *op = esdTrack->GetOuterParam();
198     Double_t xyz[3];
199     if(op){
200         op->GetXYZ(xyz);
201         op->Global2LocalPosition(xyz, op->GetAlpha());
202         if(fDebugLevel>=2) printf("op @ X[%7.3f]\n", xyz[0]);
203     }
204
205     // read MC info
206     Int_t fPdg = -1;
207     Int_t label = -1;
208     if(HasMCdata()){
209       label = esdTrack->GetLabel();
210       if(label < fMC->GetNumberOfTracks()) trackMap[TMath::Abs(label)] = kTRUE; // register the track
211       //if (TMath::Abs(label) > mStack->GetNtrack()) continue; 
212       AliMCParticle *mcParticle = 0x0; 
213       if(!(mcParticle = fMC->GetTrack(TMath::Abs(label)))){
214         printf("E - AliTRDtrackInfoGen::Exec() : MC particle missing for ESD label %d\n", label);
215         continue;
216       }
217       fPdg = mcParticle->Particle()->GetPdgCode();
218       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
219       Int_t iref = 0; AliTrackReference *ref = 0x0; 
220       while(iref<nRefs){
221         ref = mcParticle->GetTrackReference(iref);
222         if(ref->LocalX() > xTPC) break;
223         //printf("\ttrackRef[%2d] @ %7.3f\n", iref, ref->LocalX());
224         iref++;
225       }
226
227       new(fTrackInfo) AliTRDtrackInfo();
228       fTrackInfo->SetPDG(fPdg);
229       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
230       Int_t jref = iref;//, kref = 0;
231       while(jref<nRefs){
232         ref = mcParticle->GetTrackReference(jref);
233         if(ref->LocalX() > xTOF) break;
234         if(fDebugLevel>=3) printf("\ttrackRef[%2d (%2d)] @ %7.3f OK\n", jref-iref, jref, ref->LocalX());
235         fTrackInfo->AddTrackRef(ref);
236         jref++;
237       }
238       if(!fTrackInfo->GetNTrackRefs()){ 
239           //if(!esdTrack->GetNcls(2)) continue;
240   /*            printf("No TRD Track References in the Track [%d] II\n", itrk);
241             printf("Label = %d ITS[%d] TPC[%d] TRD[%d]\n", label, esdTrack->GetITSLabel(), esdTrack->GetTPCLabel(), esdTrack-   >GetTRDLabel());
242             Int_t kref = 0;
243             while(kref<nRefs){
244               ref = mcParticle->GetTrackReference(kref);
245               printf("\ttrackRef[%2d] @ %7.3f\n", kref, ref->LocalX());
246               kref++;
247             }*/
248       }
249       if(fDebugLevel>=2) printf("NtrackRefs[%d(%d)]\n", fTrackInfo->GetNTrackRefs(), nRefs);
250     } else {
251       new (fTrackInfo) AliTRDtrackInfo();
252       fTrackInfo->SetPDG(fPdg);
253     }
254
255     // copy some relevant info to TRD track info
256     fTrackInfo->SetStatus(esdTrack->GetStatus());
257     fTrackInfo->SetTrackId(esdTrack->GetID());
258     fTrackInfo->SetLabel(label);
259     fTrackInfo->SetNumberOfClustersRefit(esdTrack->GetNcls(2));
260     nclsTrklt = 0;
261   
262
263     // read REC info
264     esdFriendTrack = fESDfriend->GetTrack(itrk);
265     if(esdFriendTrack){
266       Int_t icalib = 0;
267       while((calObject = esdFriendTrack->GetCalibObject(icalib++))){
268         if(strcmp(calObject->IsA()->GetName(),"AliTRDtrackV1") != 0) continue; // Look for the TRDtrack
269         track = dynamic_cast<AliTRDtrackV1*>(calObject);
270         //nTRD++;
271         if(!esdTrack->GetNcls(2)){
272           printf("No TRD clusters but track\n");
273           nTRD++;  
274         }  
275         if(!track) continue;
276         if(fDebugLevel>=3) printf("TRD track OK\n");
277         fTrackInfo->SetTRDtrack(track);
278         break;
279       }
280       if(fDebugLevel>=2) printf("Ntracklets[%d]\n", fTrackInfo->GetNTracklets());
281     } else if(fDebugLevel>=2) printf("No ESD friends\n");
282     if(op) fTrackInfo->SetOuterParam(op);
283
284     if(fDebugLevel >= 2){
285       Int_t ncls = esdTrack->GetNcls(2);
286       nclsTrklt = fTrackInfo->GetNumberOfClusters();
287       (*fDebugStream) << "trackInfo"
288       << "ncls="       << ncls
289       << "Ncls="       << nclsTrklt
290       << "TrackInfo.=" << fTrackInfo
291       << "\n";
292     }
293   
294     fContainer->Add(new AliTRDtrackInfo(*fTrackInfo));
295     fTrackInfo->Delete("");
296   }
297   if(fDebugLevel>=1) printf("%3d Tracks: TPC[%d] TRD[%d]\n", (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTPC, nTRD);
298
299   // Insert also MC tracks which are passing TRD where the track is not reconstructed
300   if(HasMCdata()){
301     if(fDebugLevel > 10){
302       printf("Output of the MC track map:\n");
303       for(Int_t itk = 0; itk < fMC->GetNumberOfTracks();  itk++)
304         printf("trackMap[%d] = %s\n", itk, trackMap[itk] == kTRUE ? "TRUE" : "kFALSE");
305     }
306   
307     for(Int_t itk = 0; itk < fMC->GetNumberOfTracks(); itk++){
308       if(fDebugLevel >=2 ) printf("Number of MC tracks: %d\n", fMC->GetNumberOfTracks());
309       if(trackMap[itk]) continue;
310       AliMCParticle *mcParticle = fMC->GetTrack(TMath::Abs(itk));
311       Int_t fPdg = mcParticle->Particle()->GetPdgCode();
312       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
313       Int_t iref = 0; AliTrackReference *ref = 0x0; 
314       Int_t nRefsTRD = 0;
315       new(fTrackInfo) AliTRDtrackInfo();
316       fTrackInfo->SetPDG(fPdg);
317       while(iref<nRefs){
318         ref = mcParticle->GetTrackReference(iref);
319         if(fDebugLevel > 3) printf("\ttrackRef[%2d] @ %7.3f", iref, ref->LocalX());
320         if(ref->LocalX() > 250. && ref->LocalX() < 370.){
321           if(fDebugLevel > 3) printf(" OK\n");
322           fTrackInfo->AddTrackRef(ref);
323           nRefsTRD++;
324         }
325         else
326           if(fDebugLevel > 3) printf("\n");
327         iref++;
328       }
329       if(!nRefsTRD){
330         // In this stage we at least require 1 hit inside TRD. What will be done with this tracks is a task for the 
331         // analysis job
332         fTrackInfo->Delete("");
333         continue;
334       }
335       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
336       fTrackInfo->SetLabel(itk);
337       if(fDebugLevel >= 1){
338         Int_t ncls = esdTrack->GetNcls(2);
339         (*fDebugStream) << "trackInfo"
340         << "ntrackRefs=" << ncls
341         << "TrackInfo.=" << fTrackInfo
342         << "\n";
343       }
344       if(fDebugLevel > 2)printf("Registering rejected MC track with label %d\n", itk);
345       fContainer->Add(new AliTRDtrackInfo(*fTrackInfo));
346       fTrackInfo->Delete("");
347     }
348     delete[] trackMap;
349   }
350   PostData(0, fContainer);
351   PostData(1, fEventInfo);
352 }
353
354
355 //____________________________________________________________________
356 void AliTRDtrackInfoGen::Terminate(Option_t *)
357 {
358   //
359   // Stays empty because we are only interested in the tree
360   //
361   if(fDebugLevel>=1) printf("Terminate:\n");
362   //TFile *f =((TFile*)gROOT->FindObject("TRD.TrackInfo.root"));
363   //f->cd(); f->Write(); f->Close();
364
365   if(fDebugStream){ 
366     delete fDebugStream;
367     fDebugStream = 0x0;
368   }
369 }