00001
00010 #include "RecoLocalCalo/EcalRecProducers/interface/EcalWeightUncalibRecHitProducer.h"
00011 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
00012 #include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
00013 #include "DataFormats/Common/interface/Handle.h"
00014
00015 #include <iostream>
00016 #include <iomanip>
00017 #include <cmath>
00018
00019 #include "FWCore/Framework/interface/ESHandle.h"
00020
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022
00023 #include "CondFormats/EcalObjects/interface/EcalPedestals.h"
00024 #include "CondFormats/DataRecord/interface/EcalPedestalsRcd.h"
00025
00026 #include "DataFormats/EcalRecHit/interface/EcalUncalibratedRecHit.h"
00027 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00028
00029 #include "CondFormats/EcalObjects/interface/EcalXtalGroupId.h"
00030 #include "CondFormats/EcalObjects/interface/EcalWeightXtalGroups.h"
00031 #include "CondFormats/DataRecord/interface/EcalWeightXtalGroupsRcd.h"
00032
00033 #include "CondFormats/EcalObjects/interface/EcalWeight.h"
00034 #include "CondFormats/EcalObjects/interface/EcalWeightSet.h"
00035 #include "CondFormats/EcalObjects/interface/EcalTBWeights.h"
00036 #include "CondFormats/DataRecord/interface/EcalTBWeightsRcd.h"
00037
00038 #include "CondFormats/EcalObjects/interface/EcalMGPAGainRatio.h"
00039 #include "CondFormats/EcalObjects/interface/EcalGainRatios.h"
00040 #include "CondFormats/DataRecord/interface/EcalGainRatiosRcd.h"
00041
00042 #include "Math/SVector.h"
00043 #include "Math/SMatrix.h"
00044 #include <vector>
00045
00046 EcalWeightUncalibRecHitProducer::EcalWeightUncalibRecHitProducer(const edm::ParameterSet& ps) {
00047
00048 EBdigiCollection_ = ps.getParameter<edm::InputTag>("EBdigiCollection");
00049 EEdigiCollection_ = ps.getParameter<edm::InputTag>("EEdigiCollection");
00050 EBhitCollection_ = ps.getParameter<std::string>("EBhitCollection");
00051 EEhitCollection_ = ps.getParameter<std::string>("EEhitCollection");
00052 produces< EBUncalibratedRecHitCollection >(EBhitCollection_);
00053 produces< EEUncalibratedRecHitCollection >(EEhitCollection_);
00054 }
00055
00056 EcalWeightUncalibRecHitProducer::~EcalWeightUncalibRecHitProducer() {
00057 }
00058
00059 void
00060 EcalWeightUncalibRecHitProducer::produce(edm::Event& evt, const edm::EventSetup& es) {
00061
00062 using namespace edm;
00063
00064 Handle< EBDigiCollection > pEBDigis;
00065 Handle< EEDigiCollection > pEEDigis;
00066
00067 const EBDigiCollection* EBdigis =0;
00068 const EEDigiCollection* EEdigis =0;
00069
00070 if ( EBdigiCollection_.label() != "" && EBdigiCollection_.instance() != "" ) {
00071 evt.getByLabel( EBdigiCollection_, pEBDigis);
00072
00073 if ( pEBDigis.isValid() ) {
00074 EBdigis = pEBDigis.product();
00075 edm::LogInfo("EcalUncalibRecHitInfo") << "total # EBdigis: " << EBdigis->size() ;
00076 } else {
00077 edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product " << EBdigiCollection_;
00078 }
00079 }
00080
00081 if ( EEdigiCollection_.label() != "" && EEdigiCollection_.instance() != "" ) {
00082 evt.getByLabel( EEdigiCollection_, pEEDigis);
00083
00084 if ( pEEDigis.isValid() ) {
00085 EEdigis = pEEDigis.product();
00086 edm::LogInfo("EcalUncalibRecHitInfo") << "total # EEdigis: " << EEdigis->size() ;
00087 } else {
00088 edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product " << EEdigiCollection_;
00089 }
00090 }
00091
00092
00093 #ifdef DEBUG
00094 LogDebug("EcalUncalibRecHitDebug") << "fetching gainRatios....";
00095 #endif
00096 edm::ESHandle<EcalWeightXtalGroups> pGrp;
00097 es.get<EcalWeightXtalGroupsRcd>().get(pGrp);
00098 const EcalWeightXtalGroups & grps = *pGrp.product();
00099
00100
00101 edm::ESHandle<EcalGainRatios> pRatio;
00102 es.get<EcalGainRatiosRcd>().get(pRatio);
00103 const EcalGainRatios& gains = *pRatio.product();
00104
00105
00106 #ifdef DEBUG
00107 LogDebug("EcalUncalibRecHitDebug") <<"Fetching EcalTBWeights from DB " ;
00108 #endif
00109 edm::ESHandle<EcalTBWeights> pWgts;
00110 es.get<EcalTBWeightsRcd>().get(pWgts);
00111 const EcalTBWeights & wgts = *pWgts.product();
00112 EcalTBWeights::EcalTBWeightMap const & wgtsMap = wgts.getMap();
00113 #ifdef DEBUG
00114 LogDebug("EcalUncalibRecHitDebug") << "EcalTBWeightMap.size(): " << std::setprecision(3) << wgtsMap.size() ;
00115 #endif
00116
00117
00118 #ifdef DEBUG
00119 LogDebug("EcalUncalibRecHitDebug") << "fetching pedestals....";
00120 #endif
00121 edm::ESHandle<EcalPedestals> pedHandle;
00122 es.get<EcalPedestalsRcd>().get( pedHandle );
00123 const EcalPedestals & peds = *pedHandle.product();
00124 #ifdef DEBUG
00125 LogDebug("EcalUncalibRecHitDebug") << "done." ;
00126 #endif
00127
00128
00129 std::auto_ptr< EBUncalibratedRecHitCollection > EBuncalibRechits( new EBUncalibratedRecHitCollection );
00130 std::auto_ptr< EEUncalibratedRecHitCollection > EEuncalibRechits( new EEUncalibratedRecHitCollection );
00131
00132 EcalTBWeights::EcalTBWeightMap::const_iterator wit;
00133
00134
00135 if (EBdigis)
00136 {
00137 EBuncalibRechits->reserve(EBdigis->size());
00138 for(EBDigiCollection::const_iterator itdg = EBdigis->begin(); itdg != EBdigis->end(); ++itdg) {
00139 unsigned int hashedIndex = EBDetId(itdg->id()).hashedIndex();
00140
00141
00142
00143
00144 #ifdef DEBUG
00145 LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EBDetId(itdg->id()) ;
00146 #endif
00147 const EcalPedestals::Item& aped = peds.barrel(hashedIndex);
00148 double pedVec[3];
00149 pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
00150
00151
00152 #ifdef DEBUG
00153 LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EBDetId(itdg->id()) ;
00154 #endif
00155 const EcalMGPAGainRatio& aGain = gains.barrel(hashedIndex);
00156 double gainRatios[3];
00157 gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
00158
00159
00160 const EcalXtalGroupId& gid = grps.barrel(hashedIndex);
00161
00162 EcalTBWeights::EcalTDCId tdcid(1);
00163
00164
00165 wit = wgtsMap.find( std::make_pair(gid,tdcid) );
00166 if( wit == wgtsMap.end() ) {
00167 edm::LogError("EcalUncalibRecHitError") << "No weights found for EcalGroupId: " << gid.id() << " and EcalTDCId: " << tdcid
00168 << "\n skipping digi with id: " << EBDetId(itdg->id())
00169 ;
00170 continue;
00171 }
00172 const EcalWeightSet& wset = wit->second;
00173
00174
00175 #ifdef DEBUG
00176 LogDebug("EcalUncalibRecHitDebug") << "accessing matrices of weights...";
00177 #endif
00178 const EcalWeightSet::EcalWeightMatrix& mat1 = wset.getWeightsBeforeGainSwitch();
00179 const EcalWeightSet::EcalWeightMatrix& mat2 = wset.getWeightsAfterGainSwitch();
00180 const EcalWeightSet::EcalChi2WeightMatrix& mat3 = wset.getChi2WeightsBeforeGainSwitch();
00181 const EcalWeightSet::EcalChi2WeightMatrix& mat4 = wset.getChi2WeightsAfterGainSwitch();
00182 #ifdef DEBUG
00183 LogDebug("EcalUncalibRecHitDebug") << "done." ;
00184 #endif
00185
00186 const EcalWeightSet::EcalWeightMatrix* weights[2];
00187 weights[0]=&mat1;
00188 weights[1]=&mat2;
00189
00190
00191
00192
00193
00194
00195
00196 const EcalWeightSet::EcalChi2WeightMatrix* chi2mat[2];
00197 chi2mat[0]=&mat3;
00198 chi2mat[1]=&mat4;
00199
00200
00201
00202
00203 EcalUncalibratedRecHit aHit =
00204 EBalgo_.makeRecHit(*itdg, pedVec, gainRatios, weights, chi2mat);
00205 EBuncalibRechits->push_back( aHit );
00206
00207 #ifdef DEBUG
00208 if(aHit.amplitude()>0.) {
00209 LogDebug("EcalUncalibRecHitDebug") << "processed EBDataFrame with id: "
00210 << EBDetId(itdg->id()) << "\n"
00211 << "uncalib rechit amplitude: " << aHit.amplitude()
00212 ;
00213 }
00214 #endif
00215 }
00216 }
00217
00218
00219 if (EEdigis)
00220 {
00221 EEuncalibRechits->reserve(EEdigis->size());
00222 for(EEDigiCollection::const_iterator itdg = EEdigis->begin(); itdg != EEdigis->end(); ++itdg) {
00223 unsigned int hashedIndex = EEDetId(itdg->id()).hashedIndex();
00224
00225
00226
00227 #ifdef DEBUG
00228 LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EEDetId(itdg->id()) ;
00229 #endif
00230
00231 const EcalPedestals::Item& aped = peds.endcap(hashedIndex);
00232 double pedVec[3];
00233 pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
00234
00235
00236 #ifdef DEBUG
00237 LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EEDetId(itdg->id()) ;
00238 #endif
00239 const EcalMGPAGainRatio& aGain = gains.endcap(hashedIndex);
00240 double gainRatios[3];
00241 gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
00242
00243
00244 const EcalXtalGroupId& gid = grps.endcap(hashedIndex);
00245
00246 EcalTBWeights::EcalTDCId tdcid(1);
00247
00248
00249 wit = wgtsMap.find( std::make_pair(gid,tdcid) );
00250 if( wit == wgtsMap.end() ) {
00251 edm::LogError("EcalUncalibRecHitError") << "No weights found for EcalGroupId: " << gid.id() << " and EcalTDCId: " << tdcid
00252 << "\n skipping digi with id: " << EEDetId(itdg->id())
00253 ;
00254 continue;
00255 }
00256 const EcalWeightSet& wset = wit->second;
00257
00258
00259 #ifdef DEBUG
00260 LogDebug("EcalUncalibRecHitDebug") << "accessing matrices of weights...";
00261 #endif
00262 const EcalWeightSet::EcalWeightMatrix& mat1 = wset.getWeightsBeforeGainSwitch();
00263 const EcalWeightSet::EcalWeightMatrix& mat2 = wset.getWeightsAfterGainSwitch();
00264 const EcalWeightSet::EcalChi2WeightMatrix& mat3 = wset.getChi2WeightsBeforeGainSwitch();
00265 const EcalWeightSet::EcalChi2WeightMatrix& mat4 = wset.getChi2WeightsAfterGainSwitch();
00266 #ifdef DEBUG
00267 LogDebug("EcalUncalibRecHitDebug") << "done." ;
00268 #endif
00269
00270 const EcalWeightSet::EcalWeightMatrix* weights[2];
00271 weights[0]=&mat1;
00272 weights[1]=&mat2;
00273
00274
00275
00276
00277
00278
00279
00280 const EcalWeightSet::EcalChi2WeightMatrix* chi2mat[2];
00281 chi2mat[0]=&mat3;
00282 chi2mat[1]=&mat4;
00283
00284
00285
00286
00287 EcalUncalibratedRecHit aHit =
00288 EEalgo_.makeRecHit(*itdg, pedVec, gainRatios, weights, chi2mat);
00289 EEuncalibRechits->push_back( aHit );
00290
00291 #ifdef DEBUG
00292 if(aHit.amplitude()>0.) {
00293 LogDebug("EcalUncalibRecHitDebug") << "processed EEDataFrame with id: "
00294 << EEDetId(itdg->id()) << "\n"
00295 << "uncalib rechit amplitude: " << aHit.amplitude()
00296 ;
00297 }
00298 #endif
00299 }
00300 }
00301
00302
00303 evt.put( EBuncalibRechits, EBhitCollection_ );
00304 evt.put( EEuncalibRechits, EEhitCollection_ );
00305
00306 }
00307
00308
00309