]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliRunDigitizer.cxx
Taking into account the dE/dx crossing points in the TPC (Yu.Belikov)
[u/mrichter/AliRoot.git] / STEER / AliRunDigitizer.cxx
index 4aef248481b69f9ab7aabe69f442ea2f4dae99bf..03414e1c9bbd4bfa5e88295ebe52c5e3e48eef61 100644 (file)
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
 
-/*
-$Log$
-Revision 1.2  2001/07/28 10:44:32  hristov
-Loop variable declared once; typos corrected
+/* $Id$ */
 
-Revision 1.1  2001/07/27 12:59:00  jchudoba
-Manager class for merging/digitization
-
-*/
-
-////////////////////////////////////////////////////////////////////////
+//_______________________________________________________________________
 //
 // AliRunDigitizer.cxx
 //
@@ -33,451 +25,476 @@ Manager class for merging/digitization
 // Sdigits into Digits. 
 //
 // Only one instance of this class is created in the macro:
-//   AliRunDigitizer * manager = new AliRunDigitizer();
+//   AliRunDigitizer * manager = 
+//      new AliRunDigitizer(nInputStreams,SPERB);
+// where nInputStreams is number of input streams and SPERB is
+// signals per background variable, which determines how combinations
+// of signal and background events are generated.
 // Then instances of specific detector digitizers are created:
 //   AliMUONDigitizer *dMUON  = new AliMUONDigitizer(manager)
-// and the manager is configured (you have to specify input files 
-// and an output file). The manager generates a combination of input 
-// events according to an option set by SetCombinationType. Then it
-// connects appropriate trees from the input files, creates TreeD 
-// in the output and runs once per event Digitize method of all existing 
-// AliDetDigitizers (without any option). AliDetDigitizers ask manager
-// for a TTree with input (manager->GetNextTreeH(TTree *) 
-// or manager->GetNextTreeS(TTree *),
+// and the I/O configured (you have to specify input files 
+// and an output file). The manager connects appropriate trees from 
+// the input files according a combination returned by AliMergeCombi 
+// class. It creates TreeD in the output and runs once per 
+// event Digitize method of all existing AliDetDigitizers 
+// (without any option). AliDetDigitizers ask manager
+// for a TTree with input (manager->GetInputTreeS(Int_t i),
 // merge all inputs, digitize it, and save it in the TreeD 
 // obtained by manager->GetTreeD(). Output events are stored with 
 // numbers from 0, this default can be changed by 
 // manager->SetFirstOutputEventNr(Int_t) method. The particle numbers
 // in the output are shifted by MASK, which is taken from manager.
 //
-// Single input file is permitted. Maximum MAXFILESTOMERGE can be merged.
+// The default output is to the signal file (stream 0). This can be 
+// changed with the SetOutputFile(TString fn)  method.
+//
+// Single input file is permitted. Maximum MAXSTREAMSTOMERGE can be merged.
 // Input from the memory (on-the-fly merging) is not yet 
 // supported, as well as access to the input data by invoking methods
 // on the output data.
 //
-// Access to the geometrical data is via gAlice for now (supposing the 
-// same geometry in all input files), gAlice is taken from the defined 
-// input file.
+// Access to the some data is via gAlice for now (supposing the 
+// same geometry in all input files), gAlice is taken from the first 
+// input file on the first stream.
+//
+// Example with MUON digitizer, no merging, just digitization
+//
+//  AliRunDigitizer * manager = new AliRunDigitizer(1,1);
+//  manager->SetInputStream(0,"galice.root");
+//  AliMUONDigitizer *dMUON  = new AliMUONDigitizer(manager);
+//  manager->Exec("");
 //
-// Example with MUON digitizer:
+// Example with MUON digitizer, merge all events from 
+//   galice.root (signal) file with events from bgr.root 
+//   (background) file. Number of merged events is
+//   min(number of events in galice.root, number of events in bgr.root)
 //
-//   AliRunDigitizer * manager = new AliRunDigitizer();
-//   manager->SetInput("1track_10events_phi45_60.root");
-//   manager->SetInput("1track_10events_phi120_135.root");
-//   manager->SetOutputDir("/home/z2/jchudoba/batch/jobtmp");
-//   manager->SetOutputFile("digits.root");
-//   AliMUONDigitizer *dMUON  = new AliMUONDigitizer(manager);
-//   manager->SetNrOfEventsToWrite(3);
-//   manager->SetCopyTreesFromInput(1);
-//   manager->Digitize();
+//  AliRunDigitizer * manager = new AliRunDigitizer(2,1);
+//  manager->SetInputStream(0,"galice.root");
+//  manager->SetInputStream(1,"bgr.root");
+//  AliMUONDigitizer *dMUON  = new AliMUONDigitizer(manager);
+//  manager->Exec("");
 //
