]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliEMCALBiasAPD.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALBiasAPD.cxx
index 7480984bbf894a40e45f5563adf16f312ac55098..760f63b490183f84127164a6954aec66fb8a1ee8 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <fstream>
 #include <TString.h>
+#include <TFile.h>
+#include <TTree.h>
 
 #include "AliEMCALBiasAPD.h"
 
@@ -28,16 +30,21 @@ using namespace std;
 ClassImp(AliEMCALBiasAPD)
 
 //____________________________________________________________________________
-AliEMCALBiasAPD::AliEMCALBiasAPD() : 
-  fNSuperModule(0),
-  fSuperModuleData(0)
+AliEMCALBiasAPD::AliEMCALBiasAPD(const int nSM) : 
+  fNSuperModule(nSM), // make space for everyone 
+  fSuperModuleData()
 {
   //Default constructor.
+  for (int i=0; i<fNSuperModule; i++) {
+    fSuperModuleData.Add(new AliEMCALSuperModuleBiasAPD(i));
+  }
+  fSuperModuleData.Compress(); // compress the TObjArray
+  fSuperModuleData.SetOwner(kTRUE); 
 }
 
 //____________________________________________________________________________
-void AliEMCALBiasAPD::ReadBiasAPDInfo(Int_t nSM, const TString &txtFileName,
-                                     Bool_t swapSides)
+void AliEMCALBiasAPD::ReadTextBiasAPDInfo(Int_t nSM, const TString &txtFileName,
+                                         Bool_t swapSides)
 {
   //Read data from txt file. ; coordinates given on SuperModule basis
 
@@ -48,8 +55,6 @@ void AliEMCALBiasAPD::ReadBiasAPDInfo(Int_t nSM, const TString &txtFileName,
   }
 
   fNSuperModule = nSM;
-  if (fSuperModuleData) delete [] fSuperModuleData;
-  fSuperModuleData = new AliEMCALSuperModuleBiasAPD[fNSuperModule];
 
   Int_t iSM = 0; // SuperModule index
   Int_t iCol = 0;
@@ -61,17 +66,25 @@ void AliEMCALBiasAPD::ReadBiasAPDInfo(Int_t nSM, const TString &txtFileName,
   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
 
   for (Int_t i = 0; i < fNSuperModule; i++) {
-    AliEMCALSuperModuleBiasAPD &t = fSuperModuleData[i];
+    AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[i];
+
     if (!inputFile) {
-      printf("AliEMCALBiasAPD::ReadBiasAPDInfo - Error while reading input file; likely EOF..");
+      printf("AliEMCALBiasAPD::ReadBiasAPDInfo - Error while reading input file; likely EOF..\n");
       return;
     }
     inputFile >> iSM;
-    t.fSuperModuleNum = iSM;
+    t->SetSuperModuleNum(iSM);
 
     for (Int_t j=0; j<nAPDPerSM; j++) {
       inputFile >> iCol >> iRow >> iElecId >> iDAC >> voltage;
 
+      // check that input values are not out bounds
+      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;
+      }
+
       // assume that this info is already swapped and done for this basis?
       if (swapSides) {
        // C side, oriented differently than A side: swap is requested
@@ -79,9 +92,9 @@ void AliEMCALBiasAPD::ReadBiasAPDInfo(Int_t nSM, const TString &txtFileName,
        iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
       }
 
-      t.fElecId[iCol][iRow] = iElecId;
-      t.fDAC[iCol][iRow] = iDAC;
-      t.fVoltage[iCol][iRow] = voltage;
+      t->SetElecId(iCol, iRow, iElecId);
+      t->SetDAC(iCol, iRow, iDAC);
+      t->SetVoltage(iCol, iRow, voltage);
     }
 
   } // i, SuperModule
@@ -92,8 +105,8 @@ void AliEMCALBiasAPD::ReadBiasAPDInfo(Int_t nSM, const TString &txtFileName,
 }
 
 //____________________________________________________________________________
-void AliEMCALBiasAPD::WriteBiasAPDInfo(const TString &txtFileName,
-                                      Bool_t swapSides)
+void AliEMCALBiasAPD::WriteTextBiasAPDInfo(const TString &txtFileName,
+                                          Bool_t swapSides)
 {
   // write data to txt file. ; coordinates given on SuperModule basis
 
@@ -112,16 +125,16 @@ void AliEMCALBiasAPD::WriteBiasAPDInfo(const TString &txtFileName,
   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
 
   for (Int_t i = 0; i < fNSuperModule; i++) {
-    AliEMCALSuperModuleBiasAPD &t = fSuperModuleData[i];
-    outputFile << t.fSuperModuleNum << endl;
+    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.fElecId[iCol][iRow];
-      iDAC = t.fDAC[iCol][iRow];
-      voltage = t.fVoltage[iCol][iRow];
+      iElecId = t->GetElecId(iCol, iRow);
+      iDAC = t->GetDAC(iCol, iRow);
+      voltage = t->GetVoltage(iCol, iRow);
 
       if (swapSides) {
        // C side, oriented differently than A side: swap is requested
@@ -142,34 +155,144 @@ void AliEMCALBiasAPD::WriteBiasAPDInfo(const TString &txtFileName,
 }
 
 //____________________________________________________________________________
-AliEMCALBiasAPD::~AliEMCALBiasAPD()
+void AliEMCALBiasAPD::ReadRootBiasAPDInfo(const TString &rootFileName,
+                                         Bool_t swapSides)
+{
+  //Read data from root file. ; coordinates given on SuperModule basis
+  TFile inputFile(rootFileName, "read");  
+
+  TTree *tree = (TTree*) inputFile.Get("tree");
+
+  ReadTreeBiasAPDInfo(tree, swapSides);
+
+  inputFile.Close();
+
+  return;
+}
+
+//____________________________________________________________________________
+void AliEMCALBiasAPD::ReadTreeBiasAPDInfo(TTree *tree,
+                                         Bool_t swapSides)
 {
-  delete [] fSuperModuleData;
+ // how many SuperModule's worth of entries / APDs do we have?
+  Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
+  fNSuperModule = tree->GetEntries() / nAPDPerSM;
+
+  Int_t iSM = 0; // SuperModule index
+  Int_t iCol = 0;
+  Int_t iRow = 0;
+  // list of values to be read
+  Int_t iElecId = 0;
+  Int_t iDAC = 0;
+  Float_t voltage = 0;     
+  // end - all values
+
+  // declare the branches
+  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);
+
+    // assume the index SuperModules come in order: i=iSM
+    AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[iSM];
+    t->SetSuperModuleNum(iSM);
+
+    // assume that this info is already swapped and done for this basis?
+    if (swapSides) {
+      // C side, oriented differently than A side: swap is requested
+      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;
 }
 
 //____________________________________________________________________________
-AliEMCALBiasAPD::AliEMCALSuperModuleBiasAPD AliEMCALBiasAPD::GetSuperModuleBiasAPDId(Int_t supModIndex)const
+void AliEMCALBiasAPD::WriteRootBiasAPDInfo(const TString &rootFileName,
+                                          Bool_t swapSides)
 {
-  AliEMCALSuperModuleBiasAPD t;  // just to maybe prevent a crash, but we are returning something not-initialized so maybe not better really..
-  if (!fSuperModuleData)
-    return t;
+  // write data to root file. ; coordinates given on SuperModule basis
+  TFile destFile(rootFileName, "recreate");  
+  if (destFile.IsZombie()) {
+    return;
+  }  
+  destFile.cd();
+
+  TTree *tree = new TTree("tree","");
 
-  return fSuperModuleData[supModIndex];
+  // variables for filling the TTree
+  Int_t iSM = 0; // SuperModule index
+  Int_t iCol = 0;
+  Int_t iRow = 0;
+  Int_t iElecId = 0;
+  Int_t iDAC = 0;
+  Float_t voltage = 0;
+  // declare the branches
+  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) {
+       // C side, oriented differently than A side: swap is requested
+       iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
+       iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
+      }
+
+      tree->Fill();
+    }
+
+  } // i, SuperModule
+
+  tree->Write();
+  destFile.Close();
+
+  return;
 }
 
 //____________________________________________________________________________
-AliEMCALBiasAPD::AliEMCALSuperModuleBiasAPD AliEMCALBiasAPD::GetSuperModuleBiasAPDNum(Int_t supModIndex)const
+AliEMCALBiasAPD::~AliEMCALBiasAPD()
 {
-  AliEMCALSuperModuleBiasAPD t;  // just to maybe prevent a crash, but we are returning something not-initialized so maybe not better really..
-  if (!fSuperModuleData)
-    return t;
+  fSuperModuleData.Delete();
+}
 
+//____________________________________________________________________________
+AliEMCALSuperModuleBiasAPD * AliEMCALBiasAPD::GetSuperModuleBiasAPDNum(Int_t supModIndex)const
+{ // getter via index
   for (int i=0; i<fNSuperModule; i++) {
-    if (fSuperModuleData[i].fSuperModuleNum == supModIndex) {
-      return fSuperModuleData[i];
+    AliEMCALSuperModuleBiasAPD * t = (AliEMCALSuperModuleBiasAPD*) fSuperModuleData[i];
+    if (t->GetSuperModuleNum() == supModIndex) {
+      return t;
     }
   }
 
-  return t;
+  // if we arrived here, then nothing was found.. just return a NULL pointer 
+  return NULL;
 }