]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/SMcalib/AliEMCALCalibAPD.cxx
Bug fix in the constructor (thanks to A.Marin)
[u/mrichter/AliRoot.git] / EMCAL / SMcalib / AliEMCALCalibAPD.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 read txt file with APD number data
19 //
20
21 #include <fstream>
22 #include <TString.h>
23
24 #include "AliEMCALCalibAPD.h"
25
26 const int kMaxLen = 1000; // maximum length of single line (# of characters)
27 // an OK line with complete info should have a certain number of characters
28 const int kMinLenAPDLine = 135;
29 const int kMaxLenAPDLine = 1000;
30
31 ClassImp(AliEMCALCalibAPD)
32
33 //____________________________________________________________________________
34 AliEMCALCalibAPD::AliEMCALCalibAPD() : 
35   fNCalibAPD(0),
36   fData(0)
37 {
38   //Default constructor.
39 }
40
41 //____________________________________________________________________________
42 void AliEMCALCalibAPD::ReadCalibAPDInfo(Int_t nAPD, const TString &txtFileName)
43 {
44   //Read data from txt file. ; coordinates given on SuperModule basis
45
46   std::ifstream inputFile(txtFileName.Data());
47   if (!inputFile) {
48     printf("AliEMCALCalibAPD::ReadCalibAPDInfo - Cannot open the APD info file %s\n", txtFileName.Data());
49     return;
50   }
51
52   fNCalibAPD = nAPD;
53   if (fData) delete [] fData;
54   fData = new AliEMCALCalibAPDData[fNCalibAPD];
55
56   char line[kMaxLen];
57
58   /* DS: header lines skipped when switched to white-spaced separeted dat files instead of csv:
59      conversion from spreadsheet can be done a la
60      sed 's/,/ /g' APD-database-Houston.csv | egrep ^2 | awk '{if (NF==19) {print $0}}' > APD-database-Houston.dat
61      - meaning "replace , with whitespace, only get the columns that start with the number 2 (Houston APDs),
62      and only get rows with the full 19 fields
63
64   // get header lines:
65   inputFile.getline(line, kMaxLen);
66   //  printf(" 1st header line character count %d\n", inputFile.gcount());
67   inputFile.getline(line, kMaxLen);
68   //  printf(" 2nd header line character count %d\n", inputFile.gcount());
69   */
70
71   // variables for reading
72   int i1,i2,i3,i4,i5;
73   float f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12;
74   char c1[10], c2[20];
75
76   int j = 0; // index for the ones that were read OK
77   for (Int_t i = 0; i < fNCalibAPD; i++) {
78     AliEMCALCalibAPDData &t = fData[j];
79     if (!inputFile) {
80       printf("AliEMCALCalibAPD::ReadCalibAPDInfo - Error while reading input file; likely EOF..\n");
81       fNCalibAPD = j; // that's how many we actually read succesfully
82       printf("AliEMCALCalibAPD::ReadCalibAPDInfo - read %d OK\n", fNCalibAPD);
83       return;
84     }
85
86     // use some temporary/local values to perhaps help with the order when 
87     // trying to read all the many fields in a line..
88     inputFile.getline(line, kMaxLen);
89     int nchar = inputFile.gcount();
90     //printf(" line %d ok %d - character count %d\n", i, j, nchar);
91
92     if (nchar>kMinLenAPDLine && nchar<kMaxLenAPDLine) {
93       // looks like the line has about the right number of characters, let's
94       // try to decode it now..
95
96       //      printf("input: %s\n",line);
97       sscanf(line, "%d %u %s %s %d %d %f %f %f %f %f %f %f %f %f %d %f %f %f",
98              &i1, &i2, c1, c2, &i3, &i4, // header-type info
99              &f1, &f2, &f3, &f4, &f5, &f6, &f7, &f8, &f9, // measurements
100              &i5, &f10, &f11, &f12); // Hamamatsu
101
102       //      printf("after scanf: %s\n",line);
103       /*
104       printf("%d,%u,%s,%s,%d,%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%d,%g,%13.11f,%g\n",
105              i1, i2, c1, c2, i3, i4, // header-type info
106              f1, f2, f3, f4, f5, f6, f7, f8, f9, // measurements
107              i5, f10, f11, f12); // Hamamatsu
108       */
109       // assign the variables:
110       t.fAPDNum = i1; 
111       t.fSerialNum = i2; 
112       sprintf(t.fStatus,"%s",c1);
113       sprintf(t.fLocation,"%s",c2);
114       t.fRunNum = i3; 
115       t.fTestPos = i4; 
116
117       t.fV30 = f1; 
118       t.fV50 = f2; 
119       t.fVoltCoeff = f3;
120       t.fPar[0] = f4;
121       t.fPar[1] = f5;
122       t.fPar[2] = f6;
123       t.fParErr[0] = f7;
124       t.fParErr[1] = f8;
125       t.fParErr[2] = f9;
126
127       t.fBreakDown = i5; 
128       t.fHamV50 = f10;
129       t.fDarkCurrent = f11;
130       t.fTestTemp = f12;
131
132       j++; // increment our 'OK' counter..
133     } // line length appears OK      
134   } // i, APD
135
136   inputFile.close();
137
138   return;
139 }
140
141 //____________________________________________________________________________
142 void AliEMCALCalibAPD::WriteCalibAPDInfo(const TString &txtFileName)
143 {
144   // write data to txt file. ; coordinates given on SuperModule basis
145
146   std::ofstream outputFile(txtFileName.Data());
147   if (!outputFile) {
148     printf("AliEMCALCalibAPD::WriteCalibAPDInfo - Cannot open the APD output file %s\n", txtFileName.Data());
149     return;
150   }
151
152   char *comma = ",";
153
154   for (Int_t i = 0; i < fNCalibAPD; i++) {
155     AliEMCALCalibAPDData &t = fData[i];
156
157     outputFile << t.fAPDNum << comma
158                << t.fSerialNum << comma // Serial Number; from Hamamatsu
159                << t.fStatus << comma //
160                << t.fLocation << comma //
161                << t.fRunNum << comma
162                << t.fTestPos << comma
163                << t.fV30 << comma // Catania/Houston Voltage V30 (V) at T = 25 deg C
164                << t.fV50 << comma 
165                << t.fVoltCoeff << comma // 1/M x dM/dV
166                << t.fPar[0] << comma // fit parameters, p0,p1,p2 - for ADC vs bias measurement
167                << t.fPar[1] << comma
168                << t.fPar[2] << comma
169                << t.fParErr[0] << comma // error on fit parameters      
170                << t.fParErr[1] << comma 
171                << t.fParErr[2] << comma 
172                << t.fBreakDown << comma // Hamamatsu Breakdown Voltage (V)      
173                << t.fHamV50 << comma; // Hamamatsu Voltage V50 (V)
174     // I wasn't able to quite reproduce the values as they appeared on the 
175     // original file: e.g. dark current is not always 11-field - if last digit is zero..
176     // the other floats have 6 significant digits, but sometimes switch to 
177     // scientific notation - don't know how to handle this for varying length of fields.. -leave it as is for now..
178     outputFile << t.fDarkCurrent << comma; // Hamamatsu Dark Current (A)        
179     /*
180     // some tweaking for the dark-current field to get the output the same
181     // as on the original CSV
182     outputFile.precision(11);   
183     outputFile << fixed << t.fDarkCurrent << comma; // Hamamatsu Dark Current (A)       
184     // back to normal..
185     outputFile.precision(6); outputFile.unsetf(ios_base::floatfield);   
186     */
187
188     outputFile << t.fTestTemp // Hamamatsu Testing Temperature (deg C)  
189                << endl; 
190
191   } // i, APD
192
193   outputFile.close();
194
195   return;
196 }
197
198 //____________________________________________________________________________
199 AliEMCALCalibAPD::~AliEMCALCalibAPD()
200 {
201   delete [] fData;
202 }
203
204 //____________________________________________________________________________
205 AliEMCALCalibAPD::AliEMCALCalibAPDData AliEMCALCalibAPD::GetCalibAPDDataId(Int_t apdIndex)const
206 {
207   AliEMCALCalibAPDData t;  // just to maybe prevent a crash, but we are returning something not-initialized so maybe not better really..
208   if (!fData)
209     return t;
210
211   return fData[apdIndex];
212 }
213
214 //____________________________________________________________________________
215 AliEMCALCalibAPD::AliEMCALCalibAPDData AliEMCALCalibAPD::GetCalibAPDDataNum(Int_t apdNum)const
216 {
217   AliEMCALCalibAPDData t;  // just to maybe prevent a crash, but we are returning something not-initialized so maybe not better really..
218   if (!fData)
219     return t;
220
221   for (int i=0; i<fNCalibAPD; i++) {
222     if (fData[i].fAPDNum == apdNum) {
223       return fData[i];
224     }
225   }
226
227   // if we made it to here, then no match was found - just return then
228   return t;
229 }
230
231