Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:29

0001 #ifndef L1Trigger_TrackFindingTracklet_interface_Util_h
0002 #define L1Trigger_TrackFindingTracklet_interface_Util_h
0003 
0004 #include <sstream>
0005 #include <fstream>
0006 #include <cassert>
0007 #include <cmath>
0008 #include <string>
0009 #include <algorithm>
0010 #include <sys/types.h>
0011 #include <sys/stat.h>
0012 
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 namespace trklet {
0017 
0018   //Converts string in binary to hex (used in writing out memory content)
0019   inline std::string hexFormat(const std::string& binary) {
0020     std::stringstream ss;
0021 
0022     unsigned int radix = 1, value = 0;
0023     for (int i = binary.length() - 1; i >= 0; i--) {
0024       if (binary.at(i) != '0' && binary.at(i) != '1')
0025         continue;
0026       value += (binary.at(i) - '0') * radix;
0027       if (radix == 8) {
0028         ss << std::hex << value;
0029         radix = 1;
0030         value = 0;
0031       } else
0032         radix <<= 1;
0033     }
0034     if (radix != 1)
0035       ss << std::hex << value;
0036 
0037     std::string str = ss.str() + "x0";
0038     std::reverse(str.begin(), str.end());
0039     return str;
0040   }
0041 
0042   inline double bendstrip(double r, double rinv, double stripPitch) {
0043     constexpr double dr = 0.18;
0044     double delta = r * dr * 0.5 * rinv;
0045     double bend = delta / stripPitch;
0046     return bend;
0047   }
0048 
0049   inline double rinv(double phi1, double phi2, double r1, double r2) {
0050     if (r2 <= r1) {  //FIXME can not form tracklet should not call function with r2<=r1
0051       return 20.0;
0052     }
0053 
0054     double dphi = phi2 - phi1;
0055     double dr = r2 - r1;
0056 
0057     return 2.0 * sin(dphi) / dr / sqrt(1.0 + 2 * r1 * r2 * (1.0 - cos(dphi)) / (dr * dr));
0058   }
0059 
0060   inline std::string convertHexToBin(const std::string& stubwordhex) {
0061     std::string stubwordbin = "";
0062 
0063     for (char word : stubwordhex) {
0064       std::string hexword = "";
0065       if (word == '0')
0066         hexword = "0000";
0067       else if (word == '1')
0068         hexword = "0001";
0069       else if (word == '2')
0070         hexword = "0010";
0071       else if (word == '3')
0072         hexword = "0011";
0073       else if (word == '4')
0074         hexword = "0100";
0075       else if (word == '5')
0076         hexword = "0101";
0077       else if (word == '6')
0078         hexword = "0110";
0079       else if (word == '7')
0080         hexword = "0111";
0081       else if (word == '8')
0082         hexword = "1000";
0083       else if (word == '9')
0084         hexword = "1001";
0085       else if (word == 'A')
0086         hexword = "1010";
0087       else if (word == 'B')
0088         hexword = "1011";
0089       else if (word == 'C')
0090         hexword = "1100";
0091       else if (word == 'D')
0092         hexword = "1101";
0093       else if (word == 'E')
0094         hexword = "1110";
0095       else if (word == 'F')
0096         hexword = "1111";
0097       else {
0098         throw cms::Exception("Inconsistency")
0099             << __FILE__ << " " << __LINE__ << " hex string format invalid: " << stubwordhex;
0100       }
0101       stubwordbin += hexword;
0102     }
0103     return stubwordbin;
0104   }
0105 
0106   inline int ilog2(double factor) {
0107     double power = log(factor) / log(2);
0108     int ipower = round(power);
0109     assert(std::abs(power - ipower) < 0.1);
0110     return ipower;
0111   }
0112 
0113   /******************************************************************************
0114  * Checks to see if a directory exists. Note: This method only checks the
0115  * existence of the full path AND if path leaf is a dir.
0116  *
0117  * @return   1 if dir exists AND is a dir,
0118  *           0 if dir does not exist OR exists but not a dir,
0119  *          -1 if an error occurred (errno is also set)
0120  *****************************************************************************/
0121   inline int dirExists(const std::string& path) {
0122     struct stat info;
0123 
0124     int statRC = stat(path.c_str(), &info);
0125     if (statRC != 0) {
0126       if (errno == ENOENT) {
0127         return 0;
0128       }  // something along the path does not exist
0129       if (errno == ENOTDIR) {
0130         return 0;
0131       }  // something in path prefix is not a dir
0132       return -1;
0133     }
0134 
0135     return (info.st_mode & S_IFDIR) ? 1 : 0;
0136   }
0137 
0138   //Open file - create directory if not existent.
0139   inline std::ofstream openfile(const std::string& dir, const std::string& fname, const char* file, int line) {
0140     if (dirExists(dir) != 1) {
0141       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0142       int fail = system((std::string("mkdir -p ") + dir).c_str());
0143       if (fail) {
0144         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0145       }
0146     }
0147 
0148     std::ofstream out(dir + "/" + fname);
0149 
0150     if (out.fail()) {
0151       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0152     }
0153 
0154     return out;
0155   }
0156 
0157   //Open file - create directory if not existent.
0158   //If first==true open file in create mode, if first==false open in append mode
0159   inline void openfile(
0160       std::ofstream& out, bool first, const std::string& dir, const std::string& fname, const char* file, int line) {
0161     if (dirExists(dir) != 1) {
0162       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0163       int fail = system((std::string("mkdir -p ") + dir).c_str());
0164       if (fail) {
0165         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0166       }
0167     }
0168 
0169     if (first) {
0170       out.open(fname);
0171     } else {
0172       out.open(fname, std::ofstream::app);
0173     }
0174 
0175     if (out.fail()) {
0176       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0177     }
0178   }
0179 
0180 };  // namespace trklet
0181 #endif