]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFDigitizer.cxx
Runloader is updated when moving to next file (quick fix).
[u/mrichter/AliRoot.git] / TOF / AliTOFDigitizer.cxx
index 70d2fb62dc2790081cbcfb6fddfa8e758636f2db..f324aa41dd9275ab05ff8c407768cfadb14a04e1 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-//_________________________________________________________________________
-// This is a TTask that makes TOF-Digits out of TOF-SDigits. 
-// The simulation of the detector is performed at sdigits level:
-// during digitization the unique task is the sum of all sdigits in the
-// same pad.
-// Digits are written to TreeD in branch "TOF".
-//
-// -- Author :  F. Pierella (Bologna University) pierella@bo.infn.it
-//////////////////////////////////////////////////////////////////////////////
-
-#include <TTree.h> 
-#include <TVector.h>
-#include <TObjArray.h>
-#include <TFile.h>
-#include <TDirectory.h>
-#include <TRandom.h>
+//_________________________________________________________________________//
+//                                                                         //
+// This is a TTask that makes TOF-Digits out of TOF-SDigits.               //
+// The simulation of the detector is performed at sdigits level:           //
+// during digitization the unique task is the sum of all sdigits in the    //
+// same pad.                                                               //
+// Digits are written to TreeD in branch "TOF".                            //
+//                                                                         //
+// -- Author :  F. Pierella (Bologna University) pierella@bo.infn.it       //
+//                                                                         //
+//_________________________________________________________________________//
+
+#include "Riostream.h"
+
+#include "TFile.h"
+#include "TH1F.h"
+#include "TTree.h"
+#include "TRandom.h"
 
+#include "AliLoader.h"
+#include "AliLog.h"
+#include "AliRunDigitizer.h"
+#include "AliRunLoader.h"
+#include "AliRun.h"
 
+#include "AliTOFCal.h"
+#include "AliTOFcalib.h"
+#include "AliTOFChannel.h"
 #include "AliTOFDigitizer.h"
-#include "AliTOF.h"
-#include "AliTOFSDigitizer.h"
-#include "AliTOFhit.h"
 #include "AliTOFdigit.h"
-#include "AliTOFSDigit.h"
 #include "AliTOFHitMap.h"
-#include "AliDigitizer.h"
-#include "AliRunDigitizer.h"
+#include "AliTOFGeometryV5.h"
+#include "AliTOFSDigit.h"
+#include "AliTOF.h"
 
-#include "AliLog.h"
-#include "AliRun.h"
-#include "AliRunLoader.h"
-#include "AliLoader.h"
-#include "AliPDG.h"
+extern TDirectory *gDirectory;
+extern TFile *gFile;
+extern TRandom *gRandom;
+
+extern AliRun *gAlice;
 
-#include <stdlib.h>
-#include <Riostream.h>
 
 ClassImp(AliTOFDigitizer)
 
 //___________________________________________
-  AliTOFDigitizer::AliTOFDigitizer()  :AliDigitizer()
+  AliTOFDigitizer::AliTOFDigitizer()  :
+    AliDigitizer(),
+    fGeom(0x0),
+    fDigits(0x0),
+    fSDigitsArray(0x0),
+    fhitMap(0x0)
 {
   // Default ctor - don't use it
-  fDigits=0;
-  fSDigitsArray=0;
-  fhitMap=0;
 }
 
 //___________________________________________
-AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager) 
-    :AliDigitizer(manager) 
+AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager): 
+  AliDigitizer(manager), 
+  fGeom(0x0),
+  fDigits(0x0),
+  fSDigitsArray(0x0),
+  fhitMap(0x0)
 {
-  fDigits=0;
-  fSDigitsArray=0;
-  fhitMap=0;
+  //ctor with RunDigitizer
+}
+
+//------------------------------------------------------------------------
+AliTOFDigitizer::AliTOFDigitizer(const AliTOFDigitizer &source):
+  AliDigitizer(source),
+  fGeom(0x0), 
+  fDigits(0),
+  fSDigitsArray(0),
+  fhitMap(0)
+{
+  // copy constructor
+  this->fDigits=source.fDigits;
+  this->fSDigitsArray=source.fSDigitsArray;
+  this->fhitMap=source.fhitMap;
+  this->fGeom=source.fGeom; 
+
+}
+
+//------------------------------------------------------------------------
+  AliTOFDigitizer& AliTOFDigitizer::operator=(const AliTOFDigitizer &source)
+{
+  // ass. op.
+  this->fDigits=source.fDigits;
+  this->fSDigitsArray=source.fSDigitsArray;
+  this->fhitMap=source.fhitMap;
+  this->fGeom=source.fGeom; 
+  return *this;
+
 }
 
 //------------------------------------------------------------------------
