MUON task for the analysis train to fill the muon information in the generic AODs...
[u/mrichter/AliRoot.git] / PWG3 / AliAnalysisTaskESDMuonFilter.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: AliAnalysisTaskESDMuonFilter.cxx 24535 2008-03-16 22:43:30Z fca $ */
17  
18 #include <TChain.h>
19 #include <TFile.h>
20
21 #include "AliAnalysisTaskESDMuonFilter.h"
22 #include "AliAnalysisManager.h"
23 #include "AliESDEvent.h"
24 #include "AliAODEvent.h"
25 #include "AliESDInputHandler.h"
26 #include "AliAODHandler.h"
27 #include "AliAnalysisFilter.h"
28 #include "AliESDtrack.h"
29 #include "AliESDMuonTrack.h"
30 #include "AliESDVertex.h"
31 #include "AliMultiplicity.h"
32 #include "AliLog.h"
33
34 ClassImp(AliAnalysisTaskESDMuonFilter)
35
36 ////////////////////////////////////////////////////////////////////////
37
38 AliAnalysisTaskESDMuonFilter::AliAnalysisTaskESDMuonFilter():
39     AliAnalysisTaskSE(),
40     fTrackFilter(0x0)
41 {
42   // Default constructor
43 }
44
45 AliAnalysisTaskESDMuonFilter::AliAnalysisTaskESDMuonFilter(const char* name):
46     AliAnalysisTaskSE(name),
47     fTrackFilter(0x0)
48 {
49   // Constructor
50 }
51
52 void AliAnalysisTaskESDMuonFilter::UserCreateOutputObjects()
53 {
54 // Create the output container
55     OutputTree()->GetUserInfo()->Add(fTrackFilter);
56 }
57
58 void AliAnalysisTaskESDMuonFilter::Init()
59 {
60     // Initialization
61     if (fDebug > 1) AliInfo("Init() \n");
62     // Call configuration file
63 }
64
65
66 void AliAnalysisTaskESDMuonFilter::UserExec(Option_t */*option*/)
67 {
68 // Execute analysis for current event
69 //                                          
70   Long64_t ientry = Entry();
71   printf("Filter: Analysing event # %5d\n", (Int_t) ientry);
72
73   ConvertESDtoAOD();
74 }
75
76 void AliAnalysisTaskESDMuonFilter::ConvertESDtoAOD() {
77     // ESD Filter analysis task executed for each event
78     AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
79     AliESD* old = esd->GetAliESDOld();
80     
81     // set arrays and pointers
82     Double_t pos[3];
83     Double_t p[3];
84     Double_t p_pos[3];
85     Double_t p_neg[3];
86     Double_t covVtx[6];
87     Double_t covTr[21];
88     Double_t pid[10];
89
90     for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
91     for (Int_t i = 0; i < 21; i++) covTr [i] = 0.;
92
93     
94   // loop over events and fill them
95   
96   // Multiplicity information needed by the header (to be revised!)
97     Int_t nTracks    = esd->GetNumberOfTracks();
98     Int_t nPosTracks = 0;
99     for (Int_t iTrack = 0; iTrack < nTracks; ++iTrack) 
100         if (esd->GetTrack(iTrack)->GetSign()> 0) nPosTracks++;
101     
102     // Update the header
103
104     AliAODHeader* header = AODEvent()->GetHeader();
105     header->SetRunNumber(esd->GetRunNumber());
106     if (old) {
107         header->SetBunchCrossNumber(0);
108         header->SetOrbitNumber(0);
109         header->SetPeriodNumber(0);
110         header->SetEventType(0);
111         header->SetMuonMagFieldScale(-999.); // FIXME
112         header->SetCentrality(-999.);        // FIXME
113     } else {
114         header->SetBunchCrossNumber(esd->GetBunchCrossNumber());
115         header->SetOrbitNumber(esd->GetOrbitNumber());
116         header->SetPeriodNumber(esd->GetPeriodNumber());
117         header->SetEventType(esd->GetEventType());
118         header->SetMuonMagFieldScale(-999.); // FIXME
119         header->SetCentrality(-999.);        // FIXME
120     }
121     
122     header->SetTriggerMask(esd->GetTriggerMask()); 
123     header->SetTriggerCluster(esd->GetTriggerCluster());
124     header->SetMagneticField(esd->GetMagneticField());
125     header->SetZDCN1Energy(esd->GetZDCN1Energy());
126     header->SetZDCP1Energy(esd->GetZDCP1Energy());
127     header->SetZDCN2Energy(esd->GetZDCN2Energy());
128     header->SetZDCP2Energy(esd->GetZDCP2Energy());
129     header->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
130     header->SetRefMultiplicity(nTracks);
131     header->SetRefMultiplicityPos(nPosTracks);
132     header->SetRefMultiplicityNeg(nTracks - nPosTracks);
133 //
134 //    
135     Int_t nV0s      = esd->GetNumberOfV0s();
136     Int_t nCascades = esd->GetNumberOfCascades();
137     Int_t nKinks    = esd->GetNumberOfKinks();
138     Int_t nVertices = nV0s + 2*nCascades /*could lead to two vertices, one V0 and the Xi */+ nKinks + 1 /* = prim. vtx*/;    
139     Int_t nJets     = 0;
140     Int_t nCaloClus = esd->GetNumberOfCaloClusters();
141     Int_t nFmdClus  = 0;
142     Int_t nPmdClus  = esd->GetNumberOfPmdTracks();
143     
144     printf("   NV0=%d  NCASCADES=%d  NKINKS=%d\n", nV0s, nCascades, nKinks);
145
146     AODEvent()->ResetStd(nTracks, nVertices, nV0s+nCascades, nJets, nCaloClus, nFmdClus, nPmdClus);
147
148     AliAODTrack *aodTrack = 0x0;
149     
150     // Access to the AOD container of vertices
151     TClonesArray &vertices = *(AODEvent()->GetVertices());
152     Int_t jVertices=0;
153     
154     // Access to the AOD container of tracks
155     TClonesArray &tracks = *(AODEvent()->GetTracks());
156     Int_t jTracks=0; 
157     
158     // Add primary vertex. 
159     const AliESDVertex *vtx = esd->GetPrimaryVertex();
160     
161     vtx->GetXYZ(pos); // position
162     vtx->GetCovMatrix(covVtx); //covariance matrix
163     
164     AliAODVertex * primary = new(vertices[jVertices++])
165         AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, AliAODVertex::kPrimary);
166     primary->Print();
167     
168     // muon tracks
169     Int_t nMuTracks = esd->GetNumberOfMuonTracks();
170     
171     printf("Number of Muon Tracks=%d\n",nMuTracks);
172     
173     for (Int_t nMuTrack = 0; nMuTrack < nMuTracks; ++nMuTrack) {
174         
175         AliESDMuonTrack *esdMuTrack = esd->GetMuonTrack(nMuTrack); 
176             
177         p[0] = esdMuTrack->Px(); 
178         p[1] = esdMuTrack->Py(); 
179         p[2] = esdMuTrack->Pz();
180         pos[0] = primary->GetX(); 
181         pos[1] = primary->GetY(); 
182         pos[2] = primary->GetZ();
183         
184         // has to be changed once the muon pid is provided by the ESD
185         for (Int_t i = 0; i < 10; pid[i++] = 0.); pid[AliAODTrack::kMuon]=1.;
186         
187 //      primary->AddDaughter(
188         primary->AddDaughter(aodTrack =
189             new(tracks[jTracks++]) AliAODTrack(0, // no ID provided
190                                                0, // no label provided
191                                                p,
192                                                kTRUE,
193                                                pos,
194                                                kFALSE,
195                                                NULL, // no covariance matrix provided
196                                                (Short_t)-99, // no charge provided
197                                                0, // no ITSClusterMap
198                                                pid,
199                                                primary,
200                                                kTRUE,  // check if this is right
201                                                kTRUE,  // not used for vertex fit
202                                                AliAODTrack::kPrimary)
203             );
204   
205         aodTrack->ConvertAliPIDtoAODPID();  
206         aodTrack->SetHitsPatternInTrigCh(esdMuTrack->GetHitsPatternInTrigCh());
207         Int_t track2Trigger = esdMuTrack->GetMatchTrigger();
208         aodTrack->SetMatchTrigger(track2Trigger);
209         if (track2Trigger) 
210           aodTrack->SetChi2MatchTrigger(esdMuTrack->GetChi2MatchTrigger());
211         else 
212           aodTrack->SetChi2MatchTrigger(0.);
213     }
214         
215     tracks.Expand(jTracks); // remove 'empty slots' due to unwritten tracks
216   
217
218     return;
219 }
220
221 void AliAnalysisTaskESDMuonFilter::Terminate(Option_t */*option*/)
222 {
223 // Terminate analysis
224 //
225     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
226 }