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