]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDdigitizer.cxx
Message commented out
[u/mrichter/AliRoot.git] / TRD / AliTRDdigitizer.cxx
index f414f9462b263dac57f4ebb30ba28b5d20c35741..25fcb3889d503fb25ef486212c2547fa313c8c26 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.33  2002/02/12 17:32:03  cblume
-Rearrange the deleting of the list of sdigitsmanager
-
-Revision 1.32  2002/02/12 16:07:21  cblume
-Add new constructor
-
-Revision 1.31  2002/02/11 14:27:11  cblume
-New pad plane design, new TRF+PRF, tail cancelation, cross talk
-
-Revision 1.30  2001/11/19 08:44:08  cblume
-Fix bugs reported by Rene
-
-Revision 1.29  2001/11/14 19:44:25  hristov
-Numeric const casted (Alpha)
-
-Revision 1.28  2001/11/14 16:35:58  cblume
-Inherits now from AliDetector
-
-Revision 1.27  2001/11/14 10:50:45  cblume
-Changes in digits IO. Add merging of summable digits
-
-Revision 1.26  2001/11/06 17:19:41  cblume
-Add detailed geometry and simple simulator
-
-Revision 1.25  2001/06/27 09:54:44  cblume
-Moved fField initialization to InitDetector()
-
-Revision 1.24  2001/05/21 16:45:47  hristov
-Last minute changes (C.Blume)
-
-Revision 1.23  2001/05/07 08:04:48  cblume
-New TRF and PRF. Speedup of the code. Digits from amplification region included
-
-Revision 1.22  2001/03/30 14:40:14  cblume
-Update of the digitization parameter
-
-Revision 1.21  2001/03/13 09:30:35  cblume
-Update of digitization. Moved digit branch definition to AliTRD
-
-Revision 1.20  2001/02/25 20:19:00  hristov
-Minor correction: loop variable declared only once for HP, Sun
-
-Revision 1.19  2001/02/14 18:22:26  cblume
-Change in the geometry of the padplane
-
-Revision 1.18  2001/01/26 19:56:57  hristov
-Major upgrade of AliRoot code
-
-Revision 1.17  2000/12/08 12:53:27  cblume
-Change in Copy() function for HP-compiler
-
-Revision 1.16  2000/12/07 12:20:46  cblume
-Go back to array compression. Use sampled PRF to speed up digitization
-
-Revision 1.15  2000/11/23 14:34:08  cblume
-Fixed bug in expansion routine of arrays (initialize buffers properly)
-
-Revision 1.14  2000/11/20 08:54:44  cblume
-Switch off compression as default
-
-Revision 1.13  2000/11/10 14:57:52  cblume
-Changes in the geometry constants for the DEC compiler
-
-Revision 1.12  2000/11/01 14:53:20  cblume
-Merge with TRD-develop
-
-Revision 1.1.4.9  2000/10/26 17:00:22  cblume
-Fixed bug in CheckDetector()
-
-Revision 1.1.4.8  2000/10/23 13:41:35  cblume
-Added protection against Log(0) in the gas gain calulation
-
-Revision 1.1.4.7  2000/10/17 02:27:34  cblume
-Get rid of global constants
-
-Revision 1.1.4.6  2000/10/16 01:16:53  cblume
-Changed timebin 0 to be the one closest to the readout
-
-Revision 1.1.4.5  2000/10/15 23:34:29  cblume
-Faster version of the digitizer
-
-Revision 1.1.4.4  2000/10/06 16:49:46  cblume
-Made Getters const
-
-Revision 1.1.4.3  2000/10/04 16:34:58  cblume
-Replace include files by forward declarations
-
-Revision 1.1.4.2  2000/09/22 14:41:10  cblume
-Bug fix in PRF. Included time response. New structure
-
-Revision 1.10  2000/10/05 07:27:53  cblume
-Changes in the header-files by FCA
-
-Revision 1.9  2000/10/02 21:28:19  fca
-Removal of useless dependecies via forward declarations
-
-Revision 1.8  2000/06/09 11:10:07  cblume
-Compiler warnings and coding conventions, next round
-
-Revision 1.7  2000/06/08 18:32:58  cblume
-Make code compliant to coding conventions
-
-Revision 1.6  2000/06/07 16:27:32  cblume
-Try to remove compiler warnings on Sun and HP
-
-Revision 1.5  2000/05/09 16:38:57  cblume
-Removed PadResponse(). Merge problem
-
-Revision 1.4  2000/05/08 15:53:45  cblume
-Resolved merge conflict
-
-Revision 1.3  2000/04/28 14:49:27  cblume
-Only one declaration of iDict in MakeDigits()
-
-Revision 1.1.4.1  2000/05/08 14:42:04  cblume
-Introduced AliTRDdigitsManager
-
-Revision 1.1  2000/02/28 19:00:13  cblume
-Add new TRD classes
-
-*/
+/* $Id$ */
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
@@ -176,8 +54,13 @@ Add new TRD classes
 #include <TTask.h>
 
 #include "AliRun.h"
+#include "AliRunLoader.h"
+#include "AliLoader.h"
+#include "AliConfig.h"
 #include "AliMagF.h"
 #include "AliRunDigitizer.h"
