00001
00002
00003 #include "PbGlSector.h"
00004 #include <iostream>
00005 #include <fstream>
00006 #include <cstdio>
00007 #include <cstdlib>
00008 #include <cassert>
00009 #include "emcDataManager.h"
00010 #include "emcCalibrationData.h"
00011 #include <string>
00012 #include "emcDefines.h"
00013
00014 using std::cout ;
00015 using std::cerr ;
00016 using std::endl ;
00017 using std::ifstream ;
00018 using std::string;
00019
00020 size_t PbGlSector::fgSize = 4608 ;
00021 size_t PbGlSector::fgNumberOfSuperModules = 192 ;
00022
00023
00024 PbGlSector::PbGlSector(int & SectorNumber, PHTimeStamp* ts)
00025 {
00026 SectorN = SectorNumber;
00027
00028 fC0.resize(fgSize) ;
00029 fG0.resize(fgSize) ;
00030 fCF.resize(fgSize) ;
00031 SM.resize(fgNumberOfSuperModules) ;
00032
00033 for (size_t i = 0 ; i < fgNumberOfSuperModules ; i++ ) SM[i]=0;
00034
00035 if (ts) {
00036 BuildFromDB(ts) ;
00037 }
00038 else {
00039 BuildFromFiles() ;
00040 }
00041
00042 if (!fIsOK) {
00043 cerr << EMC_ERROR_MSG << "PbGlSector::PbGlSector : Sector "
00044 << SectorNumber << " can not be built" << endl ;
00045 Reset() ;
00046 }
00047 }
00048
00049
00050
00051 PbGlSector::~PbGlSector()
00052 {
00053 Reset() ;
00054 }
00055
00056
00057 void
00058 PbGlSector::Reset(void)
00059 {
00060 for ( size_t i = 0 ; i < SM.size() ; i++ ) {
00061 delete SM[i] ;
00062 SM[i] = 0 ;
00063 }
00064 SM.clear() ;
00065
00066 for ( size_t i = 0 ; i < fgSize ; i++) {
00067 fC0[i] = fG0[i] = fCF[i] = 0.0 ;
00068 }
00069
00070 fIsOK = false ;
00071 }
00072
00073
00074
00075 void
00076 PbGlSector::BuildFromFiles(void)
00077 {
00078 string filenameShort ;
00079 string filename ;
00080
00081 if (SectorN == 6) {
00082 filenameShort = "Calibr/database_Pbgl_1.txt" ;
00083 }
00084 else {
00085 filenameShort = "Calibr/database_Pbgl_2.txt" ;
00086 }
00087
00088 char* s = getenv("EMCAL_DATA_PBGL") ;
00089
00090 if (!s) {
00091 fIsOK = false ;
00092 }
00093 else {
00094
00095 filename += s ;
00096 filename += filenameShort ;
00097
00098 FILE* fp = fopen(filename.c_str(),"r") ;
00099
00100 if ( !fp ) {
00101 cerr << EMC_ERROR_MSG <<"PbGlSector::BuildFromFiles : File open error "
00102 << filename << endl ;
00103 fIsOK = false ;
00104 }
00105 else {
00106
00107 cerr << EMC_INFO_MSG << "PbGlSector::BuildFromFiles : File opened : "
00108 << filename << endl ;
00109
00110 PbGlIndexer * gPbGl = PbGlIndexer::buildPbGlIndexer();
00111
00112 int iS ;
00113 float C0, G0, F ;
00114
00115 for (int iSM24=0;
00116 iSM24<static_cast<int>(fgNumberOfSuperModules);
00117 iSM24++){
00118
00119 SM[iSM24] = new PbGlSuperModule(iSM24,SectorN);
00120
00121 LoadNextSMData(fp, SM[iSM24]);
00122
00123
00124
00125 for (int iSM24T=0; iSM24T<24; iSM24T++) {
00126
00127 iS = gPbGl->SM24iSM24TiST(iSM24, iSM24T);
00128
00129
00130 C0 = SM[iSM24]->getC0(iSM24T);
00131
00132
00133 G0 = SM[iSM24]->getG0(iSM24T);
00134
00135
00136
00137 F = SM[iSM24]->getCF(iSM24T);
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 fC0[iS] = C0;
00153 fG0[iS] = G0;
00154 fCF[iS] = F;
00155
00156 }
00157 }
00158 fclose(fp) ;
00159 fIsOK = true ;
00160 }
00161 }
00162 }
00163
00164
00165 void
00166 PbGlSector::BuildFromDB(PHTimeStamp* when)
00167 {
00168 emcCalibrationData* inical ;
00169 emcCalibrationData driver(emcCalibrationData::kIniCal,SectorN) ;
00170 driver.SetSource(emcManageable::kDB_Objy);
00171 emcDataManager* dm = emcDataManager::GetInstance() ;
00172
00173 inical = dynamic_cast<emcCalibrationData*>(dm->Collect(driver,*when)) ;
00174 if (!inical) {
00175 cerr << "<E> PbGlSector::BuildFromDB() : Failed to fetch initial "
00176 << " calibration data for sector " << SectorN
00177 << " at time " << *when << endl ;
00178 fIsOK = false ;
00179 }
00180 else {
00181
00182 PbGlIndexer * gPbGl = PbGlIndexer::buildPbGlIndexer();
00183
00184 size_t iSM24 ;
00185 size_t iSM24T ;
00186 int iS ;
00187
00188 for ( iSM24 = 0 ; iSM24 < fgNumberOfSuperModules ; iSM24++ ) {
00189
00190 for ( iSM24T = 0 ; iSM24T < 24 ; iSM24T++ ) {
00191
00192 iS = gPbGl->SM24iSM24TiST(iSM24, iSM24T);
00193
00194
00195 float C0 = inical->GetValue(iS,0) ;
00196
00197
00198 float G0 = inical->GetValue(iS,1) ;
00199
00200
00201
00202 float F = inical->GetValue(iS,2) ;
00203
00204
00205
00206
00207
00208
00209
00210
00211 fC0[iS] = C0;
00212 fG0[iS] = G0;
00213 fCF[iS] = F;
00214 }
00215 }
00216 fIsOK = true ;
00217 }
00218 }
00219
00220
00221 void
00222 PbGlSector::GetEnergyCalibration(int iTower, float& c0, float& g0, float& cf)
00223 {
00224 assert( iTower>=0 && static_cast<size_t>(iTower) < fC0.size() ) ;
00225 assert( iTower>=0 && static_cast<size_t>(iTower) < fG0.size() ) ;
00226 assert( iTower>=0 && static_cast<size_t>(iTower) < fCF.size() ) ;
00227
00228 c0 = fC0[iTower] ;
00229 g0 = fG0[iTower] ;
00230 cf = fCF[iTower] ;
00231 }
00232
00233
00234
00235
00236 void
00237 PbGlSector::LoadNextSMData(FILE * fp, EmcSuperModule * SM24)
00238 {
00239
00240
00241
00242 float dummy[33];
00243 char dummyc1[5];
00244 char dummyc2[10];
00245
00246 PbGlSuperModule* sm = dynamic_cast<PbGlSuperModule*>(SM24) ;
00247 assert(sm!=0) ;
00248
00249
00250
00251 for (int i1=3; i1>-1; i1--) {
00252
00253 for (int i2=0; i2<6; i2++) {
00254
00255 for (int j=0; j<3; j++) {
00256 fscanf(fp,"%e", dummy+j);
00257 }
00258 fscanf(fp,"%s",dummyc1);
00259 fscanf(fp,"%e", dummy+3);
00260 fscanf(fp,"%e", dummy+4);
00261 fscanf(fp,"%s",dummyc2);
00262
00263 for(int j=5; j<33; j++) {
00264 fscanf(fp,"%e", dummy+j);
00265 }
00266 sm->LoadTowerData((i1*6+i2), dummy, dummyc1, dummyc2);
00267 }
00268 }
00269 }
00270
00271
00272 void
00273 PbGlSector::CorrectEnergyCalibration(const char * fname)
00274 {
00275 cerr << "PbGlSector::CorrectEnergyCalibration : PLEASE CHECK THIS METHOD!"
00276 << endl ;
00277 assert(0==1) ;
00278
00279 ifstream fin;
00280
00281 fin.open(fname,std::ios::in);
00282 if(!fin) {
00283 cout<<"File "<<fname<<" not found - proceed with CERN calibration"<<endl;
00284 return;
00285 }
00286 int n, TowerId;
00287 float c;
00288 while(fin>>n>>TowerId>>c){
00289
00290
00291
00292
00293 }
00294 fin.close();
00295 }
00296
00297
00298
00299
00300
00301
00302