00001 #include "CalibMuon/CSCCalibration/interface/CSCConditions.h"
00002 #include "FWCore/Framework/interface/EventSetup.h"
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/ESWatcher.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "FWCore/Framework/interface/MakerMacros.h"
00008 #include "CondFormats/DataRecord/interface/CSCDBPedestalsRcd.h"
00009 #include "CondFormats/DataRecord/interface/CSCDBNoiseMatrixRcd.h"
00010 #include "CondFormats/DataRecord/interface/CSCDBCrosstalkRcd.h"
00011 #include "CondFormats/CSCObjects/interface/CSCDBGains.h"
00012 #include "CondFormats/CSCObjects/interface/CSCDBPedestals.h"
00013 #include "CondFormats/CSCObjects/interface/CSCDBCrosstalk.h"
00014 #include "CondFormats/CSCObjects/interface/CSCBadStrips.h"
00015 #include "CondFormats/CSCObjects/interface/CSCBadWires.h"
00016 #include "CondFormats/CSCObjects/interface/CSCBadChambers.h"
00017 #include "CondFormats/CSCObjects/interface/CSCDBChipSpeedCorrection.h"
00018 #include "CondFormats/CSCObjects/interface/CSCChamberTimeCorrections.h"
00019 #include "CondFormats/CSCObjects/interface/CSCDBGasGainCorrection.h"
00020 #include "DataFormats/MuonDetId/interface/CSCIndexer.h"
00021
00022 CSCConditions::CSCConditions( const edm::ParameterSet& ps )
00023 : theGains(),
00024 theCrosstalk(),
00025 thePedestals(),
00026 theNoiseMatrix(),
00027 theBadStrips(),
00028 theBadWires(),
00029 theBadChambers(),
00030 theChipCorrections(),
00031 theChamberTimingCorrections(),
00032 theGasGainCorrections(),
00033 readBadChannels_(false), readBadChambers_(false),useTimingCorrections_(false),useGasGainCorrections_(false),
00034 theAverageGain( -1.0 )
00035 {
00036 readBadChannels_ = ps.getParameter<bool>("readBadChannels");
00037 readBadChambers_ = ps.getParameter<bool>("readBadChambers");
00038 useTimingCorrections_ = ps.getParameter<bool>("CSCUseTimingCorrections");
00039 useGasGainCorrections_ = ps.getParameter<bool>("CSCUseGasGainCorrections");
00040
00041 badStripWords.resize( MAX_LAYERS, 0 );
00042 badWireWords.resize( MAX_LAYERS, 0 );
00043 }
00044
00045
00046 CSCConditions::~CSCConditions()
00047 {
00048 }
00049
00050 void CSCConditions::initializeEvent(const edm::EventSetup & es)
00051 {
00052
00053 es.get<CSCDBGainsRcd>().get( theGains );
00054
00055 es.get<CSCDBCrosstalkRcd>().get( theCrosstalk );
00056
00057 es.get<CSCDBPedestalsRcd>().get( thePedestals );
00058
00059 es.get<CSCDBNoiseMatrixRcd>().get(theNoiseMatrix);
00060
00061 if ( useTimingCorrections()){
00062
00063 es.get<CSCDBChipSpeedCorrectionRcd>().get( theChipCorrections );
00064
00065 es.get<CSCChamberTimeCorrectionsRcd>().get( theChamberTimingCorrections );
00066 }
00067
00068 if ( readBadChannels() ) {
00069
00070 es.get<CSCBadStripsRcd>().get( theBadStrips );
00071
00072 es.get<CSCBadWiresRcd>().get( theBadWires );
00073
00074
00075 fillBadStripWords();
00076
00077
00078 fillBadWireWords();
00079
00080
00081 }
00082
00083
00084 if( gainsWatcher_.check( es ) ) {
00085 theAverageGain = -1.0;
00086 }
00087
00088 if ( readBadChambers() ) {
00089
00090 es.get<CSCBadChambersRcd>().get( theBadChambers );
00091 }
00092
00093 if ( useGasGainCorrections()){
00094 es.get<CSCDBGasGainCorrectionRcd>().get( theGasGainCorrections );
00095 }
00096
00097
00098 }
00099
00100 void CSCConditions::fillBadStripWords(){
00101
00102 badStripWords.assign( MAX_LAYERS, 0 );
00103 if ( readBadChannels() ) {
00104
00105
00106
00107
00108
00109
00110
00111 CSCIndexer indexer;
00112
00113 for ( size_t i=0; i<theBadStrips->chambers.size(); ++i ) {
00114 int indexc = theBadStrips->chambers[i].chamber_index;
00115 int start = theBadStrips->chambers[i].pointer;
00116 int nbad = theBadStrips->chambers[i].bad_channels;
00117
00118 CSCDetId id = indexer.detIdFromChamberIndex( indexc );
00119
00120 for ( int j=start-1; j<start-1+nbad; ++j ) {
00121 short lay = theBadStrips->channels[j].layer;
00122 short chan = theBadStrips->channels[j].channel;
00123
00124
00125
00126 int indexl = indexer.layerIndex( id.endcap(), id.station(), id.ring(), id.chamber(), lay );
00127 badStripWords[indexl-1].set( chan-1, 1 );
00128 }
00129 }
00130
00131 }
00132 }
00133
00134 void CSCConditions::fillBadWireWords(){
00135
00136 badWireWords.assign( MAX_LAYERS, 0 );
00137 if ( readBadChannels() ) {
00138
00139 CSCIndexer indexer;
00140
00141 for ( size_t i=0; i<theBadWires->chambers.size(); ++i ) {
00142 int indexc = theBadWires->chambers[i].chamber_index;
00143 int start = theBadWires->chambers[i].pointer;
00144 int nbad = theBadWires->chambers[i].bad_channels;
00145
00146 CSCDetId id = indexer.detIdFromChamberIndex( indexc );
00147
00148 for ( int j=start-1; j<start-1+nbad; ++j ) {
00149 short lay = theBadWires->channels[j].layer;
00150 short chan = theBadWires->channels[j].channel;
00151
00152
00153
00154 int indexl = indexer.layerIndex( id.endcap(), id.station(), id.ring(), id.chamber(), lay );
00155 badWireWords[indexl-1].set( chan-1, 1 );
00156 }
00157 }
00158
00159 }
00160 }
00161
00162 bool CSCConditions::isInBadChamber( const CSCDetId& id ) const {
00163 if ( readBadChambers() ) return theBadChambers->isInBadChamber( id );
00164 else return false;
00165 }
00166
00167 void CSCConditions::print() const
00168
00169 {
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 }
00211
00212 float CSCConditions::gain(const CSCDetId & detId, int channel) const
00213 {
00214 assert(theGains.isValid());
00215 return float( theGains->item(detId, channel).gain_slope )/theGains->factor_gain;
00216 }
00217
00218
00219 float CSCConditions::pedestal(const CSCDetId & detId, int channel) const
00220 {
00221 assert(thePedestals.isValid());
00222 return float ( thePedestals->item(detId, channel).ped )/thePedestals->factor_ped;
00223 }
00224
00225
00226 float CSCConditions::pedestalSigma(const CSCDetId&detId, int channel) const
00227 {
00228 assert(thePedestals.isValid());
00229 return float ( thePedestals->item(detId, channel).rms )/thePedestals->factor_rms;
00230 }
00231
00232
00233 float CSCConditions::crosstalkIntercept(const CSCDetId&detId, int channel, bool leftRight) const
00234 {
00235 assert(theCrosstalk.isValid());
00236 const CSCDBCrosstalk::Item & item = theCrosstalk->item(detId, channel);
00237
00238
00239 return leftRight ? float ( item.xtalk_intercept_right )/theCrosstalk->factor_intercept
00240 : float ( item.xtalk_intercept_left )/theCrosstalk->factor_intercept ;
00241 }
00242
00243
00244 float CSCConditions::crosstalkSlope(const CSCDetId&detId, int channel, bool leftRight) const
00245 {
00246 assert(theCrosstalk.isValid());
00247 const CSCDBCrosstalk::Item & item = theCrosstalk->item(detId, channel);
00248
00249
00250 return leftRight ? float ( item.xtalk_slope_right )/theCrosstalk->factor_slope
00251 : float ( item.xtalk_slope_left )/theCrosstalk->factor_slope ;
00252 }
00253
00254 const CSCDBNoiseMatrix::Item & CSCConditions::noiseMatrix(const CSCDetId& detId, int channel) const
00255 {
00256 assert(theNoiseMatrix.isValid());
00257 return theNoiseMatrix->item(detId, channel);
00258 }
00259
00260 void CSCConditions::noiseMatrixElements( const CSCDetId& id, int channel, std::vector<float>& me ) const {
00261 assert(me.size()>11);
00262 const CSCDBNoiseMatrix::Item& item = noiseMatrix(id, channel);
00263 me[0] = float ( item.elem33 )/theNoiseMatrix->factor_noise;
00264 me[1] = float ( item.elem34 )/theNoiseMatrix->factor_noise;
00265 me[2] = float ( item.elem35 )/theNoiseMatrix->factor_noise;
00266 me[3] = float ( item.elem44 )/theNoiseMatrix->factor_noise;
00267 me[4] = float ( item.elem45 )/theNoiseMatrix->factor_noise;
00268 me[5] = float ( item.elem46 )/theNoiseMatrix->factor_noise;
00269 me[6] = float ( item.elem55 )/theNoiseMatrix->factor_noise;
00270 me[7] = float ( item.elem56 )/theNoiseMatrix->factor_noise;
00271 me[8] = float ( item.elem57 )/theNoiseMatrix->factor_noise;
00272 me[9] = float ( item.elem66 )/theNoiseMatrix->factor_noise;
00273 me[10] = float ( item.elem67 )/theNoiseMatrix->factor_noise;
00274 me[11] = float ( item.elem77 )/theNoiseMatrix->factor_noise;
00275 }
00276
00277 void CSCConditions::crossTalk( const CSCDetId& id, int channel, std::vector<float>& ct ) const {
00278 assert(theCrosstalk.isValid());
00279 const CSCDBCrosstalk::Item & item = theCrosstalk->item(id, channel);
00280 ct[0] = float ( item.xtalk_slope_left )/theCrosstalk->factor_slope;
00281 ct[1] = float ( item.xtalk_intercept_left )/theCrosstalk->factor_intercept;
00282 ct[2] = float ( item.xtalk_slope_right )/theCrosstalk->factor_slope;
00283 ct[3] = float ( item.xtalk_intercept_right )/theCrosstalk->factor_intercept;
00284 }
00285
00286 float CSCConditions::chipCorrection(const CSCDetId & detId, int stripChannel) const
00287 {
00288 if ( useTimingCorrections() ){
00289 assert(theChipCorrections.isValid());
00290 CSCIndexer indexer;
00291 int chip = indexer.chipIndex(stripChannel);
00292
00293 return float ( theChipCorrections->item(detId,chip).speedCorr )/theChipCorrections->factor_speedCorr;
00294 }
00295 else
00296 return 0;
00297 }
00298 float CSCConditions::chamberTimingCorrection(const CSCDetId & detId) const
00299 {
00300 if ( useTimingCorrections() ){
00301 assert(theChamberTimingCorrections.isValid());
00302 return float ( theChamberTimingCorrections->item(detId).cfeb_tmb_skew_delay*1./theChamberTimingCorrections->factor_precision
00303 + theChamberTimingCorrections->item(detId).cfeb_timing_corr*1./theChamberTimingCorrections->factor_precision
00304 + (theChamberTimingCorrections->item(detId).cfeb_cable_delay*25.)
00305 );
00306 }
00307 else
00308 return 0;
00309 }
00310 float CSCConditions::anodeBXoffset(const CSCDetId & detId) const
00311 {
00312 if ( useTimingCorrections() ){
00313 assert(theChamberTimingCorrections.isValid());
00314 return float ( theChamberTimingCorrections->item(detId).anode_bx_offset*1./theChamberTimingCorrections->factor_precision);
00315 }
00316 else
00317 return 0;
00318 }
00319
00320 const std::bitset<80>& CSCConditions::badStripWord( const CSCDetId& id ) const {
00321 CSCIndexer indexer;
00322 return badStripWords[indexer.layerIndex(id) - 1];
00323 }
00324
00325 const std::bitset<112>& CSCConditions::badWireWord( const CSCDetId& id ) const {
00326 CSCIndexer indexer;
00327 return badWireWords[indexer.layerIndex(id) - 1];
00328 }
00329
00334 float CSCConditions::averageGain() const {
00335
00336 const float loEdge = 5.0;
00337 const float hiEdge = 10.0;
00338 const float loLimit = 6.0;
00339 const float hiLimit = 9.0;
00340 const float expectedAverage = 7.5;
00341
00342 if ( theAverageGain > 0. ) return theAverageGain;
00343
00344 int n_strip = 0;
00345 float gain_tot = 0.;
00346
00347 CSCDBGains::GainContainer::const_iterator it;
00348 for ( it=theGains->gains.begin(); it!=theGains->gains.end(); ++it ) {
00349 float the_gain = float( it->gain_slope )/theGains->factor_gain;
00350 if (the_gain > loEdge && the_gain < hiEdge ) {
00351 gain_tot += the_gain;
00352 ++n_strip;
00353 }
00354 }
00355
00356
00357 if ( n_strip > 0 ) {
00358 theAverageGain = gain_tot / n_strip;
00359 }
00360
00361
00362 if ( theAverageGain < loLimit || theAverageGain > hiLimit ) {
00363
00364
00365 theAverageGain = expectedAverage;
00366 }
00367
00368 return theAverageGain;
00369 }
00370
00371 float CSCConditions::gasGainCorrection(const CSCDetId & detId, int strip, int wiregroup) const
00372 {
00373 if ( useGasGainCorrections() ){
00374 assert(theGasGainCorrections.isValid());
00375
00376 return float ( theGasGainCorrections->item(detId,strip,wiregroup).gainCorr );
00377
00378 } else {
00379 return 1.;
00380 }
00381 }