]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOS.cxx
Introduction of decalibration in the simulations with anchor runs and raw:// OCDB.
[u/mrichter/AliRoot.git] / PHOS / AliPHOS.cxx
index 6e09cddf14f8abf0f997a2d102b30c4b54d542c8..d0d882a62d5dce981e3ed87366641f24e32d166f 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
-
 /* $Id$ */
+/* History of cvs commits:
+ *
+ * $Log$
+ * Revision 1.116  2007/10/10 09:05:10  schutz
+ * Changing name QualAss to QA
+ *
+ * Revision 1.115  2007/08/22 09:20:50  hristov
+ * Updated QA classes (Yves)
+ *
+ * Revision 1.114  2007/08/07 14:12:03  kharlov
+ * Quality assurance added (Yves Schutz)
+ *
+ * Revision 1.113  2007/07/18 16:29:54  policheh
+ * Raw Sdigits energy converted to GeV.
+ *
+ * Revision 1.112  2007/02/25 22:59:13  policheh
+ * Digits2Raw(): ALTRO buffer and mapping created per each DDL.
+ *
+ * Revision 1.111  2007/02/18 15:21:47  kharlov
+ * Corrections for memory leak in Digits2Raw due to AliAltroMapping
+ *
+ * Revision 1.110  2007/02/13 10:52:08  policheh
+ * Raw2SDigits() implemented
+ *
+ * Revision 1.109  2007/02/05 10:43:25  hristov
+ * Changes for correct initialization of Geant4 (Mihaela)
+ *
+ * Revision 1.108  2007/02/01 10:34:47  hristov
+ * Removing warnings on Solaris x86
+ *
+ * Revision 1.107  2007/01/29 16:29:37  kharlov
+ * Digits2Raw(): special workaround for digits with time out of range
+ *
+ * Revision 1.106  2007/01/17 17:28:56  kharlov
+ * Extract ALTRO sample generation to a separate class AliPHOSPulseGenerator
+ *
+ * Revision 1.105  2007/01/12 21:44:29  kharlov
+ * Simulate and reconstruct two gains simulaneouslsy
+ */
 
 //_________________________________________________________________________
 // Base Class for PHOS description:
 
 // --- ROOT system ---
 class TFile;
-#include "TROOT.h"
-#include "TTree.h"
-#include "TFolder.h" 
+#include <TF1.h> 
+#include <TFolder.h> 
+#include <TGeoGlobalMagField.h>
+#include <TH1F.h> 
+#include <TRandom.h> 
+#include <TTree.h>
+#include <TVirtualMC.h> 
 
 // --- Standard library ---
 
-#include <strstream.h>
 // --- AliRoot header files ---
-
+#include "AliMagF.h"
 #include "AliPHOS.h"
-#include "AliMC.h"
+#include "AliPHOSLoader.h"
 #include "AliRun.h"
-#include "AliMagF.h"
-#include "AliPHOSGeometry.h"
-#include "AliPHOSQAChecker.h" 
+#include "AliRawReader.h"
+#include "AliPHOSDigitizer.h"
+#include "AliPHOSSDigitizer.h"
+#include "AliPHOSDigit.h"
+#include "AliAltroBuffer.h"
+#include "AliAltroMapping.h"
+#include "AliCaloAltroMapping.h"
+#include "AliLog.h"
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
+#include "AliCDBStorage.h"
+#include "AliPHOSCalibData.h"
+#include "AliPHOSPulseGenerator.h"
+#include "AliDAQ.h"
+#include "AliPHOSRawFitterv0.h"
+#include "AliPHOSCalibData.h"
+#include "AliPHOSRawDigiProducer.h"
+#include "AliPHOSQAChecker.h"
+#include "AliPHOSRecoParam.h"
+#include "AliPHOSSimParam.h"
 
 ClassImp(AliPHOS)
-//____________________________________________________________________________
-AliPHOS:: AliPHOS() : AliDetector()
-{
-  // Create folder and task hierarchy
-  fName="PHOS";
-  CreatePHOSFolders();
-}
 
 //____________________________________________________________________________
