Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:13:53

0001 #include "L1Trigger/TrackFindingTracklet/interface/Stub.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/Globals.h"
0003 #include "L1Trigger/TrackFindingTracklet/interface/SLHCEvent.h"
0004 
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/Utilities/interface/Exception.h"
0007 #include "DataFormats/Math/interface/deltaPhi.h"
0008 
0009 #include <cmath>
0010 #include <bitset>
0011 
0012 using namespace std;
0013 using namespace trklet;
0014 
0015 Stub::Stub(Settings const& settings) : settings_(settings) {}
0016 
0017 Stub::Stub(L1TStub& stub, Settings const& settings, Globals& globals) : settings_(settings) {
0018   const string& stubwordhex = stub.stubword();
0019 
0020   const string stubwordbin = convertHexToBin(stubwordhex);
0021 
0022   layerdisk_ = stub.layerdisk();
0023 
0024   int nbendbits = stub.isPSmodule() ? N_BENDBITS_PS : N_BENDBITS_2S;
0025 
0026   int nalphabits = 0;
0027 
0028   int nrbits = settings_.nrbitsstub(layerdisk_);
0029   int nzbits = settings_.nzbitsstub(layerdisk_);
0030   int nphibits = settings_.nphibitsstub(layerdisk_);
0031 
0032   if (layerdisk_ >= N_LAYER && !stub.isPSmodule()) {
0033     nalphabits = settings.nbitsalpha();
0034     nrbits = 7;
0035   }
0036 
0037   assert(nbendbits + nalphabits + nrbits + nzbits + nphibits == 36);
0038 
0039   bitset<32> rbits(stubwordbin.substr(0, nrbits));
0040   bitset<32> zbits(stubwordbin.substr(nrbits, nzbits));
0041   bitset<32> phibits(stubwordbin.substr(nrbits + nzbits, nphibits));
0042   bitset<32> alphabits(stubwordbin.substr(nphibits + nzbits + nrbits, nalphabits));
0043   bitset<32> bendbits(stubwordbin.substr(nphibits + nzbits + nrbits + nalphabits, nbendbits));
0044 
0045   int newbend = bendbits.to_ulong();
0046 
0047   int newr = rbits.to_ulong();
0048   if (layerdisk_ < N_LAYER) {
0049     if (newr >= (1 << (nrbits - 1)))
0050       newr = newr - (1 << nrbits);
0051   }
0052 
0053   int newz = zbits.to_ulong();
0054   if (newz >= (1 << (nzbits - 1)))
0055     newz = newz - (1 << nzbits);
0056 
0057   int newphi = phibits.to_ulong();
0058 
0059   int newalpha = alphabits.to_ulong();
0060   if (newalpha >= (1 << (nalphabits - 1)))
0061     newalpha = newalpha - (1 << nalphabits);
0062 
0063   l1tstub_ = &stub;
0064 
0065   bend_.set(newbend, nbendbits, true, __LINE__, __FILE__);
0066 
0067   phi_.set(newphi, nphibits, true, __LINE__, __FILE__);
0068   phicorr_.set(newphi, nphibits, true, __LINE__, __FILE__);
0069   bool pos = false;
0070   if (layerdisk_ >= N_LAYER) {
0071     pos = true;
0072     int disk = layerdisk_ - N_LAYER + 1;
0073     if (stub.z() < 0.0)
0074       disk = -disk;
0075     disk_.set(disk, 4, false, __LINE__, __FILE__);
0076     if (!stub.isPSmodule()) {
0077       alpha_.set(newalpha, nalphabits, false, __LINE__, __FILE__);
0078       nrbits = 4;
0079     }
0080   } else {
0081     disk_.set(0, 4, false, __LINE__, __FILE__);
0082     layer_.set(layerdisk_, 3, true, __LINE__, __FILE__);
0083   }
0084   r_.set(newr, nrbits, pos, __LINE__, __FILE__);
0085   z_.set(newz, nzbits, false, __LINE__, __FILE__);
0086 
0087   if (settings.writeMonitorData("StubBend")) {
0088     unsigned int nsimtrks = globals.event()->nsimtracks();
0089 
0090     for (unsigned int isimtrk = 0; isimtrk < nsimtrks; isimtrk++) {
0091       const L1SimTrack& simtrk = globals.event()->simtrack(isimtrk);
0092       if (stub.tpmatch2(simtrk.trackid())) {
0093         double dr = 0.18;
0094         double rinv = simtrk.charge() * 0.01 * settings_.c() * settings_.bfield() / simtrk.pt();
0095         double pitch = settings_.stripPitch(stub.isPSmodule());
0096         double bend = stub.r() * dr * 0.5 * rinv / pitch;
0097 
0098         globals.ofstream("stubbend.dat") << layerdisk_ << " " << stub.isPSmodule() << " "
0099                                          << simtrk.pt() * simtrk.charge() << " " << bend << " " << newbend << " "
0100                                          << settings.benddecode(newbend, layerdisk_, stub.isPSmodule()) << " "
0101                                          << settings.bendcut(newbend, layerdisk_, stub.isPSmodule()) << endl;
0102       }
0103     }
0104   }
0105 }
0106 
0107 FPGAWord Stub::iphivmFineBins(int VMbits, int finebits) const {
0108   unsigned int finephi = (phicorr_.value() >> (phicorr_.nbits() - VMbits - finebits)) & ((1 << finebits) - 1);
0109   return FPGAWord(finephi, finebits, true, __LINE__, __FILE__);
0110 }
0111 
0112 unsigned int Stub::phiregionaddress() const {
0113   int iphi = (phicorr_.value() >> (phicorr_.nbits() - settings_.nbitsallstubs(layerdisk())));
0114   return (iphi << 7) + stubindex_.value();
0115 }
0116 
0117 std::string Stub::phiregionaddressstr() const {
0118   int iphi = (phicorr_.value() >> (phicorr_.nbits() - settings_.nbitsallstubs(layerdisk())));
0119   FPGAWord phiregion(iphi, 3, true, __LINE__, __FILE__);
0120   return phiregion.str() + stubindex_.str();
0121 }
0122 
0123 void Stub::setAllStubIndex(int nstub) {
0124   if (nstub >= (1 << N_BITSMEMADDRESS)) {
0125     if (settings_.debugTracklet())
0126       edm::LogPrint("Tracklet") << "Warning too large stubindex!";
0127     nstub = (1 << N_BITSMEMADDRESS) - 1;
0128   }
0129 
0130   stubindex_.set(nstub, N_BITSMEMADDRESS);
0131 }
0132 
0133 void Stub::setPhiCorr(int phiCorr) {
0134   int iphicorr = phi_.value() - phiCorr;
0135 
0136   if (iphicorr < 0)
0137     iphicorr = 0;
0138   if (iphicorr >= (1 << phi_.nbits()))
0139     iphicorr = (1 << phi_.nbits()) - 1;
0140 
0141   phicorr_.set(iphicorr, phi_.nbits(), true, __LINE__, __FILE__);
0142 }
0143 
0144 double Stub::rapprox() const {
0145   if (disk_.value() == 0) {
0146     int lr = 1 << (8 - settings_.nrbitsstub(layer_.value()));
0147     return r_.value() * settings_.kr() * lr + settings_.rmean(layer_.value());
0148   }
0149   if (!l1tstub_->isPSmodule()) {
0150     if (abs(disk_.value()) <= 2)
0151       return settings_.rDSSinner(r_.value());
0152     else
0153       return settings_.rDSSouter(r_.value());
0154   }
0155   return r_.value() * settings_.kr();
0156 }
0157 
0158 double Stub::zapprox() const {
0159   if (disk_.value() == 0) {
0160     int lz = 1;
0161     if (layer_.value() >= 3) {
0162       lz = 16;
0163     }
0164     return z_.value() * settings_.kz() * lz;
0165   }
0166   int sign = 1;
0167   if (disk_.value() < 0)
0168     sign = -1;
0169   if (sign < 0) {
0170     //Should understand why this is needed to get agreement with integer calculations
0171     return (z_.value() + 1) * settings_.kz() + sign * settings_.zmean(abs(disk_.value()) - 1);
0172   } else {
0173     return z_.value() * settings_.kz() + sign * settings_.zmean(abs(disk_.value()) - 1);
0174   }
0175 }
0176 
0177 double Stub::phiapprox(double phimin, double) const {
0178   int lphi = 1;
0179   if (layer_.value() >= 3) {
0180     lphi = 8;
0181   }
0182   return reco::reduceRange(phimin + phi_.value() * settings_.kphi() / lphi);
0183 }
0184 
0185 unsigned int Stub::layerdisk() const {
0186   if (layer_.value() == -1)
0187     return N_LAYER - 1 + abs(disk_.value());
0188   return layer_.value();
0189 }