- Class description on 5 lines (Coding conventions)
[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///////////////////////////////////////////////////////////////////////////////
30178c30 22
cf464691 23#include "AliMUONReconstructor.h"
b2d7df0b 24
25#include "AliESD.h"
26#include "AliESDMuonTrack.h"
27#include "AliGenEventHeader.h"
28#include "AliHeader.h"
94f6fba9 29#include "AliLog.h"
b2d7df0b 30#include "AliMUON.h"
31#include "AliMUONCalibrationData.h"
346fb008 32#include "AliMUONClusterFinderAZ.h"
b2d7df0b 33#include "AliMUONClusterFinderVS.h"
34#include "AliMUONClusterReconstructor.h"
35#include "AliMUONData.h"
36#include "AliMUONDigitCalibrator.h"
cc87ebcd 37#include "AliMUONEventRecoCombi.h"
b2d7df0b 38#include "AliMUONRawReader.h"
cac2eb58 39#include "AliMUONTrack.h"
40#include "AliMUONTrackParam.h"
b2d7df0b 41#include "AliMUONTrackReconstructor.h"
cac2eb58 42#include "AliMUONTriggerTrack.h"
cf464691 43#include "AliRawReader.h"
b2d7df0b 44#include "AliRun.h"
45#include "AliRunLoader.h"
46#include "TTask.h"
47#include <TArrayF.h>
48#include <TParticle.h>
94f6fba9 49#include "TStopwatch.h"
29f1b13a 50
cac2eb58 51ClassImp(AliMUONReconstructor)
b2d7df0b 52
cac2eb58 53//_____________________________________________________________________________
54AliMUONReconstructor::AliMUONReconstructor()
b2d7df0b 55 : AliReconstructor(), fCalibrationData(0x0)
8789635b 56{
d19b6003 57/// Default constructor
58
94f6fba9 59 AliDebug(1,"");
8789635b 60}
94f6fba9 61
884a73f1 62//______________________________________________________________________________
63AliMUONReconstructor::AliMUONReconstructor(const AliMUONReconstructor& right)
64 : AliReconstructor(right)
65{
66/// Protected copy constructor (not implemented)
67
68 AliFatal("Copy constructor not provided.");
69}
70
8789635b 71//_____________________________________________________________________________
72AliMUONReconstructor::~AliMUONReconstructor()
cac2eb58 73{
d19b6003 74/// Destructor
75
94f6fba9 76 AliDebug(1,"");
b2d7df0b 77 delete fCalibrationData;
cac2eb58 78}
b2d7df0b 79
884a73f1 80//______________________________________________________________________________
81AliMUONReconstructor&
82AliMUONReconstructor::operator=(const AliMUONReconstructor& right)
83{
84/// Protected assignement operator (not implemented)
85
86 // check assignement to self
87 if (this == &right) return *this;
88
89 AliFatal("Assignement operator not provided.");
90
91 return *this;
92}
93
b2d7df0b 94//_____________________________________________________________________________
95TTask*
96AliMUONReconstructor::GetCalibrationTask(AliMUONData* data) const
97{
d19b6003 98/// Create the calibration task(s).
b2d7df0b 99
100 const AliRun* run = fRunLoader->GetAliRun();
101
102 // Not really clean, but for the moment we must check whether the
103 // simulation has decalibrated the data or not...
104 const AliMUON* muon = static_cast<AliMUON*>(run->GetModule("MUON"));
105 if ( muon->DigitizerType().Contains("NewDigitizer") )
106 {
107 AliInfo("Calibration will occur.");
108 Int_t runNumber = run->GetRunNumber();
109 fCalibrationData = new AliMUONCalibrationData(runNumber);
110 if ( !fCalibrationData->IsValid() )
111 {
112 AliError("Could not retrieve calibrations !");
113 delete fCalibrationData;
114 fCalibrationData = 0x0;
115 return 0x0;
116 }
117 TTask* calibration = new TTask("MUONCalibrator","MUON Digit calibrator");
118 calibration->Add(new AliMUONDigitCalibrator(data,fCalibrationData));
119 //FIXME: calibration->Add(something about dead channels should go here).
120 return calibration;
121 }
122 else
123 {
124 AliInfo("Detected the usage of old digitizer (w/o decalibration). "
125 "Will not calibrate then.");
126 return 0x0;
127 }
128}
129
130//_____________________________________________________________________________
131void
132AliMUONReconstructor::Init(AliRunLoader* runLoader)
133{
d19b6003 134/// Initialize
135
b2d7df0b 136 fRunLoader = runLoader;
137}
138
cac2eb58 139//_____________________________________________________________________________
140void AliMUONReconstructor::Reconstruct(AliRunLoader* runLoader) const
141{
d19b6003 142/// Reconstruct
143/// \todo add more
144
cac2eb58 145 AliLoader* loader = runLoader->GetLoader("MUONLoader");
146 Int_t nEvents = runLoader->GetNumberOfEvents();
147
3bc8b580 148 AliMUONData* data = new AliMUONData(loader,"MUON","MUON");
149
150// passing loader as argument.
151 AliMUONTrackReconstructor* recoEvent = new AliMUONTrackReconstructor(loader, data);
152
153 if (strstr(GetOption(),"Original"))
154 recoEvent->SetTrackMethod(1); // Original tracking
155 else if (strstr(GetOption(),"Combi"))
156 recoEvent->SetTrackMethod(3); // Combined cluster / track
157 else
158 recoEvent->SetTrackMethod(2); // Kalman
159
94f6fba9 160 AliMUONClusterReconstructor* recoCluster = new AliMUONClusterReconstructor(data);
3bc8b580 161
7e4a628d 162 AliMUONClusterFinderVS *recModel = recoCluster->GetRecoModel();
3bc8b580 163
8e0ae46c 164 if (!strstr(GetOption(),"VS")) {
1af223d7 165 recModel = (AliMUONClusterFinderVS*) new AliMUONClusterFinderAZ();
9ffe3ef4 166 recoCluster->SetRecoModel(recModel);
9ffe3ef4 167 }
7e4a628d 168 recModel->SetGhostChi2Cut(10);
cac2eb58 169
170 loader->LoadDigits("READ");
171 loader->LoadRecPoints("RECREATE");
172 loader->LoadTracks("RECREATE");
cc87ebcd 173
b2d7df0b 174 TTask* calibration = GetCalibrationTask(data);
175
cc87ebcd 176 Int_t chBeg = recoEvent->GetTrackMethod() == 3 ? 6 : 0;
177 // Loop over events
cac2eb58 178 for(Int_t ievent = 0; ievent < nEvents; ievent++) {
94f6fba9 179
180 AliDebug(1,Form("Event %d",ievent));
181
cac2eb58 182 runLoader->GetEvent(ievent);
183
8210cb40 184 //----------------------- digit2cluster & Trigger2Trigger -------------------
cac2eb58 185 if (!loader->TreeR()) loader->MakeRecPointsContainer();
cc87ebcd 186
cac2eb58 187 // tracking branch
cc87ebcd 188 if (recoEvent->GetTrackMethod() != 3) {
3bc8b580 189 data->MakeBranch("RC");
190 data->SetTreeAddress("D,RC");
cc87ebcd 191 } else {
3bc8b580 192 data->SetTreeAddress("D");
193 data->SetTreeAddress("RCC");
cc87ebcd 194 }
addbf88a 195 // Important for avoiding a memory leak when reading digits ( to be investigated more in detail)
cc87ebcd 196 // In any case the reading of GLT is needed for the Trigger2Tigger method below
3bc8b580 197 data->SetTreeAddress("GLT");
cc87ebcd 198
3bc8b580 199 data->GetDigits();
b2d7df0b 200
201 if ( calibration )
202 {
203 calibration->ExecuteTask();
204 }
205
cc87ebcd 206 recoCluster->Digits2Clusters(chBeg);
b2d7df0b 207
cc87ebcd 208 if (recoEvent->GetTrackMethod() == 3) {
209 // Combined cluster / track finder
3bc8b580 210 AliMUONEventRecoCombi::Instance()->FillEvent(data, (AliMUONClusterFinderAZ*)recModel);
cc87ebcd 211 ((AliMUONClusterFinderAZ*) recModel)->SetReco(2);
212 }
3bc8b580 213 else data->Fill("RC");
cac2eb58 214
215 // trigger branch
3bc8b580 216 data->MakeBranch("TC");
217 data->SetTreeAddress("TC");
cc87ebcd 218 recoCluster->Trigger2Trigger();
3bc8b580 219 data->Fill("TC");
cac2eb58 220
cc87ebcd 221 //AZ loader->WriteRecPoints("OVERWRITE");
cac2eb58 222
223 //---------------------------- Track & TriggerTrack ---------------------
224 if (!loader->TreeT()) loader->MakeTracksContainer();
225
226 // trigger branch
3bc8b580 227 data->MakeBranch("RL"); //trigger track
228 data->SetTreeAddress("RL");
cac2eb58 229 recoEvent->EventReconstructTrigger();
3bc8b580 230 data->Fill("RL");
cac2eb58 231
232 // tracking branch
3bc8b580 233 data->MakeBranch("RT"); //track
234 data->SetTreeAddress("RT");
cac2eb58 235 recoEvent->EventReconstruct();
3bc8b580 236 data->Fill("RT");
cac2eb58 237
cc87ebcd 238 loader->WriteTracks("OVERWRITE");
239
240 if (recoEvent->GetTrackMethod() == 3) {
241 // Combined cluster / track
242 ((AliMUONClusterFinderAZ*) recModel)->SetReco(1);
3bc8b580 243 data->MakeBranch("RC");
244 data->SetTreeAddress("RC");
245 AliMUONEventRecoCombi::Instance()->FillRecP(data, recoEvent);
246 data->Fill("RC");
cc87ebcd 247 }
248 loader->WriteRecPoints("OVERWRITE");
249
cac2eb58 250 //--------------------------- Resetting branches -----------------------
3bc8b580 251 data->ResetDigits();
252 data->ResetRawClusters();
253 data->ResetTrigger();
cac2eb58 254
3bc8b580 255 data->ResetRawClusters();
256 data->ResetTrigger();
257 data->ResetRecTracks();
258 data->ResetRecTriggerTracks();
29f1b13a 259
cac2eb58 260 }
261 loader->UnloadDigits();
262 loader->UnloadRecPoints();
263 loader->UnloadTracks();
264
265 delete recoCluster;
266 delete recoEvent;
3bc8b580 267 delete data;
b2d7df0b 268 delete calibration;
cac2eb58 269}
cf464691 270
271//_____________________________________________________________________________
272void AliMUONReconstructor::Reconstruct(AliRunLoader* runLoader, AliRawReader* rawReader) const
273{
d19b6003 274/// Recontruct
275/// \todo add more
276
94f6fba9 277 // AliLoader
cf464691 278 AliLoader* loader = runLoader->GetLoader("MUONLoader");
94f6fba9 279 AliMUONData data(loader,"MUON","MUON");
cf464691 280
94f6fba9 281 // passing loader as argument.
282 AliMUONTrackReconstructor recoEvent(loader, &data);
cf464691 283
94f6fba9 284 AliMUONRawReader rawData(&data);
1197ff51 285
94f6fba9 286 AliMUONClusterReconstructor recoCluster(&data);
30eabfb8 287
288 if (strstr(GetOption(),"Original"))
94f6fba9 289 {
290 recoEvent.SetTrackMethod(1); // Original tracking
291 }
30eabfb8 292 else
94f6fba9 293 {
294 recoEvent.SetTrackMethod(2); // Kalman
295 }
296
297 AliMUONClusterFinderVS *recModel = recoCluster.GetRecoModel();
298 if (!strstr(GetOption(),"VS"))
299 {
30eabfb8 300 recModel = (AliMUONClusterFinderVS*) new AliMUONClusterFinderAZ();
94f6fba9 301 recoCluster.SetRecoModel(recModel);
30eabfb8 302 }
7e4a628d 303 recModel->SetGhostChi2Cut(10);
cf464691 304
94f6fba9 305 TTask* calibration = GetCalibrationTask(&data);
b2d7df0b 306
cf464691 307 loader->LoadRecPoints("RECREATE");
308 loader->LoadTracks("RECREATE");
94f6fba9 309 loader->LoadDigits("READ");
310
cc87ebcd 311 // Loop over events
8210cb40 312 Int_t iEvent = 0;
94f6fba9 313
314 TStopwatch totalTimer;
315 TStopwatch rawTimer;
316 TStopwatch calibTimer;
317 TStopwatch clusterTimer;
318 TStopwatch trackingTimer;
319
320 rawTimer.Start(kTRUE); rawTimer.Stop();
321 calibTimer.Start(kTRUE); calibTimer.Stop();
322 clusterTimer.Start(kTRUE); clusterTimer.Stop();
323 trackingTimer.Start(kTRUE); trackingTimer.Stop();
324
325 totalTimer.Start(kTRUE);
326
327 while (rawReader->NextEvent())
328 {
329 AliDebug(1,Form("Event %d",iEvent));
330
cf464691 331 runLoader->GetEvent(iEvent++);
ce6a659c 332
333 //----------------------- raw2digits & raw2trigger-------------------
94f6fba9 334 if (!loader->TreeD())
335 {
336 AliDebug(1,Form("Making Digit Container for event %d",iEvent));
337 loader->MakeDigitsContainer();
338 }
339
340 data.SetTreeAddress("D,GLT");
341 rawTimer.Start(kFALSE);
342 rawData.Raw2Digits(rawReader);
343 rawTimer.Stop();
344
b2d7df0b 345 if ( calibration )
346 {
94f6fba9 347 calibTimer.Start(kFALSE);
b2d7df0b 348 calibration->ExecuteTask();
94f6fba9 349 calibTimer.Stop();
b2d7df0b 350 }
94f6fba9 351
8210cb40 352 //----------------------- digit2cluster & Trigger2Trigger -------------------
94f6fba9 353 clusterTimer.Start(kFALSE);
354
cf464691 355 if (!loader->TreeR()) loader->MakeRecPointsContainer();
cc87ebcd 356
cf464691 357 // tracking branch
94f6fba9 358 data.MakeBranch("RC");
359 data.SetTreeAddress("RC");
360 recoCluster.Digits2Clusters();
361 data.Fill("RC");
cf464691 362
363 // trigger branch
94f6fba9 364 data.MakeBranch("TC");
365 data.SetTreeAddress("TC");
366// recoCluster.Trigger2Trigger();
367 data.Fill("TC");
368
cf464691 369 loader->WriteRecPoints("OVERWRITE");
370
94f6fba9 371 clusterTimer.Stop();
372
cf464691 373 //---------------------------- Track & TriggerTrack ---------------------
94f6fba9 374 trackingTimer.Start(kFALSE);
cf464691 375 if (!loader->TreeT()) loader->MakeTracksContainer();
376
377 // trigger branch
94f6fba9 378 data.MakeBranch("RL"); //trigger track
379 data.SetTreeAddress("RL");
380 recoEvent.EventReconstructTrigger();
381 data.Fill("RL");
cf464691 382
383 // tracking branch
94f6fba9 384 data.MakeBranch("RT"); //track
385 data.SetTreeAddress("RT");
386 recoEvent.EventReconstruct();
387 data.Fill("RT");
cf464691 388
cc87ebcd 389 loader->WriteTracks("OVERWRITE");
94f6fba9 390 trackingTimer.Stop();
391
cf464691 392 //--------------------------- Resetting branches -----------------------
94f6fba9 393 data.ResetDigits();
394 data.ResetRawClusters();
395 data.ResetTrigger();
396
397 data.ResetRawClusters();
398 data.ResetTrigger();
399 data.ResetRecTracks();
400 data.ResetRecTriggerTracks();
cc87ebcd 401
cf464691 402 }
94f6fba9 403
404 totalTimer.Stop();
405
cf464691 406 loader->UnloadRecPoints();
407 loader->UnloadTracks();
ce6a659c 408 loader->UnloadDigits();
cf464691 409
94f6fba9 410 AliInfo(Form("Execution time for converting RAW data to digits in MUON : R:%.2fs C:%.2fs",
411 rawTimer.RealTime(),rawTimer.CpuTime()));
412 AliInfo(Form("Execution time for calibrating MUON : R:%.2fs C:%.2fs",
413 calibTimer.RealTime(),calibTimer.CpuTime()));
414 AliInfo(Form("Execution time for clusterizing MUON : R:%.2fs C:%.2fs",
415 clusterTimer.RealTime(),clusterTimer.CpuTime()));
416 AliInfo(Form("Execution time for tracking MUON : R:%.2fs C:%.2fs",
417 trackingTimer.RealTime(),trackingTimer.CpuTime()));
418 AliInfo(Form("Total Execution time for Reconstruct(from raw) MUON : R:%.2fs C:%.2fs",
419 totalTimer.RealTime(),totalTimer.CpuTime()));
cf464691 420}
421
cac2eb58 422//_____________________________________________________________________________
423void AliMUONReconstructor::FillESD(AliRunLoader* runLoader, AliESD* esd) const
424{
d19b6003 425/// Fill ESD
426/// \todo add more
427
cac2eb58 428 TClonesArray* recTracksArray = 0;
429 TClonesArray* recTrigTracksArray = 0;
cc87ebcd 430
cac2eb58 431 AliLoader* loader = runLoader->GetLoader("MUONLoader");
432 loader->LoadTracks("READ");
cac2eb58 433 AliMUONData* muonData = new AliMUONData(loader,"MUON","MUON");
434
cc87ebcd 435 // declaration
a2da7817 436 Int_t iEvent;// nPart;
437 Int_t nTrackHits;// nPrimary;
cac2eb58 438 Double_t fitFmin;
cac2eb58 439
440 Double_t bendingSlope, nonBendingSlope, inverseBendingMomentum;
441 Double_t xRec, yRec, zRec, chi2MatchTrigger;
442 Bool_t matchTrigger;
443
b002ac70 444 // setting pointer for tracks, triggertracks & trackparam at vertex
cac2eb58 445 AliMUONTrack* recTrack = 0;
446 AliMUONTrackParam* trackParam = 0;
447 AliMUONTriggerTrack* recTriggerTrack = 0;
a2da7817 448// TParticle* particle = new TParticle();
449// AliGenEventHeader* header = 0;
cc87ebcd 450 iEvent = runLoader->GetEventNumber();
cac2eb58 451 runLoader->GetEvent(iEvent);
452
94f6fba9 453 // Get vertex
454 Double_t vertex[3] = {0};
455 const AliESDVertex *esdVert = esd->GetVertex();
456 if (esdVert) esdVert->GetXYZ(vertex);
457
458 // nPrimary = 0;
a2da7817 459// if ( (header = runLoader->GetHeader()->GenEventHeader()) ) {
460// header->PrimaryVertex(vertex);
461// } else {
462// runLoader->LoadKinematics("READ");
463// runLoader->TreeK()->GetBranch("Particles")->SetAddress(&particle);
464// nPart = (Int_t)runLoader->TreeK()->GetEntries();
465// for(Int_t iPart = 0; iPart < nPart; iPart++) {
466// runLoader->TreeK()->GetEvent(iPart);
467// if (particle->GetFirstMother() == -1) {
468// vertex[0] += particle->Vx();
469// vertex[1] += particle->Vy();
470// vertex[2] += particle->Vz();
471// nPrimary++;
472// }
473// if (nPrimary) {
474// vertex[0] /= (double)nPrimary;
475// vertex[1] /= (double)nPrimary;
476// vertex[2] /= (double)nPrimary;
477// }
478// }
479// }
cac2eb58 480 // setting ESD MUON class
30178c30 481 AliESDMuonTrack* theESDTrack = new AliESDMuonTrack() ;
cac2eb58 482
483 //-------------------- trigger tracks-------------
484 Long_t trigPat = 0;
485 muonData->SetTreeAddress("RL");
486 muonData->GetRecTriggerTracks();
487 recTrigTracksArray = muonData->RecTriggerTracks();
488
489 // ready global trigger pattern from first track
cc87ebcd 490 if (recTrigTracksArray)
cac2eb58 491 recTriggerTrack = (AliMUONTriggerTrack*) recTrigTracksArray->First();
492 if (recTriggerTrack) trigPat = recTriggerTrack->GetGTPattern();
493
494 //printf(">>> Event %d Number of Recconstructed tracks %d \n",iEvent, nrectracks);
cc87ebcd 495
cac2eb58 496 // -------------------- tracks-------------
497 muonData->SetTreeAddress("RT");
498 muonData->GetRecTracks();
499 recTracksArray = muonData->RecTracks();
cc87ebcd 500
cac2eb58 501 Int_t nRecTracks = 0;
502 if (recTracksArray)
503 nRecTracks = (Int_t) recTracksArray->GetEntriesFast(); //
cc87ebcd 504
cac2eb58 505 // loop over tracks
506 for (Int_t iRecTracks = 0; iRecTracks < nRecTracks; iRecTracks++) {
507
508 // reading info from tracks
509 recTrack = (AliMUONTrack*) recTracksArray->At(iRecTracks);
510
889a0215 511 trackParam = (AliMUONTrackParam*) (recTrack->GetTrackParamAtHit())->First();
512 trackParam->ExtrapToVertex(vertex[0],vertex[1],vertex[2]);
cac2eb58 513
514 bendingSlope = trackParam->GetBendingSlope();
515 nonBendingSlope = trackParam->GetNonBendingSlope();
516 inverseBendingMomentum = trackParam->GetInverseBendingMomentum();
517 xRec = trackParam->GetNonBendingCoor();
518 yRec = trackParam->GetBendingCoor();
519 zRec = trackParam->GetZ();
520
521 nTrackHits = recTrack->GetNTrackHits();
522 fitFmin = recTrack->GetFitFMin();
523 matchTrigger = recTrack->GetMatchTrigger();
524 chi2MatchTrigger = recTrack->GetChi2MatchTrigger();
525
526 // setting data member of ESD MUON
30178c30 527 theESDTrack->SetInverseBendingMomentum(inverseBendingMomentum);
528 theESDTrack->SetThetaX(TMath::ATan(nonBendingSlope));
529 theESDTrack->SetThetaY(TMath::ATan(bendingSlope));
889a0215 530 theESDTrack->SetZ(zRec);
531 theESDTrack->SetBendingCoor(yRec); // calculate vertex at ESD or Tracking level ?
532 theESDTrack->SetNonBendingCoor(xRec);
30178c30 533 theESDTrack->SetChi2(fitFmin);
534 theESDTrack->SetNHit(nTrackHits);
535 theESDTrack->SetMatchTrigger(matchTrigger);
536 theESDTrack->SetChi2MatchTrigger(chi2MatchTrigger);
cac2eb58 537
cc87ebcd 538 // storing ESD MUON Track into ESD Event
539 if (nRecTracks != 0)
30178c30 540 esd->AddMuonTrack(theESDTrack);
cac2eb58 541 } // end loop tracks
542
543 // add global trigger pattern
cc87ebcd 544 if (nRecTracks != 0)
cac2eb58 545 esd->SetTrigger(trigPat);
546
547 // reset muondata
548 muonData->ResetRecTracks();
549 muonData->ResetRecTriggerTracks();
550
cc87ebcd 551 //} // end loop on event
552 loader->UnloadTracks();
a2da7817 553 // if (!header)
554// runLoader->UnloadKinematics();
30178c30 555 delete theESDTrack;
cac2eb58 556 delete muonData;
b002ac70 557 // delete particle;
a2da7817 558}//_____________________________________________________________________________
559void AliMUONReconstructor::FillESD(AliRunLoader* runLoader, AliRawReader* /*rawReader*/, AliESD* esd) const
560{
d19b6003 561/// Fill ESD
562/// \todo add more
563
a2da7817 564 // don't need rawReader ???
565 FillESD(runLoader, esd);
cac2eb58 566}