+#include "AliRunLoader.h"
+#include "AliLoader.h"
 
 #include "AliTRD.h"
 #include "AliTRDhit.h"
@@ -198,21 +81,23 @@ AliTRDdigitizer::AliTRDdigitizer()
   // AliTRDdigitizer default constructor
   //
 
-  fInputFile          = 0;
+  fRunLoader          = 0;
   fDigitsManager      = 0;
-  fSDigitsManagerList = 0;
   fSDigitsManager     = 0;
+  fSDigitsManagerList = 0;
   fTRD                = 0;
   fGeo                = 0;
   fPar                = 0;
-  fMasks              = 0;
   fEvent              = 0;
+  fMasks              = 0;
   fCompress           = kTRUE;
   fDebug              = 0;
   fSDigits            = kFALSE;
   fSDigitsScale       = 0.0;
   fMergeSignalOnly    = kFALSE;
-
+  fSimpleSim          = kFALSE;
+  fSimpleDet          = 0;
 }
 
 //_____________________________________________________________________________
@@ -223,22 +108,26 @@ AliTRDdigitizer::AliTRDdigitizer(const Text_t *name, const Text_t *title)
   // AliTRDdigitizer constructor
   //
 
-  fInputFile          = 0;
+  fRunLoader          = 0;
+
+  //NewIO: These data members probably are not needed anymore
   fDigitsManager      = 0;
-  fSDigitsManagerList = 0;
   fSDigitsManager     = 0;
+  fSDigitsManagerList = 0;
   fTRD                = 0;
   fGeo                = 0;
   fPar                = 0;
-  fMasks              = 0;
+  //End NewIO comment
   fEvent              = 0;
+  fMasks              = 0;
   fCompress           = kTRUE;
   fDebug              = 0;
   fSDigits            = kFALSE;
+  fSDigitsScale       = 100.; // For the summable digits
   fMergeSignalOnly    = kFALSE;
-
-  // For the summable digits
-  fSDigitsScale       = 100.;
+  fSimpleSim          = kFALSE;
+  fSimpleDet          = 0;
 
 }
 
@@ -251,22 +140,23 @@ AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager
   // AliTRDdigitizer constructor
   //
 
-  fInputFile          = 0;
+  fRunLoader          = 0;
   fDigitsManager      = 0;
-  fSDigitsManagerList = 0;
   fSDigitsManager     = 0;
+  fSDigitsManagerList = 0;
   fTRD                = 0;
   fGeo                = 0;
   fPar                = 0;
-  fMasks              = 0;
   fEvent              = 0;
+  fMasks              = 0;
   fCompress           = kTRUE;
   fDebug              = 0;
   fSDigits            = kFALSE;
+  fSDigitsScale       = 100.; // For the summable digits
   fMergeSignalOnly    = kFALSE;
-
-  // For the summable digits
-  fSDigitsScale       = 100.;
+  fSimpleSim          = kFALSE;
+  fSimpleDet          = 0;
 
 }
 
@@ -278,27 +168,29 @@ AliTRDdigitizer::AliTRDdigitizer(AliRunDigitizer *manager)
   // AliTRDdigitizer constructor
   //
 
-  fInputFile          = 0;
+
+  fRunLoader          = 0;
   fDigitsManager      = 0;
-  fSDigitsManagerList = 0;
   fSDigitsManager     = 0;
+  fSDigitsManagerList = 0;
   fTRD                = 0;
   fGeo                = 0;
   fPar                = 0;
-  fMasks              = 0;
   fEvent              = 0;
+  fMasks              = 0;
   fCompress           = kTRUE;
   fDebug              = 0;
   fSDigits            = kFALSE;
+  fSDigitsScale       = 100.;  // For the summable digits
   fMergeSignalOnly    = kFALSE;
+  fSimpleSim          = kFALSE;
+  fSimpleDet          = 0;
 
-  // For the summable digits
-  fSDigitsScale       = 100.;
 
 }
 
 //_____________________________________________________________________________
