pxar
 All Classes Namespaces Functions Variables Typedefs Friends
spectrum.C
1 void spectrum() {
2 
3  // TFile *f = TFile::Open("/scratch/ursl/pxar/rongshyang-pxar_bb_Oct01.root");
4  TFile *f = TFile::Open("M0001/pxar.root");
5 
6  f->cd("BumpBonding");
7 
8  TH1D *h;
9  TSpectrum s;
10  int nPeaks(0);
11  zone(4,4);
12  double cutDead;
13  int bbprob(0);
14  for (int i = 0; i < 16; ++i) {
15  bbprob = 0;
16  c0.cd(i+1);
17  h = (TH1D*)gDirectory->Get(Form("dist_thr_calSMap_VthrComp_C%d_V0", i));
18  nPeaks = s.Search(h, 5, "", 0.01);
19  cout << "found " << nPeaks << " peaks in " << h->GetName() << endl;
20  cutDead = fitPeaks(h, s, nPeaks);
21  bbprob = static_cast<int>(h->Integral(cutDead, h->FindBin(255)));
22  cout << "dead bumps: " << bbprob << endl;
23  h->Draw();
24  pl->DrawLine(cutDead, 0, cutDead, h->GetMaximum());
25  }
26 
27 
28 
29 }
30 
31 
32 // ----------------------------------------------------------------------
33 int fitPeaks(TH1D *h, TSpectrum &s, int npeaks) {
34 
35  Float_t *xpeaks = s.GetPositionX();
36  string name;
37  double lcuts[3]; lcuts[0] = lcuts[1] = lcuts[2] = 255.;
38  TF1 *f(0);
39  double peak, sigma, rms;
40  int fittedPeaks(0);
41  for (int p = 0; p < npeaks; ++p) {
42  double xp = xpeaks[p];
43  if (p > 1) continue;
44  if (xp > 200) {
45  cout << "do not fit peak at " << xp << endl;
46  continue;
47  }
48  name = Form("gauss_%d", p);
49  f = new TF1(name.c_str(), "gaus(0)", 0., 256.);
50  int bin = h->GetXaxis()->FindBin(xp);
51  double yp = h->GetBinContent(bin);
52  f->SetParameters(yp, xp, 2.);
53  h->Fit(f, "Q+");
54  ++fittedPeaks;
55  peak = h->GetFunction(name.c_str())->GetParameter(1);
56  sigma = h->GetFunction(name.c_str())->GetParameter(2);
57  if (0 == p) {
58  lcuts[0] = peak + 3*sigma;
59  if (h->Integral(h->FindBin(peak + 10.*sigma), 250) > 10.) {
60  lcuts[1] = peak + 5*sigma;
61  } else {
62  lcuts[1] = peak + 10*sigma;
63  }
64  } else {
65  lcuts[1] = peak - 3*sigma;
66  lcuts[2] = peak - sigma;
67  }
68  delete f;
69  }
70 
71  int startbin = (int)(0.5*(lcuts[0] + lcuts[1]));
72  int endbin = (int)(lcuts[1]);
73  if (endbin <= startbin) {
74  endbin = (int)(lcuts[2]);
75  if (endbin < startbin) {
76  endbin = 255.;
77  }
78  }
79 
80  int minbin(0);
81  double minval(999.);
82 
83  for (int i = startbin; i <= endbin; ++i) {
84  if (h->GetBinContent(i) < minval) {
85  if (1 == fittedPeaks) {
86  if (0 == h->Integral(i, i+4)) {
87  minval = h->GetBinContent(i);
88  minbin = i;
89  } else {
90  minbin = endbin;
91  }
92  } else {
93  minval = h->GetBinContent(i);
94  minbin = i;
95  }
96  }
97  }
98 
99  cout << "cut for dead bump bonds: " << minbin << " (obtained for minval = " << minval << ")"
100  << " start: " << startbin << " .. " << endbin
101  << " last peak: " << peak << " sigma: " << sigma
102  << " lcuts[0] = " << lcuts[0] << " lcuts[1] = " << lcuts[1]
103  << endl;
104  return minbin+1;
105 }