FEDRA emulsion software from the OPERA Collaboration
ERTools Namespace Reference

Namespaces

 Internal
 

Classes

struct  PeakInfo
 

Functions

double ComputeAsymmetry (TH1 *hist1, TH1 *hist2)
 
void DiffProfile2D (const TProfile2D *prof1, const TProfile2D *prof2, TH2D *hDiff)
 
std::vector< double > FindPeaksTSpectrum (TH1 *hist, int npeaks=5)
 
std::vector< PeakInfoFindPeaksWithIntegral (TH1 *hist, int window_size, double threshold, int from_bin, int to_bin)
 
TH2D * get_h2_var (TTree *tree, const char *var1, const char *var2, const char *hname, double bin1, double bin2)
 
TH1D * get_h_var (TTree *tree, const char *var, const char *hname, double bin, const char *cut)
 
double GetMaxBinHeight (const TH1 *hist)
 
TH1D * RebinHistogram (const TH1 *hist, int group)
 

Function Documentation

◆ ComputeAsymmetry()

double ERTools::ComputeAsymmetry ( TH1 *  hist1,
TH1 *  hist2 
)

◆ DiffProfile2D()

void ERTools::DiffProfile2D ( const TProfile2D *  prof1,
const TProfile2D *  prof2,
TH2D *  hDiff 
)
77 {
78  for (int binx = 1; binx <= prof1->GetNbinsX(); ++binx) {
79  for (int biny = 1; biny <= prof1->GetNbinsY(); ++biny) {
80  int bin = prof1->GetBin(binx, biny);
81  // Skip empty bins in either histogram
82  if (prof1->GetBinEntries(bin) == 0 || prof2->GetBinEntries(bin) == 0) {
83  hDiff->SetBinContent(binx, biny, 0); // Mark as 0 (or kNaN)
84  continue;
85  }
86  double diff = prof1->GetBinContent(bin) - prof2->GetBinContent(bin);
87  hDiff->SetBinContent(binx, biny, diff);
88  }
89  }
90 }
float bin
Definition: emthickness.cpp:98

◆ FindPeaksTSpectrum()

std::vector< double > ERTools::FindPeaksTSpectrum ( TH1 *  hist,
int  npeaks = 5 
)
154  {
155  std::vector<double> peaks;
156  TSpectrum spec(npeaks);
157  spec.Search(hist, 2, "goff", 0.1);
158 
159  for (int i=0; i<spec.GetNPeaks(); i++) {
160  peaks.push_back(spec.GetPositionX()[i]);
161  }
162  return peaks;
163 }
void hist()
Definition: init.C:23

◆ FindPeaksWithIntegral()

std::vector< PeakInfo > ERTools::FindPeaksWithIntegral ( TH1 *  hist,
int  window_size,
double  threshold = 0.1,
int  from_bin = 0,
int  to_bin = 0 
)
11 {
12  std::vector<PeakInfo> peaks;
13 
14  if (!hist || window_size < 1) return peaks;
15 
16  const int nBins = hist->GetNbinsX();
17  window_size = std::min(window_size, nBins); // Ensure window isn't larger than histogram
18 
19  threshold *= hist->GetBinContent(hist->GetMaximumBin());
20  //printf("threshold = %f , maximum %f maxbin %f\n", threshold, hist->GetMaximum(), hist->GetBinContent(hist->GetMaximumBin()) );
21 
22  if(from_bin==to_bin&&from_bin==0) {from_bin=1; to_bin=nBins;}
23 
24  for (int i = from_bin; i <= to_bin; ++i) {
25  double centerContent = hist->GetBinContent(i);
26  if (centerContent <= threshold) continue;
27 
28  bool isPeak = true;
29  int left = std::max(1, i - window_size/2);
30  int right = std::min(nBins, i + window_size/2);
31 
32  // Check if current bin is maximum in the window
33  for (int j = left; j <= right; ++j) {
34  if (j == i) continue;
35  if (hist->GetBinContent(j) >= centerContent) {
36  isPeak = false;
37  break;
38  }
39  }
40 
41  if (isPeak) {
42  // Calculate window statistics
43  double sum = 0.0;
44  double sum2 = 0.0;
45  double xsum = 0.0;
46  int count = 0;
47 
48  for (int j = left; j <= right; ++j) {
49  double val = hist->GetBinContent(j);
50  sum += val;
51  sum2 += val * val;
52  double x = hist->GetBinCenter(j);
53  xsum += x*val;
54  count++;
55  }
56 
57  double mean = sum / count;
58  double rms = TMath::Sqrt(sum2/count - mean*mean);
59 
60  double xmean = xsum/sum;
61 
62  peaks.push_back({
63  i, // bin_id
64  xmean, // peak_position
65  centerContent, // peak_height
66  sum, // window_integral
67  mean, // window_mean
68  rms // window_rms
69  });
70  }
71  }
72  return peaks;
73 }
float min(TClonesArray *t)
Definition: bitview.cxx:275
int max
Definition: check_shower.C:41

