00001
00008 #include "RecoLocalCalo/EcalRecProducers/interface/EcalFixedAlphaBetaFitUncalibRecHitProducer.h"
00009 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
00010
00011 #include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
00012 #include "DataFormats/Common/interface/Handle.h"
00013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00014
00015 #include <iostream>
00016 #include <cmath>
00017 #include <fstream>
00018
00019 #include "FWCore/Framework/interface/ESHandle.h"
00020
00021
00022
00023 #include "DataFormats/EcalRecHit/interface/EcalUncalibratedRecHit.h"
00024 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00025
00026
00027
00028 #include <vector>
00029
00030 #include "CondFormats/EcalObjects/interface/EcalPedestals.h"
00031 #include "CondFormats/DataRecord/interface/EcalPedestalsRcd.h"
00032
00033 #include "CondFormats/EcalObjects/interface/EcalMGPAGainRatio.h"
00034 #include "CondFormats/EcalObjects/interface/EcalGainRatios.h"
00035 #include "CondFormats/DataRecord/interface/EcalGainRatiosRcd.h"
00036
00037 EcalFixedAlphaBetaFitUncalibRecHitProducer::EcalFixedAlphaBetaFitUncalibRecHitProducer(const edm::ParameterSet& ps) {
00038
00039 EBdigiCollection_ = ps.getParameter<edm::InputTag>("EBdigiCollection");
00040 EEdigiCollection_ = ps.getParameter<edm::InputTag>("EEdigiCollection");
00041 EBhitCollection_ = ps.getParameter<std::string>("EBhitCollection");
00042 EEhitCollection_ = ps.getParameter<std::string>("EEhitCollection");
00043
00044
00045
00046
00047 produces< EBUncalibratedRecHitCollection >(EBhitCollection_);
00048 produces< EEUncalibratedRecHitCollection >(EEhitCollection_);
00049
00050
00051 alphaEB_= ps.getParameter<double>("alphaEB");
00052 betaEB_= ps.getParameter<double>("betaEB");
00053 alphaEE_= ps.getParameter<double>("alphaEE");
00054 betaEE_= ps.getParameter<double>("betaEE");
00055
00056 alphabetaFilename_= ps.getUntrackedParameter<std::string>("AlphaBetaFilename","NOFILE");
00057 useAlphaBetaArray_=setAlphaBeta();
00058 if(!useAlphaBetaArray_){edm::LogInfo("EcalUncalibRecHitError") << " No alfa-beta file found. Using the deafult values.";}
00059
00060 algoEB_.SetMinAmpl( ps.getParameter<double> ("MinAmplBarrel") );
00061 algoEE_.SetMinAmpl( ps.getParameter<double> ("MinAmplEndcap") );
00062
00063 bool dyn_pede = ps.getParameter<bool>("UseDynamicPedestal");
00064 algoEB_.SetDynamicPedestal(dyn_pede);
00065 algoEE_.SetDynamicPedestal(dyn_pede);
00066
00067 }
00068
00069
00070 EcalFixedAlphaBetaFitUncalibRecHitProducer::~EcalFixedAlphaBetaFitUncalibRecHitProducer() {
00071 }
00072
00073
00074 bool EcalFixedAlphaBetaFitUncalibRecHitProducer::setAlphaBeta(){
00075 std::ifstream file(alphabetaFilename_.c_str());
00076 if (! file.is_open())
00077 return false;
00078
00079 alphaBetaValues_.resize(36);
00080
00081 char buffer[100];
00082 int sm, cry,ret;
00083 float a,b;
00084 std::pair<double,double> p(-1,-1);
00085
00086 while( ! file.getline(buffer,100).eof() ){
00087 ret=sscanf(buffer,"%d %d %f %f", &sm, &cry, &a, &b);
00088 if ((ret!=4)||
00089 (sm<=0) ||(sm>36)||
00090 (cry<=0)||(cry>1700)){
00091
00092 continue;
00093 }
00094
00095 if (alphaBetaValues_[sm-1].size()==0){
00096 alphaBetaValues_[sm-1].resize(1700,p);
00097 }
00098 alphaBetaValues_[sm-1][cry-1].first = a;
00099 alphaBetaValues_[sm-1][cry-1].second = b;
00100
00101 }
00102
00103 file.close();
00104 return true;
00105 }
00106
00107 void
00108 EcalFixedAlphaBetaFitUncalibRecHitProducer::produce(edm::Event& evt, const edm::EventSetup& es) {
00109 using namespace edm;
00110
00111 Handle< EBDigiCollection > pEBDigis;
00112 Handle< EEDigiCollection > pEEDigis;
00113
00114 const EBDigiCollection* EBdigis =0;
00115 const EEDigiCollection* EEdigis =0;
00116
00117
00118 if ( EBdigiCollection_.label() != "" && EBdigiCollection_.instance() != "" ) {
00119 evt.getByLabel( EBdigiCollection_, pEBDigis);
00120
00121 if ( pEBDigis.isValid() ) {
00122 EBdigis = pEBDigis.product();
00123 edm::LogInfo("EcalUncalibRecHitInfo") << "total # EBdigis: " << EBdigis->size();
00124 } else {
00125 edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product for EB: " << EBdigiCollection_;
00126 }
00127 }
00128
00129 if ( EEdigiCollection_.label() != "" && EEdigiCollection_.instance() != "" ) {
00130 evt.getByLabel( EEdigiCollection_, pEEDigis);
00131
00132 if ( pEEDigis.isValid() ) {
00133 EEdigis = pEEDigis.product();
00134 edm::LogInfo("EcalUncalibRecHitInfo") << "total # EEdigis: " << EEdigis->size() ;
00135 } else {
00136 edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product for EE: " << EEdigiCollection_;
00137 }
00138 }
00139
00140
00141 LogDebug("EcalUncalibRecHitDebug") << "fetching gainRatios....";
00142 edm::ESHandle<EcalGainRatios> pRatio;
00143 es.get<EcalGainRatiosRcd>().get(pRatio);
00144 const EcalGainRatioMap& gainMap = pRatio.product()->getMap();
00145
00146
00147
00148 LogDebug("EcalUncalibRecHitDebug") << "fetching pedestals....";
00149 edm::ESHandle<EcalPedestals> pedHandle;
00150 es.get<EcalPedestalsRcd>().get( pedHandle );
00151 const EcalPedestalsMap & pedMap = pedHandle.product()->getMap();
00152 LogDebug("EcalUncalibRecHitDebug") << "done." ;
00153
00154
00155 std::auto_ptr< EBUncalibratedRecHitCollection > EBuncalibRechits( new EBUncalibratedRecHitCollection );
00156 std::auto_ptr< EEUncalibratedRecHitCollection > EEuncalibRechits( new EEUncalibratedRecHitCollection );
00157
00158
00159
00160
00161 EcalPedestalsMapIterator pedIter;
00162 EcalPedestals::Item aped;
00163
00164 EcalGainRatioMap::const_iterator gainIter;
00165 EcalMGPAGainRatio aGain;
00166
00167
00168 if( EBdigis ){
00169 for(EBDigiCollection::const_iterator itdg = EBdigis->begin(); itdg != EBdigis->end(); ++itdg) {
00170
00171
00172 LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EBDetId(itdg->id()) ;
00173 pedIter = pedMap.find(itdg->id());
00174 if( pedIter != pedMap.end() ) {
00175 aped = (*pedIter);
00176 } else {
00177 edm::LogError("EcalUncalibRecHitError") << "error!! could not find pedestals for channel: " << EBDetId(itdg->id())
00178 << "\n no uncalib rechit will be made for this digi!"
00179 ;
00180 continue;
00181 }
00182 double pedVec[3];
00183 pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
00184
00185
00186 LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EBDetId(itdg->id()) ;
00187 gainIter = gainMap.find(itdg->id());
00188 if( gainIter != gainMap.end() ) {
00189 aGain = (*gainIter);
00190 } else {
00191 edm::LogError("EcalUncalibRecHitError") << "error!! could not find gain ratios for channel: " << EBDetId(itdg->id())
00192 << "\n no uncalib rechit will be made for this digi!"
00193 ;
00194 continue;
00195 }
00196 double gainRatios[3];
00197 gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
00198
00199 double a,b;
00200
00201
00202 if (useAlphaBetaArray_){
00203 if ( alphaBetaValues_[EBDetId(itdg->id()).ism()-1].size()!=0){
00204 a=alphaBetaValues_[EBDetId(itdg->id()).ism()-1][EBDetId(itdg->id()).ic()-1].first;
00205 b=alphaBetaValues_[EBDetId(itdg->id()).ism()-1][EBDetId(itdg->id()).ic()-1].second;
00206 if ((a==-1)&&(b==-1)){
00207 a=alphaEB_;
00208 b=betaEB_;
00209 }
00210 }else{
00211 a=alphaEB_;
00212 b=betaEB_;
00213 }
00214 }else{
00215 a=alphaEB_;
00216 b=betaEB_;
00217 }
00218
00219 algoEB_.SetAlphaBeta(a,b);
00220
00221 EcalUncalibratedRecHit aHit = algoEB_.makeRecHit(*itdg, pedVec, gainRatios, 0, 0);
00222 EBuncalibRechits->push_back( aHit );
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 }
00233 }
00234 evt.put( EBuncalibRechits, EBhitCollection_ );
00235
00236
00237 if( EEdigis ){
00238 for(EEDigiCollection::const_iterator itdg = EEdigis->begin(); itdg != EEdigis->end(); ++itdg) {
00239
00240 algoEE_.SetAlphaBeta(alphaEE_,betaEE_);
00241
00242
00243 LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EEDetId(itdg->id()) ;
00244 pedIter = pedMap.find(itdg->id());
00245 if( pedIter != pedMap.end() ) {
00246 aped = (*pedIter);
00247 } else {
00248 edm::LogError("EcalUncalibRecHitError") << "error!! could not find pedestals for channel: " << EEDetId(itdg->id())
00249 << "\n no uncalib rechit will be made for this digi!"
00250 ;
00251 continue;
00252 }
00253 double pedVec[3];
00254 pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
00255
00256
00257 LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EEDetId(itdg->id()) ;
00258 gainIter = gainMap.find(itdg->id());
00259 if( gainIter != gainMap.end() ) {
00260 aGain = (*gainIter);
00261 } else {
00262 edm::LogError("EcalUncalibRecHitError") << "error!! could not find gain ratios for channel: " << EEDetId(itdg->id())
00263 << "\n no uncalib rechit will be made for this digi!"
00264 ;
00265 continue;
00266 }
00267 double gainRatios[3];
00268 gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
00269
00270 EcalUncalibratedRecHit aHit = algoEE_.makeRecHit(*itdg, pedVec, gainRatios, 0 , 0);
00271 EEuncalibRechits->push_back( aHit );
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 }
00282 }
00283
00284
00285 evt.put( EEuncalibRechits, EEhitCollection_ );
00286 }
00287