-//////////////////////////////////////////////////////////////////////// 
+// Example with MUON digitizer, save digits in a new file digits.root,
+//   process only 1 event
+//
+//  AliRunDigitizer * manager = new AliRunDigitizer(2,1);
+//  manager->SetInputStream(0,"galice.root");
+//  manager->SetInputStream(1,"bgr.root");
+//  manager->SetOutputFile("digits.root");
+//  AliMUONDigitizer *dMUON  = new AliMUONDigitizer(manager);
+//  manager->SetNrOfEventsToWrite(1);
+//  manager->Exec("");
+//
+//_______________________________________________________________________ 
 
 // system includes
 
-#include <iostream.h>
-
 // ROOT includes
 
-#include "TFile.h"
-#include "TTree.h"
+#include <Riostream.h>
+class TList;
 
 // AliROOT includes
 
-#include "AliRunDigitizer.h"
 #include "AliDigitizer.h"
-#include "AliRun.h"
 #include "AliHeader.h"
-#include "TParticle.h"
+#include "AliLoader.h"
+#include "AliMergeCombi.h"
+#include "AliRun.h"
+#include "AliRunDigitizer.h"
+#include "AliRunLoader.h"
 
 ClassImp(AliRunDigitizer)
 
-////////////////////////////////////////////////////////////////////////
-
-  AliRunDigitizer::AliRunDigitizer() : TNamed("AliRunDigitizer","")
+const TString AliRunDigitizer::fgkDefOutFolderName("Output");
+const TString AliRunDigitizer::fgkBaseInFolderName("Input");
+
+
+//_______________________________________________________________________
+AliRunDigitizer::AliRunDigitizer():
+ fkMASKSTEP(0),
+ fOutputFileName(0),
+ fOutputDirName(0),
+ fEvent(0),
+ fNrOfEventsToWrite(0),
+ fNrOfEventsWritten(0),
+ fCopyTreesFromInput(0),
+ fNinputs(0),
+ fNinputsGiven(0),
+ fInputStreams(0x0),
+ fOutRunLoader(0x0),
+ fOutputInitialized(kFALSE),
+ fCombi(0),
+ fCombination(0),
+ fCombinationFileName(0),
+ fDebug(0)
 {
-// default ctor
+  //
+  // root requires default ctor, where no new objects can be created
+  // do not use this ctor, it is supplied only for root needs
+}
 
-  Int_t i;
+//_______________________________________________________________________
+AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb):
+ TTask("AliRunDigitizer","The manager for Merging"),
+ fkMASKSTEP(10000000),
+ fOutputFileName(""),
+ fOutputDirName("."),
+ fEvent(0),
+ fNrOfEventsToWrite(-1),
+ fNrOfEventsWritten(0),
+ fCopyTreesFromInput(-1),
+ fNinputs(nInputStreams),
+ fNinputsGiven(0),
+ fInputStreams(new TClonesArray("AliStream",nInputStreams)),
+ fOutRunLoader(0x0),
+ fOutputInitialized(kFALSE),
+ fCombi(new AliMergeCombi(nInputStreams,sperb)),
+ fCombination(MAXSTREAMSTOMERGE),
+ fCombinationFileName(0),
+ fDebug(0)
 
