Belle II Software  release-05-01-25
eclcovmat_cut.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2011 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Program for calculation some parameters that needs for *
7  * covariance matrices calculation *
8  * *
9  * Contributors: Alexander Bobrov (a.v.bobrov@inp.nsk.su) , *
10  * Guglielmo De Nardo *
11  * *
12  * This software is provided "as is" without any warranty. *
13  **************************************************************************/
14 
15 #include <TChain.h>
16 #include <TH2.h>
17 #include <TH1.h>
18 #include <TF1.h>
19 #include <TMath.h>
20 #include <TCanvas.h>
21 #include <iostream>
22 #include <fstream>
23 #include <cassert>
24 #include <ecl/digitization/WrapArray2D.h>
25 
26 
27 using namespace std;
28 using namespace Belle2;
29 using namespace ECL;
30 
31 Double_t glog(Double_t* x, Double_t* par)
32 {
33 // par(1) - normalization constant
34 // par(2) - maximum of lg-function
35 // par(3) - sigma=fwhm/2.355 {=2*sqrt(2*alog(2))}
36 // par(4) - assymetry parameter
37 
38 
39  Double_t cc = TMath::Sqrt(2.*TMath::Log(2.));
40  Double_t sig = TMath::Log(cc * par[3] + TMath::Sqrt(1. + cc * cc * par[3] * par[3])) / cc;
41  Double_t N = par[0] * par[3] / par[2] / TMath::Sqrt(2.*3.1415) / TMath::Exp(sig * sig / 2) / sig;
42  Double_t arg = TMath::Log(1. + (x[0] - par[1]) * par[3] / par[2]) / sig;
43 
44  Double_t fitval = 0;
45  if (x[0] > par[1] - par[2] / par[3]) {
46  fitval = N * TMath::Exp(-arg * arg / 2);
47  }
48  return fitval;
49 }
50 
51 void cut(const char* inputRootFilename, const char* outputCutFilename,
52  const char* outputpdfdir, int crystalMin, int crystalMax,
53  int ampMin, int ampMax)
54 {
55 
56  int nbins = ampMax - ampMin;
57  TH2F* Box = new TH2F("Box", "box", 8736, 0., 8736., nbins, float(ampMin), float(ampMax));
58  TH1F* Ms = new TH1F("Ms", "ev", nbins, float(ampMin), float(ampMax));
59  TChain fChain("m_tree");
60  Int_t poq;
61  cout << "!!! file for calibration:" << inputRootFilename << endl;
62  fChain.Add(inputRootFilename);
63 
64 
65  string outputpdffile(outputpdfdir);
66  bool doplot = !(outputpdffile == "noplot");
67  outputpdffile += "/amplitudefits.pdf";
68 
69  Int_t nhits;
70  WrapArray2D<Int_t> hitA(8736, 31);
71 
72  TBranch* b_nhits;
73  TBranch* b_hitA;
74 
75  fChain.SetBranchAddress("nhits", &nhits, &b_nhits);
76  fChain.SetBranchAddress("hitA", hitA, &b_hitA);
77 
78  Int_t k;
79  Int_t u;
80 
81  Double_t InT;
82  Double_t mug;
83  Double_t rg;
84 
85  vector<Double_t> Mu(8736, 0);
86  vector<Double_t> Sg(8736, 0);
87  vector<Double_t> As(8736, 0);
88  vector<Double_t> Sr(8736, 0);
89  vector<Double_t> Sl(8736, 0);
90  vector<Double_t> Chi(8736, 0)
91  ;
92  Double_t cc = TMath::Sqrt(2.*TMath::Log(2.));
93 
94  Int_t nevent = fChain.GetEntries();
95  std::cout << "! nevent=" << nevent << std::endl;
96  //fill 2D historgamm
97  // for (Int_t i=0;i<250;i++) {
98 
99  for (Int_t i = 0; i < nevent; i++) {
100  fChain.GetEntry(i);
101  for (k = 0; k < nhits; k++) {
102  for (u = 0; u < 31; u++) {
103  Box->Fill(k, hitA[k][u]);
104  }
105  }
106  if (i % 100 == 0) {std::cout << " event=" << i << std::endl;}
107  } //event cicle
108 
109  // open output pdf file
110 
111  TCanvas* plot = nullptr;
112  if (doplot) {
113  plot = new TCanvas;
114  plot->SaveAs((outputpdffile + string("[")).c_str());
115  }
116  //fill 1D histigramm and fit
117  for (k = crystalMin ; k <= crystalMax; k++) {
118  cout << "Crystal " << k << endl;
119 
120  // for (k = 0; k < 86; k++) {
121  InT = 0.;
122  for (u = 1; u < 2901; u++) {
123  Double_t Ty = Box->GetBinContent(k + 1, u);
124  if (u < 3001) {
125  Ms->SetBinContent(u, Ty);
126  InT = InT + Ty;
127  //cout << Ty << endl;
128  }
129  }
130  rg = Ms->GetRMS();
131  // both choices do not converge
132  mug = Ms->GetMean();
133  // mug = Ms->GetXaxis()->GetBinCenter(Ms->GetMaximumBin());
134  TF1* func = new TF1("glog", glog, 2990., 3500., 4);
135  func->SetParNames("Nornamisation", "Maximum", "Sigma", "Assimetry");
136  func->SetParameters(InT, mug, rg, 0.1);
137  Ms->Fit(func, "l");
138  Ms->Draw();
139 
140  if (doplot) plot->SaveAs(outputpdffile.c_str());
141  Double_t par[4];
142  func->GetParameters(par);
143  Mu[k] = par[1];
144  Sg[k] = par[2];
145  As[k] = par[3];
146  Double_t chi2 = func->GetChisquare();
147  Double_t ndf = func->GetNDF();
148 
149  if (ndf > 0) {
150  Chi[k] = chi2 / ndf;
151  } else {Chi[k] = -1.;}
152 
153  cout << "Chi[ " << k << "] = " << Chi[k] << "calculated but not used." << endl;
154 
155  Sr[k] = (-1. + As[k] * cc + TMath::Sqrt(1. + cc * cc * As[k] * As[k])) * Sg[k] / As[k] / cc;
156  Sl[k] = (1. - 1. / (As[k] * cc + TMath::Sqrt(1. + cc * cc * As[k] * As[k]))) * Sg[k] / As[k] / cc;
157  } // channels cicle
158 
159  // close output pdf file
160  if (doplot) plot->SaveAs((outputpdffile + string("]")).c_str());
161  // write data
162 
163  ofstream outputCutFile(outputCutFilename);
164  for (poq = 0; poq < 8736; poq++) {
165  outputCutFile << poq << " " << Mu[poq] << " "
166  << Sg[poq] << " " << Sl[poq] << " "
167  << Sr[poq] << " " << As[poq] << endl;
168  // fprintf(McoIN, "%d %f %f %f %f %f \n ", poq, Mu[poq], Sg[poq], Sl[poq], Sr[poq], As[poq]);
169  }
170  outputCutFile.close();
171 }
172 
173 int main(int argc, char** argv)
174 
175 {
176  assert(argc > 3 && argc < 7);
177  // first argument is input root file
178  // second argument is output cut text file
179  // third argument output plot file or "noplot"
180  // 4th argument crystal min = 0
181  // 5th argument crystal max = 8735
182  int crystalMin = 0;
183  int crystalMax = 8735;
184  int ampMin = 2000;
185  int ampMax = 4900;
186  if (argc >= 5) crystalMin = stoi(argv[4]);
187  if (argc >= 6) crystalMax = stoi(argv[5]);
188  if (argc >= 7) ampMin = stoi(argv[6]);
189  if (argc >= 8) ampMax = stoi(argv[7]);
190  cut(argv[1], argv[2], argv[3], crystalMin, crystalMax, ampMin, ampMax);
191  //cut("/gpfs/home/belle/avbobrov/belle2/j15/ecl/examples/rootfile.txt");
192  // sprintf(Min, "/home/belle/avbobrov/binp2/chdatuXX.txt");
193 }
Belle2::ECL::WrapArray2D
class to replace POD 2D array to save stack usage since it just allocates memory dynamically.
Definition: WrapArray2D.h:33
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
prepareAsicCrosstalkSimDB.u
u
merged u1 and u2
Definition: prepareAsicCrosstalkSimDB.py:46