-AliPHOS::AliPHOS(const char* name, const char* title): AliDetector(name, title) 
+  AliPHOS:: AliPHOS() : AliDetector(),fgCalibData(0)
 {
-  // Create folder and task hierarchy
-  CreatePHOSFolders();
+  // Default ctor
+  fName   = "PHOS" ;
+
 }
 
 //____________________________________________________________________________
-void AliPHOS::CreatePHOSFolders()
+AliPHOS::AliPHOS(const char* name, const char* title): AliDetector(name, title),
+fgCalibData(0)
 {
-  // create the ALICE TFolder
-  // create the ALICE TTasks
-  //  add the Alice QA TTAsks 
-  // create the ALICE main TFolder
-  //  add the Alice QA Alarms
-  // this should be done of course by AliRun
-
-  TFolder * alice = new TFolder(); 
-  alice->SetNameTitle("YSAlice", "Alice Folder") ; 
-  gROOT->GetListOfBrowsables()->Add(alice) ;
-
-  TFolder * aliceF  = alice->AddFolder("folders", "Alice memory Folder") ; 
-  //  make it the owner of the objects that it contains
-  aliceF->SetOwner() ;
-  // geometry folder 
-  TFolder * geomF = aliceF->AddFolder("Geometry", "Geometry objects") ; 
-  // alarms folder
-  TFolder * alarmsF = aliceF->AddFolder("QAAlarms", "Alarms raised by QA check") ; 
-  //  make it the owner of the objects that it contains
-  alarmsF->SetOwner() ;
-  TFolder * aliceT  = alice->AddFolder("tasks", "Alice tasks Folder") ; 
-  //  make it the owner of the objects that it contains
-  aliceT->SetOwner() ;
-
-  TTask * aliceQA = new TTask("QA", "Alice QA tasks") ;
-  aliceT->Add(aliceQA); 
-  TTask * aliceDi = new TTask("(S)Digitizer", "Alice SDigitizer & Digitizer") ;
-  aliceT->Add(aliceDi); 
-
-  TTask * aliceRe = new TTask("Reconstructioner", "Alice Reconstructioner") ;
-  aliceT->Add(aliceRe); 
-
-  char * tempo = new char[80] ; 
-
-  // creates the PHOSQA (QAChecker knows how to add itself in the tasks list)
-  sprintf(tempo, "%sCheckers container",GetName() ) ; 
-  fQATask = new AliPHOSQAChecker(GetName(), tempo);  
-
-  // creates the PHOS(S)Digitizer and adds it to alice main (S)Digitizer task 
-  sprintf(tempo, "%sDigitizers container",GetName() ) ; 
-  fSDTask = new TTask(GetName(), tempo);   
-  aliceDi->Add(fSDTask) ; 
-
-  // creates the PHOS reconstructioner and adds it to alice main Reconstructioner task 
-  sprintf(tempo, "%sReconstructioner container",GetName() ) ; 
-  fReTask = new TTask(GetName(), tempo); 
-  aliceRe->Add(fReTask) ; 
-
-  delete tempo ;  
-
-  // creates the PHOS geometry  folder
-  geomF->AddFolder("PHOS", "Geometry for PHOS") ; 
-  // creates the PHOSQA alarm folder
-  alarmsF->AddFolder("PHOS", "QA alarms from PHOS") ; 
+  //   ctor : title is used to identify the layout
 }
+
 //____________________________________________________________________________
 AliPHOS::~AliPHOS() 
 {  
-  // remove the alice folder and alice QA task that PHOS creates instead of AliRun
-  
-  // remove and delete the PHOS QA tasks
-  TFolder * alice = (TFolder*)gROOT->GetListOfBrowsables()->FindObject("YSAlice") ; 
-  TTask * aliceQA = (TTask*)alice->FindObject("tasks/QA") ; 
-  fQATask->GetListOfTasks()->Delete() ;
-  aliceQA->GetListOfTasks()->Remove(fQATask) ; 
-  delete fQATask ;  
-  
-  // remove and delete aliceQA (should be done by AliRun) 
+  if(fgCalibData) delete fgCalibData ;
+}
+
+//____________________________________________________________________________
+AliDigitizer* AliPHOS::CreateDigitizer(AliRunDigitizer* manager) const
+{
+  return new AliPHOSDigitizer(manager);
 }
 
 //____________________________________________________________________________
