]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONDigitizerv2.cxx
Minor fixes in the event tag to take into account the new way of storing the trigger...
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizerv2.cxx
index 7f433044c50797d9aa00d6121a2307b0bd6834ce..0daf860981de7bd2d8920b971dd86116e5839c77 100644 (file)
+/**************************************************************************
+ * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ *                                                                        *
+ * Author: The ALICE Off-line Project.                                    *
+ * Contributors are mentioned in the code where appropriate.              *
+ *                                                                        *
+ * Permission to use, copy, modify and distribute this software and its   *
+ * documentation strictly for non-commercial purposes is hereby granted   *
+ * without fee, provided that the above copyright notice appears in all   *
+ * copies and that both the copyright notice and this permission notice   *
+ * appear in the supporting documentation. The authors make no claims     *
+ * about the suitability of this software for any purpose. It is          *
+ * provided "as is" without express or implied warranty.                  *
+ **************************************************************************/
+
+/* $Id$ */
 
 //  Do the Digitization (Digit) from summable Digits (SDigit)
 //  Allow the merging of signal file with background file(s).
 
-#include <Riostream.h>
-#include <TDirectory.h>
-#include <TFile.h>
-#include <TObjArray.h>
-#include <TPDGCode.h>
-#include <TTree.h> 
-#include <TMath.h>
+/////////////////////////////////////////////////////////////////////////////////
+//
+// AliMUONDigitizerv2 digitizes digits from s-digits. 
+// Merging is allowed from multiple input streams. The first input stream is 
+// assumed to be the signal and all other input streams as assumed to be 
+// background.
+// Chamber response is applied to the digits identically to AliMUONDigitizerv1.
+//
+/////////////////////////////////////////////////////////////////////////////////
 
 #include "AliMUON.h"
-#include "AliMUONChamber.h"
+#include "AliMUONData.h"
+#include "AliMUONLoader.h"
 #include "AliMUONConstants.h"
+#include "AliMUONChamber.h"
 #include "AliMUONDigit.h"
 #include "AliMUONDigitizerv2.h"
-#include "AliMUONHit.h"
-#include "AliMUONHitMapA1.h"
-#include "AliMUONPadHit.h"
 #include "AliMUONTransientDigit.h"
-#include "AliRun.h"
-#include "AliRunDigitizer.h"
-#include "AliRunLoader.h"
-#include "AliLoader.h"
+#include "AliMUONTriggerDecision.h"
+#include "AliLog.h"
 
 ClassImp(AliMUONDigitizerv2)
 
 //___________________________________________
-AliMUONDigitizerv2::AliMUONDigitizerv2() :
-  AliDigitizer(),
-  fHitMap(0),
-  fTDList(0),
-  fTDCounter(0),
-  fDebug(0),
-  fMask(0),
-  fSignal(0)
+AliMUONDigitizerv2::AliMUONDigitizerv2() : AliMUONDigitizer()
 {
-// Default ctor - don't use it
-  if (GetDebug()>2) 
-    cerr<<"AliMUONDigitizerv2::AliMUONDigitizerv2"
-       <<"(AliRunDigitizer* manager) was processed"<<endl;
+/// Default ctor - don't use it
 }
 
 //___________________________________________
-AliMUONDigitizerv2::AliMUONDigitizerv2(AliRunDigitizer* manager):
-  AliDigitizer(manager),
-  fHitMap(0),
-  fTDList(0),
-  fTDCounter(0),
-  fDebug(0),
-  fMask(0),
-  fSignal(0)
+AliMUONDigitizerv2::AliMUONDigitizerv2(AliRunDigitizer* manager) : AliMUONDigitizer(manager)
 {
-// ctor which should be used
-  if (GetDebug()>2) 
-    cerr<<"AliMUONDigitizerv2::AliMUONDigitizerv2"
-       <<"(AliRunDigitizer* manager) was processed"<<endl;
+/// ctor which should be used
 }
 