@@ -109,6 +146,21 @@ void AliTOFDigitizer::Exec(Option_t* /*option*/)
      return;
    }
    
+  outrl->CdGAFile();
+  TFile *in=(TFile*)gFile;
+  TDirectory *savedir=gDirectory;
+
+  if (!in->IsOpen()) {
+    AliWarning("Geometry file is not open default  TOF geometry will be used");
+    fGeom = new AliTOFGeometryV5();
+  }
+  else {
+    in->cd();
+    fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
+  }
+
+  savedir->cd();
+
   AliLoader* outgime = outrl->GetLoader("TOFLoader");
   if (outgime == 0x0)
    {
@@ -129,7 +181,7 @@ void AliTOFDigitizer::Exec(Option_t* /*option*/)
   fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
   
   // create hit map (to be created in Init())
-  fhitMap = new AliTOFHitMap(fSDigitsArray);
+  fhitMap = new AliTOFHitMap(fSDigitsArray, fGeom);
   
   // Loop over files to digitize
 
@@ -181,7 +233,7 @@ void AliTOFDigitizer::CreateDigits()
     // TOF sdigit volumes (always the same for all slots)
     Int_t sector    = tofsdigit->GetSector(); // range [0-17]
     Int_t plate     = tofsdigit->GetPlate();  // range [0- 4]
-    Int_t strip     = tofsdigit->GetStrip();  // range [0-19]
+    Int_t strip     = tofsdigit->GetStrip();  // range [0-14/18/19]
     Int_t padz      = tofsdigit->GetPadz();   // range [0- 1]
     Int_t padx      = tofsdigit->GetPadx();   // range [0-47]
     
@@ -196,22 +248,23 @@ void AliTOFDigitizer::CreateDigits()
     Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
     
     if (isSDigitBad) {
-      AliFatal("strange sdigit found");
+      //AliFatal("strange sdigit found");
+      AliFatal(Form("strange sdigit found   %3i  %2i  %2i  %3i    %3i", sector, plate, padz, padx, strip));
     }
     //-------------------------------------------------------
     
     //------------------- Dump section ----------------------
     if(k<ndump){
-      cout << k << "-th | " << "Sector " << sector << " | Plate " << plate << " | Strip " << strip << " | PadZ " << padz << " | PadX " << padx << endl;
-      cout << k << "-th sdigit" << endl;
-      cout << "----------------------------------------------------"<< endl;
+      AliInfo(Form("%2i-th | Sector %2i | Plate %1i | Strip %2i | PadZ %1i | PadX %2i ", k, sector, plate, strip, padz, padx));
+      AliInfo(Form("%2i-th", k));
+      AliInfo("----------------------------------------------------");
     }
     // ------------------------------------------------------
     
     // start loop on number of slots for current sdigit
     for (Int_t islot = 0; islot < nslot; islot++) {
-      Float_t  digit[2];     // TOF digit variables
-      Int_t tracknum[kMAXDIGITS];     // contributing tracks for the current slot
+      Float_t  digit[4] = {-1.,-1.,-1.,-1.};     // TOF digit variables
+      Int_t tracknum[AliTOFSDigit::kMAXDIGITS];     // contributing tracks for the current slot
       
       Float_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
       Float_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
@@ -230,6 +283,12 @@ void AliTOFDigitizer::CreateDigits()
     
   } // end loop on sdigits - end digitizing all collected sdigits
 
+  //Insert Decalibration 
+
+  AliTOFcalib * calib = new AliTOFcalib(fGeom);
+  InitDecalibration(calib);
+  DecalibrateTOFSignal(calib);
+  delete calib;
 }
 
 //---------------------------------------------------------------------
@@ -344,3 +403,113 @@ void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
   // put it into tmp array
   new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
 }
