]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUON.cxx
new class AliMUONLoader
[u/mrichter/AliRoot.git] / MUON / AliMUON.cxx
index 59552da9c0ad05731d6c4b4f113b328ec583179c..a0cb78fb1046fbea4598d39855aba852e5042bdc 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
-/*
-$Log$
-Revision 1.46  2001/01/26 21:34:59  morsch
-Use access functions for AliMUONHit, AliMUONDigit and AliMUONPadHit data members.
-
-Revision 1.45  2001/01/26 20:00:49  hristov
-Major upgrade of AliRoot code
-
-Revision 1.44  2001/01/25 17:39:09  morsch
-Pass size of fNdch and fNrawch to CINT.
-
-Revision 1.43  2001/01/23 18:58:19  hristov
-Initialisation of some pointers
-
-Revision 1.42  2001/01/17 20:53:40  hristov
-Destructors corrected to avoid memory leaks
-
-Revision 1.41  2000/12/21 22:12:40  morsch
-Clean-up of coding rule violations,
-
-Revision 1.40  2000/11/29 20:32:26  gosset
-Digitize:
-1. correction for array index out of bounds
-2. one printout commented
-
-Revision 1.39  2000/11/12 17:17:03  pcrochet
-BuildGeometry of AliMUON for trigger chambers delegated to AliMUONSegmentationTriggerX (same strategy as for tracking chambers)
-
-Revision 1.38  2000/11/06 09:20:43  morsch
-AliMUON delegates part of BuildGeometry() to AliMUONSegmentation using the
-Draw() method. This avoids code and parameter replication.
-
-Revision 1.37  2000/10/26 09:53:37  pcrochet
-put back trigger chambers in the display (there was a problem in buildgeometry)
-
-Revision 1.36  2000/10/25 19:51:18  morsch
-Correct x-position of chambers.
-
-Revision 1.35  2000/10/24 19:46:21  morsch
-BuildGeometry updated for slats in station 3-4.
-
-Revision 1.34  2000/10/18 11:42:06  morsch
-- AliMUONRawCluster contains z-position.
-- Some clean-up of useless print statements during initialisations.
-
-Revision 1.33  2000/10/09 14:01:57  morsch
-Unused variables removed.
-
-Revision 1.32  2000/10/06 09:08:10  morsch
-Built geometry includes slat geometry for event display.
-
-Revision 1.31  2000/10/02 21:28:08  fca
-Removal of useless dependecies via forward declarations
-
-Revision 1.30  2000/10/02 16:58:29  egangler
-Cleaning of the code :
--> coding conventions
--> void Streamers
--> some useless includes removed or replaced by "class" statement
-
-Revision 1.29  2000/07/28 13:49:38  morsch
-SetAcceptance defines inner and outer chamber radii according to angular acceptance.
-Can be used for simple acceptance studies.
-
-Revision 1.28  2000/07/22 16:43:15  morsch
-Same comment as before, but now done correctly I hope (sorry it's Saturday evening)
-
-Revision 1.27  2000/07/22 16:36:50  morsch
-Change order of indices in creation (new) of xhit and yhit
-
-Revision 1.26  2000/07/03 11:54:57  morsch
-AliMUONSegmentation and AliMUONHitMap have been replaced by AliSegmentation and AliHitMap in STEER
-The methods GetPadIxy and GetPadXxy of AliMUONSegmentation have changed name to GetPadI and GetPadC.
-
-Revision 1.25  2000/06/29 12:34:09  morsch
-AliMUONSegmentation class has been made independent of AliMUONChamber. This makes
-it usable with any other geometry class. The link to the object to which it belongs is
-established via an index. This assumes that there exists a global geometry manager
-from which the pointer to the parent object can be obtained (in our case gAlice).
-
-Revision 1.24  2000/06/28 15:16:35  morsch
-(1) Client code adapted to new method signatures in AliMUONSegmentation (see comments there)
-to allow development of slat-muon chamber simulation and reconstruction code in the MUON
-framework. The changes should have no side effects (mostly dummy arguments).
-(2) Hit disintegration uses 3-dim hit coordinates to allow simulation
-of chambers with overlapping modules (MakePadHits, Disintegration).
 
-Revision 1.23  2000/06/28 12:19:17  morsch
-More consequent seperation of global input data services (AliMUONClusterInput singleton) and the
-cluster and hit reconstruction algorithms in AliMUONClusterFinderVS.
-AliMUONClusterFinderVS becomes the base class for clustering and hit reconstruction.
-It requires two cathode planes. Small modifications in the code will make it usable for
-one cathode plane and, hence, more general (for test beam data).
-AliMUONClusterFinder is now obsolete.
-
-Revision 1.22  2000/06/28 08:06:10  morsch
-Avoid global variables in AliMUONClusterFinderVS by seperating the input data for the fit from the
-algorithmic part of the class. Input data resides inside the AliMUONClusterInput singleton.
-It also naturally takes care of the TMinuit instance.
-
-Revision 1.21  2000/06/27 08:54:41  morsch
-Problems with on constant array sizes (in hitMap, nmuon, xhit, yhit) corrected.
-
-Revision 1.20  2000/06/26 14:02:38  morsch
-Add class AliMUONConstants with MUON specific constants using static memeber data and access methods.
-
-Revision 1.19  2000/06/22 13:40:51  morsch
-scope problem on HP, "i" declared once
-pow changed to TMath::Power (PH, AM)
-
-Revision 1.18  2000/06/15 07:58:48  morsch
-Code from MUON-dev joined
-
-Revision 1.14.4.17  2000/06/14 14:36:46  morsch
-- add TriggerCircuit (PC)
-- add GlobalTrigger and LocalTrigger and specific methods (PC)
-
-Revision 1.14.4.16  2000/06/09 21:20:28  morsch
-Most coding rule violations corrected
-
-Revision 1.14.4.15  2000/05/02 09:54:32  morsch
-RULE RN17 violations corrected
-
-Revision 1.14.4.12  2000/04/26 12:25:02  morsch
-Code revised by P. Crochet:
-- Z position of TriggerChamber changed according to A.Tournaire Priv.Comm.
-- ToF included in the method MakePadHits
-- inner radius of flange between beam shielding and trigger corrected
-- Trigger global volume updated (according to the new geometry)
-
-Revision 1.14.4.11  2000/04/19 19:42:08  morsch
-Some changes of variable names curing viols and methods concerning
-correlated clusters removed.
-
-Revision 1.14.4.10  2000/03/22 16:44:07  gosset
-Memory leak suppressed in function Digitise:
-p_adr->Delete() instead of Clear (I.Chevrot and A.Baldisseri)
-
-Revision 1.14.4.9  2000/03/20 18:15:25  morsch
-Positions of trigger chambers corrected (P.C.)
-
-Revision 1.14.4.8  2000/02/21 15:38:01  morsch
-Call to AddHitList introduced to make this version compatible with head.
-
-Revision 1.14.4.7  2000/02/20 07:45:53  morsch
-Bugs in Trigger part of BuildGeomemetry corrected (P.C)
-
-Revision 1.14.4.6  2000/02/17 14:28:54  morsch
-Trigger included into initialization and digitization
-
-Revision 1.14.4.5  2000/02/15 10:02:58  morsch
-Log messages of previous revisions added
-
-Revision 1.14.4.2  2000/02/04 10:57:34  gosset
-Z position of the chambers:
-it was the Z position of the stations;
-it is now really the Z position of the chambers.
-   !!!! WARNING: THE CALLS TO "AliMUONChamber::SetZPOS"
-   !!!!                   AND "AliMUONChamber::ZPosition"
-   !!!! HAVE TO BE CHANGED TO "AliMUONChamber::"SetZ"
-   !!!!                   AND "AliMUONChamber::Z"
-
-Revision 1.14.4.3  2000/02/04 16:19:04  gosset
-Correction for mis-spelling of NCH 
-
-Revision 1.14.4.4  2000/02/15 09:43:38  morsch
-Log message added
-
-*/
+/* $Id$ */
 
 
 ///////////////////////////////////////////////
 //  Manager and hits classes for set:MUON     //
 ////////////////////////////////////////////////
 
