]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFDigitizer.cxx
Adding the full covariance matrix for the TOF space-points
[u/mrichter/AliRoot.git] / TOF / AliTOFDigitizer.cxx
index 5a816dc1790ce3cd4baaf16717e62f2cb4b70413..0f505e99eabdb1398ffa3a7f53bcca13f4cba25f 100644 (file)
 // -- Author :  F. Pierella (Bologna University) pierella@bo.infn.it
 //////////////////////////////////////////////////////////////////////////////
 
+
+#include <stdlib.h>
+#include <Riostream.h>
 #include <TTree.h> 
 #include <TVector.h>
 #include <TObjArray.h>
 #include <TFile.h>
 #include <TDirectory.h>
 #include <TRandom.h>
+#include "TF1.h"
+#include "TH1F.h"
+#include "TList.h"
+#include "TRandom.h"
 
+#include "AliLog.h"
+#include "AliRun.h"
+#include "AliRunLoader.h"
+#include "AliLoader.h"
+#include "AliDigitizer.h"
+#include "AliRunDigitizer.h"
+#include "AliPDG.h"
 
-#include "AliTOFDigitizer.h"
 #include "AliTOF.h"
+#include "AliTOFDigitizer.h"
 #include "AliTOFSDigitizer.h"
 #include "AliTOFhit.h"
 #include "AliTOFdigit.h"
 #include "AliTOFSDigit.h"
 #include "AliTOFHitMap.h"
-#include "AliDigitizer.h"
-#include "AliRunDigitizer.h"
-
-#include "AliRun.h"
-#include "AliPDG.h"
-
-#include <stdlib.h>
-#include <iostream.h>
-#include <fstream.h>
+#include "AliTOFChannel.h"
+#include "AliTOFCal.h"
+#include "AliTOFGeometryV5.h"
 
 ClassImp(AliTOFDigitizer)
 
@@ -57,15 +65,18 @@ ClassImp(AliTOFDigitizer)
   fDigits=0;
   fSDigitsArray=0;
   fhitMap=0;
+  fGeom=0x0; 
 }
 
 //___________________________________________
 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager) 
     :AliDigitizer(manager) 
 {
+  //ctor with RunDigitizer
   fDigits=0;
   fSDigitsArray=0;
   fhitMap=0;
+  fGeom=0x0; 
 }
 
 //------------------------------------------------------------------------
@@ -76,7 +87,7 @@ AliTOFDigitizer::~AliTOFDigitizer()
 
 //---------------------------------------------------------------------
 
-void AliTOFDigitizer::Exec(Option_t* option)
+void AliTOFDigitizer::Exec(Option_t* /*option*/)
 {
   //
   // Perform digitization and merging.
@@ -88,7 +99,7 @@ void AliTOFDigitizer::Exec(Option_t* option)
   // - the sdigits container is used to create the array of AliTOFdigit.
   //
 
-  if(strstr(option,"deb")) cout<<"AliTOFDigitizer::Exec\n";
+  AliDebug(1, "");
 
 
   // get the ptr to TOF detector
@@ -99,8 +110,42 @@ void AliTOFDigitizer::Exec(Option_t* option)
   sprintf (branchname, "%s", tof->GetName ());
 
   fDigits=new TClonesArray("AliTOFdigit",4000);
+  AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
+  if (outrl == 0x0)
+   {
+     AliError("Can not find Run Loader in output folder.");
+     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();
 
-  TTree* treeD = fManager->GetTreeD();
+  AliLoader* outgime = outrl->GetLoader("TOFLoader");
+  if (outgime == 0x0)
+   {
+     AliError("Can not get TOF Loader from Output Run Loader.");
+     return;
+   }
+  
+  TTree* treeD = outgime->TreeD();
+  if (treeD == 0x0)
+   {
+     outgime->MakeTree("D");
+     treeD = outgime->TreeD();
+   }
   //Make branch for digits (to be created in Init())
   tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
 
@@ -108,14 +153,14 @@ 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
 
   for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
        inputFile++) {
     ReadSDigit(inputFile);
-  }
+   }
 
   // create digits
   CreateDigits();
@@ -123,9 +168,12 @@ void AliTOFDigitizer::Exec(Option_t* option)
   // free used memory for Hit Map in current event
   delete fhitMap;
   fSDigitsArray->Delete();
