Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:56:24

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