Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:56

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, double sensorSpacing) {
0043     double delta = r * sensorSpacing * 0.5 * rinv;
0044     double bend = delta / stripPitch;
0045     return bend;
0046   }
0047 
0048   inline double convertFEBend(
0049       double FEbend, double sensorSep, double sensorSpacing, double CF, bool barrel, double r = 0) {
0050     double bend = sensorSpacing * CF * FEbend / sensorSep;
0051     return bend;
0052   }
0053 
0054   inline double tan_theta(double r, double z, double z0, bool z0_max) {
0055     //Calculates tan(theta) = z_displaced/r
0056     //measure tan theta at different points to account for displaced tracks
0057     double tan;
0058     if (z0_max)
0059       tan = (z - z0) / r;
0060     else
0061       tan = (z + z0) / r;
0062 
0063     return tan;
0064   }
0065 
0066   inline double rinv(double phi1, double phi2, double r1, double r2) {
0067     assert(r1 < r2);  //Can not form tracklet should not call function with r2<=r1
0068 
0069     double dphi = phi2 - phi1;
0070     double dr = r2 - r1;
0071 
0072     return 2.0 * sin(dphi) / dr / sqrt(1.0 + 2 * r1 * r2 * (1.0 - cos(dphi)) / (dr * dr));
0073   }
0074 
0075   inline std::string convertHexToBin(const std::string& stubwordhex) {
0076     std::string stubwordbin = "";
0077 
0078     for (char word : stubwordhex) {
0079       std::string hexword = "";
0080       if (word == '0')
0081         hexword = "0000";
0082       else if (word == '1')
0083         hexword = "0001";
0084       else if (word == '2')
0085         hexword = "0010";
0086       else if (word == '3')
0087         hexword = "0011";
0088       else if (word == '4')
0089         hexword = "0100";
0090       else if (word == '5')
0091         hexword = "0101";
0092       else if (word == '6')
0093         hexword = "0110";
0094       else if (word == '7')
0095         hexword = "0111";
0096       else if (word == '8')
0097         hexword = "1000";
0098       else if (word == '9')
0099         hexword = "1001";
0100       else if (word == 'A')
0101         hexword = "1010";
0102       else if (word == 'B')
0103         hexword = "1011";
0104       else if (word == 'C')
0105         hexword = "1100";
0106       else if (word == 'D')
0107         hexword = "1101";
0108       else if (word == 'E')
0109         hexword = "1110";
0110       else if (word == 'F')
0111         hexword = "1111";
0112       else {
0113         throw cms::Exception("Inconsistency")
0114             << __FILE__ << " " << __LINE__ << " hex string format invalid: " << stubwordhex;
0115       }
0116       stubwordbin += hexword;
0117     }
0118     return stubwordbin;
0119   }
0120 
0121   inline int ilog2(double factor) {
0122     double power = log(factor) / log(2);
0123     int ipower = round(power);
0124     assert(std::abs(power - ipower) < 0.1);
0125     return ipower;
0126   }
0127 
0128   /******************************************************************************
0129  * Checks to see if a directory exists. Note: This method only checks the
0130  * existence of the full path AND if path leaf is a dir.
0131  *
0132  * @return   1 if dir exists AND is a dir,
0133  *           0 if dir does not exist OR exists but not a dir,
0134  *          -1 if an error occurred (errno is also set)
0135  *****************************************************************************/
0136   inline int dirExists(const std::string& path) {
0137     struct stat info;
0138 
0139     int statRC = stat(path.c_str(), &info);
0140     if (statRC != 0) {
0141       if (errno == ENOENT) {
0142         return 0;
0143       }  // something along the path does not exist
0144       if (errno == ENOTDIR) {
0145         return 0;
0146       }  // something in path prefix is not a dir
0147       return -1;
0148     }
0149 
0150     return (info.st_mode & S_IFDIR) ? 1 : 0;
0151   }
0152 
0153   //Open file - create directory if not existent.
0154   inline std::ofstream openfile(const std::string& dir, const std::string& fname, const char* file, int line) {
0155     if (dirExists(dir) != 1) {
0156       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0157       int fail = system((std::string("mkdir -p ") + dir).c_str());
0158       if (fail) {
0159         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0160       }
0161     }
0162 
0163     std::ofstream out(dir + "/" + fname);
0164 
0165     if (out.fail()) {
0166       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0167     }
0168 
0169     return out;
0170   }
0171 
0172   //Open file - create directory if not existent.
0173   //If first==true open file in create mode, if first==false open in append mode
0174   inline void openfile(
0175       std::ofstream& out, bool first, const std::string& dir, const std::string& fname, const char* file, int line) {
0176     if (dirExists(dir) != 1) {
0177       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0178       int fail = system((std::string("mkdir -p ") + dir).c_str());
0179       if (fail) {
0180         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0181       }
0182     }
0183 
0184     if (first) {
0185       out.open(fname);
0186     } else {
0187       out.open(fname, std::ofstream::app);
0188     }
0189 
0190     if (out.fail()) {
0191       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0192     }
0193   }
0194 
0195 };  // namespace trklet
0196 #endif