-//------------------------------------------------------------------------
+//___________________________________________
 AliMUONDigitizerv2::~AliMUONDigitizerv2()
 {
-// Destructor
+/// Destructor
+}
+
+//-----------------------------------------------------------------------
+void AliMUONDigitizerv2::GenerateTransientDigits()
+{
+/// Loop over all chambers and s-digits in the input stream and create 
+/// AliMUONTransientDigit objects from them. These are then added to fTDList.
+
+       AliDebug(2,"Generating transient digits using treeH = 0x%X");
+       //
+       // Loop over SDigits
+       Int_t ndig, k;
+       AliMUONDigit* sDigit;
+       TClonesArray* muonSDigits;
+       for (Int_t ich = 0; ich < AliMUONConstants::NCh(); ich++)  // loop over chamber
+       {
+               fMUONData->ResetSDigits();
+               fMUONData->GetSDigits();
+               muonSDigits = fMUONData->SDigits(ich); 
+               ndig = muonSDigits->GetEntriesFast();
+               for (k = 0; k < ndig; k++)
+               {
+                       sDigit = (AliMUONDigit*) muonSDigits->UncheckedAt(k);
+                       MakeTransientDigitFromSDigit(ich,sDigit);
+               }
+//             fMUONData->ResetSDigits();
+//             fMUONData->GetCathodeS(1);
+//             muonSDigits = fMUONData->SDigits(ich); 
+//             ndig=muonSDigits->GetEntriesFast();
+//             for (k = 0; k < ndig; k++)
+//             {
+//                     sDigit = (AliMUONDigit*) muonSDigits->UncheckedAt(k);
+//                     MakeTransientDigitFromSDigit(ich,sDigit);
+//             }
+       } // SDigits loop, end loop over chamber
 }
 
 //------------------------------------------------------------------------
-void AliMUONDigitizerv2::AddTransientDigit(AliMUONTransientDigit * mTD)
+void AliMUONDigitizerv2::MakeTransientDigitFromSDigit(Int_t iChamber, AliMUONDigit* sDigit)
 {
-  // Choosing the maping of the cathode plane of the chamber:
-  Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
-  fTDList->AddAtAndExpand(mTD, fTDCounter);
-  fHitMap[iNchCpl]->SetHit( mTD->PadX(), mTD->PadY(), fTDCounter);
-  fTDCounter++;
+/// Makes a transient digit from the specified s-digit from the specified chamber. 
+/// Once the digit is created it is added to the fTDList.
+
+       AliDebug(4,Form("Making transient digit from s-digit for chamber %d.", iChamber));
+       Int_t digits[7];
+       //
+       // Creating a new TransientDigits from SDigit
+       digits[0] = sDigit->PadX();  // Padx of the Digit
+       digits[1] = sDigit->PadY();  // Pady of the Digit
+       digits[2] = sDigit->Cathode()+1;  // Cathode plane
+       digits[3] = sDigit->Signal();  // Induced charge in the Pad
+       if (fSignal)
+               digits[4] = sDigit->Signal();
+       else
+               digits[4] = 0;
+               
+       digits[5] = sDigit->Hit();    // Hit number in the list
+       digits[6] = sDigit->DetElemId();
+
+       AliDebug(5,Form("Made digit from sDigit 0x%X: PadX %d\tPadY %d\tPlane %d\tCharge %d\tHit %d\tidDE %d",
+                       (void*)sDigit, digits[0], digits[1], digits[2], digits[3], digits[5], digits[6]));
+
+       
+       AliMUONTransientDigit* mTD = new AliMUONTransientDigit(iChamber, digits);
+       // Copy list of tracks and trackcharge
+       for(Int_t itrack = 0; itrack < sDigit->Ntracks(); ++itrack)
+       {
+               Int_t track = sDigit->Track(itrack);
+               if (track < 0) break;  // Check if we reached the end of the track list.
+               mTD->AddToTrackList( track + fMask, sDigit->TrackCharge(itrack) );
+       }
+
+       OnCreateTransientDigit(mTD, sDigit);
+       AddOrUpdateTransientDigit(mTD);
 }
 
 //------------------------------------------------------------------------
