Separating writing and reading of raw data (Christian)
[u/mrichter/AliRoot.git] / MUON / AliMUONReconstructor.cxx
CommitLineData
cac2eb58 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 **************************************************************************/
cac2eb58 15/* $Id$ */
16
17///////////////////////////////////////////////////////////////////////////////
18// //
b002ac70 19// class for MUON reconstruction //
cac2eb58 20// //
21///////////////////////////////////////////////////////////////////////////////
b002ac70 22#include <TParticle.h>
30178c30 23#include <TArrayF.h>
24
cac2eb58 25#include "AliRunLoader.h"
b002ac70 26#include "AliHeader.h"
27#include "AliGenEventHeader.h"
cac2eb58 28#include "AliESD.h"
cf464691 29#include "AliMUONReconstructor.h"
cc87ebcd 30
cac2eb58 31#include "AliMUONData.h"
29f1b13a 32#include "AliMUONTrackReconstructor.h"
cac2eb58 33#include "AliMUONClusterReconstructor.h"
cac2eb58 34#include "AliMUONClusterFinderVS.h"
346fb008 35#include "AliMUONClusterFinderAZ.h"
cc87ebcd 36#include "AliMUONEventRecoCombi.h"
cac2eb58 37#include "AliMUONTrack.h"
38#include "AliMUONTrackParam.h"
39#include "AliMUONTriggerTrack.h"
40#include "AliESDMuonTrack.h"
a19e2543 41#include "AliMUONRawReader.h"
1197ff51 42
cf464691 43#include "AliRawReader.h"
cac2eb58 44
29f1b13a 45
cac2eb58 46ClassImp(AliMUONReconstructor)
47//_____________________________________________________________________________
48AliMUONReconstructor::AliMUONReconstructor()
30178c30 49 : AliReconstructor()
8789635b 50{
51}
52//_____________________________________________________________________________
53AliMUONReconstructor::~AliMUONReconstructor()
cac2eb58 54{
55}
56//_____________________________________________________________________________
57void AliMUONReconstructor::Reconstruct(AliRunLoader* runLoader) const
58{
59// AliLoader
60 AliLoader* loader = runLoader->GetLoader("MUONLoader");
61 Int_t nEvents = runLoader->GetNumberOfEvents();
62
3bc8b580 63 AliMUONData* data = new AliMUONData(loader,"MUON","MUON");
64
65// passing loader as argument.
66 AliMUONTrackReconstructor* recoEvent = new AliMUONTrackReconstructor(loader, data);
67
68 if (strstr(GetOption(),"Original"))
69 recoEvent->SetTrackMethod(1); // Original tracking
70 else if (strstr(GetOption(),"Combi"))
71 recoEvent->SetTrackMethod(3); // Combined cluster / track
72 else
73 recoEvent->SetTrackMethod(2); // Kalman
74
75 AliMUONClusterReconstructor* recoCluster = new AliMUONClusterReconstructor(loader, data);
76
7e4a628d 77 AliMUONClusterFinderVS *recModel = recoCluster->GetRecoModel();
3bc8b580 78
8e0ae46c 79 if (!strstr(GetOption(),"VS")) {
1af223d7 80 recModel = (AliMUONClusterFinderVS*) new AliMUONClusterFinderAZ();
9ffe3ef4 81 recoCluster->SetRecoModel(recModel);
9ffe3ef4 82 }
7e4a628d 83 recModel->SetGhostChi2Cut(10);
cac2eb58 84
85 loader->LoadDigits("READ");
86 loader->LoadRecPoints("RECREATE");
87 loader->LoadTracks("RECREATE");
cc87ebcd 88
89 Int_t chBeg = recoEvent->GetTrackMethod() == 3 ? 6 : 0;
90 // Loop over events
cac2eb58 91 for(Int_t ievent = 0; ievent < nEvents; ievent++) {
92 printf("Event %d\n",ievent);
93 runLoader->GetEvent(ievent);
94
8210cb40 95 //----------------------- digit2cluster & Trigger2Trigger -------------------
cac2eb58 96 if (!loader->TreeR()) loader->MakeRecPointsContainer();
cc87ebcd 97
cac2eb58 98 // tracking branch
cc87ebcd 99 if (recoEvent->GetTrackMethod() != 3) {
3bc8b580 100 data->MakeBranch("RC");
101 data->SetTreeAddress("D,RC");
cc87ebcd 102 } else {
3bc8b580 103 data->SetTreeAddress("D");
104 data->SetTreeAddress("RCC");
cc87ebcd 105 }
addbf88a 106 // Important for avoiding a memory leak when reading digits ( to be investigated more in detail)
cc87ebcd 107 // In any case the reading of GLT is needed for the Trigger2Tigger method below
3bc8b580 108 data->SetTreeAddress("GLT");
cc87ebcd 109
3bc8b580 110 data->GetDigits();
cc87ebcd 111 recoCluster->Digits2Clusters(chBeg);
112 if (recoEvent->GetTrackMethod() == 3) {
113 // Combined cluster / track finder
3bc8b580 114 AliMUONEventRecoCombi::Instance()->FillEvent(data, (AliMUONClusterFinderAZ*)recModel);
cc87ebcd 115 ((AliMUONClusterFinderAZ*) recModel)->SetReco(2);
116 }
3bc8b580 117 else data->Fill("RC");
cac2eb58 118
119 // trigger branch
3bc8b580 120 data->MakeBranch("TC");
121 data->SetTreeAddress("TC");
cc87ebcd 122 recoCluster->Trigger2Trigger();
3bc8b580 123 data->Fill("TC");
cac2eb58 124
cc87ebcd 125 //AZ loader->WriteRecPoints("OVERWRITE");
cac2eb58 126
127 //---------------------------- Track & TriggerTrack ---------------------
128 if (!loader->TreeT()) loader->MakeTracksContainer();
129
130 // trigger branch
3bc8b580 131 data->MakeBranch("RL"); //trigger track
132 data->SetTreeAddress("RL");
cac2eb58 133 recoEvent->EventReconstructTrigger();
3bc8b580 134 data->Fill("RL");
cac2eb58 135
136 // tracking branch
3bc8b580 137 data->MakeBranch("RT"); //track
138 data->SetTreeAddress("RT");
cac2eb58 139 recoEvent->EventReconstruct();
3bc8b580 140 data->Fill("RT");
cac2eb58 141
cc87ebcd 142 loader->WriteTracks("OVERWRITE");
143
144 if (recoEvent->GetTrackMethod() == 3) {
145 // Combined cluster / track
146 ((AliMUONClusterFinderAZ*) recModel)->SetReco(1);
3bc8b580 147 data->MakeBranch("RC");
148 data->SetTreeAddress("RC");
149 AliMUONEventRecoCombi::Instance()->FillRecP(data, recoEvent);
150 data->Fill("RC");
cc87ebcd 151 }
152 loader->WriteRecPoints("OVERWRITE");
153
cac2eb58 154 //--------------------------- Resetting branches -----------------------
3bc8b580 155 data->ResetDigits();
156 data->ResetRawClusters();
157 data->ResetTrigger();
cac2eb58 158
3bc8b580 159 data->ResetRawClusters();
160 data->ResetTrigger();
161 data->ResetRecTracks();
162 data->ResetRecTriggerTracks();
29f1b13a 163
cac2eb58 164 }
165 loader->UnloadDigits();
166 loader->UnloadRecPoints();
167 loader->UnloadTracks();
168
169 delete recoCluster;
170 delete recoEvent;
3bc8b580 171 delete data;
cac2eb58 172}
cf464691 173
174//_____________________________________________________________________________
175void AliMUONReconstructor::Reconstruct(AliRunLoader* runLoader, AliRawReader* rawReader) const
176{
177// AliLoader
178 AliLoader* loader = runLoader->GetLoader("MUONLoader");
3bc8b580 179 AliMUONData* data = new AliMUONData(loader,"MUON","MUON");
cf464691 180
3bc8b580 181// passing loader as argument.
182 AliMUONTrackReconstructor* recoEvent = new AliMUONTrackReconstructor(loader, data);
cf464691 183
a19e2543 184 AliMUONRawReader* rawData = new AliMUONRawReader(loader, data);
1197ff51 185
3bc8b580 186 AliMUONClusterReconstructor* recoCluster = new AliMUONClusterReconstructor(loader, data);
7e4a628d 187 AliMUONClusterFinderVS *recModel = recoCluster->GetRecoModel();
188 recModel->SetGhostChi2Cut(10);
cf464691 189
cf464691 190 loader->LoadRecPoints("RECREATE");
191 loader->LoadTracks("RECREATE");
ce6a659c 192 loader->LoadDigits("RECREATE");
193
194
cc87ebcd 195 // Loop over events
8210cb40 196 Int_t iEvent = 0;
cc87ebcd 197
8210cb40 198 while (rawReader->NextEvent()) {
cf464691 199 printf("Event %d\n",iEvent);
200 runLoader->GetEvent(iEvent++);
ce6a659c 201
202 //----------------------- raw2digits & raw2trigger-------------------
203 if (!loader->TreeD()) loader->MakeDigitsContainer();
204
a2da7817 205 // tracking branch
3bc8b580 206 data->MakeBranch("D");
207 data->SetTreeAddress("D");
a2da7817 208 rawData->ReadTrackerDDL(rawReader);
3bc8b580 209 data->Fill("D");
a2da7817 210
211 // trigger branch
3bc8b580 212 data->MakeBranch("GLT");
213 data->SetTreeAddress("GLT");
a2da7817 214 rawData->ReadTriggerDDL(rawReader);
3bc8b580 215 data->Fill("GLT");
ce6a659c 216
217 loader->WriteDigits("OVERWRITE");
cf464691 218
8210cb40 219 //----------------------- digit2cluster & Trigger2Trigger -------------------
cf464691 220 if (!loader->TreeR()) loader->MakeRecPointsContainer();
cc87ebcd 221
cf464691 222 // tracking branch
3bc8b580 223 data->MakeBranch("RC");
224 data->SetTreeAddress("RC");
cc87ebcd 225 recoCluster->Digits2Clusters();
3bc8b580 226 data->Fill("RC");
cf464691 227
228 // trigger branch
3bc8b580 229 data->MakeBranch("TC");
230 data->SetTreeAddress("TC");
cc87ebcd 231 recoCluster->Trigger2Trigger();
3bc8b580 232 data->Fill("TC");
cf464691 233
234 loader->WriteRecPoints("OVERWRITE");
235
236 //---------------------------- Track & TriggerTrack ---------------------
237 if (!loader->TreeT()) loader->MakeTracksContainer();
238
239 // trigger branch
3bc8b580 240 data->MakeBranch("RL"); //trigger track
241 data->SetTreeAddress("RL");
cf464691 242 recoEvent->EventReconstructTrigger();
3bc8b580 243 data->Fill("RL");
cf464691 244
245 // tracking branch
3bc8b580 246 data->MakeBranch("RT"); //track
247 data->SetTreeAddress("RT");
cf464691 248 recoEvent->EventReconstruct();
3bc8b580 249 data->Fill("RT");
cf464691 250
cc87ebcd 251 loader->WriteTracks("OVERWRITE");
252
cf464691 253 //--------------------------- Resetting branches -----------------------
3bc8b580 254 data->ResetDigits();
255 data->ResetRawClusters();
256 data->ResetTrigger();
257
258 data->ResetRawClusters();
259 data->ResetTrigger();
260 data->ResetRecTracks();
261 data->ResetRecTriggerTracks();
cc87ebcd 262
cf464691 263 }
cf464691 264 loader->UnloadRecPoints();
265 loader->UnloadTracks();
ce6a659c 266 loader->UnloadDigits();
cf464691 267
268 delete recoCluster;
269 delete recoEvent;
3bc8b580 270 delete data;
cf464691 271}
272
cac2eb58 273//_____________________________________________________________________________
274void AliMUONReconstructor::FillESD(AliRunLoader* runLoader, AliESD* esd) const
275{
276 TClonesArray* recTracksArray = 0;
277 TClonesArray* recTrigTracksArray = 0;
cc87ebcd 278
cac2eb58 279 AliLoader* loader = runLoader->GetLoader("MUONLoader");
280 loader->LoadTracks("READ");
cac2eb58 281 AliMUONData* muonData = new AliMUONData(loader,"MUON","MUON");
282
cc87ebcd 283 // declaration
a2da7817 284 Int_t iEvent;// nPart;
285 Int_t nTrackHits;// nPrimary;
cac2eb58 286 Double_t fitFmin;
b002ac70 287 TArrayF vertex(3);
cac2eb58 288
289 Double_t bendingSlope, nonBendingSlope, inverseBendingMomentum;
290 Double_t xRec, yRec, zRec, chi2MatchTrigger;
291 Bool_t matchTrigger;
292
b002ac70 293 // setting pointer for tracks, triggertracks & trackparam at vertex
cac2eb58 294 AliMUONTrack* recTrack = 0;
295 AliMUONTrackParam* trackParam = 0;
296 AliMUONTriggerTrack* recTriggerTrack = 0;
a2da7817 297// TParticle* particle = new TParticle();
298// AliGenEventHeader* header = 0;
cc87ebcd 299 iEvent = runLoader->GetEventNumber();
cac2eb58 300 runLoader->GetEvent(iEvent);
301
b002ac70 302 // vertex calculation (maybe it exists already somewhere else)
303 vertex[0] = vertex[1] = vertex[2] = 0.;
a2da7817 304 // nPrimary = 0;
305// if ( (header = runLoader->GetHeader()->GenEventHeader()) ) {
306// header->PrimaryVertex(vertex);
307// } else {
308// runLoader->LoadKinematics("READ");
309// runLoader->TreeK()->GetBranch("Particles")->SetAddress(&particle);
310// nPart = (Int_t)runLoader->TreeK()->GetEntries();
311// for(Int_t iPart = 0; iPart < nPart; iPart++) {
312// runLoader->TreeK()->GetEvent(iPart);
313// if (particle->GetFirstMother() == -1) {
314// vertex[0] += particle->Vx();
315// vertex[1] += particle->Vy();
316// vertex[2] += particle->Vz();
317// nPrimary++;
318// }
319// if (nPrimary) {
320// vertex[0] /= (double)nPrimary;
321// vertex[1] /= (double)nPrimary;
322// vertex[2] /= (double)nPrimary;
323// }
324// }
325// }
cac2eb58 326 // setting ESD MUON class
30178c30 327 AliESDMuonTrack* theESDTrack = new AliESDMuonTrack() ;
cac2eb58 328
329 //-------------------- trigger tracks-------------
330 Long_t trigPat = 0;
331 muonData->SetTreeAddress("RL");
332 muonData->GetRecTriggerTracks();
333 recTrigTracksArray = muonData->RecTriggerTracks();
334
335 // ready global trigger pattern from first track
cc87ebcd 336 if (recTrigTracksArray)
cac2eb58 337 recTriggerTrack = (AliMUONTriggerTrack*) recTrigTracksArray->First();
338 if (recTriggerTrack) trigPat = recTriggerTrack->GetGTPattern();
339
340 //printf(">>> Event %d Number of Recconstructed tracks %d \n",iEvent, nrectracks);
cc87ebcd 341
cac2eb58 342 // -------------------- tracks-------------
343 muonData->SetTreeAddress("RT");
344 muonData->GetRecTracks();
345 recTracksArray = muonData->RecTracks();
cc87ebcd 346
cac2eb58 347 Int_t nRecTracks = 0;
348 if (recTracksArray)
349 nRecTracks = (Int_t) recTracksArray->GetEntriesFast(); //
cc87ebcd 350
cac2eb58 351 // loop over tracks
352 for (Int_t iRecTracks = 0; iRecTracks < nRecTracks; iRecTracks++) {
353
354 // reading info from tracks
355 recTrack = (AliMUONTrack*) recTracksArray->At(iRecTracks);
356
889a0215 357 trackParam = (AliMUONTrackParam*) (recTrack->GetTrackParamAtHit())->First();
358 trackParam->ExtrapToVertex(vertex[0],vertex[1],vertex[2]);
cac2eb58 359
360 bendingSlope = trackParam->GetBendingSlope();
361 nonBendingSlope = trackParam->GetNonBendingSlope();
362 inverseBendingMomentum = trackParam->GetInverseBendingMomentum();
363 xRec = trackParam->GetNonBendingCoor();
364 yRec = trackParam->GetBendingCoor();
365 zRec = trackParam->GetZ();
366
367 nTrackHits = recTrack->GetNTrackHits();
368 fitFmin = recTrack->GetFitFMin();
369 matchTrigger = recTrack->GetMatchTrigger();
370 chi2MatchTrigger = recTrack->GetChi2MatchTrigger();
371
372 // setting data member of ESD MUON
30178c30 373 theESDTrack->SetInverseBendingMomentum(inverseBendingMomentum);
374 theESDTrack->SetThetaX(TMath::ATan(nonBendingSlope));
375 theESDTrack->SetThetaY(TMath::ATan(bendingSlope));
889a0215 376 theESDTrack->SetZ(zRec);
377 theESDTrack->SetBendingCoor(yRec); // calculate vertex at ESD or Tracking level ?
378 theESDTrack->SetNonBendingCoor(xRec);
30178c30 379 theESDTrack->SetChi2(fitFmin);
380 theESDTrack->SetNHit(nTrackHits);
381 theESDTrack->SetMatchTrigger(matchTrigger);
382 theESDTrack->SetChi2MatchTrigger(chi2MatchTrigger);
cac2eb58 383
cc87ebcd 384 // storing ESD MUON Track into ESD Event
385 if (nRecTracks != 0)
30178c30 386 esd->AddMuonTrack(theESDTrack);
cac2eb58 387 } // end loop tracks
388
389 // add global trigger pattern
cc87ebcd 390 if (nRecTracks != 0)
cac2eb58 391 esd->SetTrigger(trigPat);
392
393 // reset muondata
394 muonData->ResetRecTracks();
395 muonData->ResetRecTriggerTracks();
396
cc87ebcd 397 //} // end loop on event
398 loader->UnloadTracks();
a2da7817 399 // if (!header)
400// runLoader->UnloadKinematics();
30178c30 401 delete theESDTrack;
cac2eb58 402 delete muonData;
b002ac70 403 // delete particle;
a2da7817 404}//_____________________________________________________________________________
405void AliMUONReconstructor::FillESD(AliRunLoader* runLoader, AliRawReader* /*rawReader*/, AliESD* esd) const
406{
407 // don't need rawReader ???
408 FillESD(runLoader, esd);
cac2eb58 409}