PbScSector.C

Go to the documentation of this file.
00001 //$Id: PbScSector.C,v 1.12 2003/10/02 11:54:50 aphecete Exp $ 
00002 //#############################################################################
00003 // E.Kistenev         6/10/99 
00004 // send comments to kistenev@bnl.gov 
00005 //#############################################################################
00006 
00007 #include <iostream>
00008 #include <cstdio>
00009 
00010 #include "EmcStaticData.h"
00011 #include "PbScCalibrationData.h"
00012 #include "PbScSuperModule.h"
00013 #include "PbScIndexer.h"
00014 #include "PbScSector.h"
00015 #include "emcDataManager.h"
00016 #include "emcCalibrationData.h"
00017 #include "emcDefines.h"
00018 #include <cassert>
00019 #include <fstream>
00020 #include <string>
00021 
00022 using std::cout ;
00023 using std::cerr ;
00024 using std::endl ;
00025 using std::ifstream ;
00026 using std::string;
00027 
00028 size_t PbScSector::fgSize = 2592 ;
00029 size_t PbScSector::fgNumberOfSuperModules = 18 ;
00030 
00031 //_____________________________________________________________________________
00032 PbScSector::PbScSector(int & SectorNumber, PHTimeStamp* ts) 
00033 {
00034   SectorN = SectorNumber ;
00035 
00036   fECalib.resize(fgSize) ;
00037   fNorm0.resize(fgSize) ;
00038 
00039   SM.resize(fgNumberOfSuperModules) ;
00040   for (size_t i = 0 ; i < fgNumberOfSuperModules ; i++ ) SM[i]=0;
00041 
00042   string source = "Files";
00043   if (ts) {
00044     BuildFromDB(ts) ;
00045     source = "DB";
00046   }
00047   else {
00048     BuildFromFiles() ;
00049   }
00050 
00051   if (!fIsOK) { // the fIsOK flag is set by the BuildFromXXX methods
00052     cerr << EMC_ERROR_MSG << "PbScSector::PbScSector : Sector "
00053          << SectorNumber << " can not be built from "
00054          << source << endl;
00055     Reset() ;
00056   }
00057 }
00058 
00059 //_____________________________________________________________________________
00060 PbScSector::~PbScSector() 
00061 { 
00062   Reset() ;
00063 }
00064 
00065 //_____________________________________________________________________________
00066 void 
00067 PbScSector::Reset(void)
00068 {
00069   for (size_t i = 0 ; i < SM.size() ; i++ ) {
00070     delete SM[i]; 
00071   }
00072   SM.clear() ;
00073 
00074   for (size_t i = 0 ; i < fECalib.size() ; i++ ) {
00075     fECalib[i] = 0.0 ;
00076   }
00077 
00078   for (size_t i = 0 ; i < fNorm0.size() ; i++ ) {
00079     fNorm0[i] = 0.0 ;
00080   }
00081 
00082   fIsOK = false ;
00083 }
00084 
00085 //_____________________________________________________________________________
00086 void
00087 PbScSector::BuildFromFiles(void)
00088 { 
00089   EmcStaticData* gEmcStaticData = EmcStaticData::buildEmcStaticData() ;
00090   PbScCalibrationData* PbScData = gEmcStaticData->getPbScData() ;
00091   
00092   if (PbScData==0) {
00093     fIsOK = false ;
00094   }
00095   else if (PbScData->getStatus()==false) {
00096     fIsOK = false ;
00097   }
00098   else {
00099 
00100     PbScIndexer* gPbSc = PbScIndexer::buildPbScIndexer() ;
00101 
00102     int j = fgNumberOfSuperModules*SectorN; 
00103 
00104     for (int iSM=0; iSM<static_cast<int>(fgNumberOfSuperModules); iSM++){ 
00105       
00106       int SMId = PbScData->getSMId(j+iSM); 
00107     
00108       SM[iSM] = new PbScSuperModule(SMId); 
00109       
00110       if (SM[iSM]->LoadSMData()) { 
00111         //  convert calibration data from 902 format to that suited for  
00112         //  analysis chain 
00113         for (int iSMT=0; iSMT<144; iSMT++){ 
00114 
00115           int iS = gPbSc->SMiSMTiST(iSM, iSMT); 
00116 
00117           // FACTOR 1.1 is responsible for the N2-to-YAG laser swap
00118           //    PbScECalib[iS] = 0.038/SM[iSM]->getMuPeak(iSMT)*1.1; 
00119           // Corrected based upon Edwards studies, Nov. 28, 2000
00120           // Nov. 29, 2000 G. D.
00121           fECalib[iS] = 0.038/SM[iSM]->getMuPeak(iSMT)*1.026; 
00122           fNorm0[iS]  = SM[iSM]->getLaserRaw(iSMT)/ 
00123             SM[iSM]->getIntSPD()* 
00124             SM[iSM]->getIntSPDTP(); 
00125         } 
00126       }
00127     }
00128     fIsOK = true ;
00129   }
00130 }
00131 
00132 //_____________________________________________________________________________
00133 void 
00134 PbScSector::BuildFromDB(PHTimeStamp* when)
00135 {
00136   emcCalibrationData* inical ; 
00137   emcCalibrationData driver(emcCalibrationData::kIniCal,SectorN) ;
00138   driver.SetSource(emcManageable::kDB_Objy);
00139   emcDataManager* dm = emcDataManager::GetInstance() ;
00140 
00141   inical =  dynamic_cast<emcCalibrationData*>(dm->Collect(driver,*when)) ;
00142 
00143   if (!inical) {
00144     fIsOK = false ;
00145     cerr << EMC_ERROR_MSG << "PbScSector::BuildFromDB() : Failed to fetch "
00146          << "initial calibration data " 
00147          << " for sector " << SectorN << " at time " << *when << endl ;
00148   }
00149   else {
00150 
00151     size_t iSM ;
00152     size_t iSMT ; 
00153 
00154     PbScIndexer* gPbSc = PbScIndexer::buildPbScIndexer() ;
00155 
00156     for ( iSM = 0 ; iSM < fgNumberOfSuperModules ; iSM++ ) { 
00157       for ( iSMT = 0 ; iSMT < 144 ; iSMT++ ) { 
00158         int iS = gPbSc->SMiSMTiST(iSM, iSMT) ; 
00159         fECalib[iS] = inical->GetValue(iS,0) ;
00160         fNorm0[iS] = inical->GetValue(iS,1) ;
00161       }
00162     }
00163     fIsOK = true ;   
00164   }
00165 }
00166  
00167 //_____________________________________________________________________________
00168 void 
00169 PbScSector::GetEnergyCalibration(int iTower, float& EScale, 
00170                                  float& Normalization, float& Nothing)
00171 { 
00172   // This method should only be called after checking
00173   // that this sector is OK (using the IsOK() method).
00174   
00175   assert(iTower>=0 && static_cast<size_t>(iTower)<fECalib.size());
00176   assert(iTower>=0 && static_cast<size_t>(iTower)<fNorm0.size());
00177 
00178   EScale = fECalib[iTower]; 
00179   Normalization = fNorm0[iTower]; 
00180   Nothing = 1.0 ;
00181 }
00182   
00183 //_____________________________________________________________________________
00184 void 
00185 PbScSector::CorrectEnergyCalibration(const char * fname)
00186 {
00187   // LOAD AND APPLY CORRECTION COEFFICIENTS BASED UPON MIP PEAKS 
00188   // IN INDIVIDUAL SUPERMODULES.
00189   // This method should only be called after checking
00190   // that this sector is OK (using the IsOK() method).
00191   
00192   ifstream fin;
00193 
00194   fin.open(fname,std::ios::in);
00195 
00196   if (!fin) {
00197     cerr << EMC_ERROR_MSG << "File " << fname 
00198          << " not found - proceed with original calibration" << endl ;
00199     fIsOK = false ;
00200   }
00201   else {
00202     int sect, smz, smy, status, ii;
00203     float corr, eCorr ;
00204     //  Skip title line
00205     char c;
00206     while(fin.get(c)&&c!='\n');
00207     while(fin>>smz>>smy>>sect>>ii>>corr>>eCorr>>status){
00208       smz /=-12; smz += 5; smy /=12;
00209       cout<<sect<<" "<<smz<<" "<<smy<<" "<<corr<<" "<<eCorr<<" "<<status<<endl;
00210       if(!status||sect!=SectorN) continue;
00211       int iSM = smy*6+smz;
00212       for(int iCh=0; iCh<144;iCh++) {
00213         int iTwr = EmcIndexer::iSiSMiSMTiST(SectorN, iSM, EmcIndexer::iCHiSMT(iCh));
00214         fECalib[iTwr] *=corr;
00215       }
00216     }
00217     fin.close();
00218   }
00219 }