-#include <TTUBE.h>
+#include "Riostream.h"
+
+#include <AliPDG.h>
 #include <TBRIK.h>
-#include <TRotMatrix.h>
+#include <TCanvas.h>
+#include <TDirectory.h>
+#include <TFile.h>
 #include <TGeometry.h>
+#include <TMinuit.h>
 #include <TNode.h> 
-#include <TTree.h> 
-#include <TRandom.h> 
-#include <TObject.h>
-#include <TVector.h>
+#include <TNtuple.h>
 #include <TObjArray.h>
-#include <TMinuit.h>
+#include <TObject.h>
+#include <TObjectTable.h>
+#include <TPad.h>
 #include <TParticle.h>
 #include <TROOT.h>
-#include <TFile.h>
-#include <TNtuple.h>
-#include <TCanvas.h>
-#include <TPad.h>
-#include <TDirectory.h>
-#include <TObjectTable.h>
-#include <AliPDG.h>
+#include <TRandom.h> 
+#include <TRotMatrix.h>
+#include <TTUBE.h>
 #include <TTUBE.h>
+#include <TTree.h> 
+#include <TVector.h>
+#include <TVirtualMC.h>
 
+#include "AliConst.h" 
+#include "AliHeader.h"
+#include "AliHitMap.h"
+#include "AliLoader.h"
+#include "AliMUONLoader.h"
 #include "AliMUON.h"
+#include "AliMUONChamberTrigger.h"
+#include "AliMUONClusterFinderVS.h"
+#include "AliMUONClusterInput.h"
+#include "AliMUONConstants.h"
+#include "AliMUONDigit.h"
+#include "AliMUONGlobalTrigger.h"
 #include "AliMUONHit.h"
+#include "AliMUONHitMapA1.h"
+#include "AliMUONLocalTrigger.h"
+#include "AliMUONMerger.h"     
 #include "AliMUONPadHit.h"
-#include "AliMUONDigit.h"
-#include "AliMUONTransientDigit.h"
 #include "AliMUONRawCluster.h"
-#include "AliMUONLocalTrigger.h"
-#include "AliMUONGlobalTrigger.h"
+#include "AliMUONTransientDigit.h"
 #include "AliMUONTriggerCircuit.h"
-#include "AliHitMap.h"
-#include "AliMUONHitMapA1.h"
-#include "AliMUONChamberTrigger.h"
-#include "AliMUONConstants.h"
-#include "AliMUONClusterFinderVS.h"
 #include "AliMUONTriggerDecision.h"
-#include "AliRun.h"
-#include "AliMC.h"
-#include "AliMUONClusterInput.h"
-#include "AliMUONMerger.h"     
-#include "iostream.h"
-#include "AliCallf77.h" 
-#include "AliConst.h" 
+#include "AliRun.h"    
+
 
 // Defaults parameters for Z positions of chambers
 // taken from values for "stations" in AliMUON::AliMUON