-Bool_t AliMUONDigitizerv2::ExistTransientDigit(AliMUONTransientDigit * mTD)
+void AliMUONDigitizerv2::AddDigit(Int_t chamber, Int_t tracks[kMAXTRACKS], Int_t charges[kMAXTRACKS], Int_t digits[7])
 {
-  // Choosing the maping of the cathode plane of the chamber:
-  Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
-  return( fHitMap[iNchCpl]->TestHit(mTD->PadX(), mTD->PadY()) );
+/// Override to add new digits to the digits tree TreeD.
+       fMUONData->AddDigit(chamber, tracks, charges, digits);   
 }
 
 //------------------------------------------------------------------------
-Bool_t AliMUONDigitizerv2::Init()
+Int_t AliMUONDigitizerv2::GetSignalFrom(AliMUONTransientDigit* td)
 {
+/// Derived to apply the chamber response model to the digit. 
+/// Using AliMUONChamber::ResponseModel() for this.
 
-// Initialization 
-  if (GetDebug()>2) Info("Init","AliMUONDigitizerv2::Init() starts");
-
-  //Loaders (We assume input0 to be the output too)
-  AliRunLoader * runloader;    // Input   loader
-  AliLoader    * gime; 
-  // Getting runloader
-  runloader    = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
-  if (runloader == 0x0) {
-    Error("Init","RunLoader is not in input file 0");
-    return kFALSE;  // RunDigitizer is not working.  
+       AliDebug(4, "Applying response of chamber to TransientDigit signal.");
+       //
+       //  Digit Response (noise, threshold, saturation, ...)
+       Int_t q = td->Signal(); 
+       AliMUONChamber& chamber = fMUON->Chamber(td->Chamber());
+       AliMUONResponse* response = chamber.ResponseModel();
+       q = response->DigitResponse(q, td);
+  if ( q >= response->Saturation() )
+  {
+    td->Saturated(kTRUE);
   }
-  // Getting MUONloader
-  gime        = runloader->GetLoader("MUONLoader");
-  gime->LoadSDigits("READ");
-  gime->LoadDigits("RECREATE");
-  
-  return kTRUE;
+       return q;
 }
 
 //------------------------------------------------------------------------
-void AliMUONDigitizerv2::UpdateTransientDigit(Int_t /*track*/, AliMUONTransientDigit * mTD)
+Bool_t AliMUONDigitizerv2::InitInputData(AliMUONLoader* muonloader)
 {
-  // Choosing the maping of the cathode plane of the chamber:
-  Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
-  AliMUONTransientDigit *pdigit = 
-    static_cast<AliMUONTransientDigit*>(fHitMap[iNchCpl]->GetHit(mTD->PadX(),mTD->PadY()));
-  // update charge
-  //
-  Int_t iqpad  = mTD->Signal();        // charge per pad
-  pdigit->AddSignal(iqpad);
-  pdigit->AddPhysicsSignal(iqpad);             
-  // update list of tracks
-  //
-  // List of tracks and trackcharge
-  Int_t itrack;
-  for(itrack=0;itrack<kMAXTRACKS;itrack++) {
-    pdigit->UpdateTrackList(mTD->Track(itrack), mTD->TrackCharge(itrack));
-  }
+/// Overridden to initialize fMUONData to read from the s-digits tree TreeS. 
+/// If the s-digits are not loaded then the muon loader is used to load the
+/// s-digits into memory.
+
+       AliDebug(3,"Loading s-digits in READ mode and setting the tree address.");
+       fMUONData->SetLoader(muonloader);
+
+       if (muonloader->TreeS() == NULL)
+       {
+               muonloader->LoadSDigits("READ");
+               if (muonloader->TreeS() == NULL)
+               {
+                       AliError("Can not load the s-digits tree.");
+                       return kFALSE;
+               }
+       }
+
+       fMUONData->SetTreeAddress("S");
+       return kTRUE;
 }
 
-//--------------------------------------------------------------------------
-void AliMUONDigitizerv2::MakeTransientDigitFromSDigit(Int_t iChamber, AliMUONDigit * sDigit)
+//------------------------------------------------------------------------
+void AliMUONDigitizerv2::CleanupInputData(AliMUONLoader* muonloader)
 {
-  Int_t digits[6];
-  
-
-  //
-  // Creating a new TransientDigits from SDigit
-  digits[0] = sDigit->PadX();  // Padx of the Digit
-  digits[1] = sDigit->PadY();  // Pady of the Digit
-  digits[2] = sDigit->Cathode()+1;  // Cathode plane
-  digits[3] = sDigit->Signal();  // Induced charge in the Pad
-  if (fSignal) digits[4] = sDigit->Signal();
-  else         digits[4] = 0;
-  digits[5] = sDigit->Hit();    // Hit number in the list
-  if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::MakeTransientDigitFromSDigit " <<
-                     "PadX "<< digits[0] << " " <<
-                     "PadY "<< digits[1] << " " <<
-                     "Plane " << digits[2] << " " <<
-                     "Charge " << digits[3] <<" " <<
-                     "Hit " << digits[5] << endl;   
-                
-  if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::MakeTransientDigitFromSDigit Creating AliMUONTransientDigit"<<endl;
-  AliMUONTransientDigit * mTD = new AliMUONTransientDigit(iChamber, digits);
-
-  // List of tracks and trackcharge
-  Int_t itrack;
-  for(itrack=0;itrack<kMAXTRACKS;itrack++) {
-    mTD->AddToTrackList(fMask+sDigit->Track(itrack), sDigit->TrackCharge(itrack));
-  }
+/// Overridden to release and unload s-digits from memory.
 
-  if (!ExistTransientDigit(mTD)) {
-    AddTransientDigit(mTD);
-    if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::MakeTransientDigitFromSDigit Adding TransientDigit"<<endl;
-  }
-  else {
-    if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::MakeTransientDigitFromSDigit updating TransientDigit"<<endl;
-    UpdateTransientDigit(0, mTD);
-    delete mTD;
-  }
+       AliDebug(3,"Releasing loaded s-digits.");
+       fMUONData->ResetSDigits();
+       muonloader->UnloadSDigits();
 }
 
-//-----------------------------------------------------------------------
-void AliMUONDigitizerv2::Exec(Option_t* option)
+//------------------------------------------------------------------------
+Bool_t AliMUONDigitizerv2::InitOutputData(AliMUONLoader* muonloader)
 {
-  TString optionString = option;
-  if (optionString.Data() == "deb") {
-    Info("Digitize","Called with option deb ");
-    fDebug = 3;
-  }
+/// Derived to initialize the output digits tree TreeD, create it if necessary
+/// and sets the fMUONData tree address to treeD.
+
+       AliDebug(3, "Creating digits branch and setting the tree address.");
+
+       fMUONData->SetLoader(muonloader);
+
+       // New branch per chamber for MUON digit in the tree of digits
+       if (muonloader->TreeD() == NULL)
+       {
+               muonloader->MakeDigitsContainer();
+               if (muonloader->TreeD() == NULL)
+               {
+                       AliError("Could not create TreeD.");
+                       return kFALSE;
+               }
+       }
 
-  AliMUONChamber*   chamber;
-  AliSegmentation*  c1Segmentation; //Cathode plane c1 of the chamber
-  AliSegmentation*  c2Segmentation; //Cathode place c2 of the chamber
-
-  if (GetDebug()>2) Info("Exec","AliMUONDigitizerv2::Exec() starts");
-  fTDList = new TObjArray;
-
-  //Loaders (We assume input0 to be the output too)
-  AliRunLoader * runloader;    // Input   loader
-  AliLoader    * gime; 
-  // Getting runloader
-  runloader    = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
-  if (runloader == 0x0) {
-    Error("Exec","RunLoader is not in input file 0");
-    return;  // RunDigitizer is not working.  
-  }
-  // Getting MUONloader
-  gime        = runloader->GetLoader("MUONLoader");
-  if (gime->TreeS()==0x0) {
-      if (GetDebug()>2) Info("Exec","TreeS is not loaded yet. Loading...");
-     gime->LoadSDigits("READ");
-       if (GetDebug()>2) Info("Exec","Now treeS is %#x. MUONLoader is %#x",gime->TreeH(),gime);
-  }
+       fMUONData->MakeBranch("D");
+       fMUONData->SetTreeAddress("D");
+       
+       return kTRUE;
+}
 
-  if (GetDebug()>2) Info("Exec","Loaders ready");
+//------------------------------------------------------------------------
+void AliMUONDigitizerv2::FillOutputData()
+{
+/// Derived to fill TreeD and resets the digit array in fMUONData.
 
-  if (runloader->GetAliRun() == 0x0) runloader->LoadgAlice();
-  gAlice = runloader->GetAliRun();
+       AliDebug(3, "Filling trees with digits.");
+       fMUONData->Fill("D");
+       fMUONData->ResetDigits();
+}
 
-  // Getting Module MUON  
-  AliMUON *pMUON  = (AliMUON *) gAlice->GetDetector("MUON");
-  if (!pMUON) {
-    Error("Digitize","Module MUON not found in the input file");
-    return;
-  }
-  // Getting Muon data
-  AliMUONData * muondata = pMUON->GetMUONData(); 
-  muondata->SetLoader(gime);
-  muondata->SetTreeAddress("S");
-
-  // Loading Event
-  Int_t currentevent = fManager->GetOutputEventNr();
-  
-  if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::Exec() Event Number is "<<currentevent <<endl;
-  if ( (currentevent<10)                                                 || 
-       (Int_t(TMath::Log10(currentevent)) == TMath::Log10(currentevent) ) )
-    cout <<"ALiMUONDigitizerv2::Exec() Event Number is "<< currentevent <<endl;
-
-  // Output file for digits same as hits
-  //  AliRunLoader *  runloaderout  = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
-  //AliLoader * gimeout         = runloaderout->GetLoader("MUONLoader"); 
-  // New branch per chamber for MUON digit in the tree of digits
-  if (gime->TreeD() == 0x0) {
-    gime->MakeDigitsContainer();
-  }
-  TTree* treeD = gime->TreeD();
-  muondata->MakeBranch("D");
-  muondata->SetTreeAddress("D");
-
-  // Array of pointer of the AliMUONHitMapA1:
-  //  two HitMaps per chamber, or one HitMap per cahtode plane
-  fHitMap= new AliMUONHitMapA1* [2*AliMUONConstants::NCh()];
-
-  //Loop over chambers for the definition AliMUONHitMap
-  for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
-    chamber = &(pMUON->Chamber(i));
-    c1Segmentation = chamber->SegmentationModel(1); // Cathode plane 1
-    fHitMap[i] = new AliMUONHitMapA1(c1Segmentation, fTDList);
-    c2Segmentation = chamber->SegmentationModel(2); // Cathode plane 2
-    fHitMap[i+AliMUONConstants::NCh()] = new AliMUONHitMapA1(c2Segmentation, fTDList);
-  }
+//------------------------------------------------------------------------
+void AliMUONDigitizerv2::CleanupOutputData(AliMUONLoader* muonloader)
+{
+/// Derived to write the digits tree and then unload the digits tree once written.
 
-// Loop over files to merge and to digitize
-    fSignal = kTRUE;
-    for (Int_t inputFile=0; inputFile<fManager->GetNinputs(); inputFile++) {
-      if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::Exec() Input File is "<<inputFile<<endl;
-
-
-      // Connect MUON Hit branch
-      if (inputFile > 0 ) {
-       fSignal = kFALSE;
-         runloader    = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
-         if (runloader == 0x0) {
-           cerr<<"AliMUONDigitizerv2::Digitize() RunLoader for inputFile "<<inputFile<< " not found !!! "<<endl;
-         }
-         gime  = runloader->GetLoader("MUONLoader");
-         if (gime->TreeS() == 0x0) gime->LoadSDigits("READ");  
-        muondata->SetLoader(gime);
-        muondata->SetTreeAddress("S");
-      }
-
-      // Setting the address 
-      TTree *treeS = gime->TreeS();
-      if (treeS == 0x0) {
-       Error("Digitize","Can not get TreeS from input %d",inputFile);
-       Info("Digitize","Now treeS is %#x. MUONLoader is %#x",gime->TreeS(),gime);
-       return;
-      }
-      if (GetDebug()>2) {
-       cerr<<"AliMUONDigitizerv2::Exec inputFile is "<<inputFile<<" "<<endl;
-       cerr<<"AliMUONDigitizerv2::Exec treeS" << treeS <<endl;
-      }
-       
-      if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::Exec Setting tree addresses"<<endl;
-
-      fMask = fManager->GetMask(inputFile);
-      //
-      // Loop over SDigits
-      Int_t ndig,k;
-      AliMUONDigit *sDigit;
-      TClonesArray * muonSDigits;
-      for (Int_t ich = 0; ich < AliMUONConstants::NCh(); ich++) {  //  loop over chamber
-       muondata->ResetSDigits();
-       muondata->GetCathodeS(0);
-       //TClonesArray *
-       muonSDigits = muondata->SDigits(ich); 
-       ndig = muonSDigits->GetEntriesFast();
-//     printf("\n 1 Found %d Sdigits in %p chamber %d", ndig, muonSDigits,ich);
-       Int_t n = 0;
-       for (k = 0; k < ndig; k++) {
-           sDigit = (AliMUONDigit*) muonSDigits->UncheckedAt(k);
-           MakeTransientDigitFromSDigit(ich,sDigit);
-       }
-       muondata->ResetSDigits();
-       muondata->GetCathodeS(1);
-       //TClonesArray *
-       muonSDigits = muondata->SDigits(ich); 
-       ndig=muonSDigits->GetEntriesFast();
-//     printf("\n 2 Found %d Sdigits in %p chamber %d", ndig, muonSDigits,ich);
-       n = 0;
-       for (k = 0; k < ndig; k++) {
-           sDigit = (AliMUONDigit*) muonSDigits->UncheckedAt(k);
-           MakeTransientDigitFromSDigit(ich,sDigit);
-       }
-       
-      } // SDigits loop end loop over chamber
-    } // end file loop
-    if (GetDebug()>2) cerr<<"AliMUONDigitizerv2::Exec End of Sdigits, file loops"<<endl;
-
-    // Loop on cathodes
-    Int_t icat;
-    for(icat=0; icat<2; icat++) {
-      //
-      // Filling Digit List
-      Int_t tracks[kMAXTRACKS];
-      Int_t charges[kMAXTRACKS];
-      Int_t nentries = fTDList->GetEntriesFast();
-      Int_t digits[6];
-      for (Int_t nent = 0; nent < nentries; nent++) {
-       AliMUONTransientDigit *address = (AliMUONTransientDigit*)fTDList->At(nent);
-       if (address == 0) continue; 
-       Int_t ich = address->Chamber();
-       Int_t   q = address->Signal(); 
-       chamber = &(pMUON->Chamber(ich));
-       //
-       //  Digit Response (noise, threshold, saturation, ...)
-       AliMUONResponse * response = chamber->ResponseModel();
-       q = response->DigitResponse(q,address);
-       
-       if (!q) continue;
-       
-       digits[0] = address->PadX();
-       digits[1] = address->PadY();
-       digits[2] = address->Cathode()-1;
-       digits[3] = q;
-       digits[4] = address->Physics();
-       digits[5] = address->Hit();
-       
-       Int_t nptracks = address->GetNTracks();
-       
-       if (nptracks > kMAXTRACKS) {
-         if (GetDebug() >0) {
-           cerr<<"AliMUONDigitizer:Exec  nptracks > 10 "<<nptracks;
-           cerr<<"reset to max value "<<kMAXTRACKS<<endl;
-         }
-         nptracks = kMAXTRACKS;
-       }
-       if (nptracks > 2 && GetDebug() >2) {
-         cerr<<"AliMUONDigitizer::Exec  nptracks > 2 "<<nptracks<<endl;
-         //    printf("cat,ich,ix,iy,q %d %d %d %d %d \n",icat,ich,digits[0],digits[1],q);
-       }
-       for (Int_t tr = 0; tr < nptracks; tr++) {
-         tracks[tr]   = address->GetTrack(tr);
-         charges[tr]  = address->GetCharge(tr);
-       }      //end loop over list of tracks for one pad
-       // Sort list of tracks according to charge
-       if (nptracks > 1) {
-         SortTracks(tracks,charges,nptracks);
-       }
-       if (nptracks < kMAXTRACKS ) {
-         for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
-           tracks[i]  = -1;
-           charges[i] = 0;
-         }
-       }
-       
-       // Add digits
-       if (GetDebug()>3) cerr<<"AliMUONDigitzerv2::Exec TransientDigit to Digit"<<endl;
-       if ( digits[2] == icat ) muondata->AddDigit(ich,tracks,charges,digits);
-//     printf("test rm ich %d padX %d padY %d \n",ich, digits[0], digits[1]);
-      }
-      // Filling list of digits per chamber for a given cathode.
-      runloader    = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
-      gime        = runloader->GetLoader("MUONLoader");
-      muondata->SetLoader(gime);
-      muondata->Fill("D");
-      muondata->ResetDigits();    
-    } // end loop cathode
-    fTDList->Delete();  
-    
-    for(Int_t ii = 0; ii < 2*AliMUONConstants::NCh(); ++ii) {
-      if (fHitMap[ii]) {
-       delete fHitMap[ii];
-       fHitMap[ii] = 0;
-      }
-    }
-    
-    if (GetDebug()>2) 
-      cerr<<"AliMUONDigitizer::Exec: writing the TreeD: "
-         <<treeD->GetName()<<endl;
-
-    runloader    = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
-    gime        = runloader->GetLoader("MUONLoader");
-    gime->WriteDigits("OVERWRITE");
-    delete [] fHitMap;
-    delete fTDList;
-    muondata->ResetSDigits();
-    gime->UnloadSDigits();
-    gime->UnloadDigits();
+       AliDebug(3, "Writing digits and releasing pointers.");
+       muonloader->WriteDigits("OVERWRITE");
+       muonloader->UnloadDigits();
 }