+  delete fSDigitsArray;
+
   treeD->Fill();
  
-  fManager->GetTreeD()->AutoSave(); // to fit with the framework
+  outgime->WriteDigits("OVERWRITE");
+  outgime->UnloadDigits();
   fDigits->Delete();
   delete fDigits;
 
@@ -138,14 +186,15 @@ void AliTOFDigitizer::CreateDigits()
   // loop on sdigits container to fill the AliTOFdigit TClonesArray
   // start digitizing all the collected sdigits 
 
-  Int_t ndump=15; // dump the first ndump created digits for each event
+  Int_t ndump=0; // dump the first ndump created digits for each event
 
   // get the total number of collected sdigits
   Int_t ndig = fSDigitsArray->GetEntriesFast();
 
   for (Int_t k = 0; k < ndig; k++) {
     
-    Int_t    vol[5];       // location for a digit
+    Int_t  vol[5];  // location for a digit
+    for (Int_t i=0; i<5; i++) vol[i] = -1;
     
     // Get the information for this digit
     AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
@@ -154,11 +203,11 @@ void AliTOFDigitizer::CreateDigits()
     // for current sdigit
     
     // TOF sdigit volumes (always the same for all slots)
-    Int_t sector    = tofsdigit->GetSector(); // range [1-18]
-    Int_t plate     = tofsdigit->GetPlate();  // range [1- 5]
-    Int_t strip     = tofsdigit->GetStrip();  // range [1-20]
-    Int_t padz      = tofsdigit->GetPadz();   // range [1- 2]
-    Int_t padx      = tofsdigit->GetPadx();   // range [1-48]
+    Int_t sector    = tofsdigit->GetSector(); // range [0-17]
+    Int_t plate     = tofsdigit->GetPlate();  // range [0- 4]
+    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]
     
     vol[0] = sector;
     vol[1] = plate;
@@ -168,11 +217,11 @@ void AliTOFDigitizer::CreateDigits()
     
     //--------------------- QA section ----------------------
     // in the while, I perform QA
-    Bool_t isSDigitBad = (sector<1 || sector>18 || plate<1 || plate >5 || padz<1 || padz>2 || padx<1 || padx>48);
+    Bool_t isSDigitBad = (sector<0 || sector>17 || plate<0 || plate >4 || padz<0 || padz>1 || padx<0 || padx>47);
     
     if (isSDigitBad) {
-      cout << "<AliTOFSDigits2Digits>  strange sdigit found" << endl;
-      abort();
+      //AliFatal("strange sdigit found");
+      AliFatal(Form("strange sdigit found   %3i  %2i  %2i  %3i    %3i", sector, plate, padz, padx, strip));
     }
     //-------------------------------------------------------
     
@@ -196,14 +245,22 @@ void AliTOFDigitizer::CreateDigits()
       tracknum[1]=tofsdigit->GetTrack(islot,1);
       tracknum[2]=tofsdigit->GetTrack(islot,2);
       
-      
+      // new with placement must be used
       // adding a TOF digit for each slot
-      AliTOFdigit* newDigit=new AliTOFdigit(tracknum, vol, digit);
-      fDigits->Add(newDigit);
+      TClonesArray &aDigits = *fDigits;
+      Int_t last=fDigits->GetEntriesFast();
+      new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
+
     }
     
   } // end loop on sdigits - end digitizing all collected sdigits
 
+  //Insert Decalibration 
+
+  AliTOFcalib * calib = new AliTOFcalib(fGeom);
+  InitDecalibration(calib);
+  DecalibrateTOFSignal(calib);
+  delete calib;
 }
 
 //---------------------------------------------------------------------
@@ -217,8 +274,36 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
   // be created with the same simulation parameters.
   
   // get the treeS from manager
-  TTree* currentTreeS=fManager->GetInputTreeS(inputFile);
-  
+  AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
+  if (rl == 0x0)
+   {
+     AliError(Form("Can not find Run Loader in input %d folder.",inputFile));
+     return;
+   }
+
+  AliLoader* gime = rl->GetLoader("TOFLoader");
+  if (gime == 0x0)
+   {
+     AliError(Form("Can not get TOF Loader from Input %d Run Loader.",inputFile));
+     return;
+   }
+
+  TTree* currentTreeS=gime->TreeS();
+  if (currentTreeS == 0x0)
+   {
+     Int_t retval = gime->LoadSDigits();
+     if (retval) 
+      {
+         AliError(Form("Error occured while loading S. Digits for Input %d",inputFile));
+         return;
+      }
+     currentTreeS=gime->TreeS();
+     if (currentTreeS == 0x0)
+      {
+         AliError(Form("Can not get S. Digits Tree for Input %d",inputFile));
+         return;
+      }
+   } 
   // get the branch TOF inside the treeS
   TClonesArray * sdigitsDummyContainer= new TClonesArray("AliTOFSDigit",  1000); 
 
