#include <fstream>
#include <TString.h>
#include <TFile.h>
#include <TTree.h>
#include "AliEMCALBiasAPD.h"
using namespace std;
ClassImp(AliEMCALBiasAPD)
AliEMCALBiasAPD::AliEMCALBiasAPD(const int nSM) :
fNSuperModule(nSM),
fSuperModuleData()
{
for (int i=0; i<fNSuperModule; i++) {
fSuperModuleData.Add(new AliEMCALSuperModuleBiasAPD(i));
}
fSuperModuleData.Compress();
fSuperModuleData.SetOwner(kTRUE);
}
void AliEMCALBiasAPD::ReadTextBiasAPDInfo(Int_t nSM, const TString &txtFileName,
Bool_t swapSides)
{
std::ifstream inputFile(txtFileName.Data());
if (!inputFile) {
printf("AliEMCALBiasAPD::ReadBiasAPDInfo - Cannot open the APD info file %s\n", txtFileName.Data());
return;
}
fNSuperModule = nSM;
Int_t iSM = 0;
Int_t iCol = 0;
Int_t iRow = 0;
Int_t iElecId = 0;
Int_t iDAC = 0;
Float_t voltage = 0;
Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
for (Int_t i = 0; i < fNSuperModule; i++) {
AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[i];
if (!inputFile) {
printf("AliEMCALBiasAPD::ReadBiasAPDInfo - Error while reading input file; likely EOF..\n");
return;
}
inputFile >> iSM;
t->SetSuperModuleNum(iSM);
for (Int_t j=0; j<nAPDPerSM; j++) {
inputFile >> iCol >> iRow >> iElecId >> iDAC >> voltage;
if (iCol<0 || iCol>(AliEMCALGeoParams::fgkEMCALCols-1) ||
iRow<0 || iRow>(AliEMCALGeoParams::fgkEMCALRows-1) ) {
printf("AliEMCALBiasAPD::ReadBiasAPDInfo - Error while reading input file; j %d iCol %d iRow %d\n", j, iCol, iRow);
return;
}
if (swapSides) {
iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
}
t->SetElecId(iCol, iRow, iElecId);
t->SetDAC(iCol, iRow, iDAC);
t->SetVoltage(iCol, iRow, voltage);
}
}
inputFile.close();
return;
}
void AliEMCALBiasAPD::WriteTextBiasAPDInfo(const TString &txtFileName,
Bool_t swapSides)
{
std::ofstream outputFile(txtFileName.Data());
if (!outputFile) {
printf("AliEMCALBiasAPD::WriteBiasAPDInfo - Cannot open the APD output file %s\n", txtFileName.Data());
return;
}
Int_t iCol = 0;
Int_t iRow = 0;
Int_t iElecId = 0;
Int_t iDAC = 0;
Float_t voltage = 0;
Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
for (Int_t i = 0; i < fNSuperModule; i++) {
AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[i];
outputFile << t->GetSuperModuleNum() << endl;
for (Int_t j=0; j<nAPDPerSM; j++) {
iCol = j / AliEMCALGeoParams::fgkEMCALRows;
iRow = j % AliEMCALGeoParams::fgkEMCALRows;
iElecId = t->GetElecId(iCol, iRow);
iDAC = t->GetDAC(iCol, iRow);
voltage = t->GetVoltage(iCol, iRow);
if (swapSides) {
iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
}
outputFile << iCol << " " << iRow << " "
<< iElecId << " " << iDAC << " "
<< voltage << endl;
}
}
outputFile.close();
return;
}
void AliEMCALBiasAPD::ReadRootBiasAPDInfo(const TString &rootFileName,
Bool_t swapSides)
{
TFile inputFile(rootFileName, "read");
TTree *tree = (TTree*) inputFile.Get("tree");
ReadTreeBiasAPDInfo(tree, swapSides);
inputFile.Close();
return;
}
void AliEMCALBiasAPD::ReadTreeBiasAPDInfo(TTree *tree,
Bool_t swapSides)
{
Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
fNSuperModule = tree->GetEntries() / nAPDPerSM;
Int_t iSM = 0;
Int_t iCol = 0;
Int_t iRow = 0;
Int_t iElecId = 0;
Int_t iDAC = 0;
Float_t voltage = 0;
tree->SetBranchAddress("iSM", &iSM);
tree->SetBranchAddress("iCol", &iCol);
tree->SetBranchAddress("iRow", &iRow);
tree->SetBranchAddress("iElecId", &iElecId);
tree->SetBranchAddress("iDAC", &iDAC);
tree->SetBranchAddress("voltage", &voltage);
for (int ient=0; ient<tree->GetEntries(); ient++) {
tree->GetEntry(ient);
AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[iSM];
t->SetSuperModuleNum(iSM);
if (swapSides) {
iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
}
t->SetElecId(iCol, iRow, iElecId);
t->SetDAC(iCol, iRow, iDAC);
t->SetVoltage(iCol, iRow, voltage);
}
return;
}
void AliEMCALBiasAPD::WriteRootBiasAPDInfo(const TString &rootFileName,
Bool_t swapSides)
{
TFile destFile(rootFileName, "recreate");
if (destFile.IsZombie()) {
return;
}
destFile.cd();
TTree *tree = new TTree("tree","");
Int_t iSM = 0;
Int_t iCol = 0;
Int_t iRow = 0;
Int_t iElecId = 0;
Int_t iDAC = 0;
Float_t voltage = 0;
tree->Branch("iSM", &iSM, "iSM/I");
tree->Branch("iCol", &iCol, "iCol/I");
tree->Branch("iRow", &iRow, "iRow/I");
tree->Branch("iElecId", &iElecId, "iElecId/I");
tree->Branch("iDAC", &iDAC, "iDAC/I");
tree->Branch("voltage", &voltage, "voltage/F");
Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
for (iSM = 0; iSM < fNSuperModule; iSM++) {
AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[iSM];
for (Int_t j=0; j<nAPDPerSM; j++) {
iCol = j / AliEMCALGeoParams::fgkEMCALRows;
iRow = j % AliEMCALGeoParams::fgkEMCALRows;
iElecId = t->GetElecId(iCol, iRow);
iDAC = t->GetDAC(iCol, iRow);
voltage = t->GetVoltage(iCol, iRow);
if (swapSides) {
iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
}
tree->Fill();
}
}
tree->Write();
destFile.Close();
return;
}
AliEMCALBiasAPD::~AliEMCALBiasAPD()
{
fSuperModuleData.Delete();
}
AliEMCALSuperModuleBiasAPD * AliEMCALBiasAPD::GetSuperModuleBiasAPDNum(Int_t supModIndex)const
{
for (int i=0; i<fNSuperModule; i++) {
AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[i];
if (t->GetSuperModuleNum() == supModIndex) {
return t;
}
}
return NULL;
}