Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:14:12

0001 // David Stewart, Dec 1 2022
0002 // A way to pass 
0003 // used in conveinece for plotting
0004 #ifndef noi_fnc__h
0005 #define noi_fnc__h
0006 
0007 #include "noiDict.h"
0008 #include "noi_fmt.h"
0009 
0010 string voi_stem(const string& path, const string& ext="") {
0011     // copied from https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
0012     std::string base_filename = path.substr(path.find_last_of("/\\") + 1);
0013     std::string::size_type const p(base_filename.find_last_of('.'));
0014     std::string file_without_extension = base_filename.substr(0, p) + ext;
0015     return file_without_extension;
0016 }
0017 
0018 
0019 const char* noiUniqueName(int i=0,const char* prefix="__pause_") {
0020     while (gDirectory->FindObjectAny(Form("%s__%i",prefix,i))!=nullptr) ++i;
0021     return Form("%s__%i",prefix,i);
0022 };
0023 void noiPause(int i=0)  {
0024     TCanvas* c = new TCanvas( noiUniqueName(i+100), noiUniqueName(i+100), 100,100);
0025     c->SetFillColor(kCyan);
0026     c->SetFillStyle(1001);
0027     c->Draw();
0028     c->WaitPrimitive();
0029 };
0030 
0031 void noiDrawTLatex(const char* msg, double x, double y, 
0032                             noiDict options={}, noiDict dict= {{
0033                             "TextColor",kBlack, "TextColorAlpha",kBlack, 1., "TextSize",22, "TextFont",43,
0034                             "TextAlign",12, "TextAngle",0., }})
0035 {
0036     dict += options;
0037     TLatex tlatex;
0038     tlatex.SetTextColorAlpha(dict("TextColorAlpha",1),dict("TextColorAlpha",2));
0039     tlatex.SetTextAlign(dict("TextAlign"));
0040     tlatex.SetTextSize (dict("TextSize"));
0041     tlatex.SetTextFont (dict("TextFont"));
0042     tlatex.SetTextAngle (dict("TextAngle"));
0043     tlatex.DrawLatex(x, y, msg);
0044 };
0045 TLine* noiDrawTLine(double x0, double y0, double x1, double y1, noiDict options, noiDict dict={{
0046    "LineColorAlpha",kBlack,1.0, "LineStyle",1, "LineWidth",1 }})
0047 {
0048     dict += options;
0049     TLine* line = new TLine(x0,y0,x1,y1);
0050     noi_fmt(line, options);
0051     line->Draw();
0052     return line;
0053 };
0054 string noiStripEnd(string word, string sub) {
0055     auto i0  = sub.length();
0056     auto i1  = word.length();
0057     if (i1 < i0) return word;
0058     return (word.substr(i1-i0,i0)==sub)
0059         ? word.substr(0,i1-i0)
0060         : word;
0061 }
0062 string noiStripEnds(string word, vector<string> endings) {
0063     for (auto sub : endings) {
0064         word = noiStripEnd(word,sub);
0065     }
0066     return word;
0067 };
0068 string noiStripStart(string word, string sub) {
0069     auto i0  = sub.length();
0070     auto i1  = word.length();
0071     if (i0>i1) return word;
0072     word = (word.substr(0,i0) == sub) 
0073         ? word.substr(i0,i1-i0)
0074         : word;
0075     return word;
0076 };
0077 string noiStripStarts(string word, vector<string> startings) {
0078     for (auto pre : startings) {
0079         word = noiStripStart(word,pre);
0080     }
0081     return word;
0082 };
0083 const char* noi_geant05_ascii(int geantid) {
0084     switch (geantid) {
0085         case 8: return "pi";
0086         case 9: return "antipi";
0087         case 11: return "K";
0088         case 12: return "antiK";
0089         case 14: return "p";
0090         case 15: return "pbar";
0091 
0092         case 0: return "pi";
0093         case 1: return "antipi";
0094         case 2: return "K";
0095         case 3: return "antiK";
0096         case 4: return "p";
0097         case 5: return "pbar";
0098     }
0099     return "none";
0100 };
0101 int noi_geant05(int geantid) {
0102     switch (geantid) {
0103         case 8: return 0;
0104         case 9: return 1;
0105         case 11: return 2;
0106         case 12: return 3;
0107         case 14: return 4;
0108         case 15: return 5;
0109     }
0110     return -1;
0111 };
0112 const char* noi_geant05_greek(int geantid) {
0113     switch (geantid) {
0114         case 8: return "#pi";
0115         case 9: return "#pi^{-}";
0116         case 11: return "K";
0117         case 12: return "K^{-}";
0118         case 14: return "p";
0119         case 15: return "#bar{p}";
0120 
0121         case 0: return "#pi";
0122         case 1: return "#pi^{-}";
0123         case 2: return "K";
0124         case 3: return "K^{-}";
0125         case 4: return "p";
0126         case 5: return "#bar{p}";
0127     }
0128     return "none";
0129 };
0130 
0131 TH1* noiDivide(TH1* num, TH1* den, noiDict opt={}, noiDict dict={}) {
0132     dict += opt;
0133 
0134     double norm_num {1};
0135     double norm_den {1};
0136 
0137     if (dict["norm"]) {
0138         norm_num = num->Integral();
0139         norm_den = den->Integral();
0140     };
0141     if (dict["print"]) cout << " norms: " << norm_num << " and " << norm_den << endl;
0142 
0143     TH1D* ret;
0144     if (dict["style-den"]) {
0145         ret = (TH1D*) den->Clone(noiUniqueName());
0146     } else {
0147         ret = (TH1D*) num->Clone(noiUniqueName());
0148     }
0149 
0150     for (int j=1;j<den->GetNbinsX()+1;++j){
0151         double n = num->GetBinContent(j) / norm_num;
0152         double d = den->GetBinContent(j) / norm_den;
0153         if (n == 0 || d == 0) {
0154             ret->SetBinContent(j,0);
0155             ret->SetBinError(j,0);
0156             /* if (i==0) cout << "Jin: " << j << " val: " << den->GetBinContent(j) << endl; */
0157             continue;
0158         }
0159         double n_err = num->GetBinError(j) / norm_num;
0160         double d_err = den->GetBinError(j) / norm_den;
0161         double val = n / d;
0162         double err = val * pow( pow(n_err/n,2)+pow(d_err/d,2),0.5);
0163         ret->SetBinContent(j,val);
0164         ret->SetBinError(j,err);
0165     }
0166     return ret;
0167 };
0168 
0169 string noiStripExtension(const char* in) {
0170     string word = in;
0171     if (word.find(".",1) != string::npos) word = word.substr(0,word.find(".",1));
0172     return word;
0173 };
0174 
0175 TGraph* noiMakeTGraph(vector<double>& x, vector<double>& y) {
0176     if (x.size() != y.size()) 
0177         throw std::runtime_error("noiMakeTGraph(vec, vec) required vectors of same length");
0178     const unsigned int n = x.size();
0179     double* xpts = new double[n];
0180     double* ypts = new double[n];
0181     for (unsigned int i{0}; i<n; ++i) {
0182         xpts[i] = x[i];
0183         ypts[i] = y[i];
0184     }
0185     return new TGraph(n,xpts,ypts);
0186 };
0187 #endif