@@ -194,11 +191,11 @@ void AliPHOS::CreateMaterials()
   Float_t aTI[2] = {12.011, 1.00794} ;
   Float_t zTI[2] = {6.0, 1.0} ;
   Float_t wTI[2] = {1.0, 1.0} ;
-  Float_t dTI = 0.1 ;
+  Float_t dTI = 0.04 ;
 
   AliMixture(7, "Thermo Insul.$", aTI, zTI, dTI, -2, wTI) ;
 
-  // --- Textolitn ---
+  // --- Textolith ---
   Float_t aTX[4] = {16.0, 28.09, 12.011, 1.00794} ;
   Float_t zTX[4] = {8.0, 14.0, 6.0, 1.0} ;
   Float_t wTX[4] = {292.0, 68.0, 462.0, 736.0} ;
@@ -207,12 +204,12 @@ void AliPHOS::CreateMaterials()
   AliMixture(8, "Textolit$", aTX, zTX, dTX, -4, wTX) ;
 
   //--- FR4  ---
-  Float_t aFR[3] = {28.0855, 15.9994, 17.749} ; 
-  Float_t zFR[3] = {14., 8., 8.875} ; 
-  Float_t wFR[3] = {.28, .32, .4} ;
+  Float_t aFR[4] = {16.0, 28.09, 12.011, 1.00794} ;
+  Float_t zFR[4] = {8.0, 14.0, 6.0, 1.0} ;
+  Float_t wFR[4] = {292.0, 68.0, 462.0, 736.0} ;
   Float_t dFR = 1.8 ; 
 
-  AliMixture(9, "FR4$", aFR, zFR, dFR, -3, wFR) ;
+  AliMixture(9, "FR4$", aFR, zFR, dFR, -4, wFR) ;
 
   // --- The Composite Material for  micromegas (so far polyetylene) ---                                       
   Float_t aCM[2] = {12.01, 1.} ; 
@@ -229,7 +226,8 @@ void AliPHOS::CreateMaterials()
   Float_t aG10[4] = { 12., 1., 16., 28.} ;
   Float_t zG10[4] = { 6., 1., 8., 14.} ;
   Float_t wG10[4] = { .259, .288, .248, .205} ;
-  Float_t dG10  = 1.7 ;
+  Float_t dG10  = 1.7 ; 
+
   
   AliMixture(12, "G10$", aG10, zG10, dG10, -4, wG10);
 
@@ -249,44 +247,54 @@ void AliPHOS::CreateMaterials()
   Float_t dAr = 0.001782 ; 
   AliMaterial(15, "Ar$", 39.948, 18.0, dAr, 14.0, 0., 0, 0) ;   
  
- // ArCo2
-  Char_t namate[21];
-  Float_t aGM[2] ; 
-  Float_t zGM[2] ; 
-  Float_t wGM[2] ; 
-  Float_t dGM ; 
+  // Ar+CO2 Mixture (80% / 20%)
+  Float_t arContent = 0.80 ;  // Ar-content of the ArCO2-mixture
+  Float_t aArCO[3]  = {39.948, 12.0, 16.0} ;
+  Float_t zArCO[3]  = {18.0  ,  6.0,  8.0} ;
+  Float_t wArCO[3];
+  wArCO[0] = arContent;
+  wArCO[1] = (1-arContent)*1;
+  wArCO[2] = (1-arContent)*2;
+  Float_t dArCO = arContent*dAr + (1-arContent)*dCO ;
+  AliMixture(16, "ArCO2$", aArCO, zArCO, dArCO,  -3, wArCO) ; 
 
-  Float_t absL, radL, density ;
-  Float_t buf[1] ;
-  Int_t nbuf ;
 
-  gMC->Gfmate((*fIdmate)[15], namate, aGM[0], zGM[0], density, radL, absL, buf, nbuf) ; // Get properties of Ar 
-  gMC->Gfmate((*fIdmate)[14], namate, aGM[1], zGM[1], density, radL, absL, buf, nbuf) ; // Get properties of CO2 
+  // --- Stainless steel (let it be pure iron) ---
+  AliMaterial(17, "Steel$", 55.845, 26, 7.87, 1.76, 0., 0, 0) ;
 
 
-  // Create gas mixture 
+  // --- Fiberglass ---
+  Float_t aFG[4] = {16.0, 28.09, 12.011, 1.00794} ;
+  Float_t zFG[4] = {8.0, 14.0, 6.0, 1.0} ;
+  Float_t wFG[4] = {292.0, 68.0, 462.0, 736.0} ;
+  Float_t dFG    = 1.9 ;
 
