/*
<img src="picts/AliTOFClass.gif">
*/
//End_Html
#include <TClonesArray.h>
#include <TFile.h>
#include <TFolder.h>
#include <TROOT.h>
#include <TTree.h>
#include <TVirtualMC.h>
#include <TStopwatch.h>
#include "AliConst.h"
#include "AliLoader.h"
#include "AliLog.h"
#include "AliMC.h"
#include "AliRun.h"
#include "AliDAQ.h"
#include "AliRawReader.h"
#include "AliTOFDigitizer.h"
#include "AliTOFdigit.h"
#include "AliTOFhitT0.h"
#include "AliTOFhit.h"
#include "AliTOFGeometry.h"
#include "AliTOFSDigitizer.h"
#include "AliTOFSDigit.h"
#include "AliTOF.h"
#include "AliTOFrawData.h"
#include "AliTOFRawStream.h"
class AliTOFcluster;
ClassImp(AliTOF)
AliTOF::AliTOF():
fFGeom(0x0),
fSDigits(0x0),
fNSDigits(0),
fReconParticles(0x0),
fIdSens(-1),
fTZero(kFALSE),
fTOFHoles(kTRUE),
fTOFGeometry(0x0),
fTOFRawWriter(AliTOFDDLRawData())
{
for (Int_t ii=0; ii<18; ii++) fTOFSectors[ii]=0;
fDigits = 0;
fIshunt = 0;
fName = "TOF";
}
AliTOF::AliTOF(const char *name, const char *title, Option_t *option)
:
AliDetector(name,title),
fFGeom(0x0),
fSDigits(0x0),
fNSDigits(0),
fReconParticles(0x0),
fIdSens(-1),
fTZero(kFALSE),
fTOFHoles(kTRUE),
fTOFGeometry(0x0),
fTOFRawWriter(AliTOFDDLRawData())
{
fTOFGeometry = new AliTOFGeometry();
for (Int_t ii=0; ii<18; ii++) fTOFSectors[ii]=0;
if (strstr(option,"tzero")){
fHits = new TClonesArray("AliTOFhitT0", 1000);
fTZero = kTRUE;
}else{
fHits = new TClonesArray("AliTOFhit", 1000);
fTZero = kFALSE;
}
if (gAlice==0) {
AliFatal("gAlice==0 !");
}
AliMC *mcApplication = (AliMC*)gAlice->GetMCApp();
if (mcApplication->GetHitLists())
mcApplication->AddHitList(fHits);
else AliError("gAlice->GetHitLists()==0");
fIshunt = 0;
fSDigits = new TClonesArray("AliTOFSDigit", 1000);
fDigits = new TClonesArray("AliTOFdigit", 1000);
}
void AliTOF::SetTOFSectors(Int_t * const sectors)
{
for(Int_t isec=0;isec<18;isec++){
fTOFSectors[isec]=sectors[isec];
}
}
void AliTOF::GetTOFSectors(Int_t *sectors) const
{
for(Int_t isec=0;isec<18;isec++){
sectors[isec]=fTOFSectors[isec];
}
}
void AliTOF::CreateTOFFolders()
{
TFolder * alice = new TFolder();
alice->SetNameTitle("FPAlice", "Alice Folder") ;
gROOT->GetListOfBrowsables()->Add(alice) ;
TFolder * aliceF = alice->AddFolder("folders", "Alice memory Folder") ;
aliceF->SetOwner() ;
TFolder * geomF = aliceF->AddFolder("Geometry", "Geometry objects") ;
geomF->AddFolder("TOF", "Geometry for TOF") ;
}
AliTOF::~AliTOF()
{
if (fHits)
{
fHits->Delete ();
delete fHits;
fHits = 0;
}
if (fDigits)
{
fDigits->Delete ();
delete fDigits;
fDigits = 0;
}
if (fSDigits)
{
fSDigits->Delete();
delete fSDigits;
fSDigits = 0;
}
if (fReconParticles)
{
fReconParticles->Delete ();
delete fReconParticles;
fReconParticles = 0;
}
}
void AliTOF::AddHit(Int_t track, Int_t *vol, Float_t *hits)
{
TClonesArray &lhits = *fHits;
new(lhits[fNhits++]) AliTOFhit(fIshunt, track, vol, hits);
}
void AliTOF::AddT0Hit(Int_t track, Int_t *vol, Float_t *hits)
{
TClonesArray &lhits = *fHits;
new(lhits[fNhits++]) AliTOFhitT0(fIshunt, track, vol, hits);
}
void AliTOF::AddDigit(Int_t *tracks, Int_t *vol, Int_t *digits)
{
TClonesArray &ldigits = *fDigits;
new (ldigits[fNdigits++]) AliTOFdigit(tracks, vol, digits);
}
void AliTOF::AddSDigit(Int_t tracknum, Int_t *vol, Int_t *digits)
{
TClonesArray &lSDigits = *fSDigits;
new(lSDigits[fNSDigits++]) AliTOFSDigit(tracknum, vol, digits);
}
void AliTOF::SetTreeAddress ()
{
if (fLoader->TreeH())
{
if (fHits == 0x0)
{
if (fTZero) fHits = new TClonesArray("AliTOFhitT0", 1000);
else fHits = new TClonesArray("AliTOFhit", 1000);
}
}
AliDetector::SetTreeAddress ();
TBranch *branch;
if (fLoader->TreeS () )
{
branch = fLoader->TreeS ()->GetBranch ("TOF");
if (branch) {
if (fSDigits == 0x0) fSDigits = new TClonesArray("AliTOFSDigit", 1000);
branch->SetAddress (&fSDigits);
}
}
if (fLoader->TreeR() )
{
branch = fLoader->TreeR()->GetBranch("TOF");
if (branch)
{
if (fReconParticles == 0x0) fReconParticles = new TClonesArray("AliTOFcluster", 1000);
branch->SetAddress(&fReconParticles);
}
}
}
void AliTOF::CreateGeometry()
{
/*
<img src="picts/AliTOFv23.gif">
*/
//End_Html
Float_t xTof, yTof;
if (IsVersion()==8) {
xTof = 124.5;
yTof = fTOFGeometry->Rmax()-fTOFGeometry->Rmin();
Float_t zTof = fTOFGeometry->ZlenA();
TOFpc(xTof, yTof, zTof);
} else if (IsVersion()==7) {
xTof = 124.5;
yTof = fTOFGeometry->Rmax()-fTOFGeometry->Rmin();
Float_t zTof = fTOFGeometry->ZlenA();
TOFpc(xTof, yTof, zTof, fTOFGeometry->ZlenB());
} else {
Float_t wall = 4.;
xTof = 2.*(fTOFGeometry->Rmin()*TMath::Tan(10.*kDegrad)-wall/2.-0.5);
yTof = fTOFGeometry->Rmax()-fTOFGeometry->Rmin();
TOFpc(xTof, yTof, fTOFGeometry->ZlenC(), fTOFGeometry->ZlenB(), fTOFGeometry->ZlenA(), fTOFGeometry->MaxhZtof());
}
}
void AliTOF::ResetHits ()
{
AliDetector::ResetHits ();
}
void AliTOF::ResetDigits ()
{
AliDetector::ResetDigits ();
}
void AliTOF::ResetSDigits ()
{
fNSDigits = 0;
}
void AliTOF::Init()
{
if (IsVersion() !=0) fIdSens=TVirtualMC::GetMC()->VolId("FPAD");
}
void AliTOF::MakeBranch(Option_t* option)
{
const char *oH = strstr(option,"H");
if (fLoader->TreeH() && oH)
{
if (fHits == 0x0)
{
if (fTZero) fHits = new TClonesArray("AliTOFhitT0", 1000);
else fHits = new TClonesArray("AliTOFhit", 1000);
}
}
AliDetector::MakeBranch(option);
Int_t buffersize = 4000;
const Int_t kSize=10;
Char_t branchname[kSize];
snprintf(branchname,kSize,"%s",GetName());
const char *oD = strstr(option,"D");
const char *oS = strstr(option,"S");
const char *oR = strstr(option,"R");
if (fLoader->TreeD() && oD){
if (fDigits == 0x0) fDigits = new TClonesArray("AliTOFdigit", 1000);
MakeBranchInTree(fLoader->TreeD(), branchname, &fDigits,buffersize, 0) ;
}
if (fLoader->TreeS() && oS){
if (fSDigits == 0x0) fSDigits = new TClonesArray("AliTOFSDigit", 1000);
MakeBranchInTree(fLoader->TreeS(), branchname, &fSDigits,buffersize, 0) ;
}
if (fLoader->TreeR() && oR){
if (fReconParticles == 0x0) fReconParticles = new TClonesArray("AliTOFcluster", 1000);
MakeBranchInTree(fLoader->TreeR(), branchname, &fReconParticles,buffersize, 0) ;
}
}
void AliTOF::Makehits(Bool_t hits)
{
if (hits && (IsVersion()!=0))
fIdSens = TVirtualMC::GetMC()->VolId("FPAD");
else
AliInfo("Option for writing the TOF-hits branch on TreeH: disabled");
}
void AliTOF::FinishEvent()
{
}
void AliTOF::Hits2SDigits()
{
AliRunLoader * rl = fLoader->GetRunLoader();
AliDebug(2,"Initialized runLoader");
AliTOFSDigitizer sd((rl->GetFileName()).Data());
AliDebug(2,"Initialized TOF sdigitizer");
sd.Digitize("partial") ;
AliDebug(2,"I am sorting from AliTOF class");
}
void AliTOF::Hits2SDigits(Int_t evNumber1, Int_t evNumber2)
{
if ((evNumber2-evNumber1)==1)
AliDebug(1, Form("I am making sdigits for the %dth event", evNumber1));
if ((evNumber2-evNumber1)>1)
AliDebug(1, Form("I am making sdigits for the events from the %dth to the %dth", evNumber1, evNumber2-1));
AliRunLoader * rl = fLoader->GetRunLoader();
AliTOFSDigitizer sd((rl->GetFileName()).Data(),evNumber1,evNumber2) ;
ToAliDebug(1, sd.Print(""));
sd.Digitize("") ;
}
AliDigitizer* AliTOF::CreateDigitizer(AliDigitizationInput* digInput) const
{
AliDebug(2,"I am creating the TOF digitizer");
return new AliTOFDigitizer(digInput);
}
Bool_t AliTOF::CheckOverlap(const Int_t * const vol,
Int_t* digit,Int_t Track)
{
Bool_t overlap = kFALSE;
Int_t vol2[5];
for (Int_t ndig=0; ndig<fSDigits->GetEntries(); ndig++){
AliTOFdigit* currentDigit = (AliTOFdigit*)(fSDigits->UncheckedAt(ndig));
currentDigit->GetLocation(vol2);
Bool_t idem= kTRUE;
for (Int_t i=0;i<=4;i++){
if (!idem) break;
if (vol[i]!=vol2[i]) idem=kFALSE;}
if (idem){
Int_t tdc2 = digit[0];
Int_t tdc1 = currentDigit->GetTdc();
if (TMath::Abs(tdc1-tdc2)<25000){
if (tdc1>tdc2){
currentDigit->SetTdc(tdc2);
currentDigit->SetAdc(digit[1]);
}
else {
currentDigit->SetTdc(tdc1);
currentDigit->SetAdc(digit[1]);
}
currentDigit->AddTrack(Track);
overlap = kTRUE;
return overlap;
} else
overlap= kFALSE;
}
}
return overlap;
}
void AliTOF::Digits2Raw()
{
TStopwatch stopwatch;
stopwatch.Start();
fLoader->LoadDigits();
TTree* digits = fLoader->TreeD();
if (!digits) {
AliError("no digits tree");
return;
}
fTOFRawWriter.Clear();
fTOFRawWriter.SetVerbose(0);
if (fTOFRawWriter.GetPackedAcquisitionMode()) {
if(fTOFRawWriter.GetMatchingWindow()>8192)
AliWarning(Form("You are running in packing mode and the matching window is %.2f ns, i.e. greater than 199.8848 ns",
fTOFRawWriter.GetMatchingWindow()*AliTOFGeometry::TdcBinWidth()*1.e-03));
}
AliDebug(1,"Formatting raw data for TOF");
digits->GetEvent(0);
fTOFRawWriter.RawDataTOF(digits->GetBranch("TOF"));
fLoader->UnloadDigits();
AliDebug(1, Form("Execution time to write TOF raw data : R:%.2fs C:%.2fs",
stopwatch.RealTime(),stopwatch.CpuTime()));
}
void AliTOF::RecreateSDigitsArray() {
fSDigits->Clear();
}
void AliTOF::CreateSDigitsArray() {
fSDigits = new TClonesArray("AliTOFSDigit", 1000);
}
Bool_t AliTOF::Raw2SDigits(AliRawReader* rawReader)
{
TStopwatch stopwatch;
stopwatch.Start();
if(!GetLoader()->TreeS()) {MakeTree("S"); MakeBranch("S");}
AliTOFRawStream tofRawStream = AliTOFRawStream();
tofRawStream.Raw2SDigits(rawReader, fSDigits);
GetLoader()->TreeS()->Fill(); GetLoader()->WriteSDigits("OVERWRITE");
Int_t nSDigits = fSDigits->GetEntries();
ResetSDigits();
AliDebug(1, Form("Got %d TOF sdigits", nSDigits));
AliDebug(1, Form("Execution time to read TOF raw data and fill TOF sdigit tree : R:%.2fs C:%.2fs",
stopwatch.RealTime(),stopwatch.CpuTime()));
return kTRUE;
}
void AliTOF::Raw2Digits(AliRawReader* rawReader)
{
TStopwatch stopwatch;
stopwatch.Start();
if(!GetLoader()->TreeD()) {MakeTree("D"); MakeBranch("D");}
AliTOFRawStream tofRawStream = AliTOFRawStream();
tofRawStream.Raw2Digits(rawReader, fDigits);
GetLoader()->TreeD()->Fill(); GetLoader()->WriteDigits("OVERWRITE");
Int_t nDigits = fDigits->GetEntries();
ResetDigits();
AliDebug(1, Form("Got %d TOF digits", nDigits));
AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
stopwatch.RealTime(),stopwatch.CpuTime()));
}