00001 #include "l3EmcCalibration.h"
00002
00003 #include <fcntl.h>
00004 #include <unistd.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <sys/mman.h>
00008 #include <errno.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <fstream>
00012 #include <string>
00013 #include <math.h>
00014 #include <rtsLog.h>
00015
00016
00017 using namespace std;
00018
00019
00020 l3EmcTowerInfo::l3EmcTowerInfo() {
00021 set(0., 0., 0., 0., 0., 1.0, -1, -1);
00022 }
00023
00024
00025 l3EmcCalibration::l3EmcCalibration(int nTow) {
00026 nTowers = nTow;
00027
00028 tower = new l3EmcTowerInfo[nTowers];
00029 daq2id = new int[nTowers];
00030 }
00031
00032 l3EmcCalibration::~l3EmcCalibration() {
00033 delete[] tower;
00034 delete[] daq2id;
00035
00036 }
00037
00038 int l3EmcCalibration::loadMap(const char* filename)
00039 {
00040
00041 LOG(ERR, "Binary maps are no longer supported. Where did you find one?",0,0,0,0,0);
00042
00043 return -1;
00044
00045 }
00046
00047
00048
00049 #ifdef OLDLOAD
00050 int l3EmcCalibration::loadTextMap(const char* filename) {
00051
00052 ifstream txtmap(filename);
00053
00054 string s;
00055
00056 enum colDesc { col_phi, col_eta,
00057 col_ped, col_threshold, col_gain,
00058 col_id, col_daqId, col_ignore };
00059
00060
00061
00062 txtmap >> s;
00063 if (s != "EmcCalib") {
00064 LOG(ERR, "No EMC calibration map in text format in %s\n", filename,0,0,0,0);
00065 return -1;
00066 }
00067
00068 txtmap >> s;
00069 if (s != "columns:") {
00070 LOG(ERR,"No EMC calibration map in text format in %s\n", filename,0,0,0,0);
00071 return -1;
00072 }
00073
00074 int nCols;
00075 txtmap >> nCols;
00076
00077
00078 colDesc *colTarget = new colDesc[nCols];
00079
00080
00081 colTarget[0] = col_id;
00082 colTarget[1] = col_daqId;
00083 colTarget[2] = col_phi;
00084 colTarget[3] = col_eta;
00085 colTarget[4] = col_ped;
00086 colTarget[5] = col_gain;
00087 colTarget[6] = col_threshold;
00088
00089
00090 for (int i=0; i<nTowers; i++) {
00091
00092 float phi=0.;
00093 float eta=0.;
00094 float ped=0.;
00095 float gain=1.0;
00096 int id=-1;
00097 int daqId=-1;
00098 float threshold=0;
00099
00100 float ignore;
00101
00102 for (int col=0; col<nCols; col++) {
00103
00104 switch (colTarget[col]) {
00105
00106 case col_phi:
00107 txtmap >> phi ;
00108 break;
00109
00110 case col_eta:
00111 txtmap >> eta ;
00112 break;
00113
00114 case col_ped:
00115 txtmap >> ped ;
00116 break;
00117
00118 case col_gain:
00119 txtmap >> gain ;
00120 break;
00121
00122 case col_threshold:
00123 txtmap >> threshold ;
00124 break;
00125
00126 case col_id:
00127 txtmap >> id ;
00128 id--;
00129 break;
00130
00131 case col_daqId:
00132 txtmap >> daqId ;
00133 break;
00134
00135 case col_ignore:
00136 txtmap >> ignore ;
00137 break;
00138
00139 }
00140 }
00141
00142
00143
00144
00145
00146 if ( (id < 0) || (id >= 4800) ) {
00147 LOG(ERR, "%s contains info for tower %i!!!\n",filename,id,0,0,0);
00148 return -1;
00149 }
00150
00151
00152 tower[id].set(phi, eta, ped, gain, id, daqId);
00153 daq2id[daqId] = id;
00154 }
00155
00156 return 0;
00157 }
00158 #else
00159
00160 int l3EmcCalibration::loadTextMap(const char* filename)
00161 {
00162
00163
00164
00165
00166
00167
00168 string s;
00169
00170 ifstream txtmap(filename);
00171
00172 string type;
00173 txtmap >> type;
00174
00175 txtmap >> s;
00176 if (s != "columns:") {
00177 LOG(ERR, "No EMC calibration found in %s\n", filename,0,0,0,0);
00178 return -1;
00179 }
00180
00181 colDef_t colDef;
00182 colDef.set(0,-1,-1,-1,-1,-1,-1,-1,-1);
00183
00184
00185 txtmap >> colDef.nCols;
00186
00187 if (type == "EmcCalib") {
00188
00189 colDef.set(7,0,1,2,3,-1,-1,4,5);
00190
00191 } else {
00192 txtmap >> s;
00193 if (s != "format:") return -1;
00194
00195 for (int i=0; i<colDef.nCols; i++) {
00196 txtmap >> s;
00197
00198 if (s=="id") colDef.id = i;
00199 if (s=="daq") colDef.daqId = i;
00200 if (s=="phi") colDef.phi = i;
00201 if (s=="eta") colDef.eta = i;
00202 if (s=="etamin") colDef.etamin = i;
00203 if (s=="etamax") colDef.etamax = i;
00204 if (s=="ped") colDef.ped = i;
00205 if (s=="gain") colDef.gain = i;
00206 }
00207
00208 }
00209
00210
00211 int nRead = readCalib(&txtmap, colDef);
00212
00213
00214 if(nRead == nTowers)
00215 return 0;
00216 else
00217 return -1;
00218
00219
00220 }
00221
00222 #endif
00223
00224 int l3EmcCalibration::readCalib(ifstream *from, colDef_t colDef)
00225 {
00226
00227 int nTwr = 0;
00228
00229 for (int t=0; t<nTowers; t++) {
00230 float phi=0.0, eta=0.0, etamin=-999.0, etamax=-999.0;
00231 float ped=0.0, gain=1.0;
00232 int id=-1, daqId=-1;
00233
00234 string dummy;
00235
00236
00237
00238 for (int i=0; i<colDef.nCols; i++) {
00239 if (i == colDef.id) *from >> id; else
00240 if (i == colDef.daqId) *from >> daqId; else
00241 if (i == colDef.eta) *from >> eta; else
00242 if (i == colDef.etamin) *from >> etamin; else
00243 if (i == colDef.etamax) *from >> etamax; else
00244 if (i == colDef.phi) *from >> phi; else
00245 if (i == colDef.ped) *from >> ped; else
00246 if (i == colDef.gain) *from >> gain; else
00247 *from >> dummy;
00248 }
00249
00250
00251 if(from->eof()) break;
00252
00253
00254 if (nTowers == 4800) {
00255 if(etamin == -999.)
00256 etamin = floor(eta*20.)/20.;
00257
00258 if(etamax == -999.)
00259 etamax = ceil(eta*20.)/20.;
00260 }
00261
00262 tower[id-1].set(phi, eta, etamin, etamax, ped, gain, id, daqId);
00263 daq2id[daqId] = id-1;
00264 nTwr++;
00265 }
00266
00267
00268 return nTwr;
00269 }
00270
00271
00272
00273
00274
00275 int l3EmcCalibration::saveTextMap(const char* filename)
00276 {
00277 ofstream txtmap(filename);
00278
00279 txtmap << "EmcCalib" << endl
00280 << "columns: 7" << endl;
00281
00282 for (int i=0; i<nTowers; i++) {
00283 txtmap << tower[i].getID() << " "
00284 << tower[i].getDaqID() << " "
00285 << tower[i].getPhi() << " "
00286 << tower[i].getEta() << " "
00287 << tower[i].getPed() << " "
00288 << tower[i].getGain() << " "
00289 << 0.0 << endl;
00290 }
00291
00292 return 0;
00293 }