-  Float_t arContent    = 0.80 ;  // Ar-content of the Ar/CO2-mixture (80% / 20%) 
-  wGM[0] = arContent;
-  wGM[1] = 1. - arContent ;
-  dGM    = wGM[0] * dAr + wGM[1] * dCO;
+  AliMixture(18, "Fibergla$", aFG, zFG, dFG, -4, wFG) ;
+
+  // --- Cables in Air box  ---
+  // SERVICES
+
+  Float_t aCA[4] = { 1.,12.,55.8,63.5 };
+  Float_t zCA[4] = { 1.,6.,26.,29. }; 
+  Float_t wCA[4] = { .014,.086,.42,.48 };
+  Float_t dCA    = 0.8 ;  //this density is raw estimation, if you know better - correct
+
+  AliMixture(19, "Cables  $", aCA, zCA, dCA, -4, wCA) ;
 
-  AliMixture(16, "ArCO2$", aGM, zGM, dGM,  2, wGM) ;
 
-  // --- Stainless steel (let it be pure iron) ---
-  AliMaterial(17, "Steel$", 55.845, 26, 7.87, 1.76, 0., 0, 0) ;
   // --- Air ---
-  AliMaterial(99, "Air$", 14.61, 7.3, 0.001205, 30420., 67500., 0, 0) ;
-  
+  Float_t aAir[4]={12.0107,14.0067,15.9994,39.948};
+  Float_t zAir[4]={6.,7.,8.,18.};
+  Float_t wAir[4]={0.000124,0.755267,0.231781,0.012827};
+  Float_t dAir = 1.20479E-3;
  
+  AliMixture(99, "Air$", aAir, zAir, dAir, 4, wAir) ;
+
   // DEFINITION OF THE TRACKING MEDIA
 
   // for PHOS: idtmed[699->798] equivalent to fIdtmed[0->100]
-  Int_t * idtmed = fIdtmed->GetArray() - 699 ; 
-  Int_t   isxfld = gAlice->Field()->Integ() ;
-  Float_t sxmgmx = gAlice->Field()->Max() ;
+  Int_t   isxfld = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Integ() ;
+  Float_t sxmgmx = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Max() ;
 
   // The scintillator of the calorimeter made of PBW04                              -> idtmed[699]
   AliMedium(0, "PHOS Xtal    $", 0, 1,
@@ -356,78 +364,282 @@ void AliPHOS::CreateMaterials()
   AliMedium(17, "Steel     $", 17, 0,
             isxfld, sxmgmx, 10.0, 0.1, 0.1, 0.1, 0.0001, 0, 0) ;
 
+  // Fibergalss                                                                     -> idtmed[717]
+  AliMedium(18, "Fiberglass$", 18, 0,
+            isxfld, sxmgmx, 10.0, 0.1, 0.1, 0.1, 0.1, 0, 0) ;
+
+  // Cables in air                                                                  -> idtmed[718]
+  AliMedium(19, "Cables    $", 19, 0,
+            isxfld, sxmgmx, 10.0, 0.1, 0.1, 0.1, 0.1, 0, 0) ;
+
   // Air                                                                            -> idtmed[798] 
   AliMedium(99, "Air          $", 99, 0,
             isxfld, sxmgmx, 10.0, 1.0, 0.1, 0.1, 10.0, 0, 0) ;
+}
 
