Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:13:29

0001 #ifndef UTILITIES_H
0002 #define UTILITIES_H
0003 
0004 #include <vector>
0005 
0006 #include <TMath.h>
0007 #include <TFile.h>
0008 #include <TTree.h>
0009 
0010 using namespace std;
0011 
0012 template <class T> void CleanVec(std::vector<T> &v)
0013 {
0014     std::vector<T>().swap(v);
0015     v.shrink_to_fit();
0016 }
0017 
0018 float deltaPhi(float phi1, float phi2)
0019 {
0020     float dPhi = phi1 - phi2;
0021     if (dPhi > TMath::Pi())
0022         dPhi -= 2. * TMath::Pi();
0023     if (dPhi < -TMath::Pi())
0024         dPhi += 2. * TMath::Pi();
0025     return dPhi;
0026 }
0027 
0028 float deltaR(float eta1, float phi1, float eta2, float phi2)
0029 {
0030     float dEta, dPhi;
0031     dEta = eta1 - eta2;
0032     dPhi = deltaPhi(phi1, phi2);
0033     return sqrt(dEta * dEta + dPhi * dPhi);
0034 }
0035 
0036 template <typename T> std::string number_to_string(T param_)
0037 {
0038     std::string str;
0039     if (param_ < 0)
0040     {
0041         str += "M";
0042         param_ = std::abs(param_);
0043     }
0044     str += std::to_string(param_);
0045     std::size_t found = str.find('.');
0046     if (found == std::string::npos)
0047         return str;
0048     str.replace(found, 1, "p");
0049     while (*(str.end() - 1) == '0' && *(str.end() - 2) != 'p' && !str.empty())
0050         str.erase(str.end() - 1);
0051     if (*(str.end() - 1) == '0' && *(str.end() - 2) == 'p')
0052         str.erase(str.size() - 2, 2);
0053     return str;
0054 }
0055 
0056 // MVTX stave position
0057 std::vector<std::tuple<float, float, float, float>> MVTXStavePositionXY()
0058 {
0059     vector<tuple<float, float, float, float>> v; // x1, y1, x2, y2
0060     TFile *f = new TFile("./MVTX_geo.root", "READ");
0061     f->cd();
0062     TTree *t = (TTree *)f->Get("tree");
0063     float x1, y1, x2, y2;
0064     t->SetBranchAddress("x1", &x1);
0065     t->SetBranchAddress("y1", &y1);
0066     t->SetBranchAddress("x2", &x2);
0067     t->SetBranchAddress("y2", &y2);
0068 
0069     for (int i = 0; i < t->GetEntries(); i++)
0070     {
0071         t->GetEntry(i);
0072 
0073         v.push_back(make_tuple(x1, y1, x2, y2));
0074     }
0075 
0076     return v;
0077 }
0078 
0079 #endif