Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-15 01:07:50

0001 #include "L1Trigger/TrackFindingTracklet/interface/imath.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 using namespace trklet;
0005 
0006 bool VarBase::calculate(int debug_level) {
0007   bool ok1 = true;
0008   bool ok2 = true;
0009   bool ok3 = true;
0010 
0011   if (p1_)
0012     ok1 = p1_->calculate(debug_level);
0013   if (p2_)
0014     ok2 = p2_->calculate(debug_level);
0015   if (p3_)
0016     ok3 = p3_->calculate(debug_level);
0017 
0018   long int ival_prev = ival_;
0019   local_calculate();
0020 
0021   bool all_ok = ok1 && ok2 && ok3 && debug_level;
0022 
0023   if (fval_ > maxval_)
0024     maxval_ = fval_;
0025   if (fval_ < minval_)
0026     minval_ = fval_;
0027 #ifdef IMATH_ROOT
0028   if (globals_->use_root) {
0029     if (h_ == 0) {
0030       globals_->h_file_->cd();
0031       std::string hname = "h_" + name_;
0032       h_ = (TH2F *)globals_->h_file_->Get(hname.c_str());
0033       if (h_ == 0) {
0034         h_precision_ = 0.5 * h_nbins_ * K_;
0035         std::string st = name_ + ";fval;fval-ival*K";
0036         h_ = new TH2F(hname.c_str(), name_.c_str(), h_nbins_, -range(), range(), h_nbins_, -h_precision_, h_precision_);
0037         if (debug_level == 3)
0038           edm::LogVerbatim("Tracklet") << " booking histogram " << hname;
0039       }
0040     }
0041     if (ival_ != ival_prev || op_ == "def" || op_ == "const")
0042       h_->Fill(fval_, K_ * ival_ - fval_);
0043   }
0044 #endif
0045 
0046   bool todump = false;
0047   int nmax = sizeof(long int) * 8;
0048   int ns = nmax - nbits_;
0049   long int itest = ival_;
0050   itest = l1t::bitShift(itest, ns);
0051   itest = itest >> ns;
0052   if (itest != ival_) {
0053     if (debug_level == 3 || (ival_ != ival_prev && all_ok)) {
0054       edm::LogVerbatim("Tracklet") << "imath: truncated value mismatch!! " << ival_ << " != " << itest;
0055       todump = true;
0056     }
0057     all_ok = false;
0058   }
0059 
0060   val_ = ival_ * K_;
0061   float ftest = val_;
0062   float tolerance = 0.1 * std::abs(fval_);
0063   if (tolerance < 2 * K_)
0064     tolerance = 2 * K_;
0065   if (std::abs(ftest - fval_) > tolerance) {
0066     if (debug_level == 3 || (ival_ != ival_prev && (all_ok && (op_ != "inv" || debug_level >= 2)))) {
0067       edm::LogVerbatim("Tracklet") << "imath: **GROSS** value mismatch!! " << fval_ << " != " << ftest;
0068       if (op_ == "inv")
0069         edm::LogVerbatim("Tracklet") << p1_->dump() << "\n-----------------------------------";
0070       todump = true;
0071     }
0072     all_ok = false;
0073   }
0074 
0075   if (todump)
0076     edm::LogVerbatim("Tracklet") << dump();
0077 
0078   return all_ok;
0079 }
0080 
0081 void VarFlag::calculate_step() {
0082   int max_step = 0;
0083   for (const auto &cut : cuts_) {
0084     if (!cut->cut_var())
0085       continue;
0086     if (cut->cut_var()->latency() + cut->cut_var()->step() > max_step)
0087       max_step = cut->cut_var()->latency() + cut->cut_var()->step();
0088   }
0089   step_ = max_step;
0090 }
0091 
0092 //
0093 //  local calculations
0094 //
0095 
0096 void VarAdjustK::local_calculate() {
0097   fval_ = p1_->fval();
0098   ival_ = p1_->ival();
0099   if (lr_ > 0)
0100     ival_ = ival_ >> lr_;
0101   else if (lr_ < 0)
0102     ival_ = l1t::bitShift(ival_, (-lr_));
0103 }
0104 
0105 void VarAdjustKR::local_calculate() {
0106   fval_ = p1_->fval();
0107   ival_ = p1_->ival();
0108   if (lr_ > 0)
0109     ival_ = ((ival_ >> (lr_ - 1)) + 1) >> 1;  //rounding
0110   else if (lr_ < 0)
0111     ival_ = l1t::bitShift(ival_, (-lr_));
0112 }
0113 
0114 void VarAdd::local_calculate() {
0115   fval_ = p1_->fval() + p2_->fval();
0116   long int i1 = p1_->ival();
0117   long int i2 = p2_->ival();
0118   if (shift1 > 0)
0119     i1 = l1t::bitShift(i1, shift1);
0120   if (shift2 > 0)
0121     i2 = l1t::bitShift(i2, shift2);
0122   ival_ = i1 + i2;
0123   if (ps_ > 0)
0124     ival_ = ival_ >> ps_;
0125 }
0126 
0127 void VarSubtract::local_calculate() {
0128   fval_ = p1_->fval() - p2_->fval();
0129   long int i1 = p1_->ival();
0130   long int i2 = p2_->ival();
0131   if (shift1 > 0)
0132     i1 = l1t::bitShift(i1, shift1);
0133   if (shift2 > 0)
0134     i2 = l1t::bitShift(i2, shift2);
0135   ival_ = i1 - i2;
0136   if (ps_ > 0)
0137     ival_ = ival_ >> ps_;
0138 }
0139 
0140 void VarNounits::local_calculate() {
0141   fval_ = p1_->fval();
0142   ival_ = (p1_->ival() * cI_) >> ps_;
0143 }
0144 
0145 void VarTimesC::local_calculate() {
0146   fval_ = p1_->fval() * cF_;
0147   ival_ = (p1_->ival() * cI_) >> ps_;
0148 }
0149 
0150 void VarNeg::local_calculate() {
0151   fval_ = -p1_->fval();
0152   ival_ = -p1_->ival();
0153 }
0154 
0155 void VarShift::local_calculate() {
0156   fval_ = p1_->fval() * pow(2, -shift_);
0157   ival_ = p1_->ival();
0158   if (shift_ > 0)
0159     ival_ = ival_ >> shift_;
0160   if (shift_ < 0)
0161     ival_ = l1t::bitShift(ival_, (-shift_));
0162 }
0163 
0164 void VarShiftround::local_calculate() {
0165   fval_ = p1_->fval() * pow(2, -shift_);
0166   ival_ = p1_->ival();
0167   if (shift_ > 0)
0168     ival_ = ((ival_ >> (shift_ - 1)) + 1) >> 1;
0169   if (shift_ < 0)
0170     ival_ = l1t::bitShift(ival_, (-shift_));
0171 }
0172 
0173 void VarMult::local_calculate() {
0174   fval_ = p1_->fval() * p2_->fval();
0175   ival_ = (p1_->ival() * p2_->ival()) >> ps_;
0176 }
0177 
0178 void VarDSPPostadd::local_calculate() {
0179   fval_ = p1_->fval() * p2_->fval() + p3_->fval();
0180   ival_ = p3_->ival();
0181   if (shift3_ > 0)
0182     ival_ = l1t::bitShift(ival_, shift3_);
0183   if (shift3_ < 0)
0184     ival_ = ival_ >> (-shift3_);
0185   ival_ += p1_->ival() * p2_->ival();
0186   ival_ = ival_ >> ps_;
0187 }
0188 
0189 void VarInv::local_calculate() {
0190   fval_ = 1. / (offset_ + p1_->fval());
0191   ival_ = LUT[ival_to_addr(p1_->ival())];
0192 }