emcRawDataCalibratorV1.C

Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // $Id: emcRawDataCalibratorV1.C,v 1.25 2005/04/17 00:19:52 phnxemc Exp $
00003 // 
00004 // Package: Calib
00005 // 
00006 // Copyright (C) PHENIX collaboration, 1999-2001
00007 //
00008 // Implementation of class : emcRawDataCalibratorV1
00009 //
00010 // Authors: Real calculation content = E. Kistenev (kistenev@bnl.gov)
00011 //          C++ consulting = L. Aphecetche (aphecetc@in2p3.fr)
00012 //
00013 //-------------------------------------------------------------------------
00014 
00015 #include "emcRawDataCalibratorV1.h"
00016 
00017 #include "emcRawDataAccessor.h"
00018 #include "emcMixedDataObject.h"
00019 #include "emcCalibratedDataObject.h"
00020 #include "EmcDynamicData.h"
00021 #include "emcHLRatios.h"
00022 #include "emcPedestals5.h"
00023 #include "emcGains.h"
00024 #include "emcGainFEM.h" 
00025 #include "emcLCTofs.h"
00026 #include "emcQAs.h"
00027 #include "emcWalkTofs.h" 
00028 //#include "emcTofT0s.h"
00029 #include "emcTacPeds.h"
00030 #include "EmcStaticData.h"
00031 #include "emcDataManager.h"
00032 #include "EmcSector.h"
00033 #include "emcCalibrationData.h"
00034 #include "EmcIndexer.h"
00035 #include "emcFEMtupleFactory.h"
00036 #include "emcHLRatioFEM.h"
00037 #include "emcPedestalFEM.h"
00038 #include "pbscTimingFixes.h"
00039 
00040 #include <cassert>
00041 #include <iostream>
00042 #include <fstream>
00043 #include <cmath>
00044 #include <cstdlib>
00045 #include <vector>
00046 
00047 #include "emcException.h"
00048 #include "emcDefines.h"
00049 
00050 using std::vector ;
00051 using std::cout ;
00052 using std::cerr ;
00053 using std::endl ;
00054 using std::ifstream ;
00055 using std::string;
00056 
00057 static const string kTACPED = "TAC";
00058 
00059 //-------------------------------------------------------------------------
00060 emcRawDataCalibratorV1::emcRawDataCalibratorV1() : emcCalibrator()                
00061 {
00062   fName = "emcRawDataCalibratorV1" ; // must be the classname (because it's used by the Factory)
00063   Reset() ;
00064 }
00065 
00066 //-------------------------------------------------------------------------
00067 emcRawDataCalibratorV1::~emcRawDataCalibratorV1()
00068 {
00069 }
00070 
00071 //_____________________________________________________________________________
00072 bool emcRawDataCalibratorV1::Calibrate(const emcRawDataObject& const_rdo,
00073                          emcMixedDataObject& mdo,
00074                          const PHTimeStamp& when)
00075 {
00076   /* Make ADC and TDC values out of the RawDataObject.
00077 
00078      To perform this, we need to get access to some calibration objects :
00079      a) pedestals
00080      b) high/low gain ratios
00081 
00082      Those objects are collected from the database (being file or Objy)
00083      using the DataManager.
00084 
00085      NOTE: mdo can't be zero suppressed, if done - it will create a problem 
00086      in calibrating data. Associating suppressed data with Gains will 
00087      require search what is simply too time consuming.
00088    */
00089 
00090   float INVALID_FLOAT = -9999 ;
00091 
00092   // This method lies about it's use of rdo.  It claims it'll be
00093   // treated as const, but then it modifies it.  This line is a HACK
00094   // and should be properly fixed by an expert.
00095   emcRawDataObject& rdo = const_cast<emcRawDataObject&>(const_rdo);
00096     
00097   bool rv = true ; 
00098 
00099   if (fMustCollectForMDO) {
00100     CollectForMDO(when) ;
00101     fMustCollectForMDO = false ;
00102   }
00103 
00104   // Sets the corresponding flags of the MDO
00105   mdo.SetPedestalFlag(fCollectPedestalStatus) ; 
00106   mdo.SetHLRatioFlag(fCollectHLRatioStatus) ; 
00107 
00108   // Initialize the MDO from the RDO
00109   int* datamap ;
00110   long* softkeys ;
00111   int* dataerrors ;
00112   rdo.GetDataDescriptionPointers(datamap,softkeys,dataerrors) ;
00113   mdo.SetMaxSize(rdo.GetMaxSize()) ;
00114   Int_t* deadmap = 0;
00115   Int_t* warnmap = 0 ;
00116 
00117   if(fQA){
00118     deadmap = fQA->GetDeadMap();
00119     assert(deadmap!=0) ;
00120     warnmap = fQA->GetWarnMap() ;
00121     assert(warnmap!=0) ;
00122   }
00123   mdo.SetDataDescriptionPointers(datamap,softkeys,dataerrors,deadmap,warnmap) ;
00124 
00126   Int_t index ;
00127   Float_t adc,tdc ;
00128   Float_t hgpost,hgpre,lgpost,lgpre ;
00129   Float_t phg, plg;
00130   Float_t lg, hg ;
00131   int amupre,amupost,amutac ;
00132   int data_error ;
00133   float newTDCValue, tacped;
00134   float scale=0.0;
00135 
00136   // Loop over towers
00137 
00138   for (index = 0; index < rdo.GetSize(); index++ ) {
00139 
00140     // Is the channel to be declared dead ?
00141     if ( mdo.GetDead(index) & emcQAs::IamDeadMask() ) {
00142       mdo.AddDataError(index,0x2000);
00143       mdo.SetToZero(index) ;
00144       continue;
00145     }
00146 
00147     //  rdo.IsZero should identify the channels which are pedestal 
00148     //  suppressed in DCM. We are using it at all stages of data processing 
00149     //  so - there is no real need to reset to zero locations in mdo which 
00150     //  contain non-zero values from previous event (time saving). 
00151     //  We'll keep this for the time being but - it can be removed later.
00152     
00153     if (rdo.IsZero(index)) {
00154       mdo.SetToZero(index) ;
00155       continue;
00156     }
00157 
00158     rdo.Get(index,tdc,hgpost,lgpost,hgpre,lgpre,
00159             amupre,amupost,amutac,data_error) ;
00160     rdo.Get(index,tdc,hgpost,lgpost,hgpre,lgpre,data_error) ;
00161 
00162     // ADC 
00163 
00164 
00165     //=====> different treatment for PbSc and PbGl MV 2001/08/24
00166     int TowerId = rdo.GetItemId(index); 
00167     bool chooseLowGain=true;
00168 
00169     if(EmcIndexer::isPbSc(TowerId)){
00170 
00171       chooseLowGain=data_error&0x003c || (lgpre-lgpost)>192.;
00172 
00173       scale = (fCollectHLRatioStatus? fHLRatios->getValue(index) : 15.22) ;
00174       if(scale<12.||scale>18.) scale = 15.4; 
00175       
00176     } else if(EmcIndexer::isPbGl(TowerId)){
00177       
00178       bool badHighGain=(hgpre-hgpost)<0. && (lgpre-lgpost)>50.;
00179       bool goodLowGain=(lgpre-lgpost)>170.;
00180       chooseLowGain= data_error&0x003c || badHighGain || hgpost<1024. || goodLowGain;
00181 
00182       scale = (fCollectHLRatioStatus? fHLRatios->getValue(index) : 15.33);
00183       if(scale<12.||scale>18.) scale = 15.33;
00184       
00185     }
00186 
00187     // Current decision - if both High and Low are flagged as bad :
00188     // use Low but set a flag of corrupted data
00189     adc = lgpre-lgpost ;
00190     if(chooseLowGain) {
00191       //  Low Gain leg selected
00192       if (fCollectPedestalStatus&&fUseAmpPedestals) {
00193         plg = (-fPedestals->getValue(index, amupre, "LG_Pre")+
00194                fPedestals->getValue(index, amupost, "LG_Post")) ;
00195         if(fabs(plg)<20.)   adc += plg;
00196       }
00197       adc *= scale;
00198     } else {
00199       //  High Gain leg selected
00200       adc =  hgpre-hgpost ;
00201       if (fCollectPedestalStatus&&fUseAmpPedestals) { 
00202         phg = (-fPedestals->getValue(index, amupre, "HG_Pre")+
00203                fPedestals->getValue(index, amupost, "HG_Post")) ;
00204         if(fabs(phg)<20.)   adc += phg;
00205       }
00206     }
00207     if (data_error&0x23c0) {
00208       rdo.AddDataError(index,0x2000);
00209     }
00210 
00211     // TDC
00212     if(tdc>=4095) tdc = 0;
00213     newTDCValue = INVALID_FLOAT ;
00214 
00215     if (fCollectPedestalStatus) {
00216       newTDCValue = tdc;
00217       tacped   = fPedestals->getValue(index,rdo.GetTACCell(index), kTACPED) ; 
00218       //  FIXME: this is to protect against zero pedestals in TAC
00219       tdc -= ((tacped>0)? tacped : fPedestals->getValue(index, 0, kTACPED)) ; 
00220     }
00221 
00222     if (mdo.IsUsingHGLG()) {
00223       hg = hgpre-hgpost ;
00224       lg = lgpre-lgpost ;
00225       rdo.GetCells(index,amupre,amupost,amutac);
00226       if (fCollectPedestalStatus) {     
00227         //      cout<<index<<" "<<amupre<<" "<<amupost<<endl;
00228         phg = (-fPedestals->getValue(index, amupre, "HG_Pre")+
00229                fPedestals->getValue(index, amupost, "HG_Post")) ;
00230         plg = (-fPedestals->getValue(index, amupre, "LG_Pre")+
00231                fPedestals->getValue(index, amupost, "LG_Post")) ;
00232 
00233         hg += phg;
00234         lg += plg;
00235       }
00236       mdo.Set(index, adc, tdc, hg, lg) ;
00237     }
00238     else {
00239       mdo.Set(index, adc, tdc) ;
00240     }
00241 
00242   }
00243 
00244   return rv ; 
00245 } 
00246 
00247 //_____________________________________________________________________________
00248 bool emcRawDataCalibratorV1::Calibrate(const emcMixedDataObject& mdo,
00249                          emcCalibratedDataObject& cdo,
00250                          const PHTimeStamp& when)
00251 {
00252   /* Make Energie (GeV) and Time (ns) values out of the MixedDataObject 
00253 
00254      To perform this, we need to get access to some calibration objects :
00255      a) time-dependant gains
00256      b) least-count tofs
00257      c) walk tofs
00258 
00259      Those objects are collected from the database (being file or Objy)
00260      using the DataManager.
00261 
00262      We also need time-zero calibrations to get the final GeV values.
00263 
00264    */
00265 
00266   if ( fECalAtT0.empty() ) {
00267     if(!GetECalAtT0(when,true)) return false;
00268   }
00269   bool rv = true ; 
00270   Int_t index ; 
00271   Int_t eventTime   = when.getTics();
00272 
00273   if (fMustCollectForCDO) {
00274     CollectForCDO(when) ;
00275     tf = pbscTimingFixes::getInstance();
00276     if(!(tf->areFixesLoaded())) tf = 0;
00277     fMustCollectForCDO = false ;
00278   }
00279 
00280   bool energycalibrated = true; 
00281   bool timecalibrated   = true;
00282   
00283   cdo.Reset();
00284 
00285   Float_t adc, tdc ; 
00286   Int_t   outindex =0; 
00287   Float_t etotal   =0.;
00288 
00289   // Loop over towers
00290   for (index = 0; index < mdo.GetSize(); index++ ) {
00291 
00292     int TowerId = mdo.GetItemId(index) ; 
00293 
00294     // Skip reference towers, if any.
00295     if (EmcIndexer::isReference(TowerId)) continue;
00296 
00297     adc = mdo.GetADC(index) ; 
00298 
00299     int errorFlag = mdo.GetErrorFlag(index);
00300 
00301     if( adc > GetThresholdADC() && !(errorFlag&0x2000)) { 
00302       tdc = mdo.GetTDC(index) ; 
00303       if (!(errorFlag&0x2400)) {
00304         timecalibrated &= CalibrateTime(tdc, adc, index, TowerId, eventTime );
00305       }
00306       energycalibrated &= CalibrateEnergy( adc, index, TowerId, eventTime) ;
00307       cdo.Set(outindex, TowerId, mdo.GetSoftwareKey(index), errorFlag, 
00308               adc, tdc,mdo.GetDead(index),mdo.GetWarn(index)) ;
00309       etotal += adc ;
00310       outindex++;
00311     } 
00312     else {
00313       if(!fZeroSuppression) {
00314         cdo.Set(outindex, TowerId, mdo.GetSoftwareKey(index), 
00315                 errorFlag, 0., 0.,mdo.GetDead(index),mdo.GetWarn(index)) ;
00316         outindex++;
00317        }
00318     }
00319   }  
00320   cdo.SetTotalEnergy(etotal);
00321   cdo.SetZeroSuppressedFlag(fZeroSuppression);
00322   cdo.SetEnergyCalibratedFlag(energycalibrated);
00323   cdo.SetTimeCalibratedFlag(timecalibrated) ; 
00324 
00325   return rv ; 
00326  } 
00327 
00328 //_____________________________________________________________________________
00329 bool emcRawDataCalibratorV1::Calibrate(const emcRawDataObject& rdo,
00330                          emcCalibratedDataObject& cdo,
00331                          const PHTimeStamp& when)
00332 {
00337   emcMixedDataObject mdo ;
00338   bool rv = Calibrate(rdo,mdo,when) ;
00339   rv &= Calibrate(mdo,cdo,when) ;
00340   return rv ;
00341 }
00342 
00343 //-------------------------------------------------------------------------
00344 bool 
00345 emcRawDataCalibratorV1::CalibrateEnergy(Float_t & adc, const Int_t index, 
00346                                       const Int_t TowerId,int incrementalTime) 
00347 {
00348   if (fCollectGainStatus==false) { return false; }
00349 
00350   float normt, dummy;
00351   dummy = adc;
00352   if(EmcIndexer::isPbSc(TowerId)) {
00353     // PBSC
00354     normt = fGains->getValue(index,incrementalTime) ;
00355     adc  *= ((normt>0.01)?  fECalAtT0[index]/normt : 0.) ; 
00356   } else {
00357     assert(EmcIndexer::isPbGl(TowerId)) ;
00358     // PBGL  - EXPLICITLY NO TIMING DEPENDENCE FOR NOW 
00359     normt = fGains->getValue(index,0) ;
00360     adc  *= ((normt>0.01)?  fECalAtT0[index]/normt : 0.) ; 
00361   }
00362   //  cout<<"<CE> Index "<<index<<" TowerId "<<TowerId<<" ADC "<<dummy<<" NORM "<<normt<<" ECal "<<fECalAtT0[index]<<" Energy "<<adc<<endl;
00363   return true ;
00364 } 
00365 
00366 //_________________________________________________________________________
00367 bool  
00368 emcRawDataCalibratorV1::CalibrateTime(Float_t & tdc, float adc, 
00369                                     const Int_t index, const Int_t TowerId,
00370                                     int incrementalTime)
00371 {
00372   if (fCollectTofStatus==false) { return false; }
00373   float lc = fLCTofs->GetValue1(index);
00374   float t0 = 0.;
00375   //  EK 01/11/02
00376   //  we will now use time-dependent TAC pedestals to correct TAC first
00377   if(fTacPeds) {
00378     //    cout<<"TAC ped "<<index<<" "<<TowerId<<" "<<incrementalTime<<" "<<tdc<<" "<<fTacPeds->getValue((int)index, incrementalTime)<<endl;
00379     tdc -= fTacPeds->getValue((int)index, incrementalTime);
00380   }
00381 
00382 
00383 
00384 //    if(!fTofT0s) {
00385 //    //  constant T0 computed at a time of walk calibration
00386 //      t0 = fWalkTofs->GetValue1(index);
00387 //    } else {
00388 //    //  variable T0 computed using the data used to prodce gains
00389 //      t0 = fTofT0s->getValue(index, incrementalTime);
00390 //      //    cout<<index<<" "<<t0<<" "<<fWalkTofs->GetValue1(index)<<endl;
00391 //    }
00392 
00393 
00394 
00395 
00396   float wk = fWalkTofs->GetValue2(index);
00397   float dt;
00398 
00399   //=====> different functions for PbGl and PbSc MV 2001/08/24  
00400   if(TowerId<15552){
00401     lc = ((lc>25.&&lc<45.)? lc : 38.2)/1000.;    
00402     dt = ((adc>0.)? wk*log(adc)*1000. : 0.);
00403     tdc =  - (tdc-t0-dt)*lc;
00404     if(tf){
00405       int iS, iSM, iSMT;
00406       EmcIndexer::iPXiSiSMiSMT(TowerId,iS,iSM,iSMT);
00407       tdc -= (tf->getSectorT0(iS)+tf->getSMT0(iSM)+tf->getFEMTPattern(iSMT)+tf->getTowerT0(iSM,iSMT));
00408     }
00409     //    cout<<lc<<" "<<dt<<endl;
00410   } else if(TowerId<24768){
00411 
00412     // Changed at Maxim's request, Sep 28, 2001 GD
00413     //    //=====> recalculate adc to low gain MV 2001/08/24
00414     //    float scale;
00415 
00416 
00417     //    if(scale<12.|| scale>18.) scale = 15.33;
00418 
00419     //    lc = ((lc>20. && lc<55.)? lc : 38.9)/1000.;
00420     //    float lgadc=adc/scale;
00421     //    dt=((lgadc>0.)? wk*pow(lgadc, -0.2): 0.);
00422 
00423     // MV 2001/09/27 walk correction is calculated using physics data,
00424     // so T0 should be taken care of automatically
00425     t0 = fWalkTofs->GetValue1(index);
00426     lc = ((lc>20. && lc<55.)? lc : 38.9)/1000.;
00427     dt=((adc>0.)? wk*log(adc): 0.);
00428     tdc =  - (tdc-t0-dt)*lc + GetGlobalT0() ;
00429   }
00430 
00431   //  if(adc>250.) cerr<<"adc="<<adc<<" TOF t="<<t<<" lc="<<lc<<" t0="<<t0<<" wk="<<wk<<" dt="<<dt<<" T0="<<GetGlobalT0()<<" tdc="<<tdc<<endl;
00432   //  cout<<"<CT> Index "<<index<<" TowerId "<<TowerId<<" ADC "<<adc<<" TDC "<<dummy<<" TIME "<<tdc<<endl;
00433   return true ;
00434 
00435 }
00436 
00437 //_____________________________________________________________________________
00438 void emcRawDataCalibratorV1::CollectForCDO(const PHTimeStamp& when)
00439 {
00440   // Collection of gain and time calibration data.
00441   cout << "emcRawDataCalibratorV1::CollectForCDO" << endl ;
00442 
00443   static emcGains    gainDriver ;
00444   static emcLCTofs   lctofDriver ;
00445   static emcWalkTofs walktofDriver ;
00446 
00447   //  EK 01/11/02
00448   //  static emcTofT0s   drifttofDriver;
00449   static emcTacPeds   drifttofDriver;
00450   
00451   emcDataManager* dm = emcDataManager::GetInstance() ;
00452   
00453   // FIXME : THIS WILL WORK ONLY IF ALL DATA HAVE THE SAME START 
00454   // OF VALIDITY TIME 
00455   // Try to collect Tof-related data
00456   if ( fTofSource != emcManageable::kNone && maxFailTof ) {
00457     // Try to collect ToF drifts
00458     drifttofDriver.SetSource( fTofSource ) ;
00459     
00460     //  EK 01/11/02
00461     //    fTofT0s   = dynamic_cast<emcTofT0s*>( dm->Collect(drifttofDriver, when)) ; 
00462     fTacPeds   = dynamic_cast<emcTacPeds*>( dm->Collect(drifttofDriver, when)) ; 
00463     if(!fTacPeds) cout<<"failed to collect drifts"<<endl;
00464     // Try to collect least counts for ToF 
00465     lctofDriver.SetSource( fTofSource ) ;
00466     //    cout<<"Collecting LCTOFs"<<endl;
00467     fLCTofs = dynamic_cast<emcLCTofs*>( dm->Collect(lctofDriver, when) ) ;
00468     //    cout<<"Collecting LCTOFs - done"<<endl;
00469 
00470     // Try to collect walks for ToF 
00471     walktofDriver.SetSource( fTofSource ) ;
00472     fWalkTofs = dynamic_cast<emcWalkTofs*>( dm->Collect(walktofDriver, when)) ;
00473 
00474 
00475 
00476     // (*1) ... here it might be that collection was not ok ...
00477     fCollectTofStatus = (fLCTofs!=0 && fWalkTofs!=0) ;
00478     maxFailTof -= !fCollectTofStatus ;
00479   }
00480 
00481 
00482   // by default we consider that collection never happened... (*1)
00483   if (maxFailGain) fCollectGainStatus = false ;
00484   
00485   // Try to collect Gains
00486   if ( fGainsSource != emcManageable::kNone && maxFailGain ) {
00487     gainDriver.SetSource( fGainsSource ) ;
00488     fGains = dynamic_cast<emcGains*>( dm->Collect(gainDriver, when) ) ;
00489     // (*1) ... here it might be that collection was not ok ...
00490     fCollectGainStatus = (fGains!=0) ;
00491     maxFailGain -= !fCollectGainStatus ;
00492   } 
00493   
00494   if ( fVerbose>=1 ) {
00495     if (!fCollectTofStatus) {
00496       cerr << "<E> emcRawDataCalibratorV1::CollectForCDO " << endl 
00497            << "    ToFs collection failed. " << endl ;
00498     }
00499     if (!fCollectGainStatus) {
00500       cerr << "<E> emcRawDataCalibratorV1::CollectForCDO() " << endl
00501            << "    Gains collection failed. " << endl ;
00502     }
00503   }
00504 }
00505 
00506 //_____________________________________________________________________________
00507 void emcRawDataCalibratorV1::CollectForMDO(const PHTimeStamp& when)
00508 {
00509   // Collection of pedestals, hlratios and Q&As
00510   cout << "emcRawDataCalibratorV1::CollectForMDO" << endl ;
00511 
00512   static emcPedestals5 pedDriver ;
00513   static emcHLRatios hlrDriver ;
00514   static emcQAs qaDriver ;
00515 
00516   emcDataManager* dm = emcDataManager::GetInstance() ;
00517 
00518   // by default we consider that collection never happened... (*1)
00519   fCollectPedestalStatus = fCollectHLRatioStatus = false ;
00520 
00521   // Try to collect pedestals
00522   if ( fPedestalsSource != emcManageable::kNone && maxFailPed ) {
00523     pedDriver.SetSource( fPedestalsSource ) ;
00524     fPedestals = dynamic_cast<emcPedestals5*>( dm->Collect(pedDriver, when) ) ;
00525     // (*1) ... here it might be that collection was not ok ...
00526     fCollectPedestalStatus = (fPedestals!=0) ;
00527     maxFailPed -= !fCollectPedestalStatus ;
00528   }
00529 
00530   if( fVerbose>=1 && !fCollectPedestalStatus) {
00531     cout << "<E> emcRawDataCalibratorV1::CollectFromMDO() " << endl
00532          << "    Pedestal collection failed." << endl ;
00533   }
00534 
00535   // Try to collect H/L Ratios
00536   if ( fHLRatiosSource != emcManageable::kNone && maxFailHLR ) {
00537     // (*1) ... here it might also be that collection was not ok ...
00538     hlrDriver.SetSource( fHLRatiosSource) ;
00539     fHLRatios = dynamic_cast<emcHLRatios*>( dm->Collect(hlrDriver, when) ) ;
00540     // (*1) ... here it might be that collection was not ok ...
00541     fCollectHLRatioStatus = (fHLRatios!=0) ;
00542     maxFailHLR -= !fCollectHLRatioStatus ;
00543   }
00544 
00545   // Try to collect Q&A objects
00546   if ( fQASource != emcManageable::kNone ) {
00547 
00548     // Set some parameters for the qa object we want
00549     qaDriver.SetSource(fQASource) ;
00550 
00551     // FIXME: unclear if this is correctly made - call on every event
00552     qaDriver.SetExtraRejectListFilename(fExtraRejectListFilename.c_str()) ;
00553 
00554     // Collect it
00555     fQA = dynamic_cast<emcQAs*>( dm->Collect(qaDriver,when) ) ;
00556     if (!fQA) {
00557       cerr << "<E> Could not collect Q&A ?!" << endl ;
00558     }
00559     assert(fQA!=0) ;
00560   }
00561 
00562   if ( fVerbose>=1 && !fCollectHLRatioStatus) {
00563     cout << "<E> emcRawDataCalibratorV1::CollectForMDO(r) " << endl
00564          << "    HLRatio collection failed." << endl ;
00565   }
00566 }
00567 
00568 //_____________________________________________________________________________
00569 void
00570 emcRawDataCalibratorV1::ForceDBCollection(const PHTimeStamp& when)
00571 {
00572   CollectForMDO(when);
00573   fMustCollectForMDO = false;
00574   GetECalAtT0(when,true);
00575   CollectForCDO(when);
00576   fMustCollectForCDO = false;
00577 }
00578 
00579 //_____________________________________________________________________________
00580 bool 
00581 emcRawDataCalibratorV1::GetECalAtT0(const PHTimeStamp& when, 
00582                                   bool normalizationON) 
00583 {
00584   // Get initial calibration data.
00585 
00586   if (!fECalAtT0.empty()) { 
00587     // we already have it 
00588     return true ;
00589   }
00590 
00591   emcRawDataAccessor * rda = emcRawDataAccessor::GetInstance() ; 
00592 
00593   if (!rda) {
00594     cerr << "<E> emcRawDataCalibratorV1:: GetECalAtT0 " 
00595          << "- Cannot fetch time 0 calibration - No data accessor " << endl ;
00596     return false ; 
00597   }
00598 
00599   if (GetVerbose()>0) {
00600     cout << "<I> emcRawDataCalibratorV1::GetECalAtT0 : *** Reading from [" ;
00601     cout << emcManageable::GetStorageName(fIniCalSource) << "]" << endl ;
00602   }
00603 
00604   size_t ntowers = 144*rda->GetDynamicData()->getnSM() ;
00605 
00606   fECalAtT0.resize(ntowers) ;
00607 
00608   PHTimeStamp* timestamp = 0 ;
00609   EmcStaticData* sd = 0 ;
00610 
00611   // here we're just exercizing C++ exceptions.
00612   // Might not be the most suitable place for that, so 
00613   // we might revert to old error handling later.
00614   // Anyway, if the sector can not be built, we should
00615   // exit asap (because init. calibrations are simply
00616   // not there).
00617   try {
00618     sd = EmcStaticData::buildEmcStaticData() ;
00619   }
00620   catch (emcException& err) {
00621     cerr << "Got exception : " << err.what() << endl ;
00622     exit(1) ;
00623   }
00624 
00625   const int * DataMap        = rda->GetDynamicData()->getEmcMap();
00626   int item ;
00627   int sn, ist ;
00628   EmcSector * sector ;
00629   float encal, norm0 ;
00630   float nothing ;
00631   float c0,g0,cf ;
00632   // this is "WA98" kappa factor
00633   float kappa = 1.0/5.9950 ; 
00634 
00635   if ( fIniCalSource == emcManageable::kDB_Objy ) { 
00636     PHTimeStamp& ts = const_cast<PHTimeStamp&>(when) ;
00637     timestamp = &ts ; 
00638   }
00639 
00640   for (item = 0; item < rda->GetDynamicData()->getEmcSize(); item++) {
00641 
00642     EmcIndexer::iPXiSiST(DataMap[item], sn, ist) ;
00643 
00644     assert(sn>=0) ;
00645 
00646     // skip references
00647     if (sn>=8) continue ;
00648 
00649     sector = sd->getSector(sn) ;
00650 
00651     if( !sector ) {
00652       // Sector will be built from file if timestamp=0, 
00653       // from Objy DB otherwise (assuming there's calibration
00654       // constants valid at timestamp in the DB, of course!)
00655       sd ->buildEmcSector( EmcIndexer::EmcSectorId(sn), timestamp ) ;
00656       sector = sd->getSector(sn) ;
00657     }
00658  
00659     assert(sector->IsOK()) ;
00660 
00661     if ( sn < 6 ) {      
00662       // PbSc
00663       sector->GetEnergyCalibration(ist,encal,norm0,nothing) ;
00664       assert(nothing==1.0) ;
00665     }
00666     else {
00667       // PbGl
00668       sector->GetEnergyCalibration(ist,c0,g0,cf) ;
00669       encal = c0*g0*cf ; 
00670       norm0 = kappa ;
00671     }   
00672 
00673     fECalAtT0[item] = encal * ( normalizationON ? norm0 : 1. ) ;      
00674 
00675   } // end of loop over item
00676 
00677   return true ;
00678 }
00679 
00680 //_____________________________________________________________________________
00681 bool emcRawDataCalibratorV1::GetCollectionStatus(const char* type) const
00682 {
00683   string stype = type ;
00684   if ( stype == "Pedestals" ) {
00685     return fCollectPedestalStatus ;
00686   }
00687   else if ( stype == "HLRatios" ) {
00688     return fCollectHLRatioStatus ;
00689   }
00690   else if ( stype == "Gains" ) {
00691     return fCollectGainStatus ;
00692   }
00693   else if ( stype == "Tofs") {
00694     return fCollectTofStatus ;
00695   }
00696   else if ( stype == "*" ) {
00697     return 
00698       fCollectPedestalStatus &
00699       fCollectHLRatioStatus & 
00700       fCollectGainStatus &
00701       fCollectTofStatus ;
00702   }
00703   return false ;
00704 }
00705 
00706 
00707 //_____________________________________________________________________________
00708 void emcRawDataCalibratorV1::SetCollectionStatus(const char* type)
00709 {
00710   string stype = type ;
00711   if ( stype == "Pedestals" ) {
00712     fCollectPedestalStatus = true;
00713   }
00714   else if ( stype == "HLRatios" ) {
00715     fCollectHLRatioStatus  = true;
00716   }
00717   else if ( stype == "Gains" ) {
00718     fCollectGainStatus = true;
00719   }
00720   else if ( stype == "Tofs") {
00721     fCollectTofStatus = true;
00722   }
00723   else if ( stype == "*" ) {
00724     fCollectPedestalStatus = true;
00725     fCollectHLRatioStatus  = true; 
00726     fCollectGainStatus     = true;
00727     fCollectTofStatus      = true;
00728   }
00729 }
00730 
00731 //_____________________________________________________________________________
00732 
00733 void emcRawDataCalibratorV1::Print() const
00734 {
00735   cout << " emcRawDataCalibratorV1 setup : " << endl ;  
00736 
00737   cout  << " **** Pedestals  " ; 
00738   if ( fPedestalsSource == emcManageable::kFile_ASCII ) {
00739     cout << "are read from files " << endl ; 
00740   }
00741   else if ( fPedestalsSource == emcManageable::kDB_Objy  ) {
00742     cout << "are read from Database " << endl ; 
00743   }
00744   else {
00745     cout << " = 0 (collection disabled)" << endl ; 
00746   }
00747 
00748   cout << " **** high/low gain ratios  " ; 
00749   if ( fHLRatiosSource ==  emcManageable::kFile_ASCII ) {
00750     cout << "are read from files " << endl ; 
00751   }
00752   else if ( fHLRatiosSource ==  emcManageable::kDB_Objy ) {
00753     cout << "are read from Database " << endl ; 
00754   }
00755   else {
00756     cout << " = 16 " << endl ; 
00757   }
00758 
00759   cout << " **** Gains (current normalization)  " ; 
00760   if ( fGainsSource ==  emcManageable::kFile_ASCII ) {
00761     cout << "are read from files " << endl ; 
00762   }
00763   else if ( fGainsSource ==  emcManageable::kDB_Objy ) {
00764     cout << "are read from Database " << endl ; 
00765   }
00766   else {
00767     cout << " = 1 " << endl ; 
00768   }
00769 
00770   cout << " **** ToF calibration (least counts and walks)  " ; 
00771   if ( fTofSource ==  emcManageable::kFile_ASCII ) {
00772     cout << "are read from files " << endl ; 
00773   }
00774   else if ( fTofSource ==  emcManageable::kDB_Objy ) {
00775     cout << "are read from Database " << endl ; 
00776   }
00777   else {
00778     cout << " = 1 " << endl ; 
00779   }
00780 
00781   cout << " **** Q&A " ;
00782   if ( fQASource == emcManageable::kFile_ASCII ) {
00783     cout << "are read from files " << endl ;
00784   }
00785   else if ( fQASource == emcManageable::kDB_Objy ) {
00786     cout << "are read from Database " << endl ;
00787   }
00788   else {
00789     cout << "are not read (perfect towers assumed) " << endl ;
00790   } 
00791 
00792   cout << " **** Initial Calibration " ;
00793   if ( fIniCalSource == emcManageable::kFile_ASCII) { 
00794     cout << "are read from files " << endl ;
00795   }
00796   else if ( fIniCalSource == emcManageable::kDB_Objy ) {
00797     cout << "are read from Database " << endl ;
00798   }
00799   else {
00800     cout << " Huh ? : check your IniCal source !" << endl ;
00801   }
00802 
00803   cout << " **** High gain threshold = " <<  GetHighLowLimit() << endl ; 
00804   cout << " **** ADC threshold = " << GetThresholdADC() << endl ;
00805   cout << " **** Zero suppression is " << 
00806     ((fZeroSuppression==true)?"ON":"OFF") << endl ;
00807 
00808 }
00809 
00810 //_____________________________________________________________________________
00811 void emcRawDataCalibratorV1::Reset(void)
00812 {
00813   fPedestalsSource = emcManageable::kNone ; 
00814   fHLRatiosSource  = emcManageable::kNone ;  
00815   fGainsSource     = emcManageable::kNone ;  
00816   fTofSource       = emcManageable::kNone ;  
00817   fQASource        = emcManageable::kNone ;
00818   fIniCalSource    = emcManageable::kNone ;
00819   fCollectPedestalStatus = fCollectHLRatioStatus = false ;
00820   fCollectGainStatus    = fCollectTofStatus     = false;
00821   maxFailPed = maxFailHLR = maxFailGain = maxFailTof = 10;
00822   fECalAtT0.clear() ;
00823   fZeroSuppression = false;
00824   fQA = 0 ;
00825   SetExtraRejectListFilename();
00826   fPedestals = 0 ;
00827   fHLRatios = 0 ;
00828   fGains = 0 ;
00829   fLCTofs = 0 ;
00830   fWalkTofs = 0 ;
00831   fTofT0s = 0 ;
00832   fMustCollectForCDO = true ;
00833   fMustCollectForMDO = true ;
00834   tf=0;
00835 }
00836 
00837 //-------------------------------------------------------------------------
00838 bool emcRawDataCalibratorV1::SelectSource(const char* type, 
00839                                         emcManageable::EStorage source)
00840 {
00841   /* The validity of the source parameter depends on the mapping style
00842      of the calorimeter. If 144 words per FEM, all sources allowed. 
00843      If 192 words per FEM, no source at all allowed.
00844   */
00845   
00846   emcRawDataAccessor * rda = emcRawDataAccessor::GetInstance() ; 
00847 
00848   if (!rda) {
00849     cerr << "<E> emcRawDataCalibratorV1:: SelectSource - Cannot check source is valid because I cannot get access to RDA. So I am NOT changing the source." << endl ;
00850     return false ;
00851   }
00852   else {
00853     /* Check that the source is valid. 
00854        Basically, if we deal with 144 words per FEM, all sources allowed,
00855        if 192 words per FEM, valid source = none. */
00856     if ( rda->GetDynamicData()->getMapStyle() == false &&
00857          source != emcManageable::kNone ) {
00858       cerr << "<E> emcRawDataCalibratorV1::SelectSource - Map style is 192 words per FEM. Cannot use any source." << endl ;
00859       return false ;
00860     }
00861   }
00862  
00863   bool rv = true ; 
00864   string stype = type ;
00865   
00866   if ( stype == "Pedestals" ) 
00867     fPedestalsSource = source ; 
00868   
00869   else if ( stype == "HLRatios" ) 
00870     fHLRatiosSource = source ;
00871 
00872   else if ( stype == "Gains" ) 
00873     fGainsSource    = source ;
00874 
00875   else if ( stype == "ToF" ) 
00876     fTofSource    = source ;
00877 
00878   else if ( stype == "QAs" )
00879     fQASource = source ;
00880 
00881   else if ( stype == "IniCal" ) 
00882     fIniCalSource = source ; 
00883 
00884   else if ( stype == "*" ) {
00885     fPedestalsSource = source ; 
00886     fHLRatiosSource = source ;
00887     fGainsSource    = source ;
00888     fQASource = source ;
00889     fTofSource    = source ;   
00890     fIniCalSource = source ;
00891   }
00892   else {
00893     cerr << "emcRawDataCalibratorV1::SelectSource: " << type << " is an unknown type " << endl
00894          << "                                    Valid types are Pedestals, HLRatios, Gains, ToF " << endl ; 
00895     rv = false ; 
00896   }
00897   
00898   return rv ; 
00899   
00900 }
00901 
00902 //_________________________________________________________________________
00903 // Set Global T0 for every calorimeter Tower 
00904 // The simplest solution for now was to write an ASCII file without any DB connectivity and load it at will
00905 void emcRawDataCalibratorV1::SetTwrGlobalT0(char * filename){
00906   if (fTwrGlobalT0) {
00907     delete [] fTwrGlobalT0;
00908     fTwrGlobalT0 = NULL;
00909   }
00910   if(!filename)         return;
00911   ifstream fin(filename, std::ios::in);
00912   emcRawDataAccessor * rda = emcRawDataAccessor::GetInstance();
00913   assert(rda!=0);
00914   emcRawDataObject   * rdo = rda->GetRawDataObject() ;
00915   assert(rdo!=0);
00916   fTwrGlobalT0 = new float [rdo->GetMaxSize()];
00917   int lines = 0;
00918   int twr, sector, iX, iY, entries;
00919   float t0, rms;
00920   while(fin>>twr>>sector>>iX>>iY>>t0>>rms>>entries){
00921     // cout<<lines<<" "<<twr<<" "<<sector<<" "<<iX<<" "<<iY<<" "<<t0<<" "<<rms<<" "<<entries<<endl;
00922     assert(lines<rdo->GetMaxSize());
00923     fTwrGlobalT0[lines] = ((entries>2)? t0 : 0.);
00924     lines++;
00925   }
00926   assert(lines==rdo->GetMaxSize());
00927 }