-  // --- Set decent energy thresholds for gamma and electron tracking
-
-  // Tracking threshold for photons and electrons in the scintillator crystal 
-  gMC->Gstpar(idtmed[699], "CUTGAM",0.5E-4) ; 
-  gMC->Gstpar(idtmed[699], "CUTELE",1.0E-4) ;
-  // --- Generate explicitly delta rays in the titan cover ---
-  gMC->Gstpar(idtmed[704], "LOSS",3.) ;
-  gMC->Gstpar(idtmed[704], "DRAY",1.) ;
-  // --- and in aluminium parts ---
-  gMC->Gstpar(idtmed[701], "LOSS",3.) ;
-  gMC->Gstpar(idtmed[701], "DRAY",1.) ;
-  // --- and in PIN diode
-  gMC->Gstpar(idtmed[705], "LOSS",3) ;
-  gMC->Gstpar(idtmed[705], "DRAY",1) ;
-  // --- and in the passive convertor
-  gMC->Gstpar(idtmed[712], "LOSS",3) ;
-  gMC->Gstpar(idtmed[712], "DRAY",1) ;
-  // Tracking threshold for photons and electrons in the gas ArC02 
-  gMC->Gstpar(idtmed[715], "CUTGAM",1.E-5) ; 
-  gMC->Gstpar(idtmed[715], "CUTELE",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "CUTNEU",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "CUTHAD",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "CUTMUO",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "BCUTE",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "BCUTM",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "DCUTE",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "DCUTM",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "PPCUTM",1.E-5) ;
-  gMC->Gstpar(idtmed[715], "LOSS",2.) ;
-  gMC->Gstpar(idtmed[715], "DRAY",0.) ;
-  gMC->Gstpar(idtmed[715], "STRA",2.) ;
-
+//_____________________________________________________________________________
+void AliPHOS::Init()
+{
 }
+
 //____________________________________________________________________________