+
+//-----------------------------------------------------------------------
+
+void AliMUONDigitizerv2::CleanupTriggerArrays()
+{
+/// Cleanup trigger arrays
+
+  fTrigDec->ClearDigits();
+}
+
 //------------------------------------------------------------------------
-void AliMUONDigitizerv2::SortTracks(Int_t *tracks,Int_t *charges,Int_t ntr)
+void AliMUONDigitizerv2::AddDigitTrigger(
+               Int_t chamber, Int_t tracks[kMAXTRACKS],
+               Int_t charges[kMAXTRACKS], Int_t digits[7],
+               Int_t digitindex
+       )
 {
-  //
-  // Sort the list of tracks contributing to a given digit
-  // Only the 3 most significant tracks are acctually sorted
-  //
-  
-  //
-  //  Loop over signals, only 3 times
-  //
-  
-  Int_t qmax;
-  Int_t jmax;
-  Int_t idx[3] = {-2,-2,-2};
-  Int_t jch[3] = {-2,-2,-2};
-  Int_t jtr[3] = {-2,-2,-2};
-  Int_t i,j,imax;
-  
-  if (ntr<3) imax=ntr;
-  else imax=3;
-  for(i=0;i<imax;i++){
-    qmax=0;
-    jmax=0;
-    
-    for(j=0;j<ntr;j++){
-      
-      if((i == 1 && j == idx[i-1]) 
-        ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
-      
-      if(charges[j] > qmax) {
-       qmax = charges[j];
-       jmax=j;
-      }       
-    } 
-    
-    if(qmax > 0) {
-      idx[i]=jmax;
-      jch[i]=charges[jmax]; 
-      jtr[i]=tracks[jmax];
-    }
-    
-  } 
-  
-  for(i=0;i<3;i++){
-    if (jtr[i] == -2) {
-      charges[i]=0;
-      tracks[i]=0;
-    } else {
-      charges[i]=jch[i];
-      tracks[i]=jtr[i];
-    }
-  }
+/// Derived to add digits to TreeD for trigger.
+  fTrigDec->AddDigit(chamber, tracks, charges, digits, digitindex); 
+}
+
+//------------------------------------------------------------------------
+void AliMUONDigitizerv2::FillTriggerOutput()
+{
+/// Derived to fill TreeD and resets the trigger array in fMUONData.
+
+       AliDebug(3,"Filling trees with trigger.");
+       fMUONData->Fill("GLT");
+       fMUONData->ResetTrigger();
+}
+
+//------------------------------------------------------------------------
+void AliMUONDigitizerv2::CreateTrigger()
+{
+/// Create trigger data
+
+  fMUONData->MakeBranch("GLT");
+  fMUONData->SetTreeAddress("GLT");
+  fTrigDec->Digits2Trigger(); 
+  FillTriggerOutput(); 
+
 }