pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixUtil.cc
1 #include "PixUtil.hh"
2 
3 #include "TMath.h"
4 #include "TStyle.h"
5 #include "TColor.h"
6 
7 using namespace std;
8 
9 // ----------------------------------------------------------------------
10 void PixUtil::setPlotStyle() {
11  const Int_t NRGBs = 5;
12  const Int_t NCont = 255;
13 
14  Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
15  Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
16  Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
17  Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
18  TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
19  gStyle->SetNumberContours(NCont);
20 }
21 
22 
23 // ----------------------------------------------------------------------
24 void PixUtil::replaceAll(string& str, const string& from, const string& to) {
25  if (from.empty()) return;
26  size_t start_pos = 0;
27  while((start_pos = str.find(from, start_pos)) != string::npos) {
28  str.replace(start_pos, from.length(), to);
29  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
30  }
31 }
32 
33 // ----------------------------------------------------------------------
34 double PixUtil::dEff(int in, int iN) {
35  double n = (double)in;
36  double N = (double)iN;
37  return TMath::Sqrt(((n+1)*(N-n+1))/((N+3)*(N+2)*(N+2)));
38 }
39 
40 // ----------------------------------------------------------------------
41 double PixUtil::dBinomial(int in, int iN) {
42  double n = (double)in;
43  double N = (double)iN;
44  double w = n/N;
45  if (n == N) return 0.05;
46  if (n == 0) return 0.3/TMath::Sqrt(N);
47  return TMath::Sqrt(TMath::Abs(w*(1-w)/N));
48 }
49 
50 // ----------------------------------------------------------------------
51 int PixUtil::rcr2idx(int iroc, int icol, int irow) {
52  if (irow < 0 || irow > 79) return -1;
53  if (icol < 0 || icol > 51) return -1;
54  return iroc*80*52 + icol*80 + irow;
55 }
56 
57 
58 // ----------------------------------------------------------------------
59 void PixUtil::idx2rcr(int idx, int &iroc, int &icol, int &irow) {
60  iroc = idx/4160;
61  int r = idx - iroc*4160;
62  icol = r/80;
63  irow = r%80;
64 }
65 
66 // ----------------------------------------------------------------------
67 void PixUtil::cleanupString(string &s) {
68  replaceAll(s, "\t", " ");
69  string::size_type s1 = s.find("#");
70  if (string::npos != s1) s.erase(s1);
71  if (0 == s.length()) return;
72  string::iterator new_end = unique(s.begin(), s.end(), bothAreSpaces);
73  s.erase(new_end, s.end());
74  if (s.substr(0, 1) == string(" ")) s.erase(0, 1);
75  if (s.substr(s.length()-1, 1) == string(" ")) s.erase(s.length()-1, 1);
76 }
77 
78 // ----------------------------------------------------------------------
79 bool PixUtil::bothAreSpaces(char lhs, char rhs) {
80  return (lhs == rhs) && (lhs == ' ');
81 }
static void replaceAll(std::string &str, const std::string &from, const std::string &to)
in str, replace all occurences of from to to
Definition: PixUtil.cc:24
static int rcr2idx(int iroc, int icol, int irow)
convert ROC/COL/ROW into idx
Definition: PixUtil.cc:51
static bool bothAreSpaces(char lhs, char rhs)
what would you expect?
Definition: PixUtil.cc:79
static void idx2rcr(int idx, int &iroc, int &icol, int &irow)
and back again
Definition: PixUtil.cc:59
static void cleanupString(std::string &)
cleanup a string: remove everything behind #, concatenate multiple spaces into one, translate tabs into spaces
Definition: PixUtil.cc:67