-  for (i=0;i<MAXDETECTORS;i++) fDigitizers[i]=0;
-  fNDigitizers = 0;
-  fNinputs = 0;
-  fOutputFileName = "digits.root";
-  fOutputDirName = "/tmp/";
-  fCombination.Set(MAXFILESTOMERGE);
-  for (i=0;i<MAXFILESTOMERGE;i++) {
-    fArrayTreeS[i]=fArrayTreeH[i]=fArrayTreeTPCS[i]=NULL;
-    fCombination[i]=-1;
-  }
-  fkMASKSTEP = 10000000;
+{
+  //
+  // ctor which should be used to create 
+  // a manager for merging/digitization
+  //
+  if (nInputStreams == 0) 
+   {//kidding
+    Fatal("AliRunDigitizer","Specify nr of input streams");
+    return;
+   }
+  Int_t i;
+  
   fkMASK[0] = 0;
-  for (i=1;i<MAXFILESTOMERGE;i++) {
+  
+  for (i=1;i<MAXSTREAMSTOMERGE;i++) 
+   {
     fkMASK[i] = fkMASK[i-1] + fkMASKSTEP;
+   }
+  
+  TClonesArray &lInputStreams = *fInputStreams;
+  
+  new(lInputStreams[0]) AliStream(fgkBaseInFolderName+(Long_t)0,"UPDATE");
+  for (i=1;i<nInputStreams;i++) {
+    new(lInputStreams[i]) AliStream(fgkBaseInFolderName+(Long_t)i,"READ");
   }
-  fInputFileNames = new TClonesArray("TObjString",1);
-  fInputFiles = new TClonesArray("TFile",1);
-  fMinNEvents = 99999999;
-  fCombinationType=1;
-  fCombinationFileName = "combination.txt";
-  fOutput = 0;
-  fEvent = 0;
-  fNrOfEventsToWrite = 0;
-  fNrOfEventsWritten = 0;
-  fCopyTreesFromInput = -1;
-  fDebug = 3;
-  if (GetDebug()>2) 
-    cerr<<"AliRunDigitizer::AliRunDigitizer() called"<<endl;
 }
-
-////////////////////////////////////////////////////////////////////////
-
-AliRunDigitizer::~AliRunDigitizer() {
-// dtor
-
-  if (fInputFiles) delete fInputFiles;
-  if (fInputFileNames) delete fInputFileNames;
+//_______________________________________________________________________
+
+AliRunDigitizer::AliRunDigitizer(const AliRunDigitizer& dig):
+ TTask(dig),
+ fkMASKSTEP(0),
+ fOutputFileName(0),
+ fOutputDirName(0),
+ fEvent(0),
+ fNrOfEventsToWrite(0),
+ fNrOfEventsWritten(0),
+ fCopyTreesFromInput(0),
+ fNinputs(0),
+ fNinputsGiven(0),
+ fInputStreams(0x0),
+ fOutRunLoader(0x0),
+ fOutputInitialized(kFALSE),
+ fCombi(0),
+ fCombination(0),
+ fCombinationFileName(0),
+ fDebug(0)
+{
+  //
+  // Copy ctor
+  //
+  dig.Copy(*this);
 }
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::AddDigitizer(AliDigitizer *digitizer)
+void AliRunDigitizer::Copy(TObject&) const
 {
-// add digitizer to the list of active digitizers
-
-  if (fNDigitizers >= MAXDETECTORS) {
-    cerr<<"Too many detectors to digitize. Increase value of MAXDETECTORS"
-       <<" constant in AliRunDigitizer.h and recompile or decrease the"
-       <<" the number of detectors"<<endl;
-  } else {
-    fDigitizers[fNDigitizers++] = digitizer;
-  }
+  //
+  // Non implemented copy function
+  //
+  Fatal("Copy","Not installed\n");
 }
 
-////////////////////////////////////////////////////////////////////////
+//_______________________________________________________________________
 
-Bool_t AliRunDigitizer::SetInput(char *inputFile)
+AliRunDigitizer::~AliRunDigitizer() 
 {
-// receives the name of the input file. Opens it and stores pointer 
-// to it, returns kFALSE if fails
-// if inputFile = MEMORY, uses pointers from gAlice - not yet implemented
-
-// input cannot be output - open READ only
-  TFile *file = new((*fInputFiles)[fNinputs]) TFile(inputFile,"READ"); 
-
-  if (GetDebug()>2) cerr<<"AliRunDigitizer::SetInput: file = "<<file<<endl;
-  if (!file->IsOpen()) {
-    cerr<<"ERROR: AliRunDigitizer::SetInput: cannot open file "
-       <<inputFile<<endl;
-    return kFALSE;
-  }
-
-// find Header and get nr of events there
-  TTree * te = (TTree *) file->Get("TE") ;
-  if (!te) {
-    cerr<<"ERROR: AliRunDigitizer::SetInput:  input file does "
-       <<"not contain TE"<<endl;
-    return kFALSE;
-  }
-  Int_t nEntries = (Int_t) te->GetEntries();
-
-  if (GetDebug()>2) cerr<<"AliRunDigitizer::SetInput: nEntries = "
-                       <<nEntries<<endl;
-  if (nEntries < 1) {
-    cerr<<"ERROR: AliRunDigitizer::SetInput:  input file does "
-       <<"not contain any event"<<endl;
-    return kFALSE;
-  }
-
-  if (nEntries < fMinNEvents) fNrOfEventsToWrite = fMinNEvents = nEntries;
-
-// find gAlice object if it is a first input file
-//  this is only temporary solution, we need gAlice to access detector
-//  geometry parameters. Unfortunately I have to include AliRun header file.
-  if (fNinputs == 0) {
-    gAlice = (AliRun*)file->Get("gAlice");
-    if (GetDebug() > 2) cerr<<"gAlice taken from the first input: "
-                           <<gAlice<<endl;
-    if (!gAlice) {
-      cerr<<"ERROR: AliRunDigitizer::SetInput:  first input file "
-         <<"does not contain gAlice object"<<endl;
-      return kFALSE;
-    }
-  }
-    
-// store this file name if it is OK
-  new((*fInputFileNames)[fNinputs])  TObjString(inputFile);
-  fNinputs++;
-  if (GetDebug() > 2) cerr<<"fNinputs = "<<fNinputs<<endl;
-  return kTRUE;
+  //
+  // dtor
+  //
+  delete fInputStreams;
+  delete fCombi;
+  delete fOutRunLoader;
 }
-
-////////////////////////////////////////////////////////////////////////
-Bool_t AliRunDigitizer::MakeCombination()
+//_______________________________________________________________________
+void AliRunDigitizer::AddDigitizer(AliDigitizer *digitizer)
 {
-// make a new combination of events from different files
-
-  Int_t type = fCombinationType;
-  if (fNrOfEventsWritten >= fNrOfEventsToWrite) return kFALSE;
-
-  switch (type) {
-       
-  case 1:
-// type = 1:  1-1-1 - take the same event number from each file
-    if (fCombination[0]<fMinNEvents-1) {       
-      for (Int_t i=0;i<fNinputs;i++) fCombination[i]++;
-      return kTRUE;
-    }
-    if (GetDebug()>2)
-      cerr<<"AliRunDigitizer::MakeCombination: Warning: "
-         <<"maximum number of Events in an input file "
-         <<"was reached."<<endl;
-    break;
-
-  case 2:
-// type = 2: read from the file combinations.ascii
-//  not yet implemented 100% correctly - requires 4 entries in the row
-    static FILE *fp ;
-    static Int_t linesRead;
-    if (!fp) {
-      fp = fopen(fCombinationFileName.Data(),"r");
-      linesRead = 0;
-    }
-    if (!fp) {
-      cerr<<"AliRunDigitizer::MakeCombination ERROR: "
-         <<"Cannot open input file with combinations."<<endl;
-      return kFALSE;
-    }
-    Int_t var[4], nInputs;
-    char line[80];
-// since I do not close or rewind the file, the position should be correct
-    if (fgets(line,80,fp)) {
-      nInputs = sscanf(&line[0],"%d%d%d%d",&var[0],&var[1],&var[2],&var[3]);
-      if (nInputs != fNinputs) {
-       cerr<<"AliRunDigitizer::MakeCombination ERROR: "
-           <<"Nr. of input files is different from nr "
-           <<"integers defining the combination"<<endl;
-       return kFALSE;
-      }
-      while(nInputs--) {
-       fCombination[nInputs] = var[nInputs];
-      }
-      return kTRUE;
-    } else {
-      cerr<<"AliRunDigitizer::MakeCombination ERROR: "
-         <<"no more input in the file with combinations"<<endl;
-      return kFALSE;
-    }
-
-  default:
-    cerr<<"AliRunDigitizer::MakeCombination: ERROR: "
-       <<"wrong type of required combination type: "<<type<<endl;
-  }
-  return kFALSE;
+  //
+  // add digitizer to the list of active digitizers
+  //
+  this->Add(digitizer);
 }
-
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::PrintCombination()
+//_______________________________________________________________________
+void AliRunDigitizer::SetInputStream(Int_t i, const char *inputFile, TString foldername)
 {
-// debug method to print current combination
-
-  cerr<<"AliRunDigitizer::PrintCombination: Current events combination:"<<endl;
-  for (Int_t i=0;i<fNinputs;i++) {
-    cerr<<"File: "<<((TObjString *)fInputFileNames->At(i))->GetString()<<"\tEvent: "<<fCombination[i]<<endl;
+//
+// Sets the name of the input file
+//
+  if (i > fInputStreams->GetLast()) {
+    Error("SetInputStream","Input stream number too high");
+    return;
   }
+  AliStream * stream = static_cast<AliStream*>(fInputStreams->At(i)) ; 
+  if ( !foldername.IsNull() ) {
+    if ( i > 0 ) 
+      foldername += i ; // foldername should stay unchanged for the default output 
+    stream->SetFolderName(foldername) ;
+  } 
+  stream->AddFile(inputFile);
 }
 
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::Digitize()
+//_______________________________________________________________________
+void AliRunDigitizer::Digitize(Option_t* option)
 {
-// get a new combination of inputs, connect input trees and loop 
-// over all digitizers
+// get a new combination of inputs, loads events to folders
 
-  if (!InitGlobal()) {
-    cerr<<"False from InitGlobal"<<endl;
-    return;
-  }
 // take gAlice from the first input file. It is needed to access
 //  geometry data
-  while (MakeCombination()) {
-    if (GetDebug()>2) PrintCombination();
-    ConnectInputTrees();
-    InitPerEvent();
-// loop over all registered digitizers and let them do the work
-    for (Int_t i=0;i<fNDigitizers; i++) {
-      fDigitizers[i]->Digitize();
-    }
-    FinishPerEvent();
-  }
+// If gAlice is already in memory, use it
+  
+  if (!static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice()) 
+   {
+     Error("Digitize","Error occured while getting gAlice from Input 0");
+     return;
+   }
+    
+  if (!InitGlobal()) //calls Init() for all (sub)digitizers
+   {
+     Error("Digitize","InitGlobal returned error");
+     return;
+   }
+   
+  Int_t eventsCreated = 0;
+// loop until there is anything on the input in case fNrOfEventsToWrite < 0
+  while ((eventsCreated++ < fNrOfEventsToWrite) || (fNrOfEventsToWrite < 0)) 
+   {
+      if (!ConnectInputTrees()) break;
+      InitEvent();//this must be after call of Connect Input tress.
+      if (fOutRunLoader)
+       {
+         fOutRunLoader->SetEventNumber(eventsCreated-1);
+       }
+      static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice(); // use gAlice of the first input stream
+      ExecuteTasks(option);// loop over all registered digitizers and let them do the work
+      FinishEvent();
+      CleanTasks();
+   }
   FinishGlobal();
 }
 
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::ConnectInputTrees()
+//_______________________________________________________________________
+Bool_t AliRunDigitizer::ConnectInputTrees()
 {
-// fill arrays fArrayTreeS, fArrayTreeH and fArrayTreeTPCS with 
-// pointers to the correct events according fCombination values
-// null pointers can be in the output, AliDigitizer has to check it
-
-  TFile *file;
-  TTree *tree;
-  char treeName[20];
-  for (Int_t i=0;i<fNinputs;i++) {
-    file = (TFile*)(*fInputFiles)[i];
-    sprintf(treeName,"TreeS%d",fCombination[i]);
-    tree = (TTree *) file->Get(treeName);
-    fArrayTreeS[i] = tree;
-    sprintf(treeName,"TreeH%d",fCombination[i]);
-    tree = (TTree *) file->Get(treeName);
-    fArrayTreeH[i] = tree;
-    sprintf(treeName,"TreeD_75x40_100x60_%d",fCombination[i]);
-    tree = (TTree *) file->Get(treeName);
-    fArrayTreeTPCS[i] = tree;
+  //
+  // loads events 
+  //
+  Int_t eventNr[MAXSTREAMSTOMERGE], delta[MAXSTREAMSTOMERGE];
+  fCombi->Combination(eventNr, delta);
+  for (Int_t i=0;i<fNinputs;i++) 
+   {
+    if (delta[i] == 1)
+     {
+      AliStream *iStream = static_cast<AliStream*>(fInputStreams->At(i));//gets the "i" defined  in combination
+      if (!iStream->NextEventInStream()) return kFALSE; //sets serial number
+     } 
+    else if (delta[i] != 0) 
+     {
+      Error("ConnectInputTrees","Only delta 0 or 1 is implemented");
+      return kFALSE;
+     }
    }
-}
 
-////////////////////////////////////////////////////////////////////////
-Bool_t AliRunDigitizer::InitGlobal()
-{
-// called once before Digitize() is called, initialize digitizers and output
-
-  if (!InitOutputGlobal()) return kFALSE;
-  for (Int_t i=0;i<fNDigitizers; i++) {
-    if (!fDigitizers[i]->Init()) return kFALSE;
-  }
   return kTRUE;
 }
 
-////////////////////////////////////////////////////////////////////////
-Bool_t AliRunDigitizer::InitOutputGlobal()
+//_______________________________________________________________________
+Bool_t AliRunDigitizer::InitGlobal()
 {
-// Creates the output file, called once by InitGlobal()
-
-  TString fn;
-  fn = fOutputDirName + '/' + fOutputFileName;
-  fOutput = new TFile(fn,"recreate");
-  if (GetDebug()>2) {
-    cerr<<"file "<<fn.Data()<<" was opened"<<endl;
-    cerr<<"fOutput = "<<fOutput<<endl;
+  //
+  // Method called once before Digitize() is called
+  // initialize digitizers and output
+  //
+  fOutputInitialized = kFALSE;
+  TList* subTasks = this->GetListOfTasks();
+  if (subTasks) {
+    TIter next(subTasks);
+    while (AliDigitizer * dig = (AliDigitizer *) next())
+     dig->Init();
   }
-  if (fOutput) return kTRUE;
-  Error("InitOutputGlobal","Could not create output file.");
-  return kFALSE;
+  return kTRUE;
 }
 
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::InitPerEvent()
+void AliRunDigitizer::SetOutputFile(TString fn)
 {
-// Creates TreeDxx in the output file, called from Digitize() once for 
-//  each event. xx = fEvent
-
-  if (GetDebug()>2) 
-    cerr<<"AliRunDigitizer::InitPerEvent: fEvent = "<<fEvent<<endl;
-  fOutput->cd();
-  char hname[30];
-  sprintf(hname,"TreeD%d",fEvent);
-  fTreeD = new TTree(hname,"Digits");
-//  fTreeD->Write();   // Do I have to write it here???
+  //
+  // The output will be to separate file, 
+  // not to the signal file here should be protection 
+  //to avoid setting the same file as any input 
+  //
+  Info("SetOutputFile","Setting Output File Name %s ",fn.Data());
+  fOutputFileName = fn;
+  // InitOutputGlobal();
 }
 
-
-////////////////////////////////////////////////////////////////////////
-TTree* AliRunDigitizer::GetNextTreeH(TTree *current) const
-{
-// returns next one after the current one
-// returns the first if the current is NULL
-// returns NULL if the current is the last one
-
-  if (fNinputs <= 0) return 0;
-  if (current == 0) return fArrayTreeH[0];
-  for (Int_t i=0; i<fNinputs-1; i++) {
-    if (current == fArrayTreeH[i]) return fArrayTreeH[i+1];
-  }
-  return 0;
-}
-////////////////////////////////////////////////////////////////////////
-TTree* AliRunDigitizer::GetNextTreeS(TTree *current) const
-{
-// returns next one after the current one
-// returns the first if the current is NULL
-// returns NULL if the current is the last one
-
-  if (fNinputs <= 0) return 0;
-  if (current == 0) return fArrayTreeS[0];
-  for (Int_t i=0; i<fNinputs-1; i++) {
-    if (current == fArrayTreeS[i]) return fArrayTreeS[i+1];
-  }
-  return 0;
-}
-////////////////////////////////////////////////////////////////////////
-TTree* AliRunDigitizer::GetNextTreeTPCS(TTree *current) const
+//_______________________________________________________________________
+Bool_t AliRunDigitizer::InitOutputGlobal()
 {
-// returns next one after the current one
-// returns the first if the current is NULL
-// returns NULL if the current is the last one
-
-  if (fNinputs <= 0) return 0;
-  if (current == 0) return fArrayTreeTPCS[0];
-  for (Int_t i=0; i<fNinputs-1; i++) {
-    if (current == fArrayTreeTPCS[i]) return fArrayTreeTPCS[i+1];
-  }
-  return 0;
+  //
+  // Creates the output file, called by InitEvent()
+  // Needs to be called after all inputs are opened  
+  //
+  if (fOutputInitialized) return kTRUE;
+  
+  if ( !fOutputFileName.IsNull())
+   {
+    fOutRunLoader = AliRunLoader::Open(fOutputFileName,fgkDefOutFolderName,"recreate");
+    
+    if (fOutRunLoader == 0x0)
+     {
+       Error("InitOutputGlobal","Can not open ooutput");
+       return kFALSE;
+     }
+    Info("InitOutputGlobal", " 1 %s = ", GetInputFolderName(0).Data()) ; 
+    AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
+    if (inrl == 0x0)
+     {
+       Error("InitOutputGlobal","Can not get Run Loader Input 0. Maybe yet not initialized?");
+       return kFALSE;
+     }
+    Info("InitOutputGlobal", " 2 %#x = ", inrl) ; 
+
+    //Copy all detector loaders from input 0 to output
+    const TObjArray* inloaders = inrl->GetArrayOfLoaders();
+    TIter next(inloaders);
+    AliLoader *loader;
+    while((loader = (AliLoader*)next()))
+     {
+       AliLoader* cloneloader = (AliLoader*)loader->Clone();
+       cloneloader->Register(fOutRunLoader->GetEventFolder());//creates folders for this loader in Output Folder Structure
+       GetOutRunLoader()->AddLoader(cloneloader);//adds a loader for output
+     }
+
+    fOutRunLoader->MakeTree("E");
+    
+    if (GetDebug()>2)  Info("InitOutputGlobal","file %s was opened.",fOutputFileName.Data());
+   }
+  fOutputInitialized = kTRUE; 
+  return kTRUE;
 }
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
-Int_t AliRunDigitizer::GetNextMask(Int_t current) const
+void AliRunDigitizer::InitEvent()
 {
-// returns next one after the current one
-// returns the first if the current is negative
-// returns negative if the current is the last one
-
-  if (fNinputs <= 0) return -1;
-  if (current < 0) return fkMASK[0]; 
-  for (Int_t i=0; i<fNinputs-1; i++) {
-    if (current == fkMASK[i]) return fkMASK[i+1];
-  }
-  return -1;
+  //
+  // redirects output properly
+  //
+  if (GetDebug()>2)
+   {
+    Info("InitEvent","fEvent = %d",fEvent);
+    Info("InitEvent","fOutputFileName \"%s\"",fOutputFileName.Data());
+   }
+  if (fOutputInitialized == kFALSE) InitOutputGlobal();
+  
+  // if fOutputFileName was not given, write output to signal directory
 }
-////////////////////////////////////////////////////////////////////////
-void AliRunDigitizer::FinishPerEvent()
+//_______________________________________________________________________
+
+void AliRunDigitizer::FinishEvent()
 {
-// called at the end of loop over digitizers
-
-  fOutput->cd();
-  if (fCopyTreesFromInput > -1) {
-    char treeName[20];
-    Int_t i = fCopyTreesFromInput; 
-    sprintf(treeName,"TreeK%d",fCombination[i]);
-    ((TFile*)fInputFiles->At(i))->Get(treeName)->Clone()->Write();
-    sprintf(treeName,"TreeH%d",fCombination[i]);
-    ((TFile*)fInputFiles->At(i))->Get(treeName)->Clone()->Write();
-  }
+  //
+  // called at the end of loop over digitizers
+  //
+  
+  if (GetOutRunLoader() == 0x0)
+   {
+     Error("FinishEvent","fOutRunLoader is null");
+     return;
+   }
+  
   fEvent++;
   fNrOfEventsWritten++;
-  if (fTreeD) {
-    delete fTreeD;
-    fTreeD = 0;
+  
+  if (fOutRunLoader)
+  {
+     AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
+     AliHeader* outheader = fOutRunLoader->GetHeader();
+     AliHeader* inheader = inrl->GetHeader();
+     if (inheader == 0x0)
+     {
+       inrl->LoadHeader();
+       inheader = inrl->GetHeader();
+       if (inheader == 0x0) Fatal("FinishEvent","Can not get header from input 0");
+     }
+     
+     outheader->SetNprimary(inheader->GetNprimary());
+     outheader->SetNtrack(inheader->GetNtrack());
+     outheader->SetEvent(inheader->GetEvent());
+     outheader->SetEventNrInRun(inheader->GetEventNrInRun());
+     outheader->SetNvertex(inheader->GetNvertex());
+     fOutRunLoader->TreeE()->Fill();
   }
+      
+  if (fCopyTreesFromInput > -1) 
+   {
+    //this is sensless since no information would be coherent in case of merging
+    //
+    cout<<"Copy trees from input: Copy or link files manually"<<endl;
+    return;
+   }
 }
-////////////////////////////////////////////////////////////////////////
+//_______________________________________________________________________
+
 void AliRunDigitizer::FinishGlobal()
 {
-// called at the end of Exec
-// save unique objects to the output file
-
-  fOutput->cd();
-  this->Write();
-  if (fCopyTreesFromInput > -1) {
-    ((TFile*)fInputFiles->At(fCopyTreesFromInput))->Get("TE")
-      ->Clone()->Write();
-    gAlice->Write();
-  }
-  fOutput->Close();
+  //
+  // called at the end of Exec
+  // save unique objects to the output file
+  //
+  if (GetOutRunLoader() == 0x0)
+   {
+     Error("FinishGlobal","Can not get RunLoader from Output Stream folder");
+     return;
+   }
+  GetOutRunLoader()->CdGAFile();
+  this->Write(NULL, TObject::kOverwrite);
+  if (fOutRunLoader)
+   {
+     fOutRunLoader->WriteHeader("OVERWRITE");
+     fOutRunLoader->WriteRunLoader("OVERWRITE");
+     TFolder* outfolder = fOutRunLoader->GetEventFolder();
+     if (outfolder == 0x0)
+     {
+       Error("FinishEvent","Can not get Event Folder");
+       return;
+     }    
+  
+     AliRunLoader* inRN = AliRunLoader::GetRunLoader(GetInputFolderName(0));
+     outfolder->Add(inRN->GetAliRun());
+     fOutRunLoader->WriteAliRun("OVERWRITE");
+   }
+   
+  if (fCopyTreesFromInput > -1) 
+   {
+     //copy files manually
+   }
 }
+//_______________________________________________________________________
 
-
-////////////////////////////////////////////////////////////////////////
-Int_t  AliRunDigitizer::GetNParticles(Int_t event)
+Int_t  AliRunDigitizer::GetNParticles(Int_t event) const
 {
 // return number of particles in all input files for a given
 // event (as numbered in the output file)
@@ -492,55 +509,35 @@ Int_t  AliRunDigitizer::GetNParticles(Int_t event)
   }
   return sum;
 }
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
-Int_t  AliRunDigitizer::GetNParticles(Int_t event, Int_t input)
+Int_t  AliRunDigitizer::GetNParticles(Int_t /*event*/, Int_t /*input*/) const
 {
 // return number of particles in input file input for a given
 // event (as numbered in this input file)
 // return -1 if some error
 
-  TFile *file = ConnectInputFile(input);
-  if (!file) {
-    Error("GetNParticles","Cannot open input file");
-    return -1;
-  }
+// Must be revised in the version with AliStream
+
+  return -1;
 
-// find the header and get Nprimaries and Nsecondaries
-  TTree* tE = (TTree *)file->Get("TE") ;
-  if (!tE) {
-    Error("GetNParticles","input file does not contain TE");
-    return -1;
-  }
-  AliHeader* header;
-  header = 0;
-  tE->SetBranchAddress("Header", &header);
-  if (!tE->GetEntry(event)) {
-    Error("GetNParticles","event %d not found",event);
-    return -1;
-  }
-  if (GetDebug()>2) {
-    cerr<<"Nprimary: "<< header->GetNprimary()<<endl;
-    cerr<<"Nsecondary: "<<header->GetNsecondary()<<endl;
-  }
-  return header->GetNprimary() + header->GetNsecondary();
 }
 
-////////////////////////////////////////////////////////////////////////
-Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event)
+//_______________________________________________________________________
+Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) const
 {
 // return pointer to an int array with input event numbers which were
 // merged in the output event event
 
 // simplified for now, implement later
-  Int_t a[MAXFILESTOMERGE];
+  Int_t * a = new Int_t[MAXSTREAMSTOMERGE];
   for (Int_t i = 0; i < fNinputs; i++) {
     a[i] = event;
   }
   return a;
 }
-////////////////////////////////////////////////////////////////////////
-Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t input)
+//_______________________________________________________________________
+Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t /*input*/) const
 {
 // return an event number of an eventInput from input file input
 // which was merged to create output event event
@@ -548,36 +545,8 @@ Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t input)
 // simplified for now, implement later
   return event;
 }