+
+//_____________________________________________________________________________
+void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib) const {
+  // calib->ReadSimParFromCDB("TOF/Calib", 0); // original
+  calib->ReadSimParFromCDB("TOF/Calib", -1); // use AliCDBManager's number
+}
+//---------------------------------------------------------------------
+void AliTOFDigitizer::DecalibrateTOFSignal( AliTOFcalib *calib){
+
+  // Read Calibration parameters from the CDB
+
+  AliTOFCal * cal= calib->GetTOFCalSimArray();
+
+  AliDebug(2,Form("Size of AliTOFCal = %i",cal->NPads()));
+  for (Int_t ipad = 0 ; ipad<cal->NPads(); ipad++){
+    AliTOFChannel *calChannel = cal->GetChannel(ipad);
+    Float_t par[6];
+    for (Int_t j = 0; j<6; j++){
+      par[j]=calChannel->GetSlewPar(j);
+    }
+  }
+
+  // Initialize Quantities to Simulate ToT Spectra
+
+
+  TH1F * hToT= calib->GetTOFSimToT();
+  Int_t nbins = hToT->GetNbinsX();
+  Float_t delta = hToT->GetBinWidth(1);
+  Float_t maxch = hToT->GetBinLowEdge(nbins)+delta;
+  Float_t minch = hToT->GetBinLowEdge(1);
+  Float_t max=0,min=0; //maximum and minimum value of the distribution
+  Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
+  for (Int_t ii=nbins; ii>0; ii--){
+    if (hToT->GetBinContent(ii)!= 0) {
+      max = maxch - (nbins-ii-1)*delta;
+      maxbin = ii; 
+      break;}
+  }
+  for (Int_t j=1; j<nbins; j++){
+    if (hToT->GetBinContent(j)!= 0) {
+      min = minch + (j-1)*delta;
+      minbin = j; 
+      break;}
+  }
+  Float_t maxToT=max;
+  Float_t minToT=min;
+  Float_t maxToTDistr=hToT->GetMaximum();
+  
+
+  // Loop on TOF Digits
+
+  Bool_t dbEntry=kFALSE;
+  Int_t ndigits = fDigits->GetEntriesFast();    
+  for (Int_t i=0;i<ndigits;i++){
+    AliTOFdigit * dig = (AliTOFdigit*)fDigits->At(i);
+    Int_t detId[5];
+    detId[0] = dig->GetSector();
+    detId[1] = dig->GetPlate();
+    detId[2] = dig->GetStrip();
+    detId[3] = dig->GetPadz();
+    detId[4] = dig->GetPadx();
+    // For Data with no Miscalibration, set ToT signal == Adc
+    dig->SetToT(dig->GetAdc()/AliTOFGeometry::ToTBinWidth()*1.E3); 
+    if(hToT->GetEntries()>0){  
+      Float_t trix = 0;
+      Float_t triy = 0;
+      Float_t simToT = 0;
+      while (simToT <= triy){
+       trix = gRandom->Rndm(i);
+       triy = gRandom->Rndm(i);
+       trix = (maxToT-minToT)*trix + minToT; 
+       triy = maxToTDistr*triy;
+       Int_t binx=hToT->FindBin(trix);
+       simToT=hToT->GetBinContent(binx);
+      }
+    // Setting realistic ToT signal (only for Miscalibrated Data)   
+      dig->SetToT(trix/AliTOFGeometry::ToTBinWidth()*1.E3); //In ToT is in ns..
+    }
+    Int_t index = calib->GetIndex(detId);     
+    AliTOFChannel *calChannel = cal->GetChannel(index);
+    // time slewing parameters
+    Float_t par[6];
+    for (Int_t j = 0; j<6; j++){
+      par[j]=calChannel->GetSlewPar(j);
+      if(par[j]!=0)dbEntry=kTRUE;
+    }
+    // the global time shift
+    Float_t timedelay = calChannel->GetDelay();
+    Float_t tToT= dig->GetToT();
+    dig->SetTdcND(dig->GetTdc());
+    Float_t tdc = ((dig->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
+    // add slewing effect
+    Float_t timeoffset=par[0] + tToT*(par[1] +tToT*(par[2] +tToT*(par[3] +tToT*(par[4] +tToT*par[5])))); 
+    Float_t timeSlewed = tdc+timeoffset;
+    // add global time shift
+    timeSlewed = timeSlewed + timedelay;
+
+    // Setting Decalibrated Time signal    
+    dig->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());   
+  }
+
+  if(hToT->GetEntries()<=0 || !dbEntry){
+    AliDebug(1,"Standard Production, no miscalibrated digits");   
+  }else{
+    AliDebug(1,"Miscalibrated digits");   
+  }
+
+  return;
+}
+