-AliPHOSGeometry * AliPHOS::GetGeometry() const 
-{  
-  // gets the pointer to the AliPHOSGeometry unique instance from the folder
+void AliPHOS::Digits2Raw()
+{
+// convert digits of the current event to raw data
+  
+  if(AliPHOSSimParam::GetInstance()->IsEDigitizationOn()){
+    AliError("Energy digitization should be OFF if use Digits2Raw") ;
+  }
+
+  AliPHOSLoader * loader = static_cast<AliPHOSLoader*>(fLoader) ; 
+
+  // get the digits
+  loader->LoadDigits();
+  TClonesArray* digits = loader->Digits() ;
+
+  if (!digits) {
+    AliError(Form("No digits found !"));
+    return;
+  }
+
+  // get the geometry
+  AliPHOSGeometry* geom = GetGeometry();
+  if (!geom) {
+    AliError(Form("No geometry found !"));
+    return;
+  }
+
+  // get mapping from OCDB
+  const TObjArray* maps = AliPHOSRecoParam::GetMappings();
+  if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
+
+  // some digitization constants
+  const Float_t    kThreshold = 1.; // skip digits below 1 ADC channel
+  const Int_t      kAdcThreshold = 1;  // Lower ADC threshold to write to raw data
+
+  Int_t prevDDL = -1;
+
+  if(fgCalibData==0)
+    fgCalibData= new AliPHOSCalibData(-1) ;
+
+  // Create a shaper pulse object
+  AliPHOSPulseGenerator *pulse = new AliPHOSPulseGenerator();
+
+  //Set Time step of sample
+  pulse->SetTimeStep(TMath::Abs(fgCalibData->GetSampleTimeStep())) ;
   
-  AliPHOSGeometry * rv ; 
+  Int_t *adcValuesLow = new Int_t[pulse->GetRawFormatTimeBins()];
+  Int_t *adcValuesHigh= new Int_t[pulse->GetRawFormatTimeBins()];
   
-  TFolder * alice = (TFolder*)gROOT->GetListOfBrowsables()->FindObject("YSAlice") ;
-  TString path("folders/Geometry/PHOS/") ; 
-  path += GetTitle() ; 
-  rv = (AliPHOSGeometry*)alice->FindObject(path) ; 
-  if ( !rv )
-    rv = fGeom ; 
-  return rv ; 
+  const Int_t maxDDL = 20;
+  AliAltroBuffer  *buffer[maxDDL];
+  AliAltroMapping *mapping[maxDDL];
+
+  for(Int_t jDDL=0; jDDL<maxDDL; jDDL++) {
+    buffer[jDDL]=0;
+    mapping[jDDL]=0;
+  }
+
+  //!!!!for debug!!!
+  Int_t modMax=-111;
+  Int_t colMax=-111;
+  Int_t rowMax=-111;
+  Float_t eMax=-333;
+  //!!!for debug!!!
+
+  // loop over digits (assume ordered digits)
+  for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++) {
+    AliPHOSDigit* digit = static_cast<AliPHOSDigit *>(digits->At(iDigit)) ;
+
+    // Skip small energy below treshold
+    if (digit->GetEnergy() < kThreshold) 
+      continue;
+
+    Int_t relId[4];
+    geom->AbsToRelNumbering(digit->GetId(), relId);
+    Int_t module = 5-relId[0];
+    // Begin FIXME 
+    if (relId[1] != 0) 
+      continue;    // ignore digits from CPV
+    // End FIXME 
+
+    Int_t row = relId[2]-1;
+    Int_t col = relId[3]-1;
+    
+    Int_t iRCU = -111;
+    
+    //RCU0
+    if(0<=row&&row<16 && 0<=col&&col<56) iRCU=0;
+    
+    //RCU1
+    if(16<=row&&row<32 && 0<=col&&col<56) iRCU=1;
+
+    //RCU2
+    if(32<=row&&row<48 && 0<=col&&col<56) iRCU=2;
+
+    //RCU3
+    if(48<=row&&row<64 && 0<=col&&col<56) iRCU=3;
+    
+    
+    // PHOS EMCA has 4 DDL per module. Splitting is based on the (row,column) numbers.
+    // here module already in PHOS online convention: 0<module<4 and inverse order.
+    Int_t iDDL = 4 * module  + iRCU;
+
+    // new DDL
+    if (iDDL != prevDDL) {
+      if (buffer[iDDL] == 0) {
+       // open new file and write dummy header
+       TString fileName = AliDAQ::DdlFileName("PHOS",iDDL);
+
+       mapping[iDDL] = (AliAltroMapping*)maps->At(iDDL);
+       buffer[iDDL]  = new AliAltroBuffer(fileName.Data(),mapping[iDDL]);
+       buffer[iDDL]->WriteDataHeader(kTRUE, kFALSE);  //Dummy;
+      }
+      prevDDL = iDDL;
+    }
+
+    AliDebug(2,Form("digit E=%.4f GeV, t=%g s, (mod,col,row)=(%d,%d,%d)\n",
+                   digit->GetEnergy(),digit->GetTimeR(),
+                   relId[0]-1,relId[3]-1,relId[2]-1));
+    // if a signal is out of time range, write only trailer
+    if (digit->GetTimeR() > pulse->GetRawFormatTimeMax()*0.5 ) {
+      AliDebug(2,"Signal is out of time range.\n");
+      buffer[iDDL]->FillBuffer(0);
+      buffer[iDDL]->FillBuffer(pulse->GetRawFormatTimeBins() );  // time bin
+      buffer[iDDL]->FillBuffer(3);                               // bunch length
+      buffer[iDDL]->WriteTrailer(3, relId[3]-1, relId[2]-1, 0);  // trailer
+      
+    // calculate the time response function
+    } else {
+      Double_t energy = 0 ;
+      if (digit->GetId() <= geom->GetNModules() * geom->GetNCristalsInModule()) {
+       energy=digit->GetEnergy();
+       if(energy>eMax) {eMax=energy; modMax=relId[0]; colMax=col; rowMax=row;}
+      }
+      else {
+       energy = 0; // CPV raw data format is now know yet
+      }
+      pulse->SetAmplitude(energy);
+      pulse->SetTZero(digit->GetTimeR());
+      Double_t r =fgCalibData->GetHighLowRatioEmc(relId[0],relId[3],relId[2]) ;
+      pulse->SetHG2LGRatio(r) ;
+      pulse->MakeSamples();
+      pulse->GetSamples(adcValuesHigh, adcValuesLow) ; 
+
+      buffer[iDDL]->WriteChannel(relId[3]-1, relId[2]-1, 0, 
+                          pulse->GetRawFormatTimeBins(), adcValuesLow , kAdcThreshold);
+      buffer[iDDL]->WriteChannel(relId[3]-1, relId[2]-1, 1, 
+                          pulse->GetRawFormatTimeBins(), adcValuesHigh, kAdcThreshold);
+    }
+  }
+  delete [] adcValuesLow;
+  delete [] adcValuesHigh;
+
+  // write real header and close last file
+  for (Int_t iDDL=0; iDDL<maxDDL; iDDL++) {
+    if (buffer[iDDL]) {
+      buffer[iDDL]->Flush();
+      buffer[iDDL]->WriteDataHeader(kFALSE, kFALSE);
+      delete buffer[iDDL];
+      //if (mapping[iDDL]) delete mapping[iDDL];
+    }
+  }
+  
+  AliDebug(1,Form("Digit with max. energy:  modMax %d colMax %d rowMax %d  eMax %f\n",
+        modMax,colMax,rowMax,eMax));
+
+  delete pulse;
+  loader->UnloadDigits();
 }
 
 //____________________________________________________________________________
