/[PAMELA software]/PamCut/Collections/EffRigCollection/EffRigCollection.cpp
ViewVC logotype

Contents of /PamCut/Collections/EffRigCollection/EffRigCollection.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Tue Oct 27 10:18:54 2009 UTC (15 years, 1 month ago) by pam-fi
Branch: MAIN
Changes since 1.4: +26 -4 lines
New ownership feature added.

1 /*
2 * EffRigCollection.cpp
3 *
4 * Created on: 10/ago/2009
5 * Author: Nicola Mori
6 */
7
8 /*! @file EffRigCollection.cpp The EffRigCollection class implementation file. */
9
10 #include "EffRigCollection.h"
11
12 #include "TGraphAsymmErrors.h"
13
14 extern "C" {
15 /*! @brief Fortran routine by Sergio Ricciarini which computes errors on the efficiency.
16 *
17 * This routine has been developed to carefully evaluate the error bars on the efficiency,
18 * which must have a value between 0 and 1. See the appendix of Sergio's PhD thesis for more
19 * details.
20 * The important thing is that the computed efficiency will have the unphysical value 1.1
21 * if there are less than 8 events in the efficiency sample (eg., the sample used to compute the
22 * efficiency). This is necessary to have a reliable estimate of the errors.
23 *
24 * @param sel Pointer to an Int_t variable containing the number of events in the efficiency sample.
25 * @param det Pointer to an Int_t variable containing the number of events in the efficiency sample
26 * which satisfied the set of cuts for which the efficiency is being measured.
27 * @param eff Pointer to a Double_t variable where the computed efficiency will be returned.
28 * @param errLow Pointer to a Double_t variable where the length of the lower error bar will
29 * be returned.
30 * @param errHigh Pointer to a Double_t variable where the length of the upper error bar will
31 * be returned.
32 * @return Not clear at this point... ignore it.
33 */
34 bool efficiency_(Int_t* sel, Int_t* det, Double_t* eff, Double_t* errLow, Double_t* errHigh);
35 }
36
37 EffRigCollection::EffRigCollection(const char *collectionName, TString outFileBase, TString rigBinsFile, int errMethod, bool owns) :
38 EffCollection(collectionName, outFileBase, errMethod, owns), _bins(0), _selVector(0), _detVector(0), _outUp(0), _outDown(0) {
39
40 ifstream rigBinListFile;
41 rigBinListFile.open(rigBinsFile);
42
43 TString auxString;
44 while (!rigBinListFile.eof()) {
45 rigBinListFile >> auxString;
46 if (auxString != "") {
47 _bins.push_back(auxString.Atof());
48 }
49 }
50 rigBinListFile.close();
51
52 _selVector.resize(_bins.size() - 1, 0);
53 _detVector.resize(_bins.size() - 1, 0);
54 }
55
56 int EffRigCollection::ApplyCut(PamLevel2 *event) {
57
58 _nEv++;
59 if (_selCollection.ApplyCut(event) == CUTOK) {
60 // Check if the event is inside the rigidity range
61 // NOTE: at this point a TrkPhSinCut should be already performed,
62 // since we are going to retrieve rigidity.
63
64 float rig = event->GetTrack(0)->GetTrkTrack()->GetRigidity();
65
66 if (rig >= _bins[0]) {
67 int i = 1;
68 while (rig >= _bins[i] && i < (int) _bins.size()) {
69 i++;
70 }
71 i--;
72
73 if (i < (int) (_selVector.size())) {
74 _selVector[i]++;
75 _sel++;
76 if (_detCollection.ApplyCut(event) == CUTOK) {
77 _detVector[i]++;
78 _det++;
79 _nGood++;
80 return CUTOK;
81 }
82 }
83 else
84 _outUp++;
85
86 }
87 else {
88 _outDown++;
89 return 0;
90 }
91 }
92
93 return 0;
94 }
95
96 void EffRigCollection::Finalize() {
97
98 // Print the report
99 EffCollection::Finalize();
100 cout << " Events below the minimum rigidity: " << _outDown << "\n";
101 cout << " Events above the maximum rigidity: " << _outUp << "\n";
102
103 // Compute the error
104 Int_t sel[_selVector.size()];
105 Int_t det[_detVector.size()];
106 Double_t eff[_selVector.size()];
107 Double_t errLow[_selVector.size()];
108 Double_t errHigh[_selVector.size()];
109 for (unsigned int i = 0; i < _selVector.size(); i++) {
110 sel[i] = (Int_t) _selVector[i];
111 det[i] = (Int_t) _detVector[i];
112 }
113
114 TGraphAsymmErrors errGraph;
115 errGraph.SetName(GetName());
116 errGraph.SetMarkerColor(kRed);
117 errGraph.SetMarkerStyle(7);
118 errGraph.GetYaxis()->SetRangeUser(0, 1.2);
119
120
121 if (_errMethod == EFFERR_ROOT) {
122 double binning[_bins.size()];
123 for (unsigned int i = 0; i < _bins.size(); i++)
124 binning[i] = _bins[i];
125
126 TH1I pass("pass", "pass", _bins.size() - 1, binning);
127 TH1I total("total", "total", _bins.size() - 1, binning);
128 for (unsigned int i = 0; i < _selVector.size(); i++) {
129 for (int j = 0; j < det[i]; j++)
130 pass.Fill((binning[i + 1] + binning[i]) / 2.);
131 for (int j = 0; j < sel[i]; j++)
132 total.Fill((binning[i + 1] + binning[i]) / 2.);
133 }
134
135 errGraph.BayesDivide(&pass, &total);
136 Double_t graphX;
137 double currBin;
138 int currPoint = 0;
139
140 for (unsigned int i = 0; i < _selVector.size(); i++) {
141 errGraph.GetPoint(currPoint, graphX, eff[i]);
142 currBin = (binning[i + 1] + binning[i]) / 2.;
143 if (currBin == graphX) {
144 if (_selVector[i] < 8) {
145 eff[i] = 1.1;
146 errLow[i] = errHigh[i] = 0.;
147 errGraph.SetPoint(currPoint, graphX, 1.1);
148 float halfBin = (binning[i + 1] - binning[i]) / 2.;
149 errGraph.SetPointError(currPoint, halfBin, halfBin, 0., 0.);
150 }
151 currPoint++;
152 }
153 }
154
155 }
156 if (_errMethod == EFFERR_SERGIO) {
157 for (unsigned int i = 0; i < _selVector.size(); i++) {
158 efficiency_(&(sel[i]), &(det[i]), &(eff[i]), &(errLow[i]), &(errHigh[i]));
159
160 float R = (_bins[i] + _bins[i + 1]) / 2.;
161 float halfBin = (_bins[i + 1] - _bins[i]) / 2.;
162 errGraph.SetPoint(i, R, eff[i]);
163 errGraph.SetPointError(i, halfBin, halfBin, errLow[i], errHigh[i]);
164
165 }
166
167 }
168
169 // Write the output files
170 if (_outFileBase != "") {
171 ofstream outTextFile((_outFileBase + "-" + GetName() + "-rig.txt").Data(), ios_base::out);
172 streamsize newPrec = 4;
173 outTextFile.precision(newPrec);
174 outTextFile.setf(ios::fixed, ios::floatfield);
175 for (unsigned int i = 0; i < _selVector.size(); i++) {
176 outTextFile << setw(10) << _bins[i] << setw(10) << _bins[i + 1] << setw(10) << _detVector[i] << setw(10)
177 << _selVector[i];
178 if (_selVector[i] != 0)
179 outTextFile << setw(10) << eff[i] << setw(10) << errLow[i] << setw(10) << errHigh[i] << "\n";
180 else
181 outTextFile << setw(10) << 0. << setw(10) << 0. << setw(10) << 0.;
182 if (i < _selVector.size() - 1) //Avoids to print an empty line at the end of the file
183 outTextFile << endl;
184
185 }
186
187 outTextFile.close();
188
189 TFile outRootFile((_outFileBase + "-" + GetName() + "-rig.root"), "RECREATE");
190 outRootFile.cd();
191 errGraph.Write();
192 outRootFile.Close();
193
194 }
195
196 }

  ViewVC Help
Powered by ViewVC 1.1.23