-////////////////////////////////////////////////////////////////////////
-TFile* AliRunDigitizer::ConnectInputFile(Int_t input)
-{
-// open input file with index input
-// 1st file has index 0
-// return 0x0 if fails
-
-// check if file with index input is already open
-  TFile *file = 0;
-  if (fInputFiles->GetEntriesFast() > input)
-    file = static_cast<TFile *>(fInputFiles->At(input));
-
-  if (!file) {
-    // find the file name and open it
-    TObjString *fn = static_cast<TObjString *>(fInputFileNames->At(input));
-    file = new((*fInputFiles)[input]) TFile((fn->GetString()).Data(),"READ");
-    if (!file) {
-      Error("ConnectInputFile","Cannot open input file");
-      return 0;
-    }
-    if (!file->IsOpen()) {
-      Error("ConnectInputFile","Cannot open input file");
-      return 0;
-    }
-  }
-  return file;
-}
-
-////////////////////////////////////////////////////////////////////////
-TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event)
+//_______________________________________________________________________
+TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) const
 {
 // return pointer to particle with index i (index with mask)
 
@@ -586,60 +555,83 @@ TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event)
   return GetParticle(i,input,GetInputEventNumber(event,input));
 }
 
-////////////////////////////////////////////////////////////////////////
-TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t input, Int_t event)
+//_______________________________________________________________________
+TParticle* AliRunDigitizer::GetParticle(Int_t /*i*/, Int_t /*input*/, Int_t /*event*/) const
 {
 // return pointer to particle with index i in the input file input
 // (index without mask)
 // event is the event number in the file input
 // return 0 i fit does not exist
 
-  TFile *file = ConnectInputFile(input);
-  if (!file) {
-    Error("GetParticle","Cannot open input file");
-    return 0;
-  }
+// Must be revised in the version with AliStream
 
-// find the header and get Nprimaries and Nsecondaries
-  TTree* tE = (TTree *)file->Get("TE") ;
-  if (!tE) {
-    Error("GetParticle","input file does not contain TE");
-    return 0;
-  }
-  AliHeader* header;
-  header = 0;
-  tE->SetBranchAddress("Header", &header);
-  if (!tE->GetEntry(event)) {
-    Error("GetParticle","event %d not found",event);
-    return 0;
-  }
+  return 0;
+}
+
+//_______________________________________________________________________
+void AliRunDigitizer::ExecuteTask(Option_t* option)
+{
+  //
+  // overwrite ExecuteTask to do Digitize only
+  //
+  if (!IsActive()) return;
+  Digitize(option);
+  fHasExecuted = kTRUE;
+  return;
+}
+
+//_______________________________________________________________________
+const TString& AliRunDigitizer::GetInputFolderName(Int_t i) const
+{
+  //
+  // Get the input Folder Name
+  //
+  AliStream* stream = dynamic_cast<AliStream*>(fInputStreams->At(i));
+  if (stream == 0x0)
+    Fatal("GetInputFolderName","Can not get the input stream. Index = %d. Exiting",i);
+  return stream->GetFolderName();
+}
+//_______________________________________________________________________
+
+const char* AliRunDigitizer::GetOutputFolderName()
+{
+  //
+  // Get output folder name
+  //
+  return GetOutRunLoader()->GetEventFolder()->GetName();
+}
+//_______________________________________________________________________
+
+AliRunLoader* AliRunDigitizer::GetOutRunLoader()
+{
+  //
+  // Returns Run Loader
+  //
+  if (fOutRunLoader) return fOutRunLoader;
   
-// connect TreeK  
-  char treeName[30];
-  sprintf(treeName,"TreeK%d",event);  
-  TTree* tK = static_cast<TTree*>(file->Get(treeName));
-  if (!tK) {
-    Error("GetParticle","input file does not contain TreeK%d",event);
-    return 0;
-  }
-  TParticle *particleBuffer;
-  particleBuffer = 0;
-  tK->SetBranchAddress("Particles", &particleBuffer);
-
-
-// algorithmic way of getting entry index
-// (primary particles are filled after secondaries)
-  Int_t entry;
-  if (i<header->GetNprimary())
-    entry = i+header->GetNsecondary();
-  else 
-    entry = i-header->GetNprimary();
-//  tK->GetEntry(0);          // do I need this???
-  Int_t bytesRead = tK->GetEntry(entry);
-//  new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
-  if (bytesRead)
-    return particleBuffer;
-  return  0;
+  if ( fOutputFileName.IsNull() )
+   {//guard that sombody calls it without settting file name
+    if (GetDebug()>0) {
+      Info("GetOutRunLoader", 
+          "Output file name is empty. Using Input 0 for output");
+    }
+    return AliRunLoader::GetRunLoader(GetInputFolderName(0));
+   }
+//  InitOutputGlobal();
+  return fOutRunLoader;
 }
-////////////////////////////////////////////////////////////////////////
+//_______________________________________________________________________
 
+TString AliRunDigitizer::GetInputFileName(Int_t input, Int_t order) const 
+{
+// returns file name of the order-th file in the input stream input
+// returns empty string if such file does not exist
+// first input stream is 0
+// first file in the input stream is 0
+  TString fileName("");
+  if (input >= fNinputs) return fileName;
+  AliStream * stream = static_cast<AliStream*>(fInputStreams->At(input));
+  if (order > stream->GetNInputFiles()) return fileName;
+  fileName = stream->GetFileName(order);
+  return fileName;
+}