Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-23 22:52:44

0001 #include "CalibFormats/CaloObjects/interface/CaloSamples.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 #include <cmath>
0004 #include <iostream>
0005 
0006 CaloSamples::CaloSamples() : id_(), size_(0), presamples_(0), preciseSize_(0), precisePresamples_(0) { setBlank(); }
0007 
0008 CaloSamples::CaloSamples(const DetId &id, int size)
0009     : id_(id),
0010       size_(size),
0011       presamples_(0),
0012       data_(size_, 0.0),
0013       deltaTprecise_(0.0f),
0014       preciseSize_(0),
0015       precisePresamples_(0) {
0016   setBlank();
0017 }
0018 
0019 CaloSamples::CaloSamples(const DetId &id, int size, int presize)
0020     : id_(id),
0021       size_(size),
0022       presamples_(0),
0023       data_(size_, 0.0),
0024       deltaTprecise_(0.0f),
0025       preciseSize_(presize),
0026       precisePresamples_(0) {
0027   setBlank();
0028 }
0029 
0030 // add option to set these later.
0031 void CaloSamples::resetPrecise() { preciseData_.resize(preciseSize_, 0); }
0032 
0033 void CaloSamples::setPresamples(int pre) { presamples_ = pre; }
0034 
0035 CaloSamples &CaloSamples::scale(double value) {
0036   for (int i = 0; i < size_; i++)
0037     data_[i] *= value;
0038   for (std::vector<float>::iterator j = preciseData_.begin(); j != preciseData_.end(); ++j)
0039     (*j) *= value;
0040   return (*this);
0041 }
0042 
0043 CaloSamples &CaloSamples::operator+=(double value) {
0044   for (int i = 0; i < size_; i++)
0045     data_[i] += value;
0046   for (std::vector<float>::iterator j = preciseData_.begin(); j != preciseData_.end(); ++j)
0047     (*j) += value * deltaTprecise_ / 25.0;  // note that the scale is conserved!
0048   return (*this);
0049 }
0050 
0051 CaloSamples &CaloSamples::operator+=(const CaloSamples &other) {
0052   bool addHighFidelityPreMix = false;
0053   if (size_ != other.size_ || presamples_ != other.presamples_ || preciseSize_ != other.preciseSize_) {
0054     if (presamples_ == other.presamples_ && preciseSize_ == other.size_) {
0055       addHighFidelityPreMix = true;
0056     } else {
0057       edm::LogError("CaloHitResponse") << "Mismatched calo signals ";
0058     }
0059   }
0060   if (addHighFidelityPreMix) {
0061     int sampleBin(0);
0062     for (int i = 0; i < preciseSize_; ++i) {
0063       sampleBin = floor(i * deltaTprecise_ / 25);
0064       data_[sampleBin] += other.data_[i];
0065       preciseData_[i] += other.data_[i];
0066     }
0067   } else {
0068     int i;
0069     for (i = 0; i < size_; ++i) {
0070       data_[i] += other.data_[i];
0071     }
0072     if (preciseData_.empty() && !other.preciseData_.empty())
0073       resetPrecise();
0074     if (!other.preciseData_.empty()) {
0075       for (i = 0; i < preciseSize_; ++i) {
0076         preciseData_[i] += other.preciseData_[i];
0077       }
0078     }
0079   }
0080   return *this;
0081 }
0082 
0083 CaloSamples &CaloSamples::offsetTime(double offset) {
0084   std::vector<double> data(size_, 0.0);
0085   for (int i(0); i != size_; ++i) {
0086     double t = i * 25. - offset;
0087     int firstbin = floor(t / 25.);
0088     double f = t / 25. - firstbin;
0089     int nextbin = firstbin + 1;
0090     double v1 = (firstbin < 0 || firstbin >= size_) ? 0. : data_[firstbin];
0091     double v2 = (nextbin < 0 || nextbin >= size_) ? 0. : data_[nextbin];
0092     data[i] = (v1 * (1. - f) + v2 * f);
0093   }
0094   for (int i(0); i != size_; ++i) {
0095     data_[i] = data[i];
0096   }
0097   return (*this);
0098 }
0099 
0100 bool CaloSamples::isBlank() const  // are the samples blank (zero?)
0101 {
0102   for (int i(0); i != size_; ++i) {
0103     if (1.e-6 < fabs(data_[i]))
0104       return false;
0105   }
0106   return true;
0107 }
0108 
0109 void CaloSamples::setBlank()  // keep id, presamples, size but zero out data
0110 {
0111   std::fill(data_.begin(), data_.end(), (double)0.0);
0112   std::fill(preciseData_.begin(), preciseData_.end(), (double)0.0);
0113 }
0114 
0115 std::ostream &operator<<(std::ostream &s, const CaloSamples &samples) {
0116   s << "DetId " << samples.id();
0117   // print out every so many precise samples
0118   float preciseStep = samples.preciseSize() / samples.size();
0119   s << ", " << samples.size() << " samples";
0120   if (preciseStep > 0)
0121     s << ", " << samples.preciseSize() << " preciseSamples"
0122       << ", " << preciseStep << " preciseStep";
0123   s << '\n';
0124   for (int i = 0; i < samples.size(); i++) {
0125     s << i << ":" << samples[i] << " precise:";
0126     int precise_start(i * preciseStep), precise_end(precise_start + preciseStep);
0127     for (int j(precise_start); ((j < precise_end) && (j < samples.preciseSize())); ++j)
0128       s << " " << samples.preciseAt(j);
0129     s << std::endl;
0130   }
0131   return s;
0132 }