Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:39

0001 #include "CalibTracker/SiStripAPVAnalysis/interface/SimpleNoiseCalculator.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 #include <cmath>
0004 #include <numeric>
0005 #include <algorithm>
0006 using namespace std;
0007 //
0008 //  Constructors
0009 //
0010 SimpleNoiseCalculator::SimpleNoiseCalculator() : numberOfEvents(0), alreadyUsedEvent(false) {
0011   if (false)
0012     cout << "Constructing SimpleNoiseCalculator " << endl;
0013   init();
0014 }
0015 //
0016 SimpleNoiseCalculator::SimpleNoiseCalculator(int evnt_ini, bool use_DB) : numberOfEvents(0), alreadyUsedEvent(false) {
0017   if (false)
0018     cout << "Constructing SimpleNoiseCalculator " << endl;
0019   useDB_ = use_DB;
0020   eventsRequiredToCalibrate_ = evnt_ini;
0021   //  eventsRequiredToUpdate_    = evnt_iter;
0022   //  cutToAvoidSignal_          = sig_cut;
0023   init();
0024 }
0025 //
0026 // Initialization.
0027 //
0028 void SimpleNoiseCalculator::init() {
0029   theCMPSubtractedSignal.clear();
0030   theNoise.clear();
0031   theNoiseSum.clear();
0032   theNoiseSqSum.clear();
0033   theEventPerStrip.clear();
0034   // theStatus.setCalibrating();
0035 }
0036 //
0037 //  Destructor
0038 //
0039 SimpleNoiseCalculator::~SimpleNoiseCalculator() {
0040   if (false)
0041     cout << "Destructing SimpleNoiseCalculator " << endl;
0042 }
0043 //
0044 // Update the Status of Noise Calculation
0045 //
0046 void SimpleNoiseCalculator::updateStatus() {
0047   if ((theStatus.isCalibrating() && numberOfEvents >= eventsRequiredToCalibrate_) ||
0048       (useDB_ == true && numberOfEvents == 1)) {
0049     theStatus.setUpdating();
0050   }
0051 }
0052 //
0053 // Calculate and update (when needed) Noise Values
0054 //
0055 void SimpleNoiseCalculator::updateNoise(ApvAnalysis::PedestalType& in) {
0056   if (alreadyUsedEvent == false) {
0057     alreadyUsedEvent = true;
0058     numberOfEvents++;
0059 
0060     if (numberOfEvents == 1 && theNoise.size() != in.size()) {
0061       edm::LogError("SimpleNoiseCalculator:updateNoise")
0062           << " You did not initialize the Noise correctly prior to noise calibration.";
0063     }
0064 
0065     // Initialize sums used for estimating noise.
0066     if (numberOfEvents == 1) {
0067       theNoiseSum.clear();
0068       theNoiseSqSum.clear();
0069       theEventPerStrip.clear();
0070 
0071       theNoiseSum.resize(in.size(), 0.0);
0072       theNoiseSqSum.resize(in.size(), 0.0);
0073       theEventPerStrip.resize(in.size(), 0);
0074     }
0075 
0076     unsigned int i;
0077 
0078     // At every event Update sums used for estimating noise.
0079     for (i = 0; i < in.size(); i++) {
0080       theNoiseSum[i] += in[i];
0081       theNoiseSqSum[i] += in[i] * in[i];
0082       theEventPerStrip[i]++;
0083     }
0084 
0085     // Calculate noise.
0086     if ((theStatus.isCalibrating() && numberOfEvents == eventsRequiredToCalibrate_) || theStatus.isUpdating()) {
0087       theCMPSubtractedSignal.clear();
0088       theNoise.clear();
0089 
0090       for (i = 0; i < in.size(); i++) {
0091         double avVal = (theEventPerStrip[i]) ? theNoiseSum[i] / (theEventPerStrip[i]) : 0.0;
0092         double sqAvVal = (theEventPerStrip[i]) ? theNoiseSqSum[i] / (theEventPerStrip[i]) : 0.0;
0093         double corr_fac = (theEventPerStrip[i] > 1) ? (theEventPerStrip[i] / (theEventPerStrip[i] - 1)) : 1.0;
0094         double rmsVal = (sqAvVal - avVal * avVal > 0.0) ? sqrt(corr_fac * (sqAvVal - avVal * avVal)) : 0.0;
0095 
0096         theCMPSubtractedSignal.push_back(static_cast<float>(avVal));
0097 
0098         theNoise.push_back(static_cast<float>(rmsVal));
0099 
0100         if (false)
0101           cout << " SimpleNoiseCalculator::updateNoise " << theNoiseSum[i] << " " << theNoiseSqSum[i] << " "
0102                << theEventPerStrip[i] << " " << avVal << " " << sqAvVal << " " << (sqAvVal - avVal * avVal) << " "
0103                << rmsVal << endl;
0104       }
0105     }
0106     updateStatus();
0107   }
0108 }
0109 //
0110 // Define New Event
0111 //
0112 void SimpleNoiseCalculator::newEvent() { alreadyUsedEvent = false; }