@@ -255,7 +92,6 @@ AliMUON::AliMUON()
     fNCh             = 0;
     fNTrackingCh     = 0;
     fIshunt          = 0;
-    fHits            = 0;
     fPadHits         = 0;
     fNPadHits        = 0;
     fChambers        = 0;
@@ -272,6 +108,7 @@ AliMUON::AliMUON()
     fAccMax          = 0.;   
     fAccCut          = kFALSE;
     fMerger          = 0;
+    fFileName        = 0;
 }
  
 //___________________________________________
@@ -299,7 +136,7 @@ AliMUON::AliMUON(const char *name, const char *title)
    Int_t i;
    
    for (i=0; i<AliMUONConstants::NCh() ;i++) {
-       (*fDchambers)[i] = new TClonesArray("AliMUONDigit",10000); 
+       fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
        fNdch[i]=0;
    }
 
@@ -308,7 +145,7 @@ AliMUON::AliMUON(const char *name, const char *title)
    fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
 
    for (i=0; i<AliMUONConstants::NTrackingCh();i++) {
-       (*fRawClusters)[i] = new TClonesArray("AliMUONRawCluster",10000); 
+       fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
        fNrawch[i]=0;
    }
 
@@ -339,12 +176,13 @@ AliMUON::AliMUON(const char *name, const char *title)
            ch = 2 * st + stCH;
 //
            if (ch < AliMUONConstants::NTrackingCh()) {
-               (*fChambers)[ch] = new AliMUONChamber(ch);
+             fChambers->AddAt(new AliMUONChamber(ch),ch);
            } else {
-               (*fChambers)[ch] = new AliMUONChamberTrigger(ch);
+             fChambers->AddAt(new AliMUONChamberTrigger(ch),ch);
            }
            
-           AliMUONChamber* chamber = (AliMUONChamber*) (*fChambers)[ch];
+        //PH       AliMUONChamber* chamber = (AliMUONChamber*) (*fChambers)[ch];
+           AliMUONChamber* chamber = (AliMUONChamber*) fChambers->At(ch);
            
            chamber->SetGid(0);
            // Default values for Z of chambers
@@ -357,6 +195,7 @@ AliMUON::AliMUON(const char *name, const char *title)
 //
        } // Chamber stCH (0, 1) in 
     }     // Station st (0...)
+//    fChambers->SetLast(AliMUONConstants::NCh());
     fMaxStepGas=0.01; 
     fMaxStepAlu=0.1; 
     fMaxDestepGas=-1;
@@ -372,14 +211,14 @@ AliMUON::AliMUON(const char *name, const char *title)
    // cp new design of AliMUONTriggerDecision
    fTriggerCircuits = new TObjArray(AliMUONConstants::NTriggerCircuit());
    for (Int_t circ=0; circ<AliMUONConstants::NTriggerCircuit(); circ++) {
-     (*fTriggerCircuits)[circ] = new AliMUONTriggerCircuit();     
+     fTriggerCircuits->AddAt(new AliMUONTriggerCircuit(),circ);     
 
    }
      fMerger = 0;
 }
  
 //___________________________________________
-AliMUON::AliMUON(const AliMUON& rMUON)
+AliMUON::AliMUON(const AliMUON& rMUON):AliDetector(rMUON)
 {
 // Dummy copy constructor
     ;
@@ -389,9 +228,8 @@ AliMUON::AliMUON(const AliMUON& rMUON)
 AliMUON::~AliMUON()
 {
 // Destructor
-    printf("Calling AliMUON destructor !!!\n");
+    if(fDebug) printf("%s: Calling AliMUON destructor !!!\n",ClassName());
     
-    Int_t i;
     fIshunt  = 0;
  
     // Delete TObjArrays
@@ -415,9 +253,8 @@ AliMUON::~AliMUON()
       fRawClusters->Delete();
       delete fRawClusters;
     }
-    for (i=0;i<AliMUONConstants::NTrackingCh();i++) {
-      fNrawch[i]=0;
-    }
+
+    if (fNrawch) delete [] fNrawch;
  
     // Delete TClonesArrays
  
@@ -438,29 +275,14 @@ AliMUON::~AliMUON()
     }
     fNLocalTrigger = 0;
 
-    if (fHits2){
-      fHits2->Delete();
-      delete fHits2;
-    }
-
-    if (fPadHits2){
-      fPadHits2->Delete();
-      delete fPadHits2;
-    }
-
     if (fHits) {
       fHits->Delete();
       delete fHits;
     }
 
-    // Delete hits tree for background event
-
-    if (fTrH1) {
-      fTrH1->Delete();
-      delete fTrH1;
-    }
-
     if (fMerger) delete fMerger;
+    if (fNdch) delete [] fNdch;
+
 }
  
 //___________________________________________
@@ -470,7 +292,19 @@ void AliMUON::AddHit(Int_t track, Int_t *vol, Float_t *hits)
   new(lhits[fNhits++]) AliMUONHit(fIshunt,track,vol,hits);
 }
 //___________________________________________