-AliTRDdigitizer::AliTRDdigitizer(const AliTRDdigitizer &d)
+AliTRDdigitizer::AliTRDdigitizer(const AliTRDdigitizer &d):AliDigitizer(d)
 {
   //
   // AliTRDdigitizer copy constructor
@@ -315,23 +207,16 @@ AliTRDdigitizer::~AliTRDdigitizer()
   // AliTRDdigitizer destructor
   //
 
-  if (fInputFile) {
-    fInputFile->Close();
-    delete fInputFile;
-    fInputFile = 0;
-  }
 
   if (fDigitsManager) {
     delete fDigitsManager;
     fDigitsManager = 0;
   }
 
-  if (fSDigitsManager) {
-    delete fSDigitsManager;
-    fSDigitsManager = 0;
-  }
+  fSDigitsManager = 0;
 
   if (fSDigitsManagerList) {
+    fSDigitsManagerList->Delete();
     delete fSDigitsManagerList;
     fSDigitsManagerList = 0;
   }
@@ -362,21 +247,23 @@ void AliTRDdigitizer::Copy(TObject &d)
   // Copy function
   //
 
-  ((AliTRDdigitizer &) d).fInputFile          = 0;
-  ((AliTRDdigitizer &) d).fSDigitsManagerList = 0;
-  ((AliTRDdigitizer &) d).fSDigitsManager     = 0;
+  ((AliTRDdigitizer &) d).fRunLoader          = 0;
   ((AliTRDdigitizer &) d).fDigitsManager      = 0;
+  ((AliTRDdigitizer &) d).fSDigitsManager     = 0;
+  ((AliTRDdigitizer &) d).fSDigitsManagerList = 0;
   ((AliTRDdigitizer &) d).fTRD                = 0;
   ((AliTRDdigitizer &) d).fGeo                = 0;
-  ((AliTRDdigitizer &) d).fMasks              = 0;
-  ((AliTRDdigitizer &) d).fEvent              = 0;
   ((AliTRDdigitizer &) d).fPar                = 0;
+  ((AliTRDdigitizer &) d).fEvent              = 0;
+  ((AliTRDdigitizer &) d).fMasks              = 0;
   ((AliTRDdigitizer &) d).fCompress           = fCompress;
   ((AliTRDdigitizer &) d).fDebug              = fDebug  ;
   ((AliTRDdigitizer &) d).fSDigits            = fSDigits;
   ((AliTRDdigitizer &) d).fSDigitsScale       = fSDigitsScale;
   ((AliTRDdigitizer &) d).fMergeSignalOnly    = fMergeSignalOnly;
-                                      
+  ((AliTRDdigitizer &) d).fSimpleSim          = fSimpleSim;
+  ((AliTRDdigitizer &) d).fSimpleDet          = fSimpleDet;
+                                       
 }
 
 //_____________________________________________________________________________
@@ -400,31 +287,28 @@ void AliTRDdigitizer::Exec(Option_t* option)
     printf("Called with debug option %d\n",fDebug);
   }
 
-  // Connect the AliRoot file containing Geometry, Kine, and Hits
-  fInputFile = (TFile *) fManager->GetInputTreeTRDS(0)->GetCurrentFile();
-  if (!fInputFile) {
-    if (fDebug > 0) {
-      printf("<AliTRDdigitizer::Exec> ");
-      printf("Cannot open the input file %s.\n",fInputFile->GetName());
-    }
-  }
-
-  if (gAlice) {
-    delete gAlice;
-    gAlice = 0;
-  }
-  gAlice = (AliRun *) fInputFile->Get("gAlice");
-  if (gAlice) {
+  // The AliRoot file is already connected by the manager
+  AliRunLoader* inrl;
+  
+  if (gAlice) 
+   {
     if (fDebug > 0) {
       printf("<AliTRDdigitizer::Exec> ");
       printf("AliRun object found on file.\n");
     }
-  }
+   }
   else {
-    printf("<AliTRDdigitizer::Exec> ");
-    printf("Could not find AliRun object.\n");
+    inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
+    inrl->LoadgAlice();
+    gAlice = inrl->GetAliRun();
+    if (!gAlice)
+     {
+       printf("<AliTRDdigitizer::Exec> ");
+       printf("Could not find AliRun object.\n");
+       return;
+     }
   }
-
+                                                                           
   Int_t nInput = fManager->GetNinputs();
   fMasks = new Int_t[nInput];
   for (iInput = 0; iInput < nInput; iInput++) {
@@ -432,8 +316,34 @@ void AliTRDdigitizer::Exec(Option_t* option)
   }
 
   // Initialization
-  InitDetector();
 
+  AliRunLoader* orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
+  if (InitDetector()) {
+    AliLoader* ogime = orl->GetLoader("TRDLoader");
+
+    TTree* tree = 0;
+    if (fSDigits)
+      { 
+       //if we produce SDigits
+       tree = ogime->TreeS();
+       if (!tree)
+         {
+           ogime->MakeTree("S");
+           tree = ogime->TreeS();
+         }
+      }
+    else
+      {//if we produce Digits
+       tree = ogime->TreeD();
+       if (!tree)
+         {
+           ogime->MakeTree("D");
+           tree = ogime->TreeD();
+         }
+      }
+    MakeBranch(tree);
+  }
   for (iInput = 0; iInput < nInput; iInput++) {
 
     if (fDebug > 0) {
@@ -441,11 +351,36 @@ void AliTRDdigitizer::Exec(Option_t* option)
       printf("Add input stream %d\n",iInput);
     }
 
+    // check if the input tree exists
+    inrl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
+    AliLoader* gime = inrl->GetLoader("TRDLoader");
+
+    TTree * treees =  gime->TreeS();
+    if (treees == 0x0) 
+     {
+      if (gime->LoadSDigits())
+       {
+         Error("Exec","Error Occured while loading S. Digits for input %d.",iInput);
+         return;
+       }
+      treees =  gime->TreeS();
+     }
+    
+    if (treees == 0x0) {
+      printf("<AliTRDdigitizer::Exec> ");
+      printf("Input stream %d does not exist\n",iInput);
+      return;
+    } 
+
     // Read the s-digits via digits manager
     sdigitsManager = new AliTRDdigitsManager();
     sdigitsManager->SetDebug(fDebug);
     sdigitsManager->SetSDigits(kTRUE);
-    sdigitsManager->ReadDigits(fManager->GetInputTreeTRDS(iInput));
+    
+    AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
+    AliLoader* gimme = rl->GetLoader("TRDLoader");
+    if (!gimme->TreeS()) gimme->LoadSDigits();
+    sdigitsManager->ReadDigits(gimme->TreeS());
 
     // Add the s-digits to the input list 
     AddSDigitsManager(sdigitsManager);
@@ -464,8 +399,13 @@ void AliTRDdigitizer::Exec(Option_t* option)
     printf("<AliTRDdigitizer::Exec> ");
     printf("Write the digits\n");
   }
