#include <Riostream.h>
#include <stdlib.h>
#include <TNamed.h>
#include "TROOT.h"
#include "TFile.h"
#include "TNetFile.h"
#include "TRandom.h"
#include "TTree.h"
#include "TBranch.h"
#include "TClonesArray.h"
#include "TGeoGlobalMagField.h"
#include "AliMagF.h"
#include "TStopwatch.h"
#include "TParameter.h"
#include "TF1.h"
#include "AliRun.h"
#include "AliMC.h"
#include "AliAD.h"
#include "AliADhit.h"
#include "AliADLoader.h"
#include "AliADDigitizer.h"
#include "AliADBuffer.h"
#include "AliADConst.h"
#include "AliDigitizationInput.h"
#include "AliADdigit.h"
#include "AliADSDigit.h"
#include "AliADCalibData.h"
#include "AliDAQ.h"
#include "AliRawReader.h"
#include "AliCDBManager.h"
#include "AliCDBEntry.h"
#include "AliADReconstructor.h"
ClassImp(AliAD)
AliAD::AliAD()
: AliDetector(),
fSetADAToInstalled(0),
fSetADCToInstalled(0)
{
}
AliAD::AliAD(const char *name, const char *title)
: AliDetector(name,title),
fSetADAToInstalled(kTRUE),
fSetADCToInstalled(kTRUE)
{
}
AliAD::~AliAD()
{
}
void AliAD::CreateMaterials()
{
Int_t fieldType = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Integ();
Double_t maxField = ((AliMagF*)TGeoGlobalMagField::Instance()->GetField())->Max();
Double_t maxBending = 10;
Double_t maxStepSize = 0.01;
Double_t maxEnergyLoss = 1;
Double_t precision = 0.003;
Double_t minStepSize = 0.003;
Float_t density, as[11], zs[11], ws[11];
Double_t radLength, absLength, a_ad, z_ad;
Int_t id;
as[0] = 1.00794; as[1] = 12.011;
zs[0] = 1.; zs[1] = 6.;
ws[0] = 5.23; ws[1] = 4.74;
density = 1.032;
id = 1;
AliMixture( id, "NE102", as, zs, density, -2, ws );
AliMedium( id, "NE102", id, 1, fieldType, maxField, maxBending, maxStepSize,
maxEnergyLoss, precision, minStepSize );
as[0] = 12.0107; as[1] = 1.00794; as[2] = 15.9994;
zs[0] = 6.; zs[1] = 1.; zs[2] = 8.;
ws[0] = 0.60; ws[1] = 0.081; ws[2] = 0.32;
density = 1.18;
id = 2;
AliMixture( id, "PMMA", as, zs, density, 3, ws );
AliMedium( id,"PMMA", id, 1, fieldType, maxField, maxBending, maxStepSize,
maxEnergyLoss, precision, minStepSize );
as[0] = 58.6934; as[1] = 55.845; as[2] = 95.94; as[3] = 54.9380;
zs[0] = 28.; zs[1] = 26.; zs[2] = 42.; zs[3] = 25.;
ws[0] = 0.802; ws[1] = 0.14079; ws[2] = 0.0485; ws[3] = 0.005;
as[4] = 28.0855; as[5] = 51.9961; as[6] = 58.9332; as[7] = 26.981539;
zs[4] = 14.; zs[5] = 24.; zs[6] = 27.; zs[7] = 13.;
ws[4] = 0.003; ws[5] = 0.0002; ws[6] = 0.0002; ws[7] = 0.0001;
as[8] = 12.0107; as[9] = 30.97376; as[10] = 32.066;
zs[8] = 6.; zs[9] = 15.; zs[10] = 16.;
ws[8] = 0.00015; ws[9] = 0.00005; ws[10] = 0.00001;
density = 8.25;
id = 3;
AliMixture( id, "MuMetal", as, zs, density, 11, ws );
AliMedium( id,"MuMetal", id, 1, fieldType, maxField, maxBending, maxStepSize,
maxEnergyLoss, precision, minStepSize );
a_ad = 26.98;
z_ad = 13.00;
density = 2.7;
radLength = 8.9;
absLength = 37.2;
id = 4;
AliMaterial (id, "Alum", a_ad, z_ad, density, radLength, absLength, 0, 0 );
AliMedium( id, "Alum", id, 1, fieldType, maxField, maxBending, maxStepSize,
maxEnergyLoss, precision, minStepSize );
a_ad = 26.98;
z_ad = 13.00;
density = 2.7;
radLength = 8.9;
absLength = 37.2;
id = 5;
AliMaterial( id, "Glass", a_ad, z_ad, density, radLength, absLength, 0, 0 );
AliMedium( id, "Glass", id, 1, fieldType, maxField, maxBending, maxStepSize,
maxEnergyLoss, precision, minStepSize );
}
void AliAD::SetTreeAddress()
{
TBranch *branch;
char branchname[20];
snprintf(branchname,19,"%s",GetName());
TTree *treeH = fLoader->TreeH();
if (treeH )
{
branch = treeH->GetBranch(branchname);
if (branch) branch->SetAddress(&fHits);
}
}
void AliAD::MakeBranch(Option_t* opt)
{
const char* oH = strstr(opt,"H");
if (fLoader->TreeH() && oH && (fHits==0x0))
{
fHits = new TClonesArray("AliADhit",1000);
fNhits = 0;
}
AliDetector::MakeBranch(opt);
}
AliLoader* AliAD::MakeLoader(const char* topfoldername)
{
AliDebug(1,Form("Creating AliADLoader, Top folder is %s ",topfoldername));
fLoader = new AliADLoader(GetName(),topfoldername);
return fLoader;
}
AliDigitizer* AliAD::CreateDigitizer(AliDigitizationInput* digInput) const
{
return new AliADDigitizer(digInput);
}
void AliAD::Hits2Digits(){
AliADDigitizer* dig = new AliADDigitizer(this,AliADDigitizer::kHits2Digits);
dig->Digitize("");
delete dig;
}
void AliAD::Hits2SDigits(){
AliADDigitizer* dig = new AliADDigitizer(this,AliADDigitizer::kHits2SDigits);
dig->Digitize("");
delete dig;
}
void AliAD::Digits2Raw()
{
AliAD *fAD = (AliAD*)gAlice->GetDetector("AD");
fLoader->LoadDigits();
TTree* digits = fLoader->TreeD();
if (!digits) {
Error("Digits2Raw", "no digits tree");
return;
}
TClonesArray * ADdigits = new TClonesArray("AliADdigit",1000);
fAD->SetTreeAddress();
digits->GetBranch("ADDigit")->SetAddress(&ADdigits);
const char *fileName = AliDAQ::DdlFileName("AD",0);
AliADBuffer* buffer = new AliADBuffer(fileName);
AliDataLoader * dataLoader = fLoader->GetDigitsDataLoader();
if( !dataLoader->IsFileOpen() )
dataLoader->OpenFile( "READ" );
AliTriggerDetector* trgdet = (AliTriggerDetector*)dataLoader->GetDirectory()->Get( "Trigger" );
UInt_t triggerInfo = 0;
if(trgdet) {
triggerInfo = trgdet->GetMask() & 0xffff;
}
else {
AliError(Form("There is no trigger object for %s",fLoader->GetName()));
}
buffer->WriteTriggerInfo((UInt_t)triggerInfo);
buffer->WriteTriggerScalers();
buffer->WriteBunchNumbers();
Int_t nEntries = Int_t(digits->GetEntries());
Short_t aADC[16][kNClocks];
Float_t aTime[16];
Float_t aWidth[16];
Bool_t aIntegrator[16];
for (Int_t i = 0; i < nEntries; i++) {
fAD->ResetDigits();
digits->GetEvent(i);
Int_t ndig = ADdigits->GetEntriesFast();
if(ndig == 0) continue;
for(Int_t k=0; k<ndig; k++){
AliADdigit* fADDigit = (AliADdigit*) ADdigits->At(k);
Int_t iChannel = fADDigit->PMNumber();
for(Int_t iClock = 0; iClock < kNClocks; ++iClock) aADC[iChannel][iClock] = fADDigit->ChargeADC(iClock);
aTime[iChannel] = fADDigit->Time();
aWidth[iChannel] = fADDigit->Width();
aIntegrator[iChannel]= fADDigit->Integrator();
}
}
for (Int_t iCIU = 0; iCIU < kNCIUBoards; iCIU++) {
for(Int_t iChannel_Offset = iCIU*8; iChannel_Offset < (iCIU*8)+8; iChannel_Offset=iChannel_Offset+4) {
for(Int_t iChannel = iChannel_Offset; iChannel < iChannel_Offset+4; iChannel++) {
buffer->WriteChannel(iChannel, aADC[iChannel], aIntegrator[iChannel]);
}
buffer->WriteBeamFlags();
buffer->WriteMBInfo();
buffer->WriteMBFlags();
buffer->WriteBeamScalers();
}
for(Int_t iChannel = iCIU*8 + 7; iChannel >= iCIU*8; iChannel--) {
buffer->WriteTiming(aTime[iChannel], aWidth[iChannel]);
}
}
delete buffer;
fLoader->UnloadDigits();
}
Bool_t AliAD::Raw2SDigits(AliRawReader* )
{
return kTRUE;
}