-void AliMUON::AddPadHit(Int_t *clhits)
+void AliMUON::AddHit(Int_t fIshunt, Int_t track, Int_t iChamber, 
+             Int_t idpart, Float_t X, Float_t Y, Float_t Z, 
+             Float_t tof, Float_t momentum, Float_t theta, 
+             Float_t phi, Float_t length, Float_t destep)
+{
+  TClonesArray &lhits = *fHits;
+  new(lhits[fNhits++]) AliMUONHit(fIshunt, track, iChamber, 
+              idpart, X, Y, Z, 
+              tof, momentum, theta, 
+              phi, length, destep);
+}
+//___________________________________________
+void AliMUON::AddPadHit(Int_t *clhits)  // To be removed
 {
    TClonesArray &lclusters = *fPadHits;
    new(lclusters[fNPadHits++]) AliMUONPadHit(clhits);
@@ -482,7 +316,8 @@ void AliMUON::AddDigits(Int_t id, Int_t *tracks, Int_t *charges, Int_t *digits)
     // Add a MUON digit to the list
     //
 
-    TClonesArray &ldigits = *((TClonesArray*)(*fDchambers)[id]);
+  //PH    TClonesArray &ldigits = * ((TClonesArray*)(*fDchambers)[id]);
+    TClonesArray &ldigits = * ( (TClonesArray*) fDchambers->At(id) );
     new(ldigits[fNdch[id]++]) AliMUONDigit(tracks,charges,digits);
 }
 
@@ -493,7 +328,8 @@ void AliMUON::AddRawCluster(Int_t id, const AliMUONRawCluster& c)
     // Add a MUON digit to the list
     //
 
-    TClonesArray &lrawcl = *((TClonesArray*)(*fRawClusters)[id]);
+  //PH    TClonesArray &lrawcl = *((TClonesArray*)(*fRawClusters)[id]);
+    TClonesArray &lrawcl = *((TClonesArray*)fRawClusters->At(id));
     new(lrawcl[fNrawch[id]++]) AliMUONRawCluster(c);
 }
 
@@ -535,7 +371,7 @@ Int_t AliMUON::DistancetoPrimitive(Int_t , Int_t )
 }
 
 //___________________________________________