-  fDigitsManager->MakeBranch(fManager->GetTreeDTRD());
+  
   fDigitsManager->WriteDigits();
+
+  //Write parameters
+  orl->CdGAFile();
+  if (!gFile->Get("TRDparameter")) GetParameter()->Write();
+
   if (fDebug > 0) {
     printf("<AliTRDdigitizer::Exec> ");
     printf("Done\n");
@@ -483,22 +423,23 @@ Bool_t AliTRDdigitizer::Open(const Char_t *file, Int_t nEvent)
   //
 
   // Connect the AliRoot file containing Geometry, Kine, and Hits
-  fInputFile = (TFile *) gROOT->GetListOfFiles()->FindObject(file);
-  if (!fInputFile) {
-    if (fDebug > 0) {
-      printf("<AliTRDdigitizer::Open> ");
-      printf("Open the AliROOT-file %s.\n",file);
-    }
-    fInputFile = new TFile(file,"UPDATE");
-  }
-  else {
-    if (fDebug > 0) {
-      printf("<AliTRDdigitizer::Open> ");
-      printf("%s is already open.\n",file);
-    }
-  }
+  
 
-  gAlice = (AliRun *) fInputFile->Get("gAlice");
+  TString evfoldname = AliConfig::GetDefaultEventFolderName();
+  fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
+  if (!fRunLoader)
+    fRunLoader = AliRunLoader::Open(file,AliConfig::GetDefaultEventFolderName(),
+                                   "UPDATE");
+  
+  if (!fRunLoader)
+   {
+     Error("Open","Can not open session for file %s.",file);
+     return kFALSE;
+   }
+   
+  if (!fRunLoader->GetAliRun()) fRunLoader->LoadgAlice();
+  gAlice = fRunLoader->GetAliRun();
+  
   if (gAlice) {
     if (fDebug > 0) {
       printf("<AliTRDdigitizer::Open> ");
@@ -514,15 +455,36 @@ Bool_t AliTRDdigitizer::Open(const Char_t *file, Int_t nEvent)
   fEvent = nEvent;
 
   // Import the Trees for the event nEvent in the file
-  Int_t nparticles = gAlice->GetEvent(fEvent);
-  if (nparticles <= 0) {
-    printf("<AliTRDdigitizer::Open> ");
-    printf("No entries in the trees for event %d.\n",fEvent);
-    return kFALSE;
-  }
-
+  fRunLoader->GetEvent(fEvent);
+  
+  AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
+  if (!loader)
+   {
+     Error("Open","Can not get TRD loader from Run Loader");
+     return kFALSE;
+   }
+  
   if (InitDetector()) {
-    return MakeBranch();
+    TTree* tree = 0;
+    if (fSDigits)
+     { 
+     //if we produce SDigits
+       tree = loader->TreeS();
+       if (!tree)
+        {
+         loader->MakeTree("S");
+         tree = loader->TreeS();
+        }
+     }
+    else
+     {//if we produce Digits
+       if (!tree)
+        {
+         loader->MakeTree("D");
+         tree = loader->TreeD();
+        }
+     }
+    return MakeBranch(tree);
   }
   else {
     return kFALSE;
@@ -539,6 +501,11 @@ Bool_t AliTRDdigitizer::InitDetector()
 
   // Get the pointer to the detector class and check for version 1
   fTRD = (AliTRD *) gAlice->GetDetector("TRD");
+  if (!fTRD) {
+    printf("<AliTRDdigitizer::InitDetector> ");
+    printf("No TRD module found\n");
+    exit(1);
+  }
   if (fTRD->IsVersion() != 1) {
     printf("<AliTRDdigitizer::InitDetector> ");
     printf("TRD must be version 1 (slow simulator).\n");
@@ -553,6 +520,7 @@ Bool_t AliTRDdigitizer::InitDetector()
   }
 
   // Create a digits manager
+  delete fDigitsManager;
   fDigitsManager = new AliTRDdigitsManager();
   fDigitsManager->SetSDigits(fSDigits);
   fDigitsManager->CreateArrays();
@@ -560,20 +528,24 @@ Bool_t AliTRDdigitizer::InitDetector()
   fDigitsManager->SetDebug(fDebug);
 
   // The list for the input s-digits manager to be merged
-  fSDigitsManagerList = new TList();
+  if (fSDigitsManagerList) {
+    fSDigitsManagerList->Delete();
+  } else {
+    fSDigitsManagerList = new TList();
+  }
 
   return kTRUE;
 
 }
 
 //_____________________________________________________________________________
-Bool_t AliTRDdigitizer::MakeBranch(const Char_t *file)
+Bool_t AliTRDdigitizer::MakeBranch(TTree* tree) const
 {
   // 
   // Create the branches for the digits array
   //
 
-  return fDigitsManager->MakeBranch(file);
+  return fDigitsManager->MakeBranch(tree);
 
 }
 
@@ -660,25 +632,52 @@ Bool_t AliTRDdigitizer::MakeDigits()
     printf("Start creating digits.\n");
   }
 
-  // Get the pointer to the hit tree
-  TTree *HitTree = gAlice->TreeH();
-
+  AliLoader* gimme = fRunLoader->GetLoader("TRDLoader");
+  if (!gimme->TreeH()) gimme->LoadHits();
+  TTree* hitTree = gimme->TreeH();
+  if (hitTree == 0x0)
+    {
+      Error("MakeDigits","Can not get TreeH");
+      return kFALSE;
+    }
+  fTRD->SetTreeAddress();
+  
   // Get the number of entries in the hit tree
   // (Number of primary particles creating a hit somewhere)
-  Int_t nTrack = (Int_t) HitTree->GetEntries();
-  if (fDebug > 0) {
-    printf("<AliTRDdigitizer::MakeDigits> ");
-    printf("Found %d primary particles\n",nTrack);
-  } 
+  Int_t nTrack = 1;
+  if (!fSimpleSim) {
+    nTrack = (Int_t) hitTree->GetEntries();
+    if (fDebug > 0) {
+      printf("<AliTRDdigitizer::MakeDigits> ");
+      printf("Found %d primary particles\n",nTrack);
+    } 
+  }
 
   Int_t detectorOld = -1;
   Int_t countHits   =  0; 
 
+  if (fDebug > 0) {
+    printf("<AliTRDdigitizer::MakeDigits> ");
+    printf("Driftvelocity = %.2f, Sampling = %.2fns\n",fPar->GetDriftVelocity(),
+          fPar->GetTimeBinSize() / fPar->GetDriftVelocity() * 1000.);
+    printf("<AliTRDdigitizer::MakeDigits> ");
+    printf("Gain = %d, Noise = %d\n",(Int_t)fPar->GetGasGain(),(Int_t)fPar->GetNoise());
+    if (fPar->TimeStructOn()) {
+      printf("<AliTRDdigitizer::MakeDigits> ");
+      printf("Time Structure of drift cells implemented.\n");
+    } else {
+      printf("<AliTRDdigitizer::MakeDigits> ");
+      printf("Constant drift velocity in drift cells.\n");      
+    }
+  }
+
   // Loop through all entries in the tree
   for (Int_t iTrack = 0; iTrack < nTrack; iTrack++) {
 
-    gAlice->ResetHits();
-    nBytes += HitTree->GetEvent(iTrack);
+    if (!fSimpleSim) {   
+      gAlice->ResetHits();
+      nBytes += hitTree->GetEvent(iTrack);
+    }
 
     // Loop through the TRD hits
     Int_t iHit = 0;
@@ -762,6 +761,14 @@ Bool_t AliTRDdigitizer::MakeDigits()
            }
             signals->Allocate(nRowMax,nColMax,nTimeTotal);
          }
+          else if (fSimpleSim) {
+            // Clear an old one for the simple simulation
+            if (fDebug > 1) {
+              printf("<AliTRDdigitizer::MakeDigits> ");
+              printf("Clear a old container ... ");
+            }
+            signals->Clear();
+          }
           else {
            // Expand an existing one
             if (fCompress) {
@@ -773,13 +780,15 @@ Bool_t AliTRDdigitizer::MakeDigits()
            }
          }
          // The same for the dictionary
-          for (iDict = 0; iDict < kNDict; iDict++) {       
-            dictionary[iDict] = fDigitsManager->GetDictionary(detector,iDict);
-            if (dictionary[iDict]->GetNtime() == 0) {
-              dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
-           }
-            else {
-              if (fCompress) dictionary[iDict]->Expand();
+          if (!fSimpleSim) {       
+            for (iDict = 0; iDict < kNDict; iDict++) {       
+              dictionary[iDict] = fDigitsManager->GetDictionary(detector,iDict);
+              if (dictionary[iDict]->GetNtime() == 0) {
+                dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
+             }
+              else {
+                if (fCompress) dictionary[iDict]->Expand();
+             }
            }
           }      
           if (fDebug > 1) printf("done\n");
@@ -787,16 +796,20 @@ Bool_t AliTRDdigitizer::MakeDigits()
         }
 
         // Rotate the sectors on top of each other
-        fGeo->Rotate(detector,pos,rot);
+        if (fSimpleSim) {
+          rot[0] = pos[0];
+          rot[1] = pos[1];
+          rot[2] = pos[2];
+        }
+        else {
+          fGeo->Rotate(detector,pos,rot);
+       }
 
         // The driftlength. It is negative if the hit is in the 
         // amplification region.
         Float_t driftlength = time0 - rot[0];
 
         // Take also the drift in the amplification region into account
-        // The drift length is at the moment still the same, regardless of
-        // the position relativ to the wire. This non-isochronity needs still
-        // to be implemented.
         Float_t driftlengthL = TMath::Abs(driftlength + kAmWidth);
         if (fPar->ExBOn()) driftlengthL /= TMath::Sqrt(fPar->GetLorentzFactor());
 
@@ -809,6 +822,26 @@ Bool_t AliTRDdigitizer::MakeDigits()
           xyz[1] = rot[1];
           xyz[2] = rot[2];
 
+         // Stupid patch to take care of TR photons that are absorbed
+         // outside the chamber volume. A real fix would actually need
+         // a more clever implementation of the TR hit generation
+          Float_t zz = xyz[2] - row0;
+          if ((zz < 0.0) || (zz > rowPadSize*nRowMax)) {
+            if (iEl == 0) {
+              printf("<AliTRDdigitizer::MakeDigits> ");
+              printf("Hit outside of sensitive volume, row (Q = %d)\n",((Int_t) q));
+           }
+            continue;
+         }
+          Int_t tt = ((Int_t) (10*(driftlength + 2.0*kAmWidth)));
+          if (tt < 0) {
+            if (iEl == 0) {
+              printf("<AliTRDdigitizer::MakeDigits> ");
+              printf("Hit outside of sensitive volume, time (Q = %d)\n",((Int_t) q));
+           }
+            continue;
+         }
+
           // Electron attachment
           if (fPar->ElAttachOn()) {
             if (gRandom->Rndm() < (driftlengthL * elAttachProp)) 
@@ -825,6 +858,17 @@ Bool_t AliTRDdigitizer::MakeDigits()
             if (!(fPar->ExB(driftlength+kAmWidth,xyz))) continue;   
          }
 
+         // Apply the drift time correction
+         if (fPar->TimeStructOn()) {
+           // Get z-position with respect to anode wire:
+           Float_t Z  =  xyz[2] - row0 + fPar->GetAnodeWireOffset();
+           Z -= ((Int_t)(2*Z))/2.;
+           if (Z>0.25)   Z  = 0.5-Z;
+           //if (Z>0.248) cout << Z << ": " << time0 - xyz[0] + kAmWidth << " -> ";
+            if (!(fPar->TimeStruct(driftlength+2*kAmWidth,Z,xyz))) continue;
+           //if (Z>0.248) cout << time0 - xyz[0] + kAmWidth << endl;
+         }
+
           // The electron position after diffusion and ExB in pad coordinates 
           // The pad row (z-direction)
           Float_t rowDist   = xyz[2] - row0;
@@ -924,7 +968,7 @@ Bool_t AliTRDdigitizer::MakeDigits()
 
               // Store the track index in the dictionary
               // Note: We store index+1 in order to allow the array to be compressed
-              if (signalOld[iPad] > 0) {
+              if ((signalOld[iPad] > 0) && (!fSimpleSim)) { 
                 for (iDict = 0; iDict < kNDict; iDict++) {
                   Int_t oldTrack = dictionary[iDict]->GetDataUnchecked(rowE
                                                                       ,colPos
@@ -956,13 +1000,22 @@ Bool_t AliTRDdigitizer::MakeDigits()
     printf("Finished analyzing %d hits\n",countHits);
   }
 
-  // The total conversion factor
-  Float_t convert = kEl2fC * fPar->GetPadCoupling() 
-                           * fPar->GetTimeCoupling() 
-                           * fPar->GetChipGain();
+  // The coupling factor
+  Float_t coupling = fPar->GetPadCoupling() 
+                   * fPar->GetTimeCoupling();
+
+  // The conversion factor
+  Float_t convert  = kEl2fC
+                   * fPar->GetChipGain();
 
   // Loop through all chambers to finalize the digits
-  for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
+  Int_t iDetBeg = 0;
+  Int_t iDetEnd = AliTRDgeometry::Ndet();
+  if (fSimpleSim) {
+    iDetBeg = fSimpleDet;
+    iDetEnd = iDetBeg + 1;
+  }
+  for (Int_t iDet = iDetBeg; iDet < iDetEnd; iDet++) {
 
     Int_t plane       = fGeo->GetPlane(iDet);
     Int_t sector      = fGeo->GetSector(iDet);
@@ -983,8 +1036,13 @@ Bool_t AliTRDdigitizer::MakeDigits()
     // Add a container for the digits of this detector
     digits = fDigitsManager->GetDigits(iDet);        
     // Allocate memory space for the digits buffer
-    digits->Allocate(nRowMax,nColMax,nTimeTotal);
-
+    if (digits->GetNtime() == 0) {
+      digits->Allocate(nRowMax,nColMax,nTimeTotal);
+    }
+    else if (fSimpleSim) {
+      digits->Clear();
+    }
     // Get the signal container
     signals = (AliTRDdataArrayF *) signalsArray->At(iDet);
     if (signals->GetNtime() == 0) {
@@ -996,11 +1054,13 @@ Bool_t AliTRDdigitizer::MakeDigits()
       if (fCompress) signals->Expand();
     }
     // Create the missing dictionary containers
-    for (iDict = 0; iDict < kNDict; iDict++) {       
-      dictionary[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
-      if (dictionary[iDict]->GetNtime() == 0) {
-        dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
-      }
+    if (!fSimpleSim) {    
+      for (iDict = 0; iDict < kNDict; iDict++) {       
+        dictionary[iDict] = fDigitsManager->GetDictionary(iDet,iDict);
+        if (dictionary[iDict]->GetNtime() == 0) {
+          dictionary[iDict]->Allocate(nRowMax,nColMax,nTimeTotal);
+        }
+      } 
     }
 
     Int_t nDigits = 0;
@@ -1030,11 +1090,20 @@ Bool_t AliTRDdigitizer::MakeDigits()
 
             for (iTime = 0; iTime < nTimeTotal; iTime++) {         
               Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
-              // Add the noise
-              signalAmp  = TMath::Max((Double_t) gRandom->Gaus(signalAmp,fPar->GetNoise()),0.0);
+              // Pad and time coupling
+              signalAmp *= coupling;
+              // Add the noise, starting from minus ADC baseline in electrons
+              Double_t baselineEl = fPar->GetADCbaseline() * (fPar->GetADCinRange()
+                                                           / fPar->GetADCoutRange()) 
+                                                           / convert;
+              signalAmp  = TMath::Max((Double_t) gRandom->Gaus(signalAmp,fPar->GetNoise())
+                                     ,-baselineEl);
               // Convert to mV
               signalAmp *= convert;
-             // Convert to ADC counts. Set the overflow-bit fADCoutRange if the 
+              // Add ADC baseline in mV
+              signalAmp += fPar->GetADCbaseline() * (fPar->GetADCinRange()
+                                                   / fPar->GetADCoutRange());
+             // Convert to ADC counts. Set the overflow-bit fADCoutRange if the 
              // signal is larger than fADCinRange
               Int_t adc  = 0;
               if (signalAmp >= fPar->GetADCinRange()) {
@@ -1061,7 +1130,7 @@ Bool_t AliTRDdigitizer::MakeDigits()
                         ,iRow,iCol,iTime,outADC[iTime]);
                }
                 nDigits++;
-                digits->SetDataUnchecked(iRow,iCol,iTime,outADC[iTime]);
+                digits->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
              }
            }
 
@@ -1073,39 +1142,49 @@ Bool_t AliTRDdigitizer::MakeDigits()
     }
 
     // Compress the arrays
-    digits->Compress(1,0);
-    for (iDict = 0; iDict < kNDict; iDict++) {
-      dictionary[iDict]->Compress(1,0);
-    }
+    if (!fSimpleSim) {  
+      digits->Compress(1,0);
+      for (iDict = 0; iDict < kNDict; iDict++) {
+        dictionary[iDict]->Compress(1,0);
+      }
 
-    totalSizeDigits += digits->GetSize();
-    totalSizeDict0  += dictionary[0]->GetSize();
-    totalSizeDict1  += dictionary[1]->GetSize();
-    totalSizeDict2  += dictionary[2]->GetSize();
+      totalSizeDigits += digits->GetSize();
+      totalSizeDict0  += dictionary[0]->GetSize();
+      totalSizeDict1  += dictionary[1]->GetSize();
+      totalSizeDict2  += dictionary[2]->GetSize();
 
-    Float_t nPixel = nRowMax * nColMax * nTimeMax;
-    if (fDebug > 0) {
-      printf("<AliTRDdigitizer::MakeDigits> ");
-      printf("Found %d digits in detector %d (%3.0f).\n"
-            ,nDigits,iDet
-            ,100.0 * ((Float_t) nDigits) / nPixel);
-    } 
+      Float_t nPixel = nRowMax * nColMax * nTimeMax;
+      if (fDebug > 0) {
+        printf("<AliTRDdigitizer::MakeDigits> ");
+        printf("Found %d digits in detector %d (%3.0f).\n"
+              ,nDigits,iDet
+              ,100.0 * ((Float_t) nDigits) / nPixel);
+      
 
-    if (fCompress) signals->Compress(1,0);
+      if (fCompress) signals->Compress(1,0);
+
+    }
 
     delete [] inADC;
     delete [] outADC;
 
   }
 
+  if (signalsArray) {
+    delete signalsArray;
+    signalsArray = 0;
+  }
+
   if (fDebug > 0) {
     printf("<AliTRDdigitizer::MakeDigits> ");
     printf("Total number of analyzed hits = %d\n",countHits);
-    printf("<AliTRDdigitizer::MakeDigits> ");
-    printf("Total digits data size = %d, %d, %d, %d\n",totalSizeDigits
-                                                      ,totalSizeDict0
-                                                      ,totalSizeDict1
-                                                      ,totalSizeDict2);        
+    if (!fSimpleSim) {    
+      printf("<AliTRDdigitizer::MakeDigits> ");
+      printf("Total digits data size = %d, %d, %d, %d\n",totalSizeDigits
+                                                        ,totalSizeDict0
+                                                        ,totalSizeDict1
+                                                        ,totalSizeDict2);        
+    }
   }
 
   return kTRUE;
@@ -1165,10 +1244,12 @@ Bool_t AliTRDdigitizer::ConvertSDigits()
   Double_t padCoupling  = fPar->GetPadCoupling();
   Double_t timeCoupling = fPar->GetTimeCoupling();
   Double_t chipGain     = fPar->GetChipGain();
-  Double_t convert      = kEl2fC * padCoupling * timeCoupling * chipGain;;
+  Double_t coupling     = padCoupling * timeCoupling;
+  Double_t convert      = kEl2fC * chipGain;
   Double_t adcInRange   = fPar->GetADCinRange();
   Double_t adcOutRange  = fPar->GetADCoutRange();
   Int_t    adcThreshold = fPar->GetADCthreshold();
+  Int_t    adcBaseline  = fPar->GetADCbaseline();   
 
   AliTRDdataArrayI *digitsIn;
   AliTRDdataArrayI *digitsOut;
@@ -1210,10 +1291,15 @@ Bool_t AliTRDdigitizer::ConvertSDigits()
         for (iTime = 0; iTime < nTimeTotal; iTime++) {         
           Double_t signal = (Double_t) digitsIn->GetDataUnchecked(iRow,iCol,iTime);
           signal *= sDigitsScale;
-          // Add the noise
-          signal  = TMath::Max((Double_t) gRandom->Gaus(signal,noise),0.0);
+          // Pad and time coupling
+          signal *= coupling;
+          // Add the noise, starting from minus ADC baseline in electrons
+          Double_t baselineEl = adcBaseline * (adcInRange / adcOutRange) / convert;
+          signal  = TMath::Max((Double_t) gRandom->Gaus(signal,noise),-baselineEl);
           // Convert to mV
           signal *= convert;
+          // add ADC baseline in mV
+          signal += adcBaseline * (adcInRange / adcOutRange);
          // Convert to ADC counts. Set the overflow-bit adcOutRange if the 
          // signal is larger than adcInRange
           Int_t adc  = 0;
@@ -1235,7 +1321,7 @@ Bool_t AliTRDdigitizer::ConvertSDigits()
         for (iTime = 0; iTime < nTimeTotal; iTime++) {   
           // Store the amplitude of the digit if above threshold
           if (outADC[iTime] > adcThreshold) {
-            digitsOut->SetDataUnchecked(iRow,iCol,iTime,outADC[iTime]);
+            digitsOut->SetDataUnchecked(iRow,iCol,iTime,((Int_t) outADC[iTime]));
            // Copy the dictionary
             for (iDict = 0; iDict < kNDict; iDict++) { 
               Int_t track = dictionaryIn[iDict]->GetDataUnchecked(iRow,iCol,iTime);
@@ -1427,6 +1513,8 @@ Bool_t AliTRDdigitizer::CheckDetector(Int_t plane, Int_t chamber, Int_t sector)
   // Checks whether a detector is enabled
   //
 
+  if (fSimpleSim) return kTRUE; 
+
   if ((fTRD->GetSensChamber() >=       0) &&
       (fTRD->GetSensChamber() != chamber)) return kFALSE;
   if ((fTRD->GetSensPlane()   >=       0) &&
@@ -1449,12 +1537,16 @@ Bool_t AliTRDdigitizer::CheckDetector(Int_t plane, Int_t chamber, Int_t sector)
 }
 
 //_____________________________________________________________________________
-Bool_t AliTRDdigitizer::WriteDigits()
+Bool_t AliTRDdigitizer::WriteDigits() const
 {
   //
   // Writes out the TRD-digits and the dictionaries
   //
 
+  //Write parameters
+  fRunLoader->CdGAFile();
+  if (!gFile->Get("TRDparameter")) GetParameter()->Write();
+
   // Store the digits and the dictionary in the tree
   return fDigitsManager->WriteDigits();
 
@@ -1517,3 +1609,49 @@ void AliTRDdigitizer::DeConvExp(Double_t *source, Double_t *target
   
 }
 
+//_____________________________________________________________________________
+void AliTRDdigitizer::InitOutput(Int_t iEvent)
+{
+  //
+  // Initializes the output branches
+  //
+
+  fEvent = iEvent;
+   
+  if (!fRunLoader)
+   {
+     Error("InitOutput","Run Loader is NULL");
+     return;  
+   }
+  AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
+  if (!loader)
+   {
+     Error("Open","Can not get TRD loader from Run Loader");
+     return;
+   }
+
+  TTree* tree = 0;
+  
+  if (fSDigits)
+   { 
+   //if we produce SDigits
+    tree = loader->TreeS();
+    if (!tree)
+     {
+      loader->MakeTree("S");
+      tree = loader->TreeS();
+     }
+   }
+  else
+   {//if we produce Digits
+     tree = loader->TreeD();
+     if (!tree)
+      {
+       loader->MakeTree("D");
+       tree = loader->TreeD();
+      }
+   }
+  fDigitsManager->SetEvent(iEvent);
+  fDigitsManager->MakeBranch(tree);
+
+}