]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDdigitizer.cxx
Corrected destructor (T,Kuhr)
[u/mrichter/AliRoot.git] / TRD / AliTRDdigitizer.cxx
index e217ba7ca70899d04f518d528eaf45062366bbfa..cebd2dbbfa4332ba65e37b159cce2b5a2c1aeb4e 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.33.6.3  2002/10/11 07:26:37  hristov
-Updating VirtualMC to v3-09-02
-
-Revision 1.39  2002/10/08 20:46:12  cblume
-Do coupling factors before noise is applied
-
-Revision 1.38  2002/04/30 08:30:40  cblume
-gAlice now only read by AliRunDigitizer. Therefore it is just deleted in AliTRDmerge.C
-
-Revision 1.37  2002/04/29 11:50:47  cblume
-Change initialization of gAlice in the merging case
-
-Revision 1.36  2002/04/12 12:13:23  cblume
-Add Jiris changes
-
-Revision 1.35  2002/03/28 14:59:07  cblume
-Coding conventions
-
-Revision 1.34  2002/03/25 20:00:44  cblume
-Introduce parameter class and regions of interest for merging
-
-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$ */
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
@@ -197,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"
@@ -219,15 +81,15 @@ 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;
@@ -246,24 +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;
   fSimpleSim          = kFALSE;
   fSimpleDet          = 0;
  
-  // For the summable digits
-  fSDigitsScale       = 100.;
 
 }
 
@@ -276,24 +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;
   fSimpleSim          = kFALSE;
   fSimpleDet          = 0;
  
-  // For the summable digits
-  fSDigitsScale       = 100.;
 
 }
 
@@ -305,29 +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
@@ -344,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;
   }
@@ -391,15 +247,15 @@ 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;
@@ -432,16 +288,25 @@ void AliTRDdigitizer::Exec(Option_t* option)
   }
 
   // The AliRoot file is already connected by the manager
-  if (gAlice) {
+  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");
-    return;
+    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();
@@ -451,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) {
@@ -461,7 +352,21 @@ void AliTRDdigitizer::Exec(Option_t* option)
     }
 
     // check if the input tree exists
-    if (!fManager->GetInputTreeTRDS(iInput)) {
+    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;
@@ -471,7 +376,11 @@ void AliTRDdigitizer::Exec(Option_t* option)
     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);
@@ -490,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");
@@ -509,22 +423,19 @@ 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");
+  
+  fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
+                                 "UPDATE");
+  
+  if (!fRunLoader)
+   {
+     Error("Open","Can not open session for file %s.",file);
+     return kFALSE;
+   }
+   
+  fRunLoader->LoadgAlice();
+  gAlice = fRunLoader->GetAliRun();
+  
   if (gAlice) {
     if (fDebug > 0) {
       printf("<AliTRDdigitizer::Open> ");
@@ -540,15 +451,37 @@ 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
+       tree = loader->TreeD();
+       if (!tree)
+        {
+         loader->MakeTree("D");
+         tree = loader->TreeD();
+        }
+     }
+    return MakeBranch(tree);
   }
   else {
     return kFALSE;
@@ -584,6 +517,7 @@ Bool_t AliTRDdigitizer::InitDetector()
   }
 
   // Create a digits manager
+  delete fDigitsManager;
   fDigitsManager = new AliTRDdigitsManager();
   fDigitsManager->SetSDigits(fSDigits);
   fDigitsManager->CreateArrays();
@@ -591,20 +525,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) const
+Bool_t AliTRDdigitizer::MakeBranch(TTree* tree) const
 {
   // 
   // Create the branches for the digits array
   //
 
-  return fDigitsManager->MakeBranch(file);
+  return fDigitsManager->MakeBranch(tree);
 
 }
 
@@ -691,9 +629,16 @@ 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 = 1;
@@ -1101,11 +1046,18 @@ Bool_t AliTRDdigitizer::MakeDigits()
               Float_t signalAmp = signals->GetDataUnchecked(iRow,iCol,iTime);
               // Pad and time coupling
               signalAmp *= coupling;
-              // Add the noise
-              signalAmp  = TMath::Max((Double_t) gRandom->Gaus(signalAmp,fPar->GetNoise()),0.0);
+              // 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()) {
@@ -1132,7 +1084,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]));
              }
            }
 
@@ -1251,6 +1203,7 @@ Bool_t AliTRDdigitizer::ConvertSDigits()
   Double_t adcInRange   = fPar->GetADCinRange();
   Double_t adcOutRange  = fPar->GetADCoutRange();
   Int_t    adcThreshold = fPar->GetADCthreshold();
+  Int_t    adcBaseline  = fPar->GetADCbaseline();   
 
   AliTRDdataArrayI *digitsIn;
   AliTRDdataArrayI *digitsOut;
@@ -1294,10 +1247,13 @@ Bool_t AliTRDdigitizer::ConvertSDigits()
           signal *= sDigitsScale;
           // Pad and time coupling
           signal *= coupling;
-          // Add the noise
-          signal  = TMath::Max((Double_t) gRandom->Gaus(signal,noise),0.0);
+          // 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;
@@ -1319,7 +1275,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);
@@ -1604,13 +1560,48 @@ void AliTRDdigitizer::DeConvExp(Double_t *source, Double_t *target
 }
 
 //_____________________________________________________________________________
-void AliTRDdigitizer::InitOutput(TFile *file, Int_t iEvent)
+void AliTRDdigitizer::InitOutput(Int_t iEvent)
 {
   //
   // Initializes the output branches
   //
 
   fEvent = iEvent;
-  fDigitsManager->MakeTreeAndBranches(file,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);
 
 }