-void AliMUON::MakeBranch(Option_t* option, char *file)
+void AliMUON::MakeBranch(Option_t* option)
 {
     //
     // Create Tree branches for the MUON.
@@ -544,68 +380,83 @@ void AliMUON::MakeBranch(Option_t* option, char *file)
     char branchname[30];
     sprintf(branchname,"%sCluster",GetName());
     
-    AliDetector::MakeBranch(option,file);
     
-    char *cD = strstr(option,"D");
-    char *cR = strstr(option,"R");
-    char *cH = strstr(option,"H");
-
-    if (fPadHits   && gAlice->TreeH() && cH) {
-      gAlice->MakeBranchInTree(gAlice->TreeH(), 
-                               branchname, &fPadHits, kBufferSize, file) ;       
-         printf("Making Branch %s for clusters\n",branchname);
-    }
+    const char *cD = strstr(option,"D");
+    const char *cR = strstr(option,"R");
+    const char *cH = strstr(option,"H");
+
+    if (TreeH() && cH) 
+     {
+      if (fPadHits == 0x0) fPadHits = new TClonesArray("AliMUONPadHit",10000);
+      MakeBranchInTree(TreeH(), branchname, &fPadHits, kBufferSize, 0);
+      if (fHits == 0x0) fHits     = new TClonesArray("AliMUONHit",1000);
+     }
+    //it must be under fHits creation
+    AliDetector::MakeBranch(option);
     
-    if (cD) {
+    if (cD  && fLoader->TreeD()) {
       //
       // one branch for digits per chamber
       // 
       Int_t i;
-    
-      for (i=0; i<AliMUONConstants::NCh() ;i++) {
-           sprintf(branchname,"%sDigits%d",GetName(),i+1);     
-           if (fDchambers   && gAlice->TreeD()) {
-          gAlice->MakeBranchInTree(gAlice->TreeD(), 
-                                   branchname, &((*fDchambers)[i]), kBufferSize, file) ;         
-             printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
+      if (fDchambers  == 0x0) 
+        {
+          fDchambers = new TObjArray(AliMUONConstants::NCh());
+          for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
+              fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
+          }
         }
-         }     
+    
+      for (i=0; i<AliMUONConstants::NCh() ;i++) 
+       {
+        sprintf(branchname,"%sDigits%d",GetName(),i+1);        
+        MakeBranchInTree(fLoader->TreeD(), branchname, &((*fDchambers)[i]), kBufferSize, 0);
+        printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
+       }
     }
     
-    if (cR) {
+    if (cR  && fLoader->TreeR()) {
       //     
       // one branch for raw clusters per chamber
       //  
-      printf("Make Branch - TreeR address %p\n",gAlice->TreeR());
+      printf("Make Branch - TreeR address %p\n",fLoader->TreeR());
       
       Int_t i;
+      if (fRawClusters == 0x0)
+      {
+        fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
+        for (Int_t i=0; i<AliMUONConstants::NTrackingCh();i++) {
+            fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
+        }
+      }
 
-      for (i=0; i<AliMUONConstants::NTrackingCh() ;i++) {
-           sprintf(branchname,"%sRawClusters%d",GetName(),i+1);        
-           if (fRawClusters   && gAlice->TreeR()) {
-          gAlice->MakeBranchInTree(gAlice->TreeR(), 
-                                   branchname, &((*fRawClusters)[i]), kBufferSize, file) ;       
-             printf("Making Branch %s for raw clusters in chamber %d\n",branchname,i+1);
-           }   
+      for (i=0; i<AliMUONConstants::NTrackingCh() ;i++) 
+       {
+         sprintf(branchname,"%sRawClusters%d",GetName(),i+1);  
+         MakeBranchInTree(fLoader->TreeR(), branchname, &((*fRawClusters)[i]), kBufferSize, 0);
+         printf("Making Branch %s for raw clusters in chamber %d\n",branchname,i+1);
       }
       //
       // one branch for global trigger
       //
       sprintf(branchname,"%sGlobalTrigger",GetName());
-      if (fGlobalTrigger && gAlice->TreeR()) {  
-        gAlice->MakeBranchInTree(gAlice->TreeR(), 
-                                 branchname, &fGlobalTrigger, kBufferSize, file) ;       
-           printf("Making Branch %s for Global Trigger\n",branchname);
+      
+      if (fGlobalTrigger == 0x0) {
+        fGlobalTrigger = new TClonesArray("AliMUONGlobalTrigger",1); 
       }
+      MakeBranchInTree(fLoader->TreeR(), branchname, &fGlobalTrigger, kBufferSize, 0);
+      printf("Making Branch %s for Global Trigger\n",branchname);
       //
       // one branch for local trigger
       //  
       sprintf(branchname,"%sLocalTrigger",GetName());
-      if (fLocalTrigger && gAlice->TreeR()) {  
-        gAlice->MakeBranchInTree(gAlice->TreeR(), 
-                                 branchname, &fLocalTrigger, kBufferSize, file) ;        
-           printf("Making Branch %s for Local Trigger\n",branchname);
+      
+      if (fLocalTrigger == 0x0) {
+        fLocalTrigger  = new TClonesArray("AliMUONLocalTrigger",234);
       }
+      
+      MakeBranchInTree(fLoader->TreeR(), branchname, &fLocalTrigger, kBufferSize, 0);
+      printf("Making Branch %s for Local Trigger\n",branchname);
    }
 }
 
@@ -614,24 +465,35 @@ void AliMUON::SetTreeAddress()
 {
   // Set branch address for the Hits and Digits Tree.
   char branchname[30];
-  AliDetector::SetTreeAddress();
 
   TBranch *branch;
-  TTree *treeH = gAlice->TreeH();
-  TTree *treeD = gAlice->TreeD();
-  TTree *treeR = gAlice->TreeR();
+  TTree *treeH = fLoader->TreeH();
+  TTree *treeD = fLoader->TreeD();
+  TTree *treeR = fLoader->TreeR();
 
   if (treeH) {
+    if (fPadHits == 0x0) fPadHits = new TClonesArray("AliMUONPadHit",10000);
     if (fPadHits) {
       branch = treeH->GetBranch("MUONCluster");
       if (branch) branch->SetAddress(&fPadHits);
     }
+    if (fHits == 0x0) fHits     = new TClonesArray("AliMUONHit",1000);
   }
+  //it must be under fHits creation
+  AliDetector::SetTreeAddress();
 
   if (treeD) {
+      if (fDchambers == 0x0) 
+        {
+          fDchambers = new TObjArray(AliMUONConstants::NCh());
+          for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
+              fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
+          }
+        }
       for (int i=0; i<AliMUONConstants::NCh(); i++) {
          sprintf(branchname,"%sDigits%d",GetName(),i+1);
-         if (fDchambers) {
+                        
+                      if (fDchambers) {
              branch = treeD->GetBranch(branchname);
              if (branch) branch->SetAddress(&((*fDchambers)[i]));
          }
@@ -641,6 +503,14 @@ void AliMUON::SetTreeAddress()
   // printf("SetTreeAddress --- treeR address  %p \n",treeR);
 
   if (treeR) {
+      if (fRawClusters == 0x0)
+      {
+        fRawClusters = new TObjArray(AliMUONConstants::NTrackingCh());
+        for (Int_t i=0; i<AliMUONConstants::NTrackingCh();i++) {
+            fRawClusters->AddAt(new TClonesArray("AliMUONRawCluster",10000),i); 
+        }
+      }
+      
       for (int i=0; i<AliMUONConstants::NTrackingCh(); i++) {
          sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
          if (fRawClusters) {
@@ -649,10 +519,19 @@ void AliMUON::SetTreeAddress()
          }
       }
 
+      if (fLocalTrigger == 0x0) {
+        fLocalTrigger  = new TClonesArray("AliMUONLocalTrigger",234);
+      }
+
       if (fLocalTrigger) {
        branch = treeR->GetBranch("MUONLocalTrigger");
        if (branch) branch->SetAddress(&fLocalTrigger);
       }
+      
+      if (fGlobalTrigger == 0x0) {
+        fGlobalTrigger = new TClonesArray("AliMUONGlobalTrigger",1); 
+      }
+      
       if (fGlobalTrigger) {
        branch = treeR->GetBranch("MUONGlobalTrigger");
        if (branch) branch->SetAddress(&fGlobalTrigger);
@@ -674,8 +553,10 @@ void AliMUON::ResetDigits()
     //
     // Reset number of digits and the digits array for this detector
     //
+    if (fDchambers == 0x0) return;
     for ( int i=0;i<AliMUONConstants::NCh();i++ ) {
-       if ((*fDchambers)[i])    ((TClonesArray*)(*fDchambers)[i])->Clear();
+      //PH     if ((*fDchambers)[i])    ((TClonesArray*)(*fDchambers)[i])->Clear();
+       if ((*fDchambers)[i])    ((TClonesArray*)fDchambers->At(i))->Clear();
        if (fNdch)  fNdch[i]=0;
     }
 }
@@ -686,7 +567,8 @@ void AliMUON::ResetRawClusters()
     // Reset number of raw clusters and the raw clust array for this detector
     //
     for ( int i=0;i<AliMUONConstants::NTrackingCh();i++ ) {
-       if ((*fRawClusters)[i])    ((TClonesArray*)(*fRawClusters)[i])->Clear();
+      //PH     if ((*fRawClusters)[i])    ((TClonesArray*)(*fRawClusters)[i])->Clear();
+       if ((*fRawClusters)[i])    ((TClonesArray*)fRawClusters->At(i))->Clear();
        if (fNrawch)  fNrawch[i]=0;
     }
 }
@@ -706,8 +588,10 @@ void AliMUON::SetPadSize(Int_t id, Int_t isec, Float_t p1, Float_t p2)
 {
 // Set the pad size for chamber id and cathode isec
     Int_t i=2*(id-1);
-    ((AliMUONChamber*) (*fChambers)[i])  ->SetPadSize(isec,p1,p2);
-    ((AliMUONChamber*) (*fChambers)[i+1])->SetPadSize(isec,p1,p2);
+    //PH    ((AliMUONChamber*) (*fChambers)[i])  ->SetPadSize(isec,p1,p2);
+    //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetPadSize(isec,p1,p2);
+    ((AliMUONChamber*) fChambers->At(i))  ->SetPadSize(isec,p1,p2);
+    ((AliMUONChamber*) fChambers->At(i+1))->SetPadSize(isec,p1,p2);
 }
 
 //___________________________________________
@@ -716,7 +600,8 @@ void AliMUON::SetChambersZ(const Float_t *Z)
   // Set Z values for all chambers (tracking and trigger)
   // from the array pointed to by "Z"
     for (Int_t ch = 0; ch < AliMUONConstants::NCh(); ch++)
-       ((AliMUONChamber*) ((*fChambers)[ch]))->SetZ(Z[ch]);
+      //PH     ((AliMUONChamber*) ((*fChambers)[ch]))->SetZ(Z[ch]);
+       ((AliMUONChamber*) fChambers->At(ch))->SetZ(Z[ch]);
     return;
 }
 
@@ -734,8 +619,10 @@ void AliMUON::SetChargeSlope(Int_t id, Float_t p1)
 {
 // Set the inverse charge slope for chamber id
     Int_t i=2*(id-1);
-    ((AliMUONChamber*) (*fChambers)[i])->SetChargeSlope(p1);
-    ((AliMUONChamber*) (*fChambers)[i+1])->SetChargeSlope(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i])->SetChargeSlope(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetChargeSlope(p1);
+    ((AliMUONChamber*) fChambers->At(i))->SetChargeSlope(p1);
+    ((AliMUONChamber*) fChambers->At(i+1))->SetChargeSlope(p1);
 }
 
 //___________________________________________
@@ -743,8 +630,10 @@ void AliMUON::SetChargeSpread(Int_t id, Float_t p1, Float_t p2)
 {
 // Set sigma of charge spread for chamber id
     Int_t i=2*(id-1);
-    ((AliMUONChamber*) (*fChambers)[i])->SetChargeSpread(p1,p2);
-    ((AliMUONChamber*) (*fChambers)[i+1])->SetChargeSpread(p1,p2);
+    //PH    ((AliMUONChamber*) fChambers->At(i))->SetChargeSpread(p1,p2);
+    //PH    ((AliMUONChamber*) fChambers->Ati+1])->SetChargeSpread(p1,p2);
+    ((AliMUONChamber*) fChambers->At(i))->SetChargeSpread(p1,p2);
+    ((AliMUONChamber*) fChambers->At(i+1))->SetChargeSpread(p1,p2);
 }
 
 //___________________________________________
@@ -752,8 +641,10 @@ void AliMUON::SetSigmaIntegration(Int_t id, Float_t p1)
 {
 // Set integration limits for charge spread
     Int_t i=2*(id-1);
-    ((AliMUONChamber*) (*fChambers)[i])->SetSigmaIntegration(p1);
-    ((AliMUONChamber*) (*fChambers)[i+1])->SetSigmaIntegration(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i])->SetSigmaIntegration(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetSigmaIntegration(p1);
+    ((AliMUONChamber*) fChambers->At(i))->SetSigmaIntegration(p1);
+    ((AliMUONChamber*) fChambers->At(i+1))->SetSigmaIntegration(p1);
 }
 
 //___________________________________________
@@ -761,8 +652,10 @@ void AliMUON::SetMaxAdc(Int_t id, Int_t p1)
 {
 // Set maximum number for ADCcounts (saturation)
     Int_t i=2*(id-1);
-    ((AliMUONChamber*) (*fChambers)[i])->SetMaxAdc(p1);
-    ((AliMUONChamber*) (*fChambers)[i+1])->SetMaxAdc(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i])->SetMaxAdc(p1);
+    //PH    ((AliMUONChamber*) (*fChambers)[i+1])->SetMaxAdc(p1);
+    ((AliMUONChamber*) fChambers->At(i))->SetMaxAdc(p1);
+    ((AliMUONChamber*) fChambers->At(i+1))->SetMaxAdc(p1);
 }
 
 //___________________________________________
@@ -816,38 +709,70 @@ void AliMUON::SetAcceptance(Bool_t acc, Float_t angmin, Float_t angmax)
 void   AliMUON::SetSegmentationModel(Int_t id, Int_t isec, AliSegmentation *segmentation)
 {
 // Set the segmentation for chamber id cathode isec
-    ((AliMUONChamber*) (*fChambers)[id])->SetSegmentationModel(isec, segmentation);
+  //PH    ((AliMUONChamber*) (*fChambers)[id])->SetSegmentationModel(isec, segmentation);
+    ((AliMUONChamber*) fChambers->At(id))->SetSegmentationModel(isec, segmentation);
 
 }
 //___________________________________________
 void   AliMUON::SetResponseModel(Int_t id, AliMUONResponse *response)
 {
 // Set the response for chamber id
-    ((AliMUONChamber*) (*fChambers)[id])->SetResponseModel(response);
+  //PH    ((AliMUONChamber*) (*fChambers)[id])->SetResponseModel(response);
+    ((AliMUONChamber*) fChambers->At(id))->SetResponseModel(response);
 }
 
 void   AliMUON::SetReconstructionModel(Int_t id, AliMUONClusterFinderVS *reconst)
 {
 // Set ClusterFinder for chamber id
-    ((AliMUONChamber*) (*fChambers)[id])->SetReconstructionModel(reconst);
+  //PH    ((AliMUONChamber*) (*fChambers)[id])->SetReconstructionModel(reconst);
+    ((AliMUONChamber*) fChambers->At(id))->SetReconstructionModel(reconst);
 }
 
 void   AliMUON::SetNsec(Int_t id, Int_t nsec)
 {
 // Set number of segmented cathods for chamber id
-    ((AliMUONChamber*) (*fChambers)[id])->SetNsec(nsec);
+  //PH    ((AliMUONChamber*) (*fChambers)[id])->SetNsec(nsec);
+    ((AliMUONChamber*) fChambers->At(id))->SetNsec(nsec);
 }
 
 //___________________________________________
 void AliMUON::SDigits2Digits()
 {
-    if (fMerger) {
-       fMerger->Init();
-       fMerger->Digitise();
+
+// write TreeD here 
+
+    if (!fMerger) {
+      if (gAlice->GetDebug()>0) {
+       cerr<<"AliMUON::SDigits2Digits: create default AliMUONMerger "<<endl;
+       cerr<<" no merging, just digitization of 1 event will be done"<<endl;
+      }
+      fMerger = new AliMUONMerger();
     }
+    fMerger->Init();
+    fMerger->Digitise();
+    char hname[30];
+    //    sprintf(hname,"TreeD%d",fLoader->GetHeader()->GetEvent());
+    fLoader->TreeD()->Write(hname,TObject::kOverwrite);
+    fLoader->TreeD()->Reset();
 }
 
-//___________________________________________
+
+//__________________________________________________________________________
+AliLoader* AliMUON::MakeLoader(const char* topfoldername)
+{ 
+//builds standard getter (AliLoader type)
+//if detector wants to use castomized getter, it must overload this method
+
+ if (GetDebug())
+   Info("MakeLoader",
+        "Creating standard getter for detector %s. Top folder is %s.",
+         GetName(),topfoldername);
+     
+ fLoader = new AliMUONLoader(GetName(),topfoldername);
+ return fLoader;
+}
+//__________________________________________________________________________
+// To be removed
 void AliMUON::MakePadHits(Float_t xhit,Float_t yhit, Float_t zhit,
                          Float_t eloss, Float_t tof,  Int_t idvol)
 {
@@ -870,7 +795,8 @@ void AliMUON::MakePadHits(Float_t xhit,Float_t yhit, Float_t zhit,
 //    if (idvol == 6) printf("\n ->Disintegration %f %f %f", xhit, yhit, eloss );
     
 
-    ((AliMUONChamber*) (*fChambers)[idvol])
+    //PH    ((AliMUONChamber*) (*fChambers)[idvol])
+    ((AliMUONChamber*) fChambers->At(idvol))
        ->DisIntegration(eloss, tof, xhit, yhit, zhit, nnew, newclust);
     Int_t ic=0;
 //    if (idvol == 6) printf("\n nnew  %d \n", nnew);
@@ -912,6 +838,7 @@ void AliMUON::Trigger(Int_t nev){
   decision->Trigger();   
   decision->GetGlobalTrigger(singlePlus, singleMinus, singleUndef,
                             pairUnlike, pairLike);
+
 // add a local trigger in the list 
   AddGlobalTrigger(singlePlus, singleMinus, singleUndef, pairUnlike, pairLike);
   Int_t i;
@@ -933,20 +860,37 @@ void AliMUON::Trigger(Int_t nev){
          AddLocalTrigger(localtr);  // add a local trigger in the list
       }
   }
+
   delete decision;
 
-  gAlice->TreeR()->Fill();
+  fLoader->TreeR()->Fill();
+//  char hname[30];
+//  sprintf(hname,"TreeR%d",nev);
+//  fLoader->TreeR()->Write(hname,TObject::kOverwrite);
+//  fLoader->TreeR()->Reset();
+  fLoader->WriteRecPoints("OVERWRITE");
   ResetTrigger();
-  char hname[30];
-  sprintf(hname,"TreeR%d",nev);
-  gAlice->TreeR()->Write(hname,TObject::kOverwrite);
-  gAlice->TreeR()->Reset();
+  
   printf("\n End of trigger for event %d", nev);
 }
 
 
 //____________________________________________
-void AliMUON::FindClusters(Int_t nev,Int_t lastEntry)
+void AliMUON::Digits2Reco()
+{
+  FindClusters();
+  Int_t nev = gAlice->GetHeader()->GetEvent();
+  fLoader->TreeR()->Fill();
+  // char hname[30];
+  // sprintf(hname,"TreeR%d", nev);
+  //fLoader->TreeR()->Write(hname);
+  //fLoader->TreeR()->Reset();
+  fLoader->WriteRecPoints("OVERWRITE");
+  ResetRawClusters();        
+  printf("\n End of cluster finding for event %d", nev);
+}
+
+void AliMUON::FindClusters()
 {
 //
 //  Perform cluster finding
@@ -956,18 +900,22 @@ void AliMUON::FindClusters(Int_t nev,Int_t lastEntry)
     dig1 = new TClonesArray("AliMUONDigit",1000);
     dig2 = new TClonesArray("AliMUONDigit",1000);
     AliMUONDigit *digit;
-//
 // Loop on chambers and on cathode planes
 //
-    
+    ResetRawClusters();        
+    TClonesArray * muonDigits;
+
     for (Int_t ich = 0; ich < 10; ich++) {
-       AliMUONChamber* iChamber = (AliMUONChamber*) (*fChambers)[ich];
-       AliMUONClusterFinderVS* rec = iChamber->ReconstructionModel();    
-       gAlice->ResetDigits();
-       gAlice->TreeD()->GetEvent(lastEntry);
-       TClonesArray *muonDigits = this->DigitsAddress(ich);
+      //PH     AliMUONChamber* iChamber = (AliMUONChamber*) (*fChambers)[ich];
+       AliMUONChamber* iChamber = (AliMUONChamber*) fChambers->At(ich);
+       AliMUONClusterFinderVS* rec = iChamber->ReconstructionModel();
+    
+       ResetDigits();
+       fLoader->TreeD()->GetEvent(0);
+       //TClonesArray *
+       muonDigits = (TClonesArray *) Dchambers()->At(ich);
        ndig=muonDigits->GetEntriesFast();
-       printf("\n 1 Found %d digits in %p %d", ndig, muonDigits,ich);
+       printf("\n 1 Found %d digits in %p chamber %d", ndig, muonDigits,ich);
        TClonesArray &lhits1 = *dig1;
        Int_t n = 0;
        for (k = 0; k < ndig; k++) {
@@ -975,9 +923,10 @@ void AliMUON::FindClusters(Int_t nev,Int_t lastEntry)
            if (rec->TestTrack(digit->Track(0)))
                new(lhits1[n++]) AliMUONDigit(*digit);
        }
-       gAlice->ResetDigits();
-       gAlice->TreeD()->GetEvent(lastEntry+1);
-       muonDigits  = this->DigitsAddress(ich);
+       ResetDigits();
+       fLoader->TreeD()->GetEvent(1);
+       //muonDigits  = this->DigitsAddress(ich);
+       muonDigits = (TClonesArray *) Dchambers()->At(ich);
        ndig=muonDigits->GetEntriesFast();
        printf("\n 2 Found %d digits in %p %d", ndig, muonDigits, ich);
        TClonesArray &lhits2 = *dig2;
@@ -996,17 +945,8 @@ void AliMUON::FindClusters(Int_t nev,Int_t lastEntry)
        dig1->Delete();
        dig2->Delete();
     } // for ich
-    gAlice->TreeR()->Fill();
-    ResetRawClusters();
-    char hname[30];
-    sprintf(hname,"TreeR%d",nev);
-    gAlice->TreeR()->Write(hname,TObject::kOverwrite);
-    gAlice->TreeR()->Reset();
-    printf("\n End of cluster finding for event %d", nev);
-    
     delete dig1;
     delete dig2;
-    //gObjectTable->Print();
 }
  
 #ifdef never
@@ -1124,7 +1064,7 @@ void AliMUON::Streamer(TBuffer &R__b)
 
 AliMUONPadHit* AliMUON::FirstPad(AliMUONHit*  hit, TClonesArray *clusters) 
 {
-//
+// to be removed
     // Initialise the pad iterator
     // Return the address of the first padhit for hit
     TClonesArray *theClusters = clusters;
@@ -1140,6 +1080,7 @@ AliMUONPadHit* AliMUON::FirstPad(AliMUONHit*  hit, TClonesArray *clusters)
 
 AliMUONPadHit* AliMUON::NextPad(TClonesArray *clusters) 
 {
+  // To be removed
 // Get next pad (in iterator) 
 //
     AliMUON::fCurIterPad++;
@@ -1158,7 +1099,7 @@ AliMUONRawCluster *AliMUON::RawCluster(Int_t ichamber, Int_t icathod, Int_t iclu
 //  Obsolete ??
     TClonesArray *muonRawCluster  = RawClustAddress(ichamber);
     ResetRawClusters();
-    TTree *treeR = gAlice->TreeR();
+    TTree *treeR = fLoader->TreeR();
     Int_t nent=(Int_t)treeR->GetEntries();
     treeR->GetEvent(nent-2+icathod-1);
     //treeR->GetEvent(icathod);
@@ -1184,28 +1125,41 @@ AliMUONMerger*  AliMUON::Merger()
 
 
 
-AliMUON& AliMUON::operator = (const AliMUON& rhs)
+AliMUON& AliMUON::operator = (const AliMUON& /*rhs*/)
 {
 // copy operator
 // dummy version
     return *this;
 }
 
+////////////////////////////////////////////////////////////////////////
+void AliMUON::MakeBranchInTreeD(TTree *treeD, const char *file)
+{
+    //
+    // Create TreeD branches for the MUON.
+    //
 
+  const Int_t kBufferSize = 4000;
+  char branchname[30];
+    
+  if (fDchambers  == 0x0)   {
+    fDchambers = new TObjArray(AliMUONConstants::NCh());
+    for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
+      fDchambers->AddAt(new TClonesArray("AliMUONDigit",10000),i); 
+    }
+  }
+  //
+  // one branch for digits per chamber
+  // 
+  for (Int_t i=0; i<AliMUONConstants::NCh() ;i++) {
+    sprintf(branchname,"%sDigits%d",GetName(),i+1);    
+    if (fDchambers && treeD) {
+      MakeBranchInTree(treeD, 
+                      branchname, &((*fDchambers)[i]), kBufferSize, file);
+//      printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
+    }
+  }
+}
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+//___________________________________________