CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CalibMuon/CSCCalibration/src/CSCConditions.cc

Go to the documentation of this file.
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   // set size to hold all layers, using enum defined in .h
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   // Strip gains
00053   es.get<CSCDBGainsRcd>().get( theGains );
00054   // Strip X-talk
00055   es.get<CSCDBCrosstalkRcd>().get( theCrosstalk );
00056   // Strip pedestals
00057   es.get<CSCDBPedestalsRcd>().get( thePedestals );
00058   // Strip autocorrelation noise matrix
00059   es.get<CSCDBNoiseMatrixRcd>().get(theNoiseMatrix);
00060 
00061   if ( useTimingCorrections()){
00062     // Buckeye chip speeds
00063     es.get<CSCDBChipSpeedCorrectionRcd>().get( theChipCorrections );
00064     // Cable lengths from chambers to peripheral crate and additional chamber level timing correction
00065     es.get<CSCChamberTimeCorrectionsRcd>().get( theChamberTimingCorrections );
00066   }
00067 
00068   if ( readBadChannels() ) {
00069   // Bad strip channels
00070     es.get<CSCBadStripsRcd>().get( theBadStrips );
00071   // Bad wiregroup channels
00072     es.get<CSCBadWiresRcd>().get( theBadWires );
00073 
00074     //@@    if( badStripsWatcher_.check( es ) ) { 
00075       fillBadStripWords();
00076     //@@    }
00077     //@@    if( badWiresWatcher_.check( es ) ) { 
00078       fillBadWireWords();
00079     //@    }
00080 
00081   }
00082 
00083   // Has GainsRcd changed?
00084   if( gainsWatcher_.check( es ) ) { // Yes...
00085     theAverageGain = -1.0; // ...reset, so next access will recalculate it
00086   }
00087  
00088   if ( readBadChambers() ) {
00089   // Entire bad chambers
00090     es.get<CSCBadChambersRcd>().get( theBadChambers );
00091   }
00092 
00093   if ( useGasGainCorrections()){
00094     es.get<CSCDBGasGainCorrectionRcd>().get( theGasGainCorrections );
00095   }
00096 
00097 //  print();
00098 }
00099 
00100 void CSCConditions::fillBadStripWords(){
00101   // reset existing values
00102   badStripWords.assign( MAX_LAYERS, 0 );
00103   if ( readBadChannels() ) {
00104     // unpack what we've read from theBadStrips
00105 
00106     // chambers is a vector<BadChamber>
00107     // channels is a vector<BadChannel>
00108     // Each BadChamber contains its index (1-468 or 540 w. ME42), the no. of bad channels, 
00109     // and the index within vector<BadChannel> where this chamber's bad channels start.
00110 
00111     CSCIndexer indexer;
00112 
00113     for ( size_t i=0; i<theBadStrips->chambers.size(); ++i ) { // loop over bad chambers
00114       int indexc = theBadStrips->chambers[i].chamber_index;
00115       int start =  theBadStrips->chambers[i].pointer;  // where this chamber's bad channels start in vector<BadChannel>
00116       int nbad  =  theBadStrips->chambers[i].bad_channels;
00117 
00118       CSCDetId id = indexer.detIdFromChamberIndex( indexc ); // We need this to build layer index (1-2808)
00119 
00120       for ( int j=start-1; j<start-1+nbad; ++j ) { // bad channels in this chamber
00121         short lay  = theBadStrips->channels[j].layer;    // value 1-6
00122         short chan = theBadStrips->channels[j].channel;  // value 1-80
00123     //    short f1 = theBadStrips->channels[j].flag1;
00124     //    short f2 = theBadStrips->channels[j].flag2;
00125     //    short f3 = theBadStrips->channels[j].flag3;
00126         int indexl = indexer.layerIndex( id.endcap(), id.station(), id.ring(), id.chamber(), lay );
00127         badStripWords[indexl-1].set( chan-1, 1 ); // set bit 0-79 in 80-bit bitset representing this layer
00128       } // j
00129     } // i
00130 
00131   } 
00132 }
00133 
00134 void CSCConditions::fillBadWireWords(){
00135   // reset existing values
00136   badWireWords.assign( MAX_LAYERS, 0 );
00137   if ( readBadChannels() ) {
00138     // unpack what we've read from theBadWires
00139     CSCIndexer indexer;
00140 
00141     for ( size_t i=0; i<theBadWires->chambers.size(); ++i ) { // loop over bad chambers
00142       int indexc = theBadWires->chambers[i].chamber_index;
00143       int start =  theBadWires->chambers[i].pointer;  // where this chamber's bad channels start in vector<BadChannel>
00144       int nbad  =  theBadWires->chambers[i].bad_channels;
00145 
00146       CSCDetId id = indexer.detIdFromChamberIndex( indexc ); // We need this to build layer index (1-2808)
00147 
00148       for ( int j=start-1; j<start-1+nbad; ++j ) { // bad channels in this chamber
00149         short lay  = theBadWires->channels[j].layer;    // value 1-6
00150         short chan = theBadWires->channels[j].channel;  // value 1-80
00151     //    short f1 = theBadWires->channels[j].flag1;
00152     //    short f2 = theBadWires->channels[j].flag2;
00153     //    short f3 = theBadWires->channels[j].flag3;
00154         int indexl = indexer.layerIndex( id.endcap(), id.station(), id.ring(), id.chamber(), lay );
00155         badWireWords[indexl-1].set( chan-1, 1 ); // set bit 0-111 in 112-bit bitset representing this layer
00156       } // j
00157     } // i
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   //@@ NEEDS THOROUGH UPDATING
00169 {
00170 /*
00171   std::cout << "SIZES: GAINS: " << theGains->gains.size()
00172             << "   PEDESTALS: " << thePedestals->pedestals.size()
00173             << "   NOISES "  << theNoiseMatrix->matrix.size() << std::endl;;
00174 
00175   std::map< int,std::vector<CSCDBGains::Item> >::const_iterator layerGainsItr = theGains->gains.begin(), 
00176       lastGain = theGains->gains.end();
00177   for( ; layerGainsItr != lastGain; ++layerGainsItr)
00178   {
00179     std::cout << "GAIN " << layerGainsItr->first 
00180               << " STRIPS " << layerGainsItr->second.size() << " "
00181               << layerGainsItr->second[0].gain_slope 
00182               << " " << layerGainsItr->second[0].gain_intercept << std::endl;
00183   }
00184 
00185   std::map< int,std::vector<CSCDBPedestals::Item> >::const_iterator pedestalItr = thePedestals->pedestals.begin(), 
00186                                                                   lastPedestal = thePedestals->pedestals.end();
00187   for( ; pedestalItr != lastPedestal; ++pedestalItr)
00188   {
00189     std::cout << "PEDS " << pedestalItr->first << " " 
00190               << " STRIPS " << pedestalItr->second.size() << " ";
00191     for(int i = 1; i < 80; ++i)
00192     {
00193        std::cout << pedestalItr->second[i-1].rms << " " ;
00194      }
00195      std::cout << std::endl;
00196   }
00197 
00198   std::map< int,std::vector<CSCDBCrosstalk::Item> >::const_iterator crosstalkItr = theCrosstalk->crosstalk.begin(),
00199                                                                   lastCrosstalk = theCrosstalk->crosstalk.end();
00200   for( ; crosstalkItr != lastCrosstalk; ++crosstalkItr)
00201   {
00202     std::cout << "XTALKS " << crosstalkItr->first 
00203       << " STRIPS " << crosstalkItr->second.size() << " "  
00204      << crosstalkItr->second[5].xtalk_slope_left << " " 
00205      << crosstalkItr->second[5].xtalk_slope_right << " " 
00206      << crosstalkItr->second[5].xtalk_intercept_left << " " 
00207      << crosstalkItr->second[5].xtalk_intercept_right << std::endl;
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   // resistive fraction is at the peak, where t=0
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   // resistive fraction is at the peak, where t=0
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); //Converts 1-80(64) in a layer to 1-5(4), expects ME1/1a to be channel 65-80
00292     //printf("CSCCondition  e:%d s:%d r:%d c:%d l:%d strip:%d chip: %d\n",detId.endcap(),detId.station(), detId.ring(),detId.chamber(),detId.layer(),stripChannel,chip);
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; // consider gains above this
00337   const float hiEdge = 10.0; // consider gains below this
00338   const float loLimit = 6.0; // lowest acceptable average gain
00339   const float hiLimit = 9.0; // highest acceptable average gain
00340   const float expectedAverage = 7.5; // default average gain
00341 
00342   if ( theAverageGain > 0. ) return theAverageGain; // only recalculate if necessary
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   // Average gain
00357   if ( n_strip > 0 ) {
00358     theAverageGain = gain_tot / n_strip;
00359   }
00360 
00361   // Average gain has been around 7.5 in real data
00362   if ( theAverageGain < loLimit || theAverageGain > hiLimit ) {
00363     //    LogTrace("CSC") << "Average CSC strip gain = "
00364     //                    << theAverageGain << "  is reset to expected value " << expectedAverage;
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     //printf("CSCCondition  e:%d s:%d r:%d c:%d l:%d strip:%d wire:%d\n",detId.endcap(),detId.station(),detId.ring(),detId.chamber(),detId.layer(),strip,wiregroup);
00376     return float ( theGasGainCorrections->item(detId,strip,wiregroup).gainCorr );
00377 
00378   } else {
00379     return 1.;
00380   }
00381 }