]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUON.cxx
fDchamber allocation for digitalisation in MakeBranchInTreeD
[u/mrichter/AliRoot.git] / MUON / AliMUON.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18
19 ///////////////////////////////////////////////
20 //  Manager and hits classes for set:MUON     //
21 ////////////////////////////////////////////////
22
23 #include "Riostream.h"
24
25 #include <AliPDG.h>
26 #include <TBRIK.h>
27 #include <TCanvas.h>
28 #include <TDirectory.h>
29 #include <TFile.h>
30 #include <TGeometry.h>
31 #include <TMinuit.h>
32 #include <TNode.h> 
33 #include <TNtuple.h>
34 #include <TObjArray.h>
35 #include <TObject.h>
36 #include <TObjectTable.h>
37 #include <TPad.h>
38 #include <TParticle.h>
39 #include <TROOT.h>
40 #include <TRandom.h> 
41 #include <TRotMatrix.h>
42 #include <TTUBE.h>
43 #include <TTUBE.h>
44 #include <TTree.h> 
45 #include <TVector.h>
46 #include <TVirtualMC.h>
47
48 #include "AliConst.h" 
49 #include "AliHeader.h"
50 #include "AliHitMap.h"
51 #include "AliLoader.h"
52 #include "AliMUON.h"
53 #include "AliMUONChamberTrigger.h"
54 #include "AliMUONClusterFinderVS.h"
55 #include "AliMUONClusterInput.h"
56 #include "AliMUONConstants.h"
57 #include "AliMUONDigit.h"
58 #include "AliMUONGlobalTrigger.h"
59 #include "AliMUONHit.h"
60 #include "AliMUONHitMapA1.h"
61 #include "AliMUONLocalTrigger.h"
62 #include "AliMUONMerger.h"      
63 #include "AliMUONPadHit.h"
64 #include "AliMUONRawCluster.h"
65 #include "AliMUONTransientDigit.h"
66 #include "AliMUONTriggerCircuit.h"
67 #include "AliMUONTriggerDecision.h"
68 #include "AliRun.h"     
69
70
71 // Defaults parameters for Z positions of chambers
72 // taken from values for "stations" in AliMUON::AliMUON
73 //     const Float_t zch[7]={528, 690., 975., 1249., 1449., 1610, 1710.};
74 // and from array "dstation" in AliMUONv1::CreateGeometry
75 //          Float_t dstation[5]={20., 20., 20, 20., 20.};
76 //     for tracking chambers,
77 //          according to (Z1 = zch - dstation) and  (Z2 = zch + dstation)
78 //          for the first and second chambers in the station, respectively,
79 // and from "DTPLANES" in AliMUONv1::CreateGeometry
80 //           const Float_t DTPLANES = 15.;
81 //     for trigger chambers,
82 //          according to (Z1 = zch) and  (Z2 = zch + DTPLANES)
83 //          for the first and second chambers in the station, respectively
84
85 ClassImp(AliMUON)
86 //___________________________________________
87 AliMUON::AliMUON()
88 {
89 // Default Constructor
90 //
91     fNCh             = 0;
92     fNTrackingCh     = 0;
93     fIshunt          = 0;
94     fPadHits         = 0;
95     fNPadHits        = 0;
96     fChambers        = 0;
97     fDchambers       = 0;
98     fTriggerCircuits = 0;  
99     fNdch            = 0;
100     fRawClusters     = 0;
101     fNrawch          = 0;
102     fGlobalTrigger   = 0;
103     fNLocalTrigger   = 0;
104     fLocalTrigger    = 0;
105     fNLocalTrigger   = 0;
106     fAccMin          = 0.;
107     fAccMax          = 0.;   
108     fAccCut          = kFALSE;
109     fMerger          = 0;
110     fFileName        = 0;
111 }
112  
113 //___________________________________________
114 AliMUON::AliMUON(const char *name, const char *title)
115        : AliDetector(name,title)
116 {
117 //Begin_Html
118 /*
119 <img src="gif/alimuon.gif">
120 */
121 //End_Html
122
123    fHits     = new TClonesArray("AliMUONHit",1000);
124    gAlice->AddHitList(fHits);
125    fPadHits = new TClonesArray("AliMUONPadHit",10000);
126    fNPadHits  =  0;
127    fIshunt     =  0;
128
129    fNCh             = AliMUONConstants::NCh(); 
130    fNTrackingCh     = AliMUONConstants::NTrackingCh();
131    fNdch            = new Int_t[fNCh];
132
133    fDchambers = new TObjArray(AliMUONConstants::NCh());
134
135    Int_t i;
136    
137    for (i=0; i<AliMUONConstants::NCh() ;i++) {
138        fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
139        fNdch[i]=0;
140    }
141
142    fNrawch      = new Int_t[fNTrackingCh];
143
144    fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
145
146    for (i=0; i<AliMUONConstants::NTrackingCh();i++) {
147        fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
148        fNrawch[i]=0;
149    }
150
151    fGlobalTrigger = new TClonesArray("AliMUONGlobalTrigger",1);    
152    fNGlobalTrigger = 0;
153    fLocalTrigger  = new TClonesArray("AliMUONLocalTrigger",234);    
154    fNLocalTrigger = 0;
155
156    SetMarkerColor(kRed);
157 //
158 //
159 //
160 //
161
162     Int_t ch;
163
164     fChambers = new TObjArray(AliMUONConstants::NCh());
165
166     // Loop over stations
167     for (Int_t st = 0; st < AliMUONConstants::NCh() / 2; st++) {
168       // Loop over 2 chambers in the station
169         for (Int_t stCH = 0; stCH < 2; stCH++) {
170 //
171 //    
172 //    Default Parameters for Muon Tracking Stations
173
174
175             ch = 2 * st + stCH;
176 //
177             if (ch < AliMUONConstants::NTrackingCh()) {
178               fChambers->AddAt(new AliMUONChamber(ch),ch);
179             } else {
180               fChambers->AddAt(new AliMUONChamberTrigger(ch),ch);
181             }
182             
183         //PH        AliMUONChamber* chamber = (AliMUONChamber*) (*fChambers)[ch];
184             AliMUONChamber* chamber = (AliMUONChamber*) fChambers->At(ch);
185             
186             chamber->SetGid(0);
187             // Default values for Z of chambers
188             chamber->SetZ(AliMUONConstants::DefaultChamberZ(ch));
189 //
190             chamber->InitGeo(AliMUONConstants::DefaultChamberZ(ch));
191 //          Set chamber inner and outer radius to default
192             chamber->SetRInner(AliMUONConstants::Dmin(st)/2);
193             chamber->SetROuter(AliMUONConstants::Dmax(st)/2);
194 //
195         } // Chamber stCH (0, 1) in 
196     }     // Station st (0...)
197 //    fChambers->SetLast(AliMUONConstants::NCh());
198     fMaxStepGas=0.01; 
199     fMaxStepAlu=0.1; 
200     fMaxDestepGas=-1;
201     fMaxDestepAlu=-1;
202 //
203    fMaxIterPad   = 0;
204    fCurIterPad   = 0;
205 //
206    fAccMin          = 0.;
207    fAccMax          = 0.;   
208    fAccCut          = kFALSE;
209
210    // cp new design of AliMUONTriggerDecision
211    fTriggerCircuits = new TObjArray(AliMUONConstants::NTriggerCircuit());
212    for (Int_t circ=0; circ<AliMUONConstants::NTriggerCircuit(); circ++) {
213      fTriggerCircuits->AddAt(new AliMUONTriggerCircuit(),circ);     
214
215    }
216      fMerger = 0;
217 }
218  
219 //___________________________________________
220 AliMUON::AliMUON(const AliMUON& rMUON)
221 {
222 // Dummy copy constructor
223     ;
224     
225 }
226
227 AliMUON::~AliMUON()
228 {
229 // Destructor
230     if(fDebug) printf("%s: Calling AliMUON destructor !!!\n",ClassName());
231     
232     fIshunt  = 0;
233  
234     // Delete TObjArrays
235  
236     if (fChambers){
237       fChambers->Delete();
238       delete fChambers;
239     }
240  
241     if (fTriggerCircuits){
242       fTriggerCircuits->Delete();
243       delete fTriggerCircuits;
244     }
245  
246     if (fDchambers){
247       fDchambers->Delete();
248       delete fDchambers;
249     }
250  
251     if (fRawClusters){
252       fRawClusters->Delete();
253       delete fRawClusters;
254     }
255
256     if (fNrawch) delete [] fNrawch;
257  
258     // Delete TClonesArrays
259  
260     if (fPadHits){
261       fPadHits->Delete();
262       delete fPadHits;
263     }
264  
265     if (fGlobalTrigger){
266       fGlobalTrigger->Delete();
267       delete fGlobalTrigger;
268     }
269     fNGlobalTrigger = 0;
270     
271     if (fLocalTrigger){
272       fLocalTrigger->Delete();
273       delete fLocalTrigger;
274     }
275     fNLocalTrigger = 0;
276
277     if (fHits) {
278       fHits->Delete();
279       delete fHits;
280     }
281
282     if (fMerger) delete fMerger;
283     if (fNdch) delete [] fNdch;
284
285 }
286  
287 //___________________________________________
288 void AliMUON::AddHit(Int_t track, Int_t *vol, Float_t *hits)
289 {
290   TClonesArray &lhits = *fHits;
291   new(lhits[fNhits++]) AliMUONHit(fIshunt,track,vol,hits);
292 }
293 //___________________________________________
294 void AliMUON::AddHit(Int_t fIshunt, Int_t track, Int_t iChamber, 
295               Int_t idpart, Float_t X, Float_t Y, Float_t Z, 
296               Float_t tof, Float_t momentum, Float_t theta, 
297               Float_t phi, Float_t length, Float_t destep)
298 {
299   TClonesArray &lhits = *fHits;
300   new(lhits[fNhits++]) AliMUONHit(fIshunt, track, iChamber, 
301                idpart, X, Y, Z, 
302                tof, momentum, theta, 
303                phi, length, destep);
304 }
305 //___________________________________________
306 void AliMUON::AddPadHit(Int_t *clhits)  // To be removed
307 {
308    TClonesArray &lclusters = *fPadHits;
309    new(lclusters[fNPadHits++]) AliMUONPadHit(clhits);
310 }
311 //_____________________________________________________________________________
312 void AliMUON::AddDigits(Int_t id, Int_t *tracks, Int_t *charges, Int_t *digits)
313 {
314     //
315     // Add a MUON digit to the list
316     //
317
318   //PH    TClonesArray &ldigits = * ((TClonesArray*)(*fDchambers)[id]);
319     TClonesArray &ldigits = * ( (TClonesArray*) fDchambers->At(id) );
320     new(ldigits[fNdch[id]++]) AliMUONDigit(tracks,charges,digits);
321 }
322
323 //_____________________________________________________________________________
324 void AliMUON::AddRawCluster(Int_t id, const AliMUONRawCluster& c)
325 {
326     //
327     // Add a MUON digit to the list
328     //
329
330   //PH    TClonesArray &lrawcl = *((TClonesArray*)(*fRawClusters)[id]);
331     TClonesArray &lrawcl = *((TClonesArray*)fRawClusters->At(id));
332     new(lrawcl[fNrawch[id]++]) AliMUONRawCluster(c);
333 }
334
335 //___________________________________________
336 void AliMUON::AddGlobalTrigger(Int_t *singlePlus, Int_t *singleMinus,
337                                Int_t *singleUndef,
338                                Int_t *pairUnlike, Int_t *pairLike)
339 {
340 // add a MUON Global Trigger to the list (only one GlobalTrigger per event !)
341   TClonesArray &globalTrigger = *fGlobalTrigger;
342   new(globalTrigger[fNGlobalTrigger++]) 
343     AliMUONGlobalTrigger(singlePlus, singleMinus,  singleUndef, pairUnlike, 
344                          pairLike);
345 }
346 //___________________________________________
347 void AliMUON::AddLocalTrigger(Int_t *localtr)
348 {
349 // add a MUON Local Trigger to the list
350   TClonesArray &localTrigger = *fLocalTrigger;
351   new(localTrigger[fNLocalTrigger++]) AliMUONLocalTrigger(localtr);
352 }
353
354 //___________________________________________
355 void AliMUON::BuildGeometry()
356 {
357 // Geometry for event display
358   for (Int_t i=0; i<7; i++) {
359     for (Int_t j=0; j<2; j++) {
360       Int_t id=2*i+j+1;
361       this->Chamber(id-1).SegmentationModel(1)->Draw("eventdisplay");
362     }
363   }
364 }
365
366 //___________________________________________
367 Int_t AliMUON::DistancetoPrimitive(Int_t , Int_t )
368 {
369    return 9999;
370 }
371
372 //___________________________________________
373 void AliMUON::MakeBranch(Option_t* option)
374 {
375     //
376     // Create Tree branches for the MUON.
377     //
378     const Int_t kBufferSize = 4000;
379     char branchname[30];
380     sprintf(branchname,"%sCluster",GetName());
381     
382     
383     const char *cD = strstr(option,"D");
384     const char *cR = strstr(option,"R");
385     const char *cH = strstr(option,"H");
386
387     if (TreeH() && cH) 
388      {
389       if (fPadHits == 0x0) fPadHits = new TClonesArray("AliMUONPadHit",10000);
390       MakeBranchInTree(TreeH(), branchname, &fPadHits, kBufferSize, 0);
391       if (fHits == 0x0) fHits     = new TClonesArray("AliMUONHit",1000);
392      }
393     //it must be under fHits creation
394     AliDetector::MakeBranch(option);
395     
396     if (cD  && fLoader->TreeD()) {
397       //
398       // one branch for digits per chamber
399       // 
400       Int_t i;
401       if (fDchambers  == 0x0) 
402         {
403           fDchambers = new TObjArray(AliMUONConstants::NCh());
404           for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
405               fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
406           }
407         }
408     
409       for (i=0; i<AliMUONConstants::NCh() ;i++) 
410        {
411         sprintf(branchname,"%sDigits%d",GetName(),i+1); 
412         MakeBranchInTree(fLoader->TreeD(), branchname, &((*fDchambers)[i]), kBufferSize, 0);
413         printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
414        }
415     }
416     
417     if (cR  && fLoader->TreeR()) {
418       //     
419       // one branch for raw clusters per chamber
420       //  
421       printf("Make Branch - TreeR address %p\n",fLoader->TreeR());
422       
423       Int_t i;
424       if (fRawClusters == 0x0)
425       {
426         fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
427         for (Int_t i=0; i<AliMUONConstants::NTrackingCh();i++) {
428             fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
429         }
430       }
431
432       for (i=0; i<AliMUONConstants::NTrackingCh() ;i++) 
433        {
434          sprintf(branchname,"%sRawClusters%d",GetName(),i+1);   
435          MakeBranchInTree(fLoader->TreeR(), branchname, &((*fRawClusters)[i]), kBufferSize, 0);
436          printf("Making Branch %s for raw clusters in chamber %d\n",branchname,i+1);
437       }
438       //
439       // one branch for global trigger
440       //
441       sprintf(branchname,"%sGlobalTrigger",GetName());
442       
443       if (fGlobalTrigger == 0x0) {
444         fGlobalTrigger = new TClonesArray("AliMUONGlobalTrigger",1); 
445       }
446       MakeBranchInTree(fLoader->TreeR(), branchname, &fGlobalTrigger, kBufferSize, 0);
447       printf("Making Branch %s for Global Trigger\n",branchname);
448       //
449       // one branch for local trigger
450       //  
451       sprintf(branchname,"%sLocalTrigger",GetName());
452       
453       if (fLocalTrigger == 0x0) {
454         fLocalTrigger  = new TClonesArray("AliMUONLocalTrigger",234);
455       }
456       
457       MakeBranchInTree(fLoader->TreeR(), branchname, &fLocalTrigger, kBufferSize, 0);
458       printf("Making Branch %s for Local Trigger\n",branchname);
459    }
460 }
461
462 //___________________________________________
463 void AliMUON::SetTreeAddress()
464 {
465   // Set branch address for the Hits and Digits Tree.
466   char branchname[30];
467
468   TBranch *branch;
469   TTree *treeH = fLoader->TreeH();
470   TTree *treeD = fLoader->TreeD();
471   TTree *treeR = fLoader->TreeR();
472
473   if (treeH) {
474     if (fPadHits == 0x0) fPadHits = new TClonesArray("AliMUONPadHit",10000);
475     if (fPadHits) {
476       branch = treeH->GetBranch("MUONCluster");
477       if (branch) branch->SetAddress(&fPadHits);
478     }
479     if (fHits == 0x0) fHits     = new TClonesArray("AliMUONHit",1000);
480   }
481   //it must be under fHits creation
482   AliDetector::SetTreeAddress();
483
484   if (treeD) {
485       if (fDchambers == 0x0) 
486         {
487           fDchambers = new TObjArray(AliMUONConstants::NCh());
488           for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
489               fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
490           }
491         }
492       for (int i=0; i<AliMUONConstants::NCh(); i++) {
493           sprintf(branchname,"%sDigits%d",GetName(),i+1);
494                         
495                       if (fDchambers) {
496               branch = treeD->GetBranch(branchname);
497               if (branch) branch->SetAddress(&((*fDchambers)[i]));
498           }
499       }
500   }
501
502   // printf("SetTreeAddress --- treeR address  %p \n",treeR);
503
504   if (treeR) {
505       if (fRawClusters == 0x0)
506       {
507         fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
508         for (Int_t i=0; i<AliMUONConstants::NTrackingCh();i++) {
509             fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
510         }
511       }
512       
513       for (int i=0; i<AliMUONConstants::NTrackingCh(); i++) {
514           sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
515           if (fRawClusters) {
516               branch = treeR->GetBranch(branchname);
517               if (branch) branch->SetAddress(&((*fRawClusters)[i]));
518           }
519       }
520
521       if (fLocalTrigger == 0x0) {
522         fLocalTrigger  = new TClonesArray("AliMUONLocalTrigger",234);
523       }
524
525       if (fLocalTrigger) {
526         branch = treeR->GetBranch("MUONLocalTrigger");
527         if (branch) branch->SetAddress(&fLocalTrigger);
528       }
529       
530       if (fGlobalTrigger == 0x0) {
531         fGlobalTrigger = new TClonesArray("AliMUONGlobalTrigger",1); 
532       }
533       
534       if (fGlobalTrigger) {
535         branch = treeR->GetBranch("MUONGlobalTrigger");
536         if (branch) branch->SetAddress(&fGlobalTrigger);
537       }
538   }
539 }
540 //___________________________________________
541 void AliMUON::ResetHits()
542 {
543   // Reset number of clusters and the cluster array for this detector
544   AliDetector::ResetHits();
545   fNPadHits = 0;
546   if (fPadHits) fPadHits->Clear();
547 }
548
549 //____________________________________________
550 void AliMUON::ResetDigits()
551 {
552     //
553     // Reset number of digits and the digits array for this detector
554     //
555     for ( int i=0;i<AliMUONConstants::NCh();i++ ) {
556       //PH      if ((*fDchambers)[i])    ((TClonesArray*)(*fDchambers)[i])->Clear();
557         if ((*fDchambers)[i])    ((TClonesArray*)fDchambers->At(i))->Clear();
558         if (fNdch)  fNdch[i]=0;
559     }
560 }
561 //____________________________________________
562 void AliMUON::ResetRawClusters()
563 {
564     //
565     // Reset number of raw clusters and the raw clust array for this detector
566     //
567     for ( int i=0;i<AliMUONConstants::NTrackingCh();i++ ) {
568       //PH      if ((*fRawClusters)[i])    ((TClonesArray*)(*fRawClusters)[i])->Clear();
569         if ((*fRawClusters)[i])    ((TClonesArray*)fRawClusters->At(i))->Clear();
570         if (fNrawch)  fNrawch[i]=0;
571     }
572 }
573
574 //____________________________________________
575 void AliMUON::ResetTrigger()
576 {
577   //  Reset Local and Global Trigger 
578   fNGlobalTrigger = 0;
579   if (fGlobalTrigger) fGlobalTrigger->Clear();
580   fNLocalTrigger = 0;
581   if (fLocalTrigger) fLocalTrigger->Clear();
582 }
583
584 //____________________________________________
585 void AliMUON::SetPadSize(Int_t id, Int_t isec, Float_t p1, Float_t p2)
586 {
587 // Set the pad size for chamber id and cathode isec
588     Int_t i=2*(id-1);
589     //PH    ((AliMUONChamber*) (*fChambers)[i])  ->SetPadSize(isec,p1,p2);
590     //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetPadSize(isec,p1,p2);
591     ((AliMUONChamber*) fChambers->At(i))  ->SetPadSize(isec,p1,p2);
592     ((AliMUONChamber*) fChambers->At(i+1))->SetPadSize(isec,p1,p2);
593 }
594
595 //___________________________________________
596 void AliMUON::SetChambersZ(const Float_t *Z)
597 {
598   // Set Z values for all chambers (tracking and trigger)
599   // from the array pointed to by "Z"
600     for (Int_t ch = 0; ch < AliMUONConstants::NCh(); ch++)
601       //PH      ((AliMUONChamber*) ((*fChambers)[ch]))->SetZ(Z[ch]);
602         ((AliMUONChamber*) fChambers->At(ch))->SetZ(Z[ch]);
603     return;
604 }
605
606 //___________________________________________
607 void AliMUON::SetChambersZToDefault()
608 {
609   // Set Z values for all chambers (tracking and trigger)
610   // to default values
611   SetChambersZ(AliMUONConstants::DefaultChamberZ());
612   return;
613 }
614
615 //___________________________________________
616 void AliMUON::SetChargeSlope(Int_t id, Float_t p1)
617 {
618 // Set the inverse charge slope for chamber id
619     Int_t i=2*(id-1);
620     //PH    ((AliMUONChamber*) (*fChambers)[i])->SetChargeSlope(p1);
621     //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetChargeSlope(p1);
622     ((AliMUONChamber*) fChambers->At(i))->SetChargeSlope(p1);
623     ((AliMUONChamber*) fChambers->At(i+1))->SetChargeSlope(p1);
624 }
625
626 //___________________________________________
627 void AliMUON::SetChargeSpread(Int_t id, Float_t p1, Float_t p2)
628 {
629 // Set sigma of charge spread for chamber id
630     Int_t i=2*(id-1);
631     //PH    ((AliMUONChamber*) fChambers->At(i))->SetChargeSpread(p1,p2);
632     //PH    ((AliMUONChamber*) fChambers->Ati+1])->SetChargeSpread(p1,p2);
633     ((AliMUONChamber*) fChambers->At(i))->SetChargeSpread(p1,p2);
634     ((AliMUONChamber*) fChambers->At(i+1))->SetChargeSpread(p1,p2);
635 }
636
637 //___________________________________________
638 void AliMUON::SetSigmaIntegration(Int_t id, Float_t p1)
639 {
640 // Set integration limits for charge spread
641     Int_t i=2*(id-1);
642     //PH    ((AliMUONChamber*) (*fChambers)[i])->SetSigmaIntegration(p1);
643     //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetSigmaIntegration(p1);
644     ((AliMUONChamber*) fChambers->At(i))->SetSigmaIntegration(p1);
645     ((AliMUONChamber*) fChambers->At(i+1))->SetSigmaIntegration(p1);
646 }
647
648 //___________________________________________
649 void AliMUON::SetMaxAdc(Int_t id, Int_t p1)
650 {
651 // Set maximum number for ADCcounts (saturation)
652     Int_t i=2*(id-1);
653     //PH    ((AliMUONChamber*) (*fChambers)[i])->SetMaxAdc(p1);
654     //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetMaxAdc(p1);
655     ((AliMUONChamber*) fChambers->At(i))->SetMaxAdc(p1);
656     ((AliMUONChamber*) fChambers->At(i+1))->SetMaxAdc(p1);
657 }
658
659 //___________________________________________
660 void AliMUON::SetMaxStepGas(Float_t p1)
661 {
662 // Set stepsize in gas
663      fMaxStepGas=p1;
664 }
665
666 //___________________________________________
667 void AliMUON::SetMaxStepAlu(Float_t p1)
668 {
669 // Set step size in Alu
670     fMaxStepAlu=p1;
671 }
672
673 //___________________________________________
674 void AliMUON::SetMaxDestepGas(Float_t p1)
675 {
676 // Set maximum step size in Gas
677     fMaxDestepGas=p1;
678 }
679
680 //___________________________________________
681 void AliMUON::SetMaxDestepAlu(Float_t p1)
682 {
683 // Set maximum step size in Alu
684     fMaxDestepAlu=p1;
685 }
686 //___________________________________________
687 void AliMUON::SetAcceptance(Bool_t acc, Float_t angmin, Float_t angmax)
688 {
689 // Set acceptance cuts 
690    fAccCut=acc;
691    fAccMin=angmin*TMath::Pi()/180;
692    fAccMax=angmax*TMath::Pi()/180;
693    Int_t ch;
694    if (acc) {
695        for (Int_t st = 0; st < AliMUONConstants::NCh() / 2; st++) {
696            // Loop over 2 chambers in the station
697            for (Int_t stCH = 0; stCH < 2; stCH++) {
698                ch = 2 * st + stCH;
699 //         Set chamber inner and outer radius according to acceptance cuts
700                Chamber(ch).SetRInner(AliMUONConstants::DefaultChamberZ(ch)*TMath::Tan(fAccMin));
701                Chamber(ch).SetROuter(AliMUONConstants::DefaultChamberZ(ch)*TMath::Tan(fAccMax));
702            } // chamber loop
703        } // station loop
704    }
705 }
706 //___________________________________________
707 void   AliMUON::SetSegmentationModel(Int_t id, Int_t isec, AliSegmentation *segmentation)
708 {
709 // Set the segmentation for chamber id cathode isec
710   //PH    ((AliMUONChamber*) (*fChambers)[id])->SetSegmentationModel(isec, segmentation);
711     ((AliMUONChamber*) fChambers->At(id))->SetSegmentationModel(isec, segmentation);
712
713 }
714 //___________________________________________
715 void   AliMUON::SetResponseModel(Int_t id, AliMUONResponse *response)
716 {
717 // Set the response for chamber id
718   //PH    ((AliMUONChamber*) (*fChambers)[id])->SetResponseModel(response);
719     ((AliMUONChamber*) fChambers->At(id))->SetResponseModel(response);
720 }
721
722 void   AliMUON::SetReconstructionModel(Int_t id, AliMUONClusterFinderVS *reconst)
723 {
724 // Set ClusterFinder for chamber id
725   //PH    ((AliMUONChamber*) (*fChambers)[id])->SetReconstructionModel(reconst);
726     ((AliMUONChamber*) fChambers->At(id))->SetReconstructionModel(reconst);
727 }
728
729 void   AliMUON::SetNsec(Int_t id, Int_t nsec)
730 {
731 // Set number of segmented cathods for chamber id
732   //PH    ((AliMUONChamber*) (*fChambers)[id])->SetNsec(nsec);
733     ((AliMUONChamber*) fChambers->At(id))->SetNsec(nsec);
734 }
735
736 //___________________________________________
737 void AliMUON::SDigits2Digits()
738 {
739
740 // write TreeD here 
741
742     if (!fMerger) {
743       if (gAlice->GetDebug()>0) {
744         cerr<<"AliMUON::SDigits2Digits: create default AliMUONMerger "<<endl;
745         cerr<<" no merging, just digitization of 1 event will be done"<<endl;
746       }
747       fMerger = new AliMUONMerger();
748     }
749     fMerger->Init();
750     fMerger->Digitise();
751     char hname[30];
752     //    sprintf(hname,"TreeD%d",fLoader->GetHeader()->GetEvent());
753     fLoader->TreeD()->Write(hname,TObject::kOverwrite);
754     fLoader->TreeD()->Reset();
755 }
756
757 //___________________________________________
758 // To be removed
759 void AliMUON::MakePadHits(Float_t xhit,Float_t yhit, Float_t zhit,
760                           Float_t eloss, Float_t tof,  Int_t idvol)
761 {
762 //
763 //  Calls the charge disintegration method of the current chamber and adds
764 //  the simulated cluster to the root treee 
765 //
766     Int_t clhits[7];
767     Float_t newclust[6][500];
768     Int_t nnew;
769     
770     
771 //
772 //  Integrated pulse height on chamber
773
774     
775     clhits[0]=fNhits+1;
776 //
777 //
778 //    if (idvol == 6) printf("\n ->Disintegration %f %f %f", xhit, yhit, eloss );
779     
780
781     //PH    ((AliMUONChamber*) (*fChambers)[idvol])
782     ((AliMUONChamber*) fChambers->At(idvol))
783         ->DisIntegration(eloss, tof, xhit, yhit, zhit, nnew, newclust);
784     Int_t ic=0;
785 //    if (idvol == 6) printf("\n nnew  %d \n", nnew);
786 //
787 //  Add new clusters
788     for (Int_t i=0; i<nnew; i++) {
789         if (Int_t(newclust[3][i]) > 0) {
790             ic++;
791 // Cathode plane
792             clhits[1] = Int_t(newclust[5][i]);
793 //  Cluster Charge
794             clhits[2] = Int_t(newclust[0][i]);
795 //  Pad: ix
796             clhits[3] = Int_t(newclust[1][i]);
797 //  Pad: iy 
798             clhits[4] = Int_t(newclust[2][i]);
799 //  Pad: charge
800             clhits[5] = Int_t(newclust[3][i]);
801 //  Pad: chamber sector
802             clhits[6] = Int_t(newclust[4][i]);
803             
804             AddPadHit(clhits);
805         }
806     }
807 }
808
809 //___________________________________________
810 void AliMUON::Trigger(Int_t nev){
811 // call the Trigger Algorithm and fill TreeR
812
813   Int_t singlePlus[3]  = {0,0,0}; 
814   Int_t singleMinus[3] = {0,0,0}; 
815   Int_t singleUndef[3] = {0,0,0};
816   Int_t pairUnlike[3]  = {0,0,0}; 
817   Int_t pairLike[3]    = {0,0,0};
818
819   ResetTrigger();
820   AliMUONTriggerDecision* decision= new AliMUONTriggerDecision(1);
821   decision->Trigger();   
822   decision->GetGlobalTrigger(singlePlus, singleMinus, singleUndef,
823                              pairUnlike, pairLike);
824
825 // add a local trigger in the list 
826   AddGlobalTrigger(singlePlus, singleMinus, singleUndef, pairUnlike, pairLike);
827   Int_t i;
828   
829   for (Int_t icirc=0; icirc<AliMUONConstants::NTriggerCircuit(); icirc++) { 
830       if(decision->GetITrigger(icirc)==1) {
831           Int_t localtr[7]={0,0,0,0,0,0,0};      
832           Int_t loLpt[2]={0,0}; Int_t loHpt[2]={0,0}; Int_t loApt[2]={0,0};
833           decision->GetLutOutput(icirc, loLpt, loHpt, loApt);
834           localtr[0] = icirc;
835           localtr[1] = decision->GetStripX11(icirc);
836           localtr[2] = decision->GetDev(icirc);
837           localtr[3] = decision->GetStripY11(icirc);
838           for (i=0; i<2; i++) {    // convert the Lut output in 1 digit 
839               localtr[4] = localtr[4]+Int_t(loLpt[i]*TMath::Power(2,i));
840               localtr[5] = localtr[5]+Int_t(loHpt[i]*TMath::Power(2,i));
841               localtr[6] = localtr[6]+Int_t(loApt[i]*TMath::Power(2,i));
842           }
843           AddLocalTrigger(localtr);  // add a local trigger in the list
844       }
845   }
846
847   delete decision;
848
849   fLoader->TreeR()->Fill();
850 //  char hname[30];
851 //  sprintf(hname,"TreeR%d",nev);
852 //  fLoader->TreeR()->Write(hname,TObject::kOverwrite);
853 //  fLoader->TreeR()->Reset();
854   fLoader->WriteRecPoints("OVERWRITE");
855   ResetTrigger();
856   
857   printf("\n End of trigger for event %d", nev);
858 }
859
860
861 //____________________________________________
862 void AliMUON::Digits2Reco()
863 {
864   FindClusters();
865   Int_t nev = gAlice->GetHeader()->GetEvent();
866   fLoader->TreeR()->Fill();
867   // char hname[30];
868   // sprintf(hname,"TreeR%d", nev);
869   //fLoader->TreeR()->Write(hname);
870   //fLoader->TreeR()->Reset();
871   fLoader->WriteRecPoints("OVERWRITE");
872   ResetRawClusters();        
873   printf("\n End of cluster finding for event %d", nev);
874 }
875
876 void AliMUON::FindClusters()
877 {
878 //
879 //  Perform cluster finding
880 //
881     TClonesArray *dig1, *dig2;
882     Int_t ndig, k;
883     dig1 = new TClonesArray("AliMUONDigit",1000);
884     dig2 = new TClonesArray("AliMUONDigit",1000);
885     AliMUONDigit *digit;
886 // Loop on chambers and on cathode planes
887 //
888     ResetRawClusters();        
889     TClonesArray * muonDigits;
890
891     for (Int_t ich = 0; ich < 10; ich++) {
892       //PH      AliMUONChamber* iChamber = (AliMUONChamber*) (*fChambers)[ich];
893         AliMUONChamber* iChamber = (AliMUONChamber*) fChambers->At(ich);
894         AliMUONClusterFinderVS* rec = iChamber->ReconstructionModel();
895     
896         ResetDigits();
897         fLoader->TreeD()->GetEvent(0);
898         //TClonesArray *
899         muonDigits = (TClonesArray *) Dchambers()->At(ich);
900         ndig=muonDigits->GetEntriesFast();
901         printf("\n 1 Found %d digits in %p chamber %d", ndig, muonDigits,ich);
902         TClonesArray &lhits1 = *dig1;
903         Int_t n = 0;
904         for (k = 0; k < ndig; k++) {
905             digit = (AliMUONDigit*) muonDigits->UncheckedAt(k);
906             if (rec->TestTrack(digit->Track(0)))
907                 new(lhits1[n++]) AliMUONDigit(*digit);
908         }
909         ResetDigits();
910         fLoader->TreeD()->GetEvent(1);
911         //muonDigits  = this->DigitsAddress(ich);
912         muonDigits = (TClonesArray *) Dchambers()->At(ich);
913         ndig=muonDigits->GetEntriesFast();
914         printf("\n 2 Found %d digits in %p %d", ndig, muonDigits, ich);
915         TClonesArray &lhits2 = *dig2;
916         n=0;
917         
918         for (k=0; k<ndig; k++) {
919             digit= (AliMUONDigit*) muonDigits->UncheckedAt(k);
920             if (rec->TestTrack(digit->Track(0)))
921             new(lhits2[n++]) AliMUONDigit(*digit);
922         }
923
924         if (rec) {       
925             AliMUONClusterInput::Instance()->SetDigits(ich, dig1, dig2);
926             rec->FindRawClusters();
927         }
928         dig1->Delete();
929         dig2->Delete();
930     } // for ich
931     delete dig1;
932     delete dig2;
933 }
934  
935 #ifdef never
936 void AliMUON::Streamer(TBuffer &R__b)
937 {
938    // Stream an object of class AliMUON.
939       AliMUONChamber        *iChamber;
940       AliMUONTriggerCircuit *iTriggerCircuit;
941       AliSegmentation       *segmentation;
942       AliMUONResponse       *response;
943       TClonesArray          *digitsaddress;
944       TClonesArray          *rawcladdress;
945       Int_t i;
946       if (R__b.IsReading()) {
947           Version_t R__v = R__b.ReadVersion(); if (R__v) { }
948           AliDetector::Streamer(R__b);
949           R__b >> fNPadHits;
950           R__b >> fPadHits; // diff
951           R__b >> fNLocalTrigger;       
952           R__b >> fLocalTrigger;       
953           R__b >> fNGlobalTrigger;       
954           R__b >> fGlobalTrigger;   
955           R__b >> fDchambers;
956           R__b >> fRawClusters;
957           R__b.ReadArray(fNdch);
958           R__b.ReadArray(fNrawch);
959           R__b >> fAccCut;
960           R__b >> fAccMin;
961           R__b >> fAccMax; 
962           R__b >> fChambers;
963           R__b >> fTriggerCircuits;
964           for (i =0; i<AliMUONConstants::NTriggerCircuit(); i++) {
965               iTriggerCircuit=(AliMUONTriggerCircuit*) (*fTriggerCircuits)[i];
966               iTriggerCircuit->Streamer(R__b);
967           }
968 // Stream chamber related information
969           for (i =0; i<AliMUONConstants::NCh(); i++) {
970               iChamber=(AliMUONChamber*) (*fChambers)[i];
971               iChamber->Streamer(R__b);
972               if (iChamber->Nsec()==1) {
973                   segmentation=iChamber->SegmentationModel(1);
974                   if (segmentation)
975                       segmentation->Streamer(R__b);
976               } else {
977                   segmentation=iChamber->SegmentationModel(1);
978                   if (segmentation)
979                       segmentation->Streamer(R__b);
980                   if (segmentation)
981                       segmentation=iChamber->SegmentationModel(2);
982                   segmentation->Streamer(R__b);
983               }
984               response=iChamber->ResponseModel();
985               if (response)
986                   response->Streamer(R__b);       
987               digitsaddress=(TClonesArray*) (*fDchambers)[i];
988               digitsaddress->Streamer(R__b);
989               if (i < AliMUONConstants::NTrackingCh()) {
990                   rawcladdress=(TClonesArray*) (*fRawClusters)[i];
991                   rawcladdress->Streamer(R__b);
992               }
993           }
994           
995       } else {
996           R__b.WriteVersion(AliMUON::IsA());
997           AliDetector::Streamer(R__b);
998           R__b << fNPadHits;
999           R__b << fPadHits; // diff
1000           R__b << fNLocalTrigger;       
1001           R__b << fLocalTrigger;       
1002           R__b << fNGlobalTrigger;       
1003           R__b << fGlobalTrigger; 
1004           R__b << fDchambers;
1005           R__b << fRawClusters;
1006           R__b.WriteArray(fNdch, AliMUONConstants::NCh());
1007           R__b.WriteArray(fNrawch, AliMUONConstants::NTrackingCh());
1008           
1009           R__b << fAccCut;
1010           R__b << fAccMin;
1011           R__b << fAccMax; 
1012           
1013           R__b << fChambers;
1014           R__b << fTriggerCircuits;
1015           for (i =0; i<AliMUONConstants::NTriggerCircuit(); i++) {
1016               iTriggerCircuit=(AliMUONTriggerCircuit*) (*fTriggerCircuits)[i];
1017               iTriggerCircuit->Streamer(R__b);
1018           }
1019           for (i =0; i<AliMUONConstants::NCh(); i++) {
1020               iChamber=(AliMUONChamber*) (*fChambers)[i];
1021               iChamber->Streamer(R__b);
1022               if (iChamber->Nsec()==1) {
1023                   segmentation=iChamber->SegmentationModel(1);
1024                   if (segmentation)
1025                       segmentation->Streamer(R__b);
1026               } else {
1027                   segmentation=iChamber->SegmentationModel(1);
1028                   if (segmentation)
1029                       segmentation->Streamer(R__b);
1030                   segmentation=iChamber->SegmentationModel(2);
1031                   if (segmentation)
1032                       segmentation->Streamer(R__b);
1033               }
1034               response=iChamber->ResponseModel();
1035               if (response)
1036                   response->Streamer(R__b);
1037               digitsaddress=(TClonesArray*) (*fDchambers)[i];
1038               digitsaddress->Streamer(R__b);
1039               if (i < AliMUONConstants::NTrackingCh()) {
1040                   rawcladdress=(TClonesArray*) (*fRawClusters)[i];
1041                   rawcladdress->Streamer(R__b);
1042               }
1043           }
1044       }
1045 }
1046 #endif
1047
1048 AliMUONPadHit* AliMUON::FirstPad(AliMUONHit*  hit, TClonesArray *clusters) 
1049 {
1050 // to be removed
1051     // Initialise the pad iterator
1052     // Return the address of the first padhit for hit
1053     TClonesArray *theClusters = clusters;
1054     Int_t nclust = theClusters->GetEntriesFast();
1055     if (nclust && hit->PHlast() > 0) {
1056         AliMUON::fMaxIterPad=hit->PHlast();
1057         AliMUON::fCurIterPad=hit->PHfirst();
1058         return (AliMUONPadHit*) clusters->UncheckedAt(AliMUON::fCurIterPad-1);
1059     } else {
1060         return 0;
1061     }
1062 }
1063
1064 AliMUONPadHit* AliMUON::NextPad(TClonesArray *clusters) 
1065 {
1066   // To be removed
1067 // Get next pad (in iterator) 
1068 //
1069     AliMUON::fCurIterPad++;
1070     if (AliMUON::fCurIterPad <= AliMUON::fMaxIterPad) {
1071         return (AliMUONPadHit*) clusters->UncheckedAt(AliMUON::fCurIterPad-1);
1072     } else {
1073         return 0;
1074     }
1075 }
1076
1077
1078 AliMUONRawCluster *AliMUON::RawCluster(Int_t ichamber, Int_t icathod, Int_t icluster)
1079 {
1080 //
1081 //  Return rawcluster (icluster) for chamber ichamber and cathode icathod
1082 //  Obsolete ??
1083     TClonesArray *muonRawCluster  = RawClustAddress(ichamber);
1084     ResetRawClusters();
1085     TTree *treeR = fLoader->TreeR();
1086     Int_t nent=(Int_t)treeR->GetEntries();
1087     treeR->GetEvent(nent-2+icathod-1);
1088     //treeR->GetEvent(icathod);
1089     //Int_t nrawcl = (Int_t)muonRawCluster->GetEntriesFast();
1090
1091     AliMUONRawCluster * mRaw = (AliMUONRawCluster*)muonRawCluster->UncheckedAt(icluster);
1092     //printf("RawCluster _ nent nrawcl icluster mRaw %d %d %d%p\n",nent,nrawcl,icluster,mRaw);
1093     
1094     return  mRaw;
1095 }
1096  
1097 void   AliMUON::SetMerger(AliMUONMerger* merger)
1098 {
1099 // Set pointer to merger 
1100     fMerger = merger;
1101 }
1102
1103 AliMUONMerger*  AliMUON::Merger()
1104 {
1105 // Return pointer to merger
1106     return fMerger;
1107 }
1108
1109
1110
1111 AliMUON& AliMUON::operator = (const AliMUON& rhs)
1112 {
1113 // copy operator
1114 // dummy version
1115     return *this;
1116 }
1117
1118 ////////////////////////////////////////////////////////////////////////
1119 void AliMUON::MakeBranchInTreeD(TTree *treeD, const char *file)
1120 {
1121     //
1122     // Create TreeD branches for the MUON.
1123     //
1124
1125   const Int_t kBufferSize = 4000;
1126   char branchname[30];
1127     
1128   if (fDchambers  == 0x0)   {
1129     fDchambers = new TObjArray(AliMUONConstants::NCh());
1130     for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
1131       fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
1132     }
1133   }
1134   //
1135   // one branch for digits per chamber
1136   // 
1137   for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
1138     sprintf(branchname,"%sDigits%d",GetName(),i+1);     
1139     if (fDchambers && treeD) {
1140       MakeBranchInTree(treeD, 
1141                        branchname, &((*fDchambers)[i]), kBufferSize, file);
1142 //      printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
1143     }
1144   }
1145 }
1146
1147 //___________________________________________
1148