◆ get_h2_var()

TH2D * ERTools::get_h2_var ( TTree *  tree,
const char *  var1,
const char *  var2,
const char *  hname,
double  bin1,
double  bin2 
)
127 {
128  if(!tree) return 0;
129  tree->Draw(var1, "", "goff");
130  double min1_ = tree->GetMinimum(var1);
131  double max1_ = tree->GetMaximum(var1);
132  tree->Draw(var2, "", "goff");
133  double min2_ = tree->GetMinimum(var2);
134  double max2_ = tree->GetMaximum(var2);
135  int n1 = (max1_-min1_)/bin1;
136  int n2 = (max2_-min2_)/bin2;
137  TH2D *h = new TH2D(hname,Form("%s vs %s",var1,var2),n1,min1_,max1_, n2,min2_,max2_);
138  tree->Draw(Form("%s:%s>>%s",var1,var2,hname), "", "goff");
139  return h;
140 }

◆ get_h_var()

TH1D * ERTools::get_h_var ( TTree *  tree,
const char *  var,
const char *  hname,
double  bin,
const char *  cut 
)
94 {
95  Log(3,"ERTools::get_h_var","%s %s %f %s", var,hname,bin,cut);
96  if(!tree) return 0;
97 
98  // First draw with cut to get selected entries
99  Long64_t nentries = tree->Draw(var, cut, "goff");
100 
101  if (nentries == 0) {
102  Log(3,"ERTools::get_h_var","No entries pass the cut");
103  return 0;
104  }
105 
106  // Get the drawn values array
107  Double_t *v = tree->GetV1();
108 
109  // Calculate min/max from the selected entries
110  double min_ = 1e30;
111  double max_ = -1e30;
112  for (Long64_t i = 0; i < nentries; i++) {
113  if (v[i] < min_) min_ = v[i];
114  if (v[i] > max_) max_ = v[i];
115  }
116 
117  int n = (max_-min_)/bin;
118  Log(3,"ERTools::get_h_var","%d %f %f", n,min_,max_);
119 
120  TH1D *h = new TH1D(hname,var,n,min_,max_);
121  tree->Draw(Form("%s>>%s",var,hname), cut, "goff");
122  return h;
123 }
bool Log(int level, const char *location, const char *fmt,...)
Definition: EdbLog.cxx:75
int nentries
Definition: check_shower.C:40
TCut cut
Definition: check_shower.C:6

◆ GetMaxBinHeight()

double ERTools::GetMaxBinHeight ( const TH1 *  hist)
142  {
143  if (!Internal::ValidateHistogram(hist)) return 0;
144  return hist->GetBinContent(hist->GetMaximumBin());
145 }
bool ValidateHistogram(const TH1 *hist)
Definition: ERTools.cpp:165

◆ RebinHistogram()

TH1D * ERTools::RebinHistogram ( const TH1 *  hist,
int  group 
)
147  {
148  if (!Internal::ValidateHistogram(hist)) return nullptr;
149  TH1D* hnew = dynamic_cast<TH1D*>(hist->Clone());
150  hnew->Rebin(group);
151  return hnew;
152 }