]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONEventReconstructor.cxx
New package for reconstructed tracks (A. Gheata):
[u/mrichter/AliRoot.git] / MUON / AliMUONEventReconstructor.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 /*
17 $Log$
18 Revision 1.18  2000/10/26 12:47:03  gosset
19 Real distance between chambers of each station taken into account
20 for the reconstruction parameters "fSegmentMaxDistBending[5]"
21
22 Revision 1.17  2000/10/24 09:26:20  gosset
23 Comments updated
24
25 Revision 1.16  2000/10/24 09:22:35  gosset
26 Method AddHitsForRecFromRawClusters: real Z of raw cluster and not Z of chamber
27
28 Revision 1.15  2000/10/12 15:17:30  gosset
29 Sign of fSimpleBValue corrected: sign ox Bx and not Bz (thanks to Galina)
30
31 Revision 1.14  2000/10/04 18:21:26  morsch
32 Include stdlib.h
33
34 Revision 1.13  2000/10/02 21:28:09  fca
35 Removal of useless dependecies via forward declarations
36
37 Revision 1.12  2000/10/02 16:58:29  egangler
38 Cleaning of the code :
39 -> coding conventions
40 -> void Streamers
41 -> some useless includes removed or replaced by "class" statement
42
43 Revision 1.11  2000/09/22 09:16:33  hristov
44 Casting needed on DEC
45
46 Revision 1.10  2000/09/19 09:49:50  gosset
47 AliMUONEventReconstructor package
48 * track extrapolation independent from reco_muon.F, use of AliMagF...
49 * possibility to use new magnetic field (automatic from generated root file)
50
51 Revision 1.9  2000/07/20 12:45:27  gosset
52 New "EventReconstructor..." structure,
53         hopefully more adapted to tree/streamer.
54 "AliMUONEventReconstructor::RemoveDoubleTracks"
55         to keep only one track among similar ones.
56
57 Revision 1.8  2000/07/18 16:04:06  gosset
58 AliMUONEventReconstructor package:
59 * a few minor modifications and more comments
60 * a few corrections
61   * right sign for Z of raw clusters
62   * right loop over chambers inside station
63   * symmetrized covariance matrix for measurements (TrackChi2MCS)
64   * right sign of charge in extrapolation (ExtrapToZ)
65   * right zEndAbsorber for Branson correction below 3 degrees
66 * use of TVirtualFitter instead of TMinuit for AliMUONTrack::Fit
67 * no parameter for AliMUONTrack::Fit() but more fit parameters in Track object
68
69 Revision 1.7  2000/07/03 12:28:06  gosset
70 Printout at the right place after extrapolation to vertex
71
72 Revision 1.6  2000/06/30 12:01:06  gosset
73 Correction for hit search in the right chamber (JPC)
74
75 Revision 1.5  2000/06/30 10:15:48  gosset
76 Changes to EventReconstructor...:
77 precision fit with multiple Coulomb scattering;
78 extrapolation to vertex with Branson correction in absorber (JPC)
79
80 Revision 1.4  2000/06/27 14:11:36  gosset
81 Corrections against violations of coding conventions
82
83 Revision 1.3  2000/06/16 07:27:08  gosset
84 To remove problem in running RuleChecker, like in MUON-dev
85
86 Revision 1.1.2.5  2000/06/16 07:00:26  gosset
87 To remove problem in running RuleChecker
88
89 Revision 1.1.2.4  2000/06/12 08:00:07  morsch
90 Dummy streamer to solve CINT compilation problem (to be investigated !)
91
92 Revision 1.1.2.3  2000/06/09 20:59:57  morsch
93 Make includes consistent with new file structure.
94
95 Revision 1.1.2.2  2000/06/09 12:58:05  gosset
96 Removed comment beginnings in Log sections of .cxx files
97 Suppressed most violations of coding rules
98
99 Revision 1.1.2.1  2000/06/07 14:44:53  gosset
100 Addition of files for track reconstruction in C++
101 */
102
103 //__________________________________________________________________________
104 //
105 // MUON event reconstructor in ALICE
106 //
107 // This class contains as data:
108 // * the parameters for the event reconstruction
109 // * a pointer to the array of hits to be reconstructed (the event)
110 // * a pointer to the array of segments made with these hits inside each station
111 // * a pointer to the array of reconstructed tracks
112 //
113 // It contains as methods, among others:
114 // * MakeEventToBeReconstructed to build the array of hits to be reconstructed
115 // * MakeSegments to build the segments
116 // * MakeTracks to build the tracks
117 //__________________________________________________________________________
118
119 #include <iostream.h>
120 #include <stdlib.h>
121
122 #include <TRandom.h>
123 #include <TFile.h>
124 #include <TTree.h>
125
126 #include "AliMUONEventReconstructor.h"
127 #include "AliMUON.h"
128 #include "AliMUONHitForRec.h"
129 #include "AliMUONSegment.h"
130 #include "AliMUONHit.h"
131 #include "AliMUONRawCluster.h"
132 #include "AliMUONTrack.h"
133 #include "AliMUONChamber.h"
134 #include "AliMUONTrackHit.h"
135 #include "AliMagF.h"
136 #include "AliRun.h"
137 #include "TParticle.h"
138 #include "AliMUONRecoEvent.h"
139
140 //************* Defaults parameters for reconstruction
141 static const Double_t kDefaultMinBendingMomentum = 3.0;
142 static const Double_t kDefaultMaxSigma2Distance = 16.0;
143 static const Double_t kDefaultBendingResolution = 0.01;
144 static const Double_t kDefaultNonBendingResolution = 0.144;
145 static const Double_t kDefaultChamberThicknessInX0 = 0.03;
146 // Simple magnetic field:
147 // Value taken from macro MUONtracking.C: 0.7 T, hence 7 kG
148 // Length and Position from reco_muon.F, with opposite sign:
149 // Length = ZMAGEND-ZCOIL
150 // Position = (ZMAGEND+ZCOIL)/2
151 // to be ajusted differently from real magnetic field ????
152 static const Double_t kDefaultSimpleBValue = 7.0;
153 static const Double_t kDefaultSimpleBLength = 428.0;
154 static const Double_t kDefaultSimpleBPosition = 1019.0;
155 static const Int_t kDefaultRecGeantHits = 0;
156 static const Double_t kDefaultEfficiency = 0.95;
157
158 static const Int_t kDefaultPrintLevel = 0;
159
160 ClassImp(AliMUONEventReconstructor) // Class implementation in ROOT context
161
162   //__________________________________________________________________________
163 AliMUONEventReconstructor::AliMUONEventReconstructor(void)
164 {
165   // Constructor for class AliMUONEventReconstructor
166   SetReconstructionParametersToDefaults();
167   // Memory allocation for the TClonesArray of hits for reconstruction
168   // Is 10000 the right size ????
169   fHitsForRecPtr = new TClonesArray("AliMUONHitForRec", 10000);
170   fNHitsForRec = 0; // really needed or GetEntriesFast sufficient ????
171   // Memory allocation for the TClonesArray's of segments in stations
172   // Is 2000 the right size ????
173   for (Int_t st = 0; st < kMaxMuonTrackingStations; st++) {
174     fSegmentsPtr[st] = new TClonesArray("AliMUONSegment", 2000);
175     fNSegments[st] = 0; // really needed or GetEntriesFast sufficient ????
176   }
177   // Memory allocation for the TClonesArray of reconstructed tracks
178   // Is 10 the right size ????
179   fRecTracksPtr = new TClonesArray("AliMUONTrack", 10);
180   fNRecTracks = 0; // really needed or GetEntriesFast sufficient ????
181   // Memory allocation for the TClonesArray of hits on reconstructed tracks
182   // Is 100 the right size ????
183   fRecTrackHitsPtr = new TClonesArray("AliMUONTrack", 100);
184   fNRecTrackHits = 0; // really needed or GetEntriesFast sufficient ????
185
186   // Sign of fSimpleBValue according to sign of Bx value at (50,50,950).
187   Float_t b[3], x[3];
188   x[0] = 50.; x[1] = 50.; x[2] = 950.;
189   gAlice->Field()->Field(x, b);
190   fSimpleBValue = TMath::Sign(fSimpleBValue,(Double_t) b[0]);
191   // See how to get fSimple(BValue, BLength, BPosition)
192   // automatically calculated from the actual magnetic field ????
193
194   if (fPrintLevel >= 0) {
195     cout << "AliMUONEventReconstructor constructed with defaults" << endl; Dump();
196     cout << endl << "Magnetic field from root file:" << endl;
197     gAlice->Field()->Dump();
198     cout << endl;
199   }
200   
201   // Initialize to 0 pointers to RecoEvent, tree and tree file
202   fRecoEvent = 0;
203   fEventTree = 0;
204   fTreeFile  = 0;
205   
206   return;
207 }
208
209 AliMUONEventReconstructor::AliMUONEventReconstructor (const AliMUONEventReconstructor& Reconstructor)
210 {
211   // Dummy copy constructor
212 }
213
214 AliMUONEventReconstructor & AliMUONEventReconstructor::operator=(const AliMUONEventReconstructor& Reconstructor)
215 {
216   // Dummy assignment operator
217     return *this;
218 }
219
220   //__________________________________________________________________________
221 AliMUONEventReconstructor::~AliMUONEventReconstructor(void)
222 {
223   // Destructor for class AliMUONEventReconstructor
224   if (fTreeFile) {
225      fTreeFile->Close();
226      delete fTreeFile;
227   }
228 //  if (fEventTree) delete fEventTree;
229   if (fRecoEvent) delete fRecoEvent;
230   delete fHitsForRecPtr; // Correct destruction of everything ???? or delete [] ????
231   for (Int_t st = 0; st < kMaxMuonTrackingStations; st++)
232     delete fSegmentsPtr[st]; // Correct destruction of everything ????
233   return;
234 }
235
236   //__________________________________________________________________________
237 void AliMUONEventReconstructor::SetReconstructionParametersToDefaults(void)
238 {
239   // Set reconstruction parameters to default values
240   // Would be much more convenient with a structure (or class) ????
241   fMinBendingMomentum = kDefaultMinBendingMomentum;
242   fMaxSigma2Distance = kDefaultMaxSigma2Distance;
243
244   AliMUON *pMUON = (AliMUON*) gAlice->GetModule("MUON");
245   // ******** Parameters for making HitsForRec
246   // minimum radius,
247   // like in TRACKF_STAT:
248   // 2 degrees for stations 1 and 2, or ch(0...) from 0 to 3;
249   // 30 cm for stations 3 to 5, or ch(0...) from 4 to 9
250   for (Int_t ch = 0; ch < kMaxMuonTrackingChambers; ch++) {
251     if (ch < 4) fRMin[ch] = TMath::Abs((&(pMUON->Chamber(ch)))->Z()) *
252                   2.0 * TMath::Pi() / 180.0;
253     else fRMin[ch] = 30.0;
254   }
255   // maximum radius
256   // like in TRACKF_STAT (10 degrees ????)
257   fRMax[0] = fRMax[1] = 91.5;
258   fRMax[2] = fRMax[3] = 122.5;
259   fRMax[4] = fRMax[5] = 158.3;
260   fRMax[6] = fRMax[7] = 260.0;
261   fRMax[8] = fRMax[9] = 260.0;
262
263   // ******** Parameters for making segments
264   // should be parametrized ????
265   // according to interval between chambers in a station ????
266   // Maximum distance in non bending plane
267   // 5 * 0.22 just to remember the way it was made in TRACKF_STAT
268   // SIGCUT*DYMAX(IZ)
269   for (Int_t st = 0; st < kMaxMuonTrackingStations; st++)
270     fSegmentMaxDistNonBending[st] = 5. * 0.22;
271   // Maximum distance in bending plane:
272   // values from TRACKF_STAT, corresponding to (J psi 20cm),
273   // scaled to the real distance between chambers in a station
274   fSegmentMaxDistBending[0] = 1.5 *
275     ((&(pMUON->Chamber(1)))->Z() - (&(pMUON->Chamber(0)))->Z()) / 20.0;
276   fSegmentMaxDistBending[1] = 1.5 *
277     ((&(pMUON->Chamber(3)))->Z() - (&(pMUON->Chamber(2)))->Z()) / 20.0;
278   fSegmentMaxDistBending[2] = 3.0 *
279     ((&(pMUON->Chamber(5)))->Z() - (&(pMUON->Chamber(4)))->Z()) / 20.0;
280   fSegmentMaxDistBending[3] = 6.0 *
281     ((&(pMUON->Chamber(7)))->Z() - (&(pMUON->Chamber(6)))->Z()) / 20.0;
282   fSegmentMaxDistBending[4] = 6.0 *
283     ((&(pMUON->Chamber(9)))->Z() - (&(pMUON->Chamber(8)))->Z()) / 20.0;
284   
285   fBendingResolution = kDefaultBendingResolution;
286   fNonBendingResolution = kDefaultNonBendingResolution;
287   fChamberThicknessInX0 = kDefaultChamberThicknessInX0;
288   fSimpleBValue = kDefaultSimpleBValue;
289   fSimpleBLength = kDefaultSimpleBLength;
290   fSimpleBPosition = kDefaultSimpleBPosition;
291   fRecGeantHits = kDefaultRecGeantHits;
292   fEfficiency = kDefaultEfficiency;
293   fPrintLevel = kDefaultPrintLevel;
294   return;
295 }
296
297 //__________________________________________________________________________
298 Double_t AliMUONEventReconstructor::GetImpactParamFromBendingMomentum(Double_t BendingMomentum)
299 {
300   // Returns impact parameter at vertex in bending plane (cm),
301   // from the signed bending momentum "BendingMomentum" in bending plane (GeV/c),
302   // using simple values for dipole magnetic field.
303   // The sign of "BendingMomentum" is the sign of the charge.
304   return (-0.0003 * fSimpleBValue * fSimpleBLength * fSimpleBPosition /
305           BendingMomentum);
306 }
307
308 //__________________________________________________________________________
309 Double_t AliMUONEventReconstructor::GetBendingMomentumFromImpactParam(Double_t ImpactParam)
310 {
311   // Returns signed bending momentum in bending plane (GeV/c),
312   // the sign being the sign of the charge for particles moving forward in Z,
313   // from the impact parameter "ImpactParam" at vertex in bending plane (cm),
314   // using simple values for dipole magnetic field.
315   return (-0.0003 * fSimpleBValue * fSimpleBLength * fSimpleBPosition /
316           ImpactParam);
317 }
318
319 //__________________________________________________________________________
320 void AliMUONEventReconstructor::SetBkgGeantFile(Text_t *BkgGeantFileName)
321 {
322   // Set background file ... for GEANT hits
323   // Must be called after having loaded the firts signal event
324   if (fPrintLevel >= 0) {
325     cout << "Enter SetBkgGeantFile with BkgGeantFileName ``"
326          << BkgGeantFileName << "''" << endl;}
327   if (strlen(BkgGeantFileName)) {
328     // BkgGeantFileName not empty: try to open the file
329     if (fPrintLevel >= 2) {cout << "Before File(Bkg)" << endl; gDirectory->Dump();}
330     fBkgGeantFile = new TFile(BkgGeantFileName);
331     if (fPrintLevel >= 2) {cout << "After File(Bkg)" << endl; gDirectory->Dump();}
332     if (fBkgGeantFile-> IsOpen()) {
333       if (fPrintLevel >= 0) {
334         cout << "Background for GEANT hits in file: ``" << BkgGeantFileName
335              << "'' successfully opened" << endl;}
336     }
337     else {
338       cout << "Background for GEANT hits in file: " << BkgGeantFileName << endl;
339       cout << "NOT FOUND: EXIT" << endl;
340       exit(0); // right instruction for exit ????
341     }
342     // Arrays for "particles" and "hits"
343     fBkgGeantParticles = new TClonesArray("TParticle", 200);
344     fBkgGeantHits = new TClonesArray("AliMUONHit", 2000);
345     // Event number to -1 for initialization
346     fBkgGeantEventNumber = -1;
347     // Back to the signal file:
348     // first signal event must have been loaded previously,
349     // otherwise, Segmentation violation at the next instruction
350     // How is it possible to do smething better ????
351     ((gAlice->TreeK())->GetCurrentFile())->cd();
352     if (fPrintLevel >= 2) {cout << "After cd(gAlice)" << endl; gDirectory->Dump();}
353   }
354   return;
355 }
356
357 //__________________________________________________________________________
358 void AliMUONEventReconstructor::NextBkgGeantEvent(void)
359 {
360   // Get next event in background file for GEANT hits
361   // Goes back to event number 0 when end of file is reached
362   char treeName[20];
363   TBranch *branch;
364   if (fPrintLevel >= 0) {
365     cout << "Enter NextBkgGeantEvent" << endl;}
366   // Clean previous event
367   if(fBkgGeantTK) delete fBkgGeantTK;
368   fBkgGeantTK = NULL;
369   if(fBkgGeantParticles) fBkgGeantParticles->Clear();
370   if(fBkgGeantTH) delete fBkgGeantTH;
371   fBkgGeantTH = NULL;
372   if(fBkgGeantHits) fBkgGeantHits->Clear();
373   // Increment event number
374   fBkgGeantEventNumber++;
375   // Get access to Particles and Hits for event from background file
376   if (fPrintLevel >= 2) {cout << "Before cd(Bkg)" << endl; gDirectory->Dump();}
377   fBkgGeantFile->cd();
378   if (fPrintLevel >= 2) {cout << "After cd(Bkg)" << endl; gDirectory->Dump();}
379   // Particles: TreeK for event and branch "Particles"
380   sprintf(treeName, "TreeK%d", fBkgGeantEventNumber);
381   fBkgGeantTK = (TTree*)gDirectory->Get(treeName);
382   if (!fBkgGeantTK) {
383     if (fPrintLevel >= 0) {
384       cout << "Cannot find Kine Tree for background event: " <<
385         fBkgGeantEventNumber << endl;
386       cout << "Goes back to event 0" << endl;
387     }
388     fBkgGeantEventNumber = 0;
389     sprintf(treeName, "TreeK%d", fBkgGeantEventNumber);
390     fBkgGeantTK = (TTree*)gDirectory->Get(treeName);
391     if (!fBkgGeantTK) {
392       cout << "ERROR: cannot find Kine Tree for background event: " <<
393         fBkgGeantEventNumber << endl;
394       exit(0);
395     }
396   }
397   if (fBkgGeantTK) 
398     fBkgGeantTK->SetBranchAddress("Particles", &fBkgGeantParticles);
399   fBkgGeantTK->GetEvent(0); // why event 0 ???? necessary ????
400   // Hits: TreeH for event and branch "MUON"
401   sprintf(treeName, "TreeH%d", fBkgGeantEventNumber);
402   fBkgGeantTH = (TTree*)gDirectory->Get(treeName);
403   if (!fBkgGeantTH) {
404     cout << "ERROR: cannot find Hits Tree for background event: " <<
405       fBkgGeantEventNumber << endl;
406       exit(0);
407   }
408   if (fBkgGeantTH && fBkgGeantHits) {
409     branch = fBkgGeantTH->GetBranch("MUON");
410     if (branch) branch->SetAddress(&fBkgGeantHits);
411   }
412   fBkgGeantTH->GetEntries(); // necessary ????
413   // Back to the signal file
414   ((gAlice->TreeK())->GetCurrentFile())->cd();
415   if (fPrintLevel >= 2) {cout << "After cd(gAlice)" << endl; gDirectory->Dump();}
416   return;
417 }
418
419 //__________________________________________________________________________
420 void AliMUONEventReconstructor::EventReconstruct(void)
421 {
422   // To reconstruct one event
423   if (fPrintLevel >= 1) cout << "enter EventReconstruct" << endl;
424   MakeEventToBeReconstructed();
425   MakeSegments();
426   MakeTracks();
427   return;
428 }
429
430   //__________________________________________________________________________
431 void AliMUONEventReconstructor::ResetHitsForRec(void)
432 {
433   // To reset the array and the number of HitsForRec,
434   // and also the number of HitsForRec
435   // and the index of the first HitForRec per chamber
436   if (fHitsForRecPtr) fHitsForRecPtr->Clear();
437   fNHitsForRec = 0;
438   for (Int_t ch = 0; ch < kMaxMuonTrackingChambers; ch++)
439     fNHitsForRecPerChamber[ch] = fIndexOfFirstHitForRecPerChamber[ch] = 0;
440   return;
441 }
442
443   //__________________________________________________________________________
444 void AliMUONEventReconstructor::ResetSegments(void)
445 {
446   // To reset the TClonesArray of segments and the number of Segments
447   // for all stations
448   for (Int_t st = 0; st < kMaxMuonTrackingStations; st++) {
449     if (fSegmentsPtr[st]) fSegmentsPtr[st]->Clear();
450     fNSegments[st] = 0;
451   }
452   return;
453 }
454
455   //__________________________________________________________________________
456 void AliMUONEventReconstructor::ResetTracks(void)
457 {
458   // To reset the TClonesArray of reconstructed tracks
459   if (fRecTracksPtr) fRecTracksPtr->Delete();
460   // Delete in order that the Track destructors are called,
461   // hence the space for the TClonesArray of pointers to TrackHit's is freed
462   fNRecTracks = 0;
463   return;
464 }
465
466   //__________________________________________________________________________
467 void AliMUONEventReconstructor::ResetTrackHits(void)
468 {
469   // To reset the TClonesArray of hits on reconstructed tracks
470   if (fRecTrackHitsPtr) fRecTrackHitsPtr->Clear();
471   fNRecTrackHits = 0;
472   return;
473 }
474
475   //__________________________________________________________________________
476 void AliMUONEventReconstructor::MakeEventToBeReconstructed(void)
477 {
478   // To make the list of hits to be reconstructed,
479   // either from the GEANT hits or from the raw clusters
480   // according to the parameter set for the reconstructor
481   if (fPrintLevel >= 1) cout << "enter MakeEventToBeReconstructed" << endl;
482   ResetHitsForRec();
483   if (fRecGeantHits == 1) {
484     // Reconstruction from GEANT hits
485     // Back to the signal file
486     ((gAlice->TreeK())->GetCurrentFile())->cd();
487     // Signal hits
488     // AliMUON *MUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
489     // Security on MUON ????
490     AddHitsForRecFromGEANT(gAlice->TreeH());
491     // Background hits
492     AddHitsForRecFromBkgGEANT(fBkgGeantTH, fBkgGeantHits);
493     // Sort HitsForRec in increasing order with respect to chamber number
494     SortHitsForRecWithIncreasingChamber();
495   }
496   else {
497     // Reconstruction from raw clusters
498     // AliMUON *MUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
499     // Security on MUON ????
500     // TreeR assumed to be be "prepared" in calling function
501     // by "MUON->GetTreeR(nev)" ????
502     TTree *treeR = gAlice->TreeR();
503     AddHitsForRecFromRawClusters(treeR);
504     // No sorting: it is done automatically in the previous function
505   }
506   if (fPrintLevel >= 10) {
507     cout << "end of MakeEventToBeReconstructed" << endl;
508     cout << "NHitsForRec: " << fNHitsForRec << endl;
509     for (Int_t ch = 0; ch < kMaxMuonTrackingChambers; ch++) {
510       cout << "chamber(0...): " << ch
511            << "  NHitsForRec: " << fNHitsForRecPerChamber[ch]
512            << "  index(first HitForRec): " << fIndexOfFirstHitForRecPerChamber[ch]
513            << endl;
514       for (Int_t hit = fIndexOfFirstHitForRecPerChamber[ch];
515            hit < fIndexOfFirstHitForRecPerChamber[ch] + fNHitsForRecPerChamber[ch];
516            hit++) {
517         cout << "HitForRec index(0...): " << hit << endl;
518         ((*fHitsForRecPtr)[hit])->Dump();
519       }
520     }
521   }
522   return;
523 }
524
525   //__________________________________________________________________________
526 void AliMUONEventReconstructor::AddHitsForRecFromGEANT(TTree *TH)
527 {
528   // To add to the list of hits for reconstruction
529   // the GEANT signal hits from a hit tree TH.
530   if (fPrintLevel >= 2)
531     cout << "enter AddHitsForRecFromGEANT with TH: " << TH << endl;
532   if (TH == NULL) return;
533   AliMUON *pMUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
534   // Security on MUON ????
535   // See whether it could be the same for signal and background ????
536   // Loop over tracks in tree
537   Int_t ntracks = (Int_t) TH->GetEntries();
538   if (fPrintLevel >= 2)
539     cout << "ntracks: " << ntracks << endl;
540   for (Int_t track = 0; track < ntracks; track++) {
541     gAlice->ResetHits();
542     TH->GetEvent(track);
543     // Loop over hits
544     Int_t hit = 0;
545     for (AliMUONHit* mHit = (AliMUONHit*) pMUON->FirstHit(-1); 
546          mHit;
547          mHit = (AliMUONHit*) pMUON->NextHit(), hit++) {
548       NewHitForRecFromGEANT(mHit,track, hit, 1);
549     } // end of hit loop
550   } // end of track loop
551   return;
552 }
553
554   //__________________________________________________________________________
555 void AliMUONEventReconstructor::AddHitsForRecFromBkgGEANT(TTree *TH, TClonesArray *Hits)
556 {
557   // To add to the list of hits for reconstruction
558   // the GEANT background hits from a hit tree TH and a pointer Hits to a hit list.
559   // How to have only one function "AddHitsForRecFromGEANT" ????
560   if (fPrintLevel >= 2)
561     cout << "enter AddHitsForRecFromBkgGEANT with TH: " << TH << endl;
562   if (TH == NULL) return;
563   // Loop over tracks in tree
564   Int_t ntracks = (Int_t) TH->GetEntries();
565   if (fPrintLevel >= 2)
566     cout << "ntracks: " << ntracks << endl;
567   for (Int_t track = 0; track < ntracks; track++) {
568     if (Hits) Hits->Clear();
569     TH->GetEvent(track);
570     // Loop over hits
571     for (Int_t hit = 0; hit < Hits->GetEntriesFast(); hit++) {
572       NewHitForRecFromGEANT((AliMUONHit*) (*Hits)[hit], track, hit, 0);
573     } // end of hit loop
574   } // end of track loop
575   return;
576 }
577
578   //__________________________________________________________________________
579 AliMUONHitForRec* AliMUONEventReconstructor::NewHitForRecFromGEANT(AliMUONHit* Hit, Int_t TrackNumber, Int_t HitNumber, Int_t Signal)
580 {
581   // To make a new hit for reconstruction from a GEANT hit pointed to by "Hit",
582   // with hit number "HitNumber" in the track numbered "TrackNumber",
583   // either from signal ("Signal" = 1) or background ("Signal" = 0) event.
584   // Selects hits in tracking (not trigger) chambers.
585   // Takes into account the efficiency (fEfficiency)
586   // and the smearing from resolution (fBendingResolution and fNonBendingResolution).
587   // Adds a condition on the radius between RMin and RMax
588   // to better simulate the real chambers.
589   // Returns the pointer to the new hit for reconstruction,
590   // or NULL in case of inefficiency or non tracking chamber or bad radius.
591   // No condition on at most 20 cm from a muon from a resonance
592   // like in Fortran TRACKF_STAT.
593   AliMUONHitForRec* hitForRec;
594   Double_t bendCoor, nonBendCoor, radius;
595   Int_t chamber = Hit->fChamber - 1; // chamber(0...)
596   // only in tracking chambers (fChamber starts at 1)
597   if (chamber >= kMaxMuonTrackingChambers) return NULL;
598   // only if hit is efficient (keep track for checking ????)
599   if (gRandom->Rndm() > fEfficiency) return NULL;
600   // only if radius between RMin and RMax
601   bendCoor = Hit->Y();
602   nonBendCoor = Hit->X();
603   radius = TMath::Sqrt((bendCoor * bendCoor) + (nonBendCoor * nonBendCoor));
604   if ((radius < fRMin[chamber]) || (radius > fRMax[chamber])) return NULL;
605   // new AliMUONHitForRec from GEANT hit and increment number of AliMUONHitForRec's
606   hitForRec = new ((*fHitsForRecPtr)[fNHitsForRec]) AliMUONHitForRec(Hit);
607   fNHitsForRec++;
608   // add smearing from resolution
609   hitForRec->SetBendingCoor(bendCoor + gRandom->Gaus(0., fBendingResolution));
610   hitForRec->SetNonBendingCoor(nonBendCoor
611                                + gRandom->Gaus(0., fNonBendingResolution));
612   // more information into HitForRec
613   //  resolution: angular effect to be added here ????
614   hitForRec->SetBendingReso2(fBendingResolution * fBendingResolution);
615   hitForRec->SetNonBendingReso2(fNonBendingResolution * fNonBendingResolution);
616   //  GEANT track info
617   hitForRec->SetHitNumber(HitNumber);
618   hitForRec->SetTHTrack(TrackNumber);
619   hitForRec->SetGeantSignal(Signal);
620   if (fPrintLevel >= 10) {
621     cout << "track: " << TrackNumber << " hit: " << HitNumber << endl;
622     Hit->Dump();
623     cout << "AliMUONHitForRec number (1...): " << fNHitsForRec << endl;
624     hitForRec->Dump();}
625   return hitForRec;
626 }
627
628   //__________________________________________________________________________
629 void AliMUONEventReconstructor::SortHitsForRecWithIncreasingChamber()
630 {
631   // Sort HitsForRec's in increasing order with respect to chamber number.
632   // Uses the function "Compare".
633   // Update the information for HitsForRec per chamber too.
634   Int_t ch, nhits, prevch;
635   fHitsForRecPtr->Sort();
636   for (ch = 0; ch < kMaxMuonTrackingChambers; ch++) {
637     fNHitsForRecPerChamber[ch] = 0;
638     fIndexOfFirstHitForRecPerChamber[ch] = 0;
639   }
640   prevch = 0; // previous chamber
641   nhits = 0; // number of hits in current chamber
642   // Loop over HitsForRec
643   for (Int_t hit = 0; hit < fNHitsForRec; hit++) {
644     // chamber number (0...)
645     ch = ((AliMUONHitForRec*)  ((*fHitsForRecPtr)[hit]))->GetChamberNumber();
646     // increment number of hits in current chamber
647     (fNHitsForRecPerChamber[ch])++;
648     // update index of first HitForRec in current chamber
649     // if chamber number different from previous one
650     if (ch != prevch) {
651       fIndexOfFirstHitForRecPerChamber[ch] = hit;
652       prevch = ch;
653     }
654   }
655   return;
656 }
657
658 //   //__________________________________________________________________________
659 // void AliMUONEventReconstructor::AddHitsForRecFromCathodeCorrelations(TTree* TC)
660 // {
661 //   // OLD VERSION WHEN ONE ONE WAS USING SO CALLED CATHODE CORRELATIONS
662 //   // To add to the list of hits for reconstruction
663 //   // the (cathode correlated) raw clusters
664 //   // No condition added, like in Fortran TRACKF_STAT,
665 //   // on the radius between RMin and RMax.
666 //   AliMUONHitForRec *hitForRec;
667 //   if (fPrintLevel >= 1) cout << "enter AddHitsForRecFromCathodeCorrelations" << endl;
668 //   AliMUON *MUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
669 //   // Security on MUON ????
670 //   // Loop over tracking chambers
671 //   for (Int_t ch = 0; ch < kMaxMuonTrackingChambers; ch++) {
672 //     // number of HitsForRec to 0 for the chamber
673 //     fNHitsForRecPerChamber[ch] = 0;
674 //     // index of first HitForRec for the chamber
675 //     if (ch == 0) fIndexOfFirstHitForRecPerChamber[ch] = 0;
676 //     else fIndexOfFirstHitForRecPerChamber[ch] = fNHitsForRec;
677 //     TClonesArray *reconst_hits  = MUON->ReconstHitsAddress(ch);
678 //     MUON->ResetReconstHits();
679 //     TC->GetEvent();
680 //     Int_t ncor = (Int_t)reconst_hits->GetEntries();
681 //     // Loop over (cathode correlated) raw clusters
682 //     for (Int_t cor = 0; cor < ncor; cor++) {
683 //       AliMUONReconstHit * mCor = 
684 //      (AliMUONReconstHit*) reconst_hits->UncheckedAt(cor);
685 //       // new AliMUONHitForRec from (cathode correlated) raw cluster
686 //       // and increment number of AliMUONHitForRec's (total and in chamber)
687 //       hitForRec = new ((*fHitsForRecPtr)[fNHitsForRec]) AliMUONHitForRec(mCor);
688 //       fNHitsForRec++;
689 //       (fNHitsForRecPerChamber[ch])++;
690 //       // more information into HitForRec
691 //       hitForRec->SetChamberNumber(ch);
692 //       hitForRec->SetHitNumber(cor);
693 //       // Z coordinate of the chamber (cm) with sign opposite to GEANT sign
694 //       // could (should) be more exact from chamber geometry ???? 
695 //       hitForRec->SetZ(-(&(MUON->Chamber(ch)))->Z());
696 //       if (fPrintLevel >= 10) {
697 //      cout << "chamber (0...): " << ch <<
698 //        " cathcorrel (0...): " << cor << endl;
699 //      mCor->Dump();
700 //      cout << "AliMUONHitForRec number (1...): " << fNHitsForRec << endl;
701 //      hitForRec->Dump();}
702 //     } // end of cluster loop
703 //   } // end of chamber loop
704 //   return;
705 // }
706
707   //__________________________________________________________________________
708 void AliMUONEventReconstructor::AddHitsForRecFromRawClusters(TTree* TR)
709 {
710   // To add to the list of hits for reconstruction all the raw clusters
711   // No condition added, like in Fortran TRACKF_STAT,
712   // on the radius between RMin and RMax.
713   AliMUONHitForRec *hitForRec;
714   AliMUONRawCluster *clus;
715   Int_t iclus, nclus;
716   TClonesArray *rawclusters;
717   if (fPrintLevel >= 1) cout << "enter AddHitsForRecFromRawClusters" << endl;
718   AliMUON *pMUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
719   // Security on MUON ????
720   // Loop over tracking chambers
721   for (Int_t ch = 0; ch < kMaxMuonTrackingChambers; ch++) {
722     // number of HitsForRec to 0 for the chamber
723     fNHitsForRecPerChamber[ch] = 0;
724     // index of first HitForRec for the chamber
725     if (ch == 0) fIndexOfFirstHitForRecPerChamber[ch] = 0;
726     else fIndexOfFirstHitForRecPerChamber[ch] = fNHitsForRec;
727     rawclusters = pMUON->RawClustAddress(ch);
728     pMUON->ResetRawClusters();
729     TR->GetEvent((Int_t) (TR->GetEntries()) - 1); // to be checked ????
730     nclus = (Int_t) (rawclusters->GetEntries());
731     // Loop over (cathode correlated) raw clusters
732     for (iclus = 0; iclus < nclus; iclus++) {
733       clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(iclus);
734       // new AliMUONHitForRec from raw cluster
735       // and increment number of AliMUONHitForRec's (total and in chamber)
736       hitForRec = new ((*fHitsForRecPtr)[fNHitsForRec]) AliMUONHitForRec(clus);
737       fNHitsForRec++;
738       (fNHitsForRecPerChamber[ch])++;
739       // more information into HitForRec
740       //  resolution: info should be already in raw cluster and taken from it ????
741       hitForRec->SetBendingReso2(fBendingResolution * fBendingResolution);
742       hitForRec->SetNonBendingReso2(fNonBendingResolution * fNonBendingResolution);
743       //  original raw cluster
744       hitForRec->SetChamberNumber(ch);
745       hitForRec->SetHitNumber(iclus);
746       // Z coordinate of the raw cluster (cm)
747       hitForRec->SetZ(clus->fZ[0]);
748       if (fPrintLevel >= 10) {
749         cout << "chamber (0...): " << ch <<
750           " raw cluster (0...): " << iclus << endl;
751         clus->Dump();
752         cout << "AliMUONHitForRec number (1...): " << fNHitsForRec << endl;
753         hitForRec->Dump();}
754     } // end of cluster loop
755   } // end of chamber loop
756   return;
757 }
758
759   //__________________________________________________________________________
760 void AliMUONEventReconstructor::MakeSegments(void)
761 {
762   // To make the list of segments in all stations,
763   // from the list of hits to be reconstructed
764   if (fPrintLevel >= 1) cout << "enter MakeSegments" << endl;
765   ResetSegments();
766   // Loop over stations
767   for (Int_t st = 0; st < kMaxMuonTrackingStations; st++)
768     MakeSegmentsPerStation(st); 
769   if (fPrintLevel >= 10) {
770     cout << "end of MakeSegments" << endl;
771     for (Int_t st = 0; st < kMaxMuonTrackingStations; st++) {
772       cout << "station(0...): " << st
773            << "  Segments: " << fNSegments[st]
774            << endl;
775       for (Int_t seg = 0;
776            seg < fNSegments[st];
777            seg++) {
778         cout << "Segment index(0...): " << seg << endl;
779         ((*fSegmentsPtr[st])[seg])->Dump();
780       }
781     }
782   }
783   return;
784 }
785
786   //__________________________________________________________________________
787 void AliMUONEventReconstructor::MakeSegmentsPerStation(Int_t Station)
788 {
789   // To make the list of segments in station number "Station" (0...)
790   // from the list of hits to be reconstructed.
791   // Updates "fNSegments"[Station].
792   // Segments in stations 4 and 5 are sorted
793   // according to increasing absolute value of "impact parameter"
794   AliMUONHitForRec *hit1Ptr, *hit2Ptr;
795   AliMUONSegment *segment;
796   Bool_t last2st;
797   Double_t bendingSlope, distBend, distNonBend, extBendCoor, extNonBendCoor,
798       impactParam = 0., maxImpactParam = 0.; // =0 to avoid compilation warnings.
799   AliMUON *pMUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
800   if (fPrintLevel >= 1)
801     cout << "enter MakeSegmentsPerStation (0...) " << Station << endl;
802   // first and second chambers (0...) in the station
803   Int_t ch1 = 2 * Station;
804   Int_t ch2 = ch1 + 1;
805   // variable true for stations downstream of the dipole:
806   // Station(0..4) equal to 3 or 4
807   if ((Station == 3) || (Station == 4)) {
808     last2st = kTRUE;
809     // maximum impact parameter (cm) according to fMinBendingMomentum...
810     maxImpactParam =
811       TMath::Abs(GetImpactParamFromBendingMomentum(fMinBendingMomentum));
812   }
813   else last2st = kFALSE;
814   // extrapolation factor from Z of first chamber to Z of second chamber
815   // dZ to be changed to take into account fine structure of chambers ????
816   Double_t extrapFact =
817     (&(pMUON->Chamber(ch2)))->Z() / (&(pMUON->Chamber(ch1)))->Z();
818   // index for current segment
819   Int_t segmentIndex = 0;
820   // Loop over HitsForRec in the first chamber of the station
821   for (Int_t hit1 = fIndexOfFirstHitForRecPerChamber[ch1];
822        hit1 < fIndexOfFirstHitForRecPerChamber[ch1] + fNHitsForRecPerChamber[ch1];
823        hit1++) {
824     // pointer to the HitForRec
825     hit1Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit1]);
826     // extrapolation,
827     // on the straight line joining the HitForRec to the vertex (0,0,0),
828     // to the Z of the second chamber of the station
829     extBendCoor = extrapFact * hit1Ptr->GetBendingCoor();
830     extNonBendCoor = extrapFact * hit1Ptr->GetNonBendingCoor();
831     // Loop over HitsForRec in the second chamber of the station
832     for (Int_t hit2 = fIndexOfFirstHitForRecPerChamber[ch2];
833          hit2 < fIndexOfFirstHitForRecPerChamber[ch2] + fNHitsForRecPerChamber[ch2];
834          hit2++) {
835       // pointer to the HitForRec
836       hit2Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit2]);
837       // absolute values of distances, in bending and non bending planes,
838       // between the HitForRec in the second chamber
839       // and the previous extrapolation
840       distBend = TMath::Abs(hit2Ptr->GetBendingCoor() - extBendCoor);
841       distNonBend = TMath::Abs(hit2Ptr->GetNonBendingCoor() - extNonBendCoor);
842       if (last2st) {
843         // bending slope
844         bendingSlope = (hit1Ptr->GetBendingCoor() - hit2Ptr->GetBendingCoor()) /
845           (hit1Ptr->GetZ() - hit2Ptr->GetZ());
846         // absolute value of impact parameter
847         impactParam =
848           TMath::Abs(hit1Ptr->GetBendingCoor() - hit2Ptr->GetZ() * bendingSlope);
849       }
850       // check for distances not too large,
851       // and impact parameter not too big if stations downstream of the dipole.
852       // Conditions "distBend" and "impactParam" correlated for these stations ????
853       if ((distBend < fSegmentMaxDistBending[Station]) &&
854           (distNonBend < fSegmentMaxDistNonBending[Station]) &&
855           (!last2st || (impactParam < maxImpactParam))) {
856         // make new segment
857         segment = new ((*fSegmentsPtr[Station])[segmentIndex])
858           AliMUONSegment(hit1Ptr, hit2Ptr);
859         // update "link" to this segment from the hit in the first chamber
860         if (hit1Ptr->GetNSegments() == 0)
861           hit1Ptr->SetIndexOfFirstSegment(segmentIndex);
862         hit1Ptr->SetNSegments(hit1Ptr->GetNSegments() + 1);
863         if (fPrintLevel >= 10) {
864           cout << "segmentIndex(0...): " << segmentIndex
865                << "  distBend: " << distBend
866                << "  distNonBend: " << distNonBend
867                << endl;
868           segment->Dump();
869           cout << "HitForRec in first chamber" << endl;
870           hit1Ptr->Dump();
871           cout << "HitForRec in second chamber" << endl;
872           hit2Ptr->Dump();
873         };
874         // increment index for current segment
875         segmentIndex++;
876       }
877     } //for (Int_t hit2
878   } // for (Int_t hit1...
879   fNSegments[Station] = segmentIndex;
880   // Sorting according to "impact parameter" if station(1..5) 4 or 5,
881   // i.e. Station(0..4) 3 or 4, using the function "Compare".
882   // After this sorting, it is impossible to use
883   // the "fNSegments" and "fIndexOfFirstSegment"
884   // of the HitForRec in the first chamber to explore all segments formed with it.
885   // Is this sorting really needed ????
886   if ((Station == 3) || (Station == 4)) (fSegmentsPtr[Station])->Sort();
887   if (fPrintLevel >= 1) cout << "Station: " << Station << "  NSegments: "
888                              << fNSegments[Station] << endl;
889   return;
890 }
891
892   //__________________________________________________________________________
893 void AliMUONEventReconstructor::MakeTracks(void)
894 {
895   // To make the tracks,
896   // from the list of segments and points in all stations
897   if (fPrintLevel >= 1) cout << "enter MakeTracks" << endl;
898   // The order may be important for the following Reset's
899   ResetTracks();
900   ResetTrackHits();
901   // Look for candidates from at least 3 aligned points in stations(1..) 4 and 5
902   MakeTrackCandidates();
903   // Follow tracks in stations(1..) 3, 2 and 1
904   FollowTracks();
905   // Remove double tracks
906   RemoveDoubleTracks();
907   return;
908 }
909
910   //__________________________________________________________________________
911 Int_t AliMUONEventReconstructor::MakeTrackCandidatesWithTwoSegments(AliMUONSegment *BegSegment)
912 {
913   // To make track candidates with two segments in stations(1..) 4 and 5,
914   // the first segment being pointed to by "BegSegment".
915   // Returns the number of such track candidates.
916   Int_t endStation, iEndSegment, nbCan2Seg;
917   AliMUONSegment *endSegment, *extrapSegment;
918   AliMUONTrack *recTrack;
919   Double_t mcsFactor;
920   if (fPrintLevel >= 1) cout << "enter MakeTrackCandidatesWithTwoSegments" << endl;
921   // Station for the end segment
922   endStation = 7 - (BegSegment->GetHitForRec1())->GetChamberNumber() / 2;
923   // multiple scattering factor corresponding to one chamber
924   mcsFactor = 0.0136 /
925     GetBendingMomentumFromImpactParam(BegSegment->GetBendingImpact());
926   mcsFactor     = fChamberThicknessInX0 * mcsFactor * mcsFactor;
927   // linear extrapolation to end station
928   extrapSegment =
929     BegSegment->CreateSegmentFromLinearExtrapToStation(endStation, mcsFactor);
930   // number of candidates with 2 segments to 0
931   nbCan2Seg = 0;
932   // Loop over segments in the end station
933   for (iEndSegment = 0; iEndSegment < fNSegments[endStation]; iEndSegment++) {
934     // pointer to segment
935     endSegment = (AliMUONSegment*) ((*fSegmentsPtr[endStation])[iEndSegment]);
936     // test compatibility between current segment and "extrapSegment"
937     // 4 because 4 quantities in chi2
938     if ((endSegment->
939          NormalizedChi2WithSegment(extrapSegment,
940                                    fMaxSigma2Distance)) <= 4.0) {
941       // both segments compatible:
942       // make track candidate from "begSegment" and "endSegment"
943       if (fPrintLevel >= 2)
944         cout << "TrackCandidate with Segment " << iEndSegment <<
945           " in Station(0..) " << endStation << endl;
946       // flag for both segments in one track:
947       // to be done in track constructor ????
948       BegSegment->SetInTrack(kTRUE);
949       endSegment->SetInTrack(kTRUE);
950       recTrack = new ((*fRecTracksPtr)[fNRecTracks])
951         AliMUONTrack(BegSegment, endSegment, this);
952       fNRecTracks++;
953       if (fPrintLevel >= 10) recTrack->RecursiveDump();
954       // increment number of track candidates with 2 segments
955       nbCan2Seg++;
956     }
957   } // for (iEndSegment = 0;...
958   delete extrapSegment; // should not delete HitForRec's it points to !!!!
959   return nbCan2Seg;
960 }
961
962   //__________________________________________________________________________
963 Int_t AliMUONEventReconstructor::MakeTrackCandidatesWithOneSegmentAndOnePoint(AliMUONSegment *BegSegment)
964 {
965   // To make track candidates with one segment and one point
966   // in stations(1..) 4 and 5,
967   // the segment being pointed to by "BegSegment".
968   Int_t ch, ch1, ch2, endStation, iHit, iHitMax, iHitMin, nbCan1Seg1Hit;
969   AliMUONHitForRec *extrapHitForRec, *hit;
970   AliMUONTrack *recTrack;
971   Double_t mcsFactor;
972   if (fPrintLevel >= 1)
973     cout << "enter MakeTrackCandidatesWithOneSegmentAndOnePoint" << endl;
974   // station for the end point
975   endStation = 7 - (BegSegment->GetHitForRec1())->GetChamberNumber() / 2;
976   // multiple scattering factor corresponding to one chamber
977   mcsFactor = 0.0136 /
978     GetBendingMomentumFromImpactParam(BegSegment->GetBendingImpact());
979   mcsFactor     = fChamberThicknessInX0 * mcsFactor * mcsFactor;
980   // first and second chambers(0..) in the end station
981   ch1 = 2 * endStation;
982   ch2 = ch1 + 1;
983   // number of candidates to 0
984   nbCan1Seg1Hit = 0;
985   // Loop over chambers of the end station
986   for (ch = ch2; ch >= ch1; ch--) {
987     // linear extrapolation to chamber
988     extrapHitForRec =
989       BegSegment->CreateHitForRecFromLinearExtrapToChamber(ch, mcsFactor);
990     // limits for the hit index in the loop
991     iHitMin = fIndexOfFirstHitForRecPerChamber[ch];
992     iHitMax = iHitMin + fNHitsForRecPerChamber[ch];
993     // Loop over HitForRec's in the chamber
994     for (iHit = iHitMin; iHit < iHitMax; iHit++) {
995       // pointer to HitForRec
996       hit = (AliMUONHitForRec*) ((*fHitsForRecPtr)[iHit]);
997       // test compatibility between current HitForRec and "extrapHitForRec"
998       // 2 because 2 quantities in chi2
999       if ((hit->
1000            NormalizedChi2WithHitForRec(extrapHitForRec,
1001                                        fMaxSigma2Distance)) <= 2.0) {
1002         // both HitForRec's compatible:
1003         // make track candidate from begSegment and current HitForRec
1004         if (fPrintLevel >= 2)
1005           cout << "TrackCandidate with HitForRec " << iHit <<
1006             " in Chamber(0..) " << ch << endl;
1007         // flag for beginning segments in one track:
1008         // to be done in track constructor ????
1009         BegSegment->SetInTrack(kTRUE);
1010         recTrack = new ((*fRecTracksPtr)[fNRecTracks])
1011           AliMUONTrack(BegSegment, hit, this);
1012         // the right place to eliminate "double counting" ???? how ????
1013         fNRecTracks++;
1014         if (fPrintLevel >= 10) recTrack->RecursiveDump();
1015         // increment number of track candidates
1016         nbCan1Seg1Hit++;
1017       }
1018     } // for (iHit = iHitMin;...
1019     delete extrapHitForRec;
1020   } // for (ch = ch2;...
1021   return nbCan1Seg1Hit;
1022 }
1023
1024   //__________________________________________________________________________
1025 void AliMUONEventReconstructor::MakeTrackCandidates(void)
1026 {
1027   // To make track candidates
1028   // with at least 3 aligned points in stations(1..) 4 and 5
1029   // (two Segment's or one Segment and one HitForRec)
1030   Int_t begStation, iBegSegment, nbCan1Seg1Hit, nbCan2Seg;
1031   AliMUONSegment *begSegment;
1032   if (fPrintLevel >= 1) cout << "enter MakeTrackCandidates" << endl;
1033   // Loop over stations(1..) 5 and 4 for the beginning segment
1034   for (begStation = 4; begStation > 2; begStation--) {
1035     // Loop over segments in the beginning station
1036     for (iBegSegment = 0; iBegSegment < fNSegments[begStation]; iBegSegment++) {
1037       // pointer to segment
1038       begSegment = (AliMUONSegment*) ((*fSegmentsPtr[begStation])[iBegSegment]);
1039       if (fPrintLevel >= 2)
1040         cout << "look for TrackCandidate's with Segment " << iBegSegment <<
1041           " in Station(0..) " << begStation << endl;
1042       // Look for track candidates with two segments,
1043       // "begSegment" and all compatible segments in other station.
1044       // Only for beginning station(1..) 5
1045       // because candidates with 2 segments have to looked for only once.
1046       if (begStation == 4)
1047         nbCan2Seg = MakeTrackCandidatesWithTwoSegments(begSegment);
1048       // Look for track candidates with one segment and one point,
1049       // "begSegment" and all compatible HitForRec's in other station.
1050       // Only if "begSegment" does not belong already to a track candidate.
1051       // Is that a too strong condition ????
1052       if (!(begSegment->GetInTrack()))
1053         nbCan1Seg1Hit = MakeTrackCandidatesWithOneSegmentAndOnePoint(begSegment);
1054     } // for (iBegSegment = 0;...
1055   } // for (begStation = 4;...
1056   return;
1057 }
1058
1059   //__________________________________________________________________________
1060 void AliMUONEventReconstructor::FollowTracks(void)
1061 {
1062   // Follow tracks in stations(1..) 3, 2 and 1
1063   // too long: should be made more modular !!!!
1064   AliMUONHitForRec *bestHit, *extrapHit, *hit;
1065   AliMUONSegment *bestSegment, *extrapSegment, *segment;
1066   AliMUONTrack *track, *nextTrack;
1067   AliMUONTrackParam *trackParam1, trackParam[2], trackParamVertex;
1068   // -1 to avoid compilation warnings
1069   Int_t ch = -1, chInStation, chBestHit = -1, iHit, iSegment, station, trackIndex; 
1070   Double_t bestChi2, chi2, dZ1, dZ2, dZ3, maxSigma2Distance, mcsFactor;
1071   AliMUON *pMUON = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
1072   // local maxSigma2Distance, for easy increase in testing
1073   maxSigma2Distance = fMaxSigma2Distance;
1074   if (fPrintLevel >= 2)
1075     cout << "enter FollowTracks" << endl;
1076   // Loop over track candidates
1077   track = (AliMUONTrack*) fRecTracksPtr->First();
1078   trackIndex = -1;
1079   while (track) {
1080     // Follow function for each track candidate ????
1081     trackIndex++;
1082     nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track); // prepare next track
1083     if (fPrintLevel >= 2)
1084       cout << "FollowTracks: track candidate(0..): " << trackIndex << endl;
1085     // Fit track candidate
1086     track->SetFitMCS(0); // without multiple Coulomb scattering
1087     track->SetFitNParam(3); // with 3 parameters (X = Y = 0)
1088     track->SetFitStart(0); // from parameters at vertex
1089     track->Fit();
1090     if (fPrintLevel >= 10) {
1091       cout << "FollowTracks: track candidate(0..): " << trackIndex
1092            << " after fit in stations(0..) 3 and 4" << endl;
1093       track->RecursiveDump();
1094     }
1095     // Loop over stations(1..) 3, 2 and 1
1096     // something SPECIAL for stations 2 and 1 for majority 3 coincidence ????
1097     // otherwise: majority coincidence 2 !!!!
1098     for (station = 2; station >= 0; station--) {
1099       // Track parameters at first track hit (smallest Z)
1100       trackParam1 = ((AliMUONTrackHit*)
1101                      (track->GetTrackHitsPtr()->First()))->GetTrackParam();
1102       // extrapolation to station
1103       trackParam1->ExtrapToStation(station, trackParam);
1104       extrapSegment = new AliMUONSegment(); //  empty segment
1105       // multiple scattering factor corresponding to one chamber
1106       // and momentum in bending plane (not total)
1107       mcsFactor = 0.0136 * trackParam1->GetInverseBendingMomentum();
1108       mcsFactor = fChamberThicknessInX0 * mcsFactor * mcsFactor;
1109       // Z difference from previous station
1110       dZ1 = (&(pMUON->Chamber(2 * station)))->Z() -
1111         (&(pMUON->Chamber(2 * station + 2)))->Z();
1112       // Z difference between the two previous stations
1113       dZ2 = (&(pMUON->Chamber(2 * station + 2)))->Z() -
1114         (&(pMUON->Chamber(2 * station + 4)))->Z();
1115       // Z difference between the two chambers in the previous station
1116       dZ3 = (&(pMUON->Chamber(2 * station)))->Z() -
1117         (&(pMUON->Chamber(2 * station + 1)))->Z();
1118       extrapSegment->SetBendingCoorReso2(fBendingResolution * fBendingResolution);
1119       extrapSegment->
1120         SetNonBendingCoorReso2(fNonBendingResolution * fNonBendingResolution);
1121       extrapSegment->UpdateFromStationTrackParam
1122         (trackParam, mcsFactor, dZ1, dZ2, dZ3, station,
1123          trackParam1->GetInverseBendingMomentum());
1124       bestChi2 = 5.0;
1125       bestSegment = NULL;
1126       if (fPrintLevel >= 10) {
1127         cout << "FollowTracks: track candidate(0..): " << trackIndex
1128              << " Look for segment in station(0..): " << station << endl;
1129       }
1130       // Loop over segments in station
1131       for (iSegment = 0; iSegment < fNSegments[station]; iSegment++) {
1132         // Look for best compatible Segment in station
1133         // should consider all possibilities ????
1134         // multiple scattering ????
1135         // separation in 2 functions: Segment and HitForRec ????
1136         segment = (AliMUONSegment*) ((*fSegmentsPtr[station])[iSegment]);
1137         chi2 = segment->NormalizedChi2WithSegment(extrapSegment, maxSigma2Distance);
1138         if (chi2 < bestChi2) {
1139           // update best Chi2 and Segment if better found
1140           bestSegment = segment;
1141           bestChi2 = chi2;
1142         }
1143       }
1144       if (bestSegment) {
1145         // best segment found: add it to track candidate
1146         track->AddSegment(bestSegment);
1147         // set track parameters at these two TrakHit's
1148         track->SetTrackParamAtHit(track->GetNTrackHits() - 2, &(trackParam[0]));
1149         track->SetTrackParamAtHit(track->GetNTrackHits() - 1, &(trackParam[1]));
1150         if (fPrintLevel >= 10) {
1151           cout << "FollowTracks: track candidate(0..): " << trackIndex
1152                << " Added segment in station(0..): " << station << endl;
1153           track->RecursiveDump();
1154         }
1155       }
1156       else {
1157         // No best segment found:
1158         // Look for best compatible HitForRec in station:
1159         // should consider all possibilities ????
1160         // multiple scattering ???? do about like for extrapSegment !!!!
1161         extrapHit = new AliMUONHitForRec(); //  empty hit
1162         bestChi2 = 3.0;
1163         bestHit = NULL;
1164         if (fPrintLevel >= 10) {
1165           cout << "FollowTracks: track candidate(0..): " << trackIndex
1166                << " Segment not found, look for hit in station(0..): " << station
1167                << endl;
1168         }
1169         // Loop over chambers of the station
1170         for (chInStation = 0; chInStation < 2; chInStation++) {
1171           // coordinates of extrapolated hit
1172           extrapHit->
1173             SetBendingCoor((&(trackParam[chInStation]))->GetBendingCoor());
1174           extrapHit->
1175             SetNonBendingCoor((&(trackParam[chInStation]))->GetNonBendingCoor());
1176           // resolutions from "extrapSegment"
1177           extrapHit->SetBendingReso2(extrapSegment->GetBendingCoorReso2());
1178           extrapHit->SetNonBendingReso2(extrapSegment->GetNonBendingCoorReso2());
1179           // Loop over hits in the chamber
1180           ch = 2 * station + chInStation;
1181           for (iHit = fIndexOfFirstHitForRecPerChamber[ch];
1182                iHit < fIndexOfFirstHitForRecPerChamber[ch] +
1183                  fNHitsForRecPerChamber[ch];
1184                iHit++) {
1185             hit = (AliMUONHitForRec*) ((*fHitsForRecPtr)[iHit]);
1186             // condition for hit not already in segment ????
1187             chi2 = hit->NormalizedChi2WithHitForRec(extrapHit, maxSigma2Distance);
1188             if (chi2 < bestChi2) {
1189               // update best Chi2 and HitForRec if better found
1190               bestHit = hit;
1191               bestChi2 = chi2;
1192               chBestHit = chInStation;
1193             }
1194           }
1195         }
1196         if (bestHit) {
1197           // best hit found: add it to track candidate
1198           track->AddHitForRec(bestHit);
1199           // set track parameters at this TrackHit
1200           track->SetTrackParamAtHit(track->GetNTrackHits() - 1,
1201                                     &(trackParam[chBestHit]));
1202           if (fPrintLevel >= 10) {
1203             cout << "FollowTracks: track candidate(0..): " << trackIndex
1204                  << " Added hit in station(0..): " << station << endl;
1205             track->RecursiveDump();
1206           }
1207         }
1208         else {
1209           // Remove current track candidate
1210           // and corresponding TrackHit's, ...
1211           track->Remove();
1212           delete extrapSegment;
1213           break; // stop the search for this candidate:
1214           // exit from the loop over station
1215         }
1216       }
1217       delete extrapSegment;
1218       // Sort track hits according to increasing Z
1219       track->GetTrackHitsPtr()->Sort();
1220       // Update track parameters at first track hit (smallest Z)
1221       trackParam1 = ((AliMUONTrackHit*)
1222                      (track->GetTrackHitsPtr()->First()))->GetTrackParam();
1223       // Track fit
1224       // with multiple Coulomb scattering if all stations
1225       if (station == 0) track->SetFitMCS(1);
1226       // without multiple Coulomb scattering if not all stations
1227       else track->SetFitMCS(0);
1228       track->SetFitNParam(5);  // with 5 parameters (momentum and position)
1229       track->SetFitStart(1);  // from parameters at first hit
1230       track->Fit();
1231       if (fPrintLevel >= 10) {
1232         cout << "FollowTracks: track candidate(0..): " << trackIndex
1233              << " after fit from station(0..): " << station << " to 4" << endl;
1234         track->RecursiveDump();
1235       }
1236       // Track extrapolation to the vertex through the absorber (Branson)
1237       // after going through the first station
1238       if (station == 0) {
1239         trackParamVertex = *trackParam1;
1240         (&trackParamVertex)->ExtrapToVertex();
1241         track->SetTrackParamAtVertex(&trackParamVertex);
1242         if (fPrintLevel >= 1) {
1243           cout << "FollowTracks: track candidate(0..): " << trackIndex
1244                << " after extrapolation to vertex" << endl;
1245           track->RecursiveDump();
1246         }
1247       }
1248     } // for (station = 2;...
1249     // go really to next track
1250     track = nextTrack;
1251   } // while (track)
1252   // Compression of track array (necessary after Remove ????)
1253   fRecTracksPtr->Compress();
1254   return;
1255 }
1256
1257   //__________________________________________________________________________
1258 void AliMUONEventReconstructor::RemoveDoubleTracks(void)
1259 {
1260   // To remove double tracks.
1261   // Tracks are considered identical
1262   // if they have at least half of their hits in common.
1263   // Among two identical tracks, one keeps the track with the larger number of hits
1264   // or, if these numbers are equal, the track with the minimum Chi2.
1265   AliMUONTrack *track1, *track2, *trackToRemove;
1266   Bool_t identicalTracks;
1267   Int_t hitsInCommon, nHits1, nHits2;
1268   identicalTracks = kTRUE;
1269   while (identicalTracks) {
1270     identicalTracks = kFALSE;
1271     // Loop over first track of the pair
1272     track1 = (AliMUONTrack*) fRecTracksPtr->First();
1273     while (track1 && (!identicalTracks)) {
1274       nHits1 = track1->GetNTrackHits();
1275       // Loop over second track of the pair
1276       track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
1277       while (track2 && (!identicalTracks)) {
1278         nHits2 = track2->GetNTrackHits();
1279         // number of hits in common between two tracks
1280         hitsInCommon = track1->HitsInCommon(track2);
1281         // check for identical tracks
1282         if ((4 * hitsInCommon) >= (nHits1 + nHits2)) {
1283           identicalTracks = kTRUE;
1284           // decide which track to remove
1285           if (nHits1 > nHits2) trackToRemove = track2;
1286           else if (nHits1 < nHits2) trackToRemove = track1;
1287           else if ((track1->GetFitFMin()) < (track2->GetFitFMin()))
1288             trackToRemove = track2;
1289           else trackToRemove = track1;
1290           // remove it
1291           trackToRemove->Remove();
1292         }
1293         track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
1294       } // track2
1295       track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
1296     } // track1
1297   }
1298   return;
1299 }
1300
1301   //__________________________________________________________________________
1302 void AliMUONEventReconstructor::EventDump(void)
1303 {
1304   // Dump reconstructed event (track parameters at vertex and at first hit),
1305   // and the particle parameters
1306
1307   AliMUONTrack *track;
1308   AliMUONTrackParam *trackParam, *trackParam1;
1309   TClonesArray *particles; // pointer to the particle list
1310   TParticle *p;
1311   Double_t bendingSlope, nonBendingSlope, pYZ;
1312   Double_t pX, pY, pZ, x, y, z, c;
1313   Int_t np, trackIndex, nTrackHits;
1314  
1315   if (fPrintLevel >= 1) cout << "****** enter EventDump ******" << endl;
1316   if (fPrintLevel >= 1) {
1317     cout << " Number of Reconstructed tracks :" <<  fNRecTracks << endl; 
1318   }
1319   fRecTracksPtr->Compress(); // for simple loop without "Next" since no hole
1320   // Loop over reconstructed tracks
1321   for (trackIndex = 0; trackIndex < fNRecTracks; trackIndex++) {
1322     if (fPrintLevel >= 1)
1323       cout << " track number: " << trackIndex << endl;
1324     // function for each track for modularity ????
1325     track = (AliMUONTrack*) ((*fRecTracksPtr)[trackIndex]);
1326     nTrackHits = track->GetNTrackHits();
1327     if (fPrintLevel >= 1)
1328       cout << " number of track hits: " << nTrackHits << endl;
1329     // track parameters at Vertex
1330     trackParam = track->GetTrackParamAtVertex();
1331     x = trackParam->GetNonBendingCoor();
1332     y = trackParam->GetBendingCoor();
1333     z = trackParam->GetZ();
1334     bendingSlope = trackParam->GetBendingSlope();
1335     nonBendingSlope = trackParam->GetNonBendingSlope();
1336     pYZ = 1/TMath::Abs(trackParam->GetInverseBendingMomentum());
1337     pZ = pYZ/TMath::Sqrt(1+bendingSlope*bendingSlope);
1338     pX = pZ * nonBendingSlope;
1339     pY = pZ * bendingSlope;
1340     c = TMath::Sign(1.0, trackParam->GetInverseBendingMomentum());
1341     if (fPrintLevel >= 1)
1342       printf(" track parameters at Vertex z= %f: X= %f Y= %f pX= %f pY= %f pZ= %f c= %f\n",
1343              z, x, y, pX, pY, pZ, c);
1344
1345     // track parameters at first hit
1346     trackParam1 = ((AliMUONTrackHit*)
1347                    (track->GetTrackHitsPtr()->First()))->GetTrackParam();
1348     x = trackParam1->GetNonBendingCoor();
1349     y = trackParam1->GetBendingCoor();
1350     z = trackParam1->GetZ();
1351     bendingSlope = trackParam1->GetBendingSlope();
1352     nonBendingSlope = trackParam1->GetNonBendingSlope();
1353     pYZ = 1/TMath::Abs(trackParam1->GetInverseBendingMomentum());
1354     pZ = pYZ/TMath::Sqrt(1.0 + bendingSlope * bendingSlope);
1355     pX = pZ * nonBendingSlope;
1356     pY = pZ * bendingSlope;
1357     c = TMath::Sign(1.0, trackParam1->GetInverseBendingMomentum());
1358     if (fPrintLevel >= 1)
1359       printf(" track parameters at z= %f: X= %f Y= %f pX= %f pY= %f pZ= %f c= %f\n",
1360              z, x, y, pX, pY, pZ, c);
1361   }
1362   // informations about generated particles
1363   particles = gAlice->Particles();
1364   np = particles->GetEntriesFast();
1365   printf(" **** number of generated particles: %d  \n", np);
1366   
1367   for (Int_t iPart = 0; iPart < np; iPart++) {
1368     p = (TParticle*) particles->UncheckedAt(iPart);
1369     printf(" particle %d: type= %d px= %f py= %f pz= %f pdg= %d\n",
1370            iPart, p->GetPdgCode(), p->Px(), p->Py(), p->Pz(), p->GetPdgCode());    
1371   }
1372   return;
1373 }
1374
1375 void AliMUONEventReconstructor::FillEvent()
1376 {
1377 // Create a new AliMUONRecoEvent, fill its track list, then add it as a
1378 // leaf in the Event branch of TreeRecoEvent tree
1379    cout << "Enter FillEvent() ...\n";
1380
1381    if (!fRecoEvent) {
1382       fRecoEvent = new AliMUONRecoEvent();
1383    } else {
1384       fRecoEvent->Clear();
1385    }
1386    //save current directory
1387    TDirectory *current =  gDirectory;
1388    if (!fTreeFile)  fTreeFile  = new TFile("tree_reco.root", "RECREATE");
1389    if (!fEventTree) fEventTree = new TTree("TreeRecoEvent", "MUON reconstructed events");
1390    if (fRecoEvent->MakeDumpTracks(fRecTracksPtr)) {
1391       if (fPrintLevel > 1) fRecoEvent->EventInfo();
1392       TBranch *branch = fEventTree->GetBranch("Event");
1393       if (!branch) branch = fEventTree->Branch("Event", "AliMUONRecoEvent", &fRecoEvent, 64000,1);
1394       branch->SetAutoDelete();
1395       fTreeFile->cd();
1396       fEventTree->Fill();
1397       fTreeFile->Write();
1398    }
1399    // restore directory
1400    current->cd();
1401 }