CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CalibTracker/SiStripAPVAnalysis/src/TT6NoiseCalculator.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiStripAPVAnalysis/interface/TT6NoiseCalculator.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 #include <cmath>
00004 #include <numeric>
00005 #include <algorithm>
00006 using namespace std;
00007 //
00008 //  Constructors
00009 //
00010 TT6NoiseCalculator::TT6NoiseCalculator() :
00011   numberOfEvents(0) ,
00012   alreadyUsedEvent(false)  
00013 {
00014   if (0) cout << "Constructing TT6NoiseCalculator " << endl;
00015   init();
00016 }
00017 //
00018 TT6NoiseCalculator::TT6NoiseCalculator(int evnt_ini,
00019                    int evnt_iter, float sig_cut) :
00020   numberOfEvents(0) ,
00021   alreadyUsedEvent(false)  
00022 {
00023   if (0) cout << "Constructing TT6NoiseCalculator " << endl;
00024   eventsRequiredToCalibrate_ = evnt_ini;
00025   eventsRequiredToUpdate_    = evnt_iter;
00026   cutToAvoidSignal_          = sig_cut;
00027   init();
00028 }
00029 //
00030 // Initialization.
00031 //
00032 void TT6NoiseCalculator::init() {
00033   theCMPSubtractedSignal.clear();
00034   theNoise.clear();
00035   theNoiseSum.clear();
00036   theNoiseSqSum.clear();
00037   theEventPerStrip.clear();
00038   theStatus.setCalibrating();
00039 }
00040 //
00041 //  Destructor 
00042 //
00043 TT6NoiseCalculator::~TT6NoiseCalculator() {
00044   if (0) cout << "Destructing TT6NoiseCalculator " << endl;
00045 }
00046 //
00047 // Update the Status of Noise Calculation
00048 //
00049 void TT6NoiseCalculator::updateStatus(){
00050   if (theStatus.isCalibrating() && 
00051       numberOfEvents >= eventsRequiredToCalibrate_) {
00052     theStatus.setUpdating();
00053   }
00054 }
00055 //
00056 // Calculate and update (when needed) Noise Values
00057 //
00058 void TT6NoiseCalculator::updateNoise(ApvAnalysis::PedestalType& in){
00059   if (alreadyUsedEvent == false) {
00060     alreadyUsedEvent = true;
00061     numberOfEvents++;
00062 
00063     if (numberOfEvents == 1 && theNoise.size() != in.size()) {
00064       edm::LogError("TT6NoiseCalculator:updateNoise") << " You did not initialize the Noise correctly prior to noise calibration.";
00065     }
00066 
00067     // Initialize sums used for estimating noise.
00068     if ((theStatus.isCalibrating() && numberOfEvents == 1) ||
00069         (theStatus.isUpdating() && (numberOfEvents - eventsRequiredToCalibrate_)%eventsRequiredToUpdate_ == 1)) 
00070     {
00071       theNoiseSum.clear();
00072       theNoiseSqSum.clear();
00073       theEventPerStrip.clear();
00074 
00075       theNoiseSum.resize(in.size(),0.0);
00076       theNoiseSqSum.resize(in.size(),0.0);
00077       theEventPerStrip.resize(in.size(),0);
00078     }    
00079 
00080     unsigned int i;
00081 
00082     // Update sums used for estimating noise.
00083     for (i = 0; i < in.size(); i++) {
00084       if (fabs(in[i]) < cutToAvoidSignal_*theNoise[i]) {
00085         theNoiseSum[i]   += in[i];
00086         theNoiseSqSum[i] += in[i]*in[i];
00087         theEventPerStrip[i]++;
00088       }
00089     }
00090 
00091     // Calculate noise.
00092     if ((theStatus.isCalibrating() && numberOfEvents == eventsRequiredToCalibrate_) ||
00093         (theStatus.isUpdating() && (numberOfEvents - eventsRequiredToCalibrate_)%eventsRequiredToUpdate_ == 0))
00094     {
00095       theCMPSubtractedSignal.clear();
00096       theNoise.clear();
00097 
00098       for (i = 0; i < in.size(); i++) {
00099         double avVal   = (theEventPerStrip[i]) ? theNoiseSum[i]/(theEventPerStrip[i]):0.0;
00100         double sqAvVal = (theEventPerStrip[i]) ? theNoiseSqSum[i]/(theEventPerStrip[i]):0.0;
00101         double corr_fac = (theEventPerStrip[i] > 1) ? (theEventPerStrip[i]/(theEventPerStrip[i]-1)) : 1.0;
00102         double rmsVal  =  (sqAvVal - avVal*avVal > 0.0) ? sqrt(corr_fac * (sqAvVal - avVal*avVal)) : 0.0;       
00103       
00104         theCMPSubtractedSignal.push_back(static_cast<float>(avVal));
00105 
00106         theNoise.push_back(static_cast<float>(rmsVal));
00107   
00108         if (0) cout << " TT6NoiseCalculator::updateNoise " 
00109                     << theNoiseSum[i] << " " 
00110                     << theNoiseSqSum[i] << " "
00111                     << theEventPerStrip[i] << " "  
00112                     << avVal << " " 
00113                     << sqAvVal << " " 
00114                     << (sqAvVal - avVal*avVal) << " " 
00115                     << rmsVal << endl;
00116       }
00117     }
00118     updateStatus();
00119   }
00120 }
00121 //
00122 // Define New Event
00123 // 
00124 void TT6NoiseCalculator::newEvent() {
00125   alreadyUsedEvent = false;
00126 }
00127 
00128