]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALCalibTimeDepCorrection.cxx
typo O instead of 0
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALCalibTimeDepCorrection.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id: $ */
17
18 // Objects of this class contain info on time-dependent corrections
19 //
20
21 #include <fstream>
22 #include <TString.h>
23 #include <TFile.h>
24 #include <TTree.h>
25
26 #include "AliEMCALCalibTimeDepCorrection.h"
27
28 using namespace std;
29
30 ClassImp(AliEMCALCalibTimeDepCorrection)
31
32 //____________________________________________________________________________
33 AliEMCALCalibTimeDepCorrection::AliEMCALCalibTimeDepCorrection(const int nSM) : 
34   fNSuperModule(nSM),
35   fSuperModuleData(),
36   fStartTime(0),
37   fNTimeBins(0),
38   fTimeBinSize(0)
39 {
40   //Default constructor.
41   for (int i=0; i<fNSuperModule; i++) {
42     fSuperModuleData.Add(new AliEMCALSuperModuleCalibTimeDepCorrection(i));
43   }
44   fSuperModuleData.Compress(); // compress the TObjArray
45 }
46
47 //____________________________________________________________________________
48 void AliEMCALCalibTimeDepCorrection::InitCorrection(Int_t nSM, Int_t nBins, Float_t val)
49 {
50   // This methods assumes that you are using SuperModules 0..nSM-1
51   fNSuperModule = nSM;
52
53   Int_t iSM = 0; // SuperModule index
54   Int_t iCol = 0;
55   Int_t iRow = 0;
56   Int_t nCorr = nBins;
57   Float_t correction = val;
58
59   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
60
61   for (Int_t i = 0; i < fNSuperModule; i++) {
62     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[i];
63     iSM = i;
64     t->SetSuperModuleNum(iSM); // assume SMs are coming in order
65
66     for (Int_t j=0; j<nAPDPerSM; j++) {
67       iCol = j / AliEMCALGeoParams::fgkEMCALRows;
68       iRow = j % AliEMCALGeoParams::fgkEMCALRows;
69
70       // set size of TArray
71       t->GetCorrection(iCol,iRow)->Set(nCorr);
72       for (Int_t k=0; k<nCorr; k++) {
73         // add to TArray
74         t->GetCorrection(iCol,iRow)->AddAt(correction, k); // AddAt = SetAt..
75       }
76     }
77
78   } // i, SuperModule
79
80   return;
81 }
82
83 //____________________________________________________________________________
84 void AliEMCALCalibTimeDepCorrection::SetCorrection(Int_t supModIndex, Int_t iCol, Int_t iRow, Int_t iBin, Float_t val)
85 { // if you call for non-existing data, there may be a crash..
86   ((AliEMCALSuperModuleCalibTimeDepCorrection*)fSuperModuleData[supModIndex])->GetCorrection(iCol,iRow)->AddAt(val, iBin); // AddAt = SetAt..
87  return; 
88 }
89
90 //____________________________________________________________________________
91 Float_t AliEMCALCalibTimeDepCorrection::GetCorrection(Int_t supModIndex, Int_t iCol, Int_t iRow, Int_t iBin) const
92 { // if you call for non-existing data, there may be a crash..
93   return ((AliEMCALSuperModuleCalibTimeDepCorrection*)fSuperModuleData[supModIndex])->GetCorrection(iCol,iRow)->At(iBin);
94 }
95
96 //____________________________________________________________________________
97 void AliEMCALCalibTimeDepCorrection::ReadTextInfo(Int_t nSM, const TString &txtFileName,
98                                                   Bool_t swapSides)
99 {
100   //Read data from txt file. ; coordinates given on SuperModule basis
101
102   std::ifstream inputFile(txtFileName.Data());
103   if (!inputFile) {
104     printf("AliEMCALCalibTimeDepCorrection::ReadTextInfo - Cannot open the APD info file %s\n", txtFileName.Data());
105     return;
106   }
107
108   fNSuperModule = nSM;
109
110   Int_t iSM = 0; // SuperModule index
111   Int_t iCol = 0;
112   Int_t iRow = 0;
113   Int_t nCorr = 0;
114   Float_t correction = 0;
115
116   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
117
118   // read header info 
119   inputFile >> fStartTime >> fNTimeBins >> fTimeBinSize;
120
121   for (Int_t i = 0; i < fNSuperModule; i++) {
122     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[i];
123
124     if (!inputFile) {
125       printf("AliEMCALCalibTimeDepCorrection::ReadTextInfo - Error while reading input file; likely EOF..\n");
126       return;
127     }
128     inputFile >> iSM;
129     t->SetSuperModuleNum(iSM);
130
131     for (Int_t j=0; j<nAPDPerSM; j++) {
132       inputFile >> iCol >> iRow >> nCorr;
133
134       // check that input values are not out bounds
135       if (iCol<0 || iCol>(AliEMCALGeoParams::fgkEMCALCols-1) ||
136           iRow<0 || iRow>(AliEMCALGeoParams::fgkEMCALRows-1) ) {
137         printf("AliEMCALCalibTimeDepCorrection::ReadTextInfo - Error while reading input file; j %d iCol %d iRow %d\n", j, iCol, iRow);
138       return;
139       }
140
141       // assume that this info is already swapped and done for this basis?
142       if (swapSides) {
143         // C side, oriented differently than A side: swap is requested
144         iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
145         iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
146       }
147
148       // set size of TArray
149       t->GetCorrection(iCol,iRow)->Set(nCorr);
150       for (Int_t k=0; k<nCorr; k++) {
151         inputFile >> correction;
152         // add to TArray
153         t->GetCorrection(iCol,iRow)->AddAt(correction, k);
154       }
155     }
156
157   } // i, SuperModule
158
159   inputFile.close();
160
161   return;
162 }
163
164 //____________________________________________________________________________
165 void AliEMCALCalibTimeDepCorrection::WriteTextInfo(const TString &txtFileName,
166                                                    Bool_t swapSides)
167 {
168   // write data to txt file. ; coordinates given on SuperModule basis
169
170   std::ofstream outputFile(txtFileName.Data());
171   if (!outputFile) {
172     printf("AliEMCALCalibTimeDepCorrection::WriteTextInfo - Cannot open the APD output file %s\n", txtFileName.Data());
173     return;
174   }
175
176   Int_t iCol = 0;
177   Int_t iRow = 0;
178   Int_t nCorr = 0;
179
180   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
181
182   // write header info 
183   outputFile << fStartTime << " " << fNTimeBins << " " << fTimeBinSize << endl;
184
185   for (Int_t i = 0; i < fNSuperModule; i++) {
186     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[i];
187     outputFile << t->GetSuperModuleNum() << endl;
188
189     for (Int_t j=0; j<nAPDPerSM; j++) {
190       iCol = j / AliEMCALGeoParams::fgkEMCALRows;
191       iRow = j % AliEMCALGeoParams::fgkEMCALRows;
192
193       nCorr = t->GetCorrection(iCol,iRow)->GetSize();
194
195       if (swapSides) {
196         // C side, oriented differently than A side: swap is requested
197         iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
198         iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
199       }
200
201       outputFile << iCol << " " << iRow << " " << nCorr << endl;
202       for (Int_t k=0; k<nCorr; k++) {
203         outputFile << t->GetCorrection(iCol,iRow)->At(k) << " ";
204       }
205       outputFile << endl;
206
207     }
208
209   } // i, SuperModule
210
211   outputFile.close();
212
213   return;
214 }
215
216 //____________________________________________________________________________
217 void AliEMCALCalibTimeDepCorrection::ReadRootInfo(const TString &rootFileName,
218                                                   Bool_t swapSides)
219 {
220   //Read data from root file. ; coordinates given on SuperModule basis
221   TFile inputFile(rootFileName, "read");  
222
223   TTree *treeGlob = (TTree*) inputFile.Get("treeGlob");
224   TTree *treeCorr = (TTree*) inputFile.Get("treeCorr");
225
226   ReadTreeInfo(treeGlob, treeCorr, swapSides);
227
228   inputFile.Close();
229
230   return;
231 }
232
233 //____________________________________________________________________________
234 void AliEMCALCalibTimeDepCorrection::ReadTreeInfo(TTree *treeGlob, TTree *treeCorr,
235                                                   Bool_t swapSides)
236 {
237  // how many SuperModule's worth of entries / APDs do we have?
238   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
239   fNSuperModule = treeCorr->GetEntries() / nAPDPerSM;
240
241   // global variables : only one entry
242   treeGlob->SetBranchAddress("fStartTime", &fStartTime);
243   treeGlob->SetBranchAddress("fNTimeBins", &fNTimeBins);
244   treeGlob->SetBranchAddress("fTimeBinSize", &fTimeBinSize);
245   treeGlob->GetEntry(0); 
246
247   Int_t iSM = 0; // SuperModule index
248   Int_t iCol = 0;
249   Int_t iRow = 0;
250   // list of values to be read
251   Int_t nCorr = 0;
252   Float_t correction[fgkMaxTimeBins] = {0};
253   // make sure it's really initialized correctly
254   memset(correction, 0, sizeof(correction)); // better safe than sorry
255   // end - all values
256
257   // declare the branches
258   treeCorr->SetBranchAddress("iSM", &iSM);
259   treeCorr->SetBranchAddress("iCol", &iCol);
260   treeCorr->SetBranchAddress("iRow", &iRow);
261   treeCorr->SetBranchAddress("nCorr", &nCorr);
262   treeCorr->SetBranchAddress("correction", correction);
263
264   for (int ient=0; ient<treeCorr->GetEntries(); ient++) {
265     treeCorr->GetEntry(ient);
266
267     // assume the index SuperModules come in order: i=iSM
268     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[iSM];
269     t->SetSuperModuleNum(iSM);
270
271     // assume that this info is already swapped and done for this basis?
272     if (swapSides) {
273       // C side, oriented differently than A side: swap is requested
274       iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
275       iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
276     }
277
278
279     // set size of TArray
280     t->GetCorrection(iCol,iRow)->Set(nCorr);
281     for (Int_t k=0; k<nCorr; k++) {
282       // add to TArray
283       t->GetCorrection(iCol,iRow)->AddAt(correction[k], k);
284     }
285
286   } // entry
287
288   return;
289 }
290
291 //____________________________________________________________________________
292 void AliEMCALCalibTimeDepCorrection::WriteRootInfo(const TString &rootFileName,
293                                                    Bool_t swapSides)
294 {
295   // write data to root file. ; coordinates given on SuperModule basis
296   TFile destFile(rootFileName, "recreate");  
297   if (destFile.IsZombie()) {
298     return;
299   }  
300   destFile.cd();
301
302   TTree *treeGlob = new TTree("treeGlob","");
303   TTree *treeCorr = new TTree("treeCorr","");
304
305   // global part only has one entry
306   treeGlob->Branch("fStartTime", &fStartTime, "fStartTime/I"); // really unsigned int..
307   treeGlob->Branch("fNTimeBins", &fNTimeBins, "fNTimeBins/I");
308   treeGlob->Branch("fTimeBinSize", &fTimeBinSize, "fTimeBinSize/I");
309   treeGlob->Fill();
310
311   // variables for filling the TTree
312   Int_t iSM = 0; // SuperModule index
313   Int_t iCol = 0;
314   Int_t iRow = 0;
315   Int_t nCorr = 0;
316   Float_t correction[fgkMaxTimeBins] = {0};
317   // make sure it's really initialized correctly
318   memset(correction, 0, sizeof(correction)); // better safe than sorry
319
320   // declare the branches
321   treeCorr->Branch("iSM", &iSM, "iSM/I");
322   treeCorr->Branch("iCol", &iCol, "iCol/I");
323   treeCorr->Branch("iRow", &iRow, "iRow/I");
324   treeCorr->Branch("nCorr", &nCorr, "nCorr/I");
325   treeCorr->Branch("correction", &correction, "correction[nCorr]/F");
326
327   Int_t nAPDPerSM = AliEMCALGeoParams::fgkEMCALCols * AliEMCALGeoParams::fgkEMCALRows;
328
329   for (iSM = 0; iSM < fNSuperModule; iSM++) {
330     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[iSM];
331
332     for (Int_t j=0; j<nAPDPerSM; j++) {
333       iCol = j / AliEMCALGeoParams::fgkEMCALRows;
334       iRow = j % AliEMCALGeoParams::fgkEMCALRows;
335
336       nCorr = t->GetCorrection(iCol,iRow)->GetSize();
337       if (nCorr > fgkMaxTimeBins) {
338         printf("AliEMCALCalibTimeDepCorrection::WriteRootInfo - too many correction/timebins %d kept\n", nCorr);
339         return;
340       }
341
342       if (swapSides) {
343         // C side, oriented differently than A side: swap is requested
344         iCol = AliEMCALGeoParams::fgkEMCALCols-1 - iCol;
345         iRow = AliEMCALGeoParams::fgkEMCALRows-1 - iRow;
346       }
347
348       for (Int_t k=0; k<nCorr; k++) {
349         correction[k] = t->GetCorrection(iCol,iRow)->At(k);
350       }
351
352       treeCorr->Fill();
353     }
354
355   } // i, SuperModule
356
357   treeGlob->Write();
358   treeCorr->Write();
359   destFile.Close();
360
361   return;
362 }
363
364 //____________________________________________________________________________
365 AliEMCALCalibTimeDepCorrection::~AliEMCALCalibTimeDepCorrection()
366 {
367   fSuperModuleData.Delete();
368 }
369
370 //____________________________________________________________________________
371 AliEMCALSuperModuleCalibTimeDepCorrection * AliEMCALCalibTimeDepCorrection::GetSuperModuleCalibTimeDepCorrectionNum(Int_t supModIndex)const
372 {
373   for (int i=0; i<fNSuperModule; i++) {
374     AliEMCALSuperModuleCalibTimeDepCorrection * t = (AliEMCALSuperModuleCalibTimeDepCorrection*) fSuperModuleData[i];
375     if (t->GetSuperModuleNum() == supModIndex) {
376       return t;
377     }
378   }
379
380   // if we arrived here, then nothing was found.. just return a NULL pointer 
381   return NULL;
382 }
383