-void AliPHOS::SetTreeAddress()
+void AliPHOS::Hits2SDigits()  
 { 
+// create summable digits
 
+  AliPHOSSDigitizer phosDigitizer(fLoader->GetRunLoader()->GetFileName().Data()) ;
+  phosDigitizer.SetEventRange(0, -1) ; // do all the events
+  phosDigitizer.ExecuteTask("all") ; 
+}
 
-  // TBranch *branch;
-  //  AliDetector::SetTreeAddress();
 
+//____________________________________________________________________________
+AliLoader* AliPHOS::MakeLoader(const char* topfoldername)
+{
+//different behaviour than standard (singleton getter)
+// --> to be discussed and made eventually coherent
+ fLoader = new AliPHOSLoader(GetName(),topfoldername);
+ return fLoader;
+}
+
+//____________________________________________________________________________
+void AliPHOS::SetTreeAddress()
+{ 
+  // Links Hits in the Tree to Hits array
   TBranch *branch;
   char branchname[20];
-  sprintf(branchname,"%s",GetName());
-  
+  snprintf(branchname,20,"%s",GetName());
   // Branch address for hit tree
-  TTree *treeH = gAlice->TreeH();
-  if (treeH && fHits) {
+    TTree *treeH = fLoader->TreeH();
+  if (treeH) {
     branch = treeH->GetBranch(branchname);
-    if (branch) branch->SetAddress(&fHits);
+    if (branch) 
+     { 
+       if (fHits == 0x0) fHits= new TClonesArray("AliPHOSHit",1000);
+       //AliInfo(Form("<%s> Setting Hits Address",GetName()));
+       branch->SetAddress(&fHits);
+     }
   }
 }
 
+//____________________________________________________________________________          
+Bool_t AliPHOS::Raw2SDigits(AliRawReader* rawReader)    
+{       
+                
+  AliPHOSLoader * loader = static_cast<AliPHOSLoader*>(fLoader) ;       
+                
+  TTree * tree = 0 ;    
+  tree = loader->TreeS() ;      
+  if ( !tree ) {        
+    loader->MakeTree("S");      
+    tree = loader->TreeS() ;    
+  }     
+                
+  TClonesArray * sdigits = loader->SDigits() ;          
+  if(!sdigits) {        
+    loader->MakeSDigitsArray();         
+    sdigits = loader->SDigits();        
+  }     
+  sdigits->Clear();     
+                
+  rawReader->Reset() ;
+
+  const TObjArray* maps = AliPHOSRecoParam::GetMappings();
+  if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
+
+  AliAltroMapping *mapping[20];
+  for(Int_t i = 0; i < 20; i++) {
+    mapping[i] = (AliAltroMapping*)maps->At(i);
+  }
 
+  AliPHOSRawFitterv0 fitter;
+
+  fitter.SubtractPedestals(AliPHOSSimParam::GetInstance()->EMCSubtractPedestals());
+  fitter.SetAmpOffset(AliPHOSSimParam::GetInstance()->GetGlobalAltroOffset());
+  fitter.SetAmpThreshold(AliPHOSSimParam::GetInstance()->GetGlobalAltroThreshold());
+
+  AliPHOSRawDigiProducer pr(rawReader,mapping);
+  pr.SetSampleQualityCut(AliPHOSSimParam::GetInstance()->GetEMCSampleQualityCut());     
+  pr.MakeDigits(sdigits,&fitter);
+                
+  Int_t bufferSize = 32000 ;    
+  // TBranch * sdigitsBranch = tree->Branch("PHOS",&sdigits,bufferSize);        
+  tree->Branch("PHOS",&sdigits,bufferSize);     
+  tree->Fill();         
+                
+  fLoader->WriteSDigits("OVERWRITE");   
+  return kTRUE;         
+  
+}