@@ -226,7 +311,7 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
   TBranch* tofBranch=currentTreeS->GetBranch("TOF");
 
   if(!tofBranch){
-    Fatal("ReadSDigit","TOF branch not found for input %d",inputFile);
+    AliFatal(Form("TOF branch not found for input %d",inputFile));
   }
   
   tofBranch->SetAddress(&sdigitsDummyContainer);           
@@ -234,7 +319,7 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
   Int_t nEntries = (Int_t)tofBranch->GetEntries();                                
 
   // Loop through all entries in the tree
-  Int_t nbytes;
+  Int_t nbytes = 0;
   
   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
     
@@ -247,8 +332,9 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
     for (Int_t k=0; k<ndig; k++) {
       AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
       
-      Int_t    vol[5];       // location for a sdigit
-      
+      Int_t  vol[5]; // location for a sdigit
+      for (Int_t i=0; i<5; i++) vol[i] = -1;
+
       // check the sdigit volume
       vol[0] = tofSdigit->GetSector();
       vol[1] = tofSdigit->GetPlate();
@@ -268,11 +354,11 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
       } // if (hitMap->TestHit(vol) != kEmpty)
       
     } // for (Int_t k=0; k<ndig; k++)
+    sdigitsDummyContainer->Delete();
 
   } // end loop on entries
 
-  sdigitsDummyContainer->Delete();
-  sdigitsDummyContainer=0;
+  delete sdigitsDummyContainer;
 
 }
 
@@ -281,8 +367,114 @@ void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
 {
   //
-  // Add a TOF digit
-  // new with placement used
-  AliTOFSDigit sdigitCopy=AliTOFSDigit(*sdigit); // make a copy of the current sdigit
-  fSDigitsArray->Add(&sdigitCopy); // put it into tmp array
+  // Add a TOF sdigit in container
+  // new with placement must be used
+  TClonesArray &aSDigitsArray = *fSDigitsArray;
+  Int_t last=fSDigitsArray->GetEntriesFast();
+  // make a copy of the current sdigit and
+  // put it into tmp array
+  new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
 }
+
+//_____________________________________________________________________________
+void AliTOFDigitizer::InitDecalibration( AliTOFcalib *calib) const {
+  calib->ReadSimParFromCDB("TOF/Calib", 0);
+}
+//---------------------------------------------------------------------
+void AliTOFDigitizer::DecalibrateTOFSignal( AliTOFcalib *calib){
+
+  // Read Calibration parameters from the CDB
+
+  AliTOFCal * cal= calib->GetTOFCalSimArray();
+
+  AliInfo(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());
+    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);
+    }
+    Int_t index = calib->GetIndex(detId);     
+    AliTOFChannel *calChannel = cal->GetChannel(index);
+    Float_t par[6];
+    for (Int_t j = 0; j<6; j++){
+      par[j]=calChannel->GetSlewPar(j);
+      if(par[j]!=0)dbEntry=kTRUE;
+    }
+
+    Float_t tToT= dig->GetToT();
+    dig->SetTdcND(dig->GetTdc());
+    Float_t tdc = ((dig->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
+    Float_t timeoffset=par[0] + tToT*par[1] +tToT*tToT*par[2] +tToT*tToT*tToT*par[3] +tToT*tToT*tToT*tToT*par[4] +tToT*tToT*tToT*tToT*tToT*par[5]; 
+    Float_t timeSlewed = tdc + timeoffset;
+    // Setting Decalibrated Time signal    
+    dig->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());   
+  }
+
+  if(hToT->GetEntries()<=0 || !dbEntry){
+    AliInfo("Standard Production, no miscalibrated digits");   
+  }else{
+    AliInfo("Miscalibrated digits");   
+  }
+
+  return;
+}
+