-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPainter2.cpp
More file actions
306 lines (271 loc) · 6.85 KB
/
HPainter2.cpp
File metadata and controls
306 lines (271 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdio.h>
#include <TChain.h>
#include <TCut.h>
#include <TFile.h>
#include <TH1.h>
#include <TH2.h>
#include <TLeaf.h>
#include <TROOT.h>
#include "HPainter2.h"
#define NRANDOM 16
// We need that even zero bins have reasonable errors.
// We assume that one sigma range fits in 0 and use Poisson distribution: sigma=0.3817
void MakeNonZeroErrors(TH1 *h)
{
int i, N;
N = h->GetNbinsX();
for (i=0; i<N; i++) if (!h->GetBinError(i+1)) h->SetBinError(i+1, 0.3817);
}
void MakeNonZeroErrors(TH2 *h)
{
int i, N;
int j, K;
N = h->GetNbinsX();
K = h->GetNbinsY();
for (i=0; i<N; i++) for (j=0; j<K; j++) if (!h->GetBinError(i+1, j+1)) h->SetBinError(i+1, j+1, 0.3817);
}
// type = 0 - neutrino
// type = 1 - muon induced reactions
HPainter2::HPainter2(int mask, int run_from, int run_to, const char *root2dir, int type)
{
const char *prefix;
TChain *info;
fRes = NULL;
ClosefRes = 0;
tSig = tRand = tMuRand = NULL;
upTime = 0;
tBegin = 0;
tEnd = 0;
int *list;
int lsize;
int i, num;
char str[1024];
lsize = run_to - run_from + 1;
if (lsize <= 0) {
printf("Nothing to do!\n");
return;
}
list = (int *) malloc(lsize * sizeof(int));
num = Make_file_list(list, lsize, mask, run_from, run_to);
if (num <= 0) {
printf("No runs found!\n");
return;
}
switch (type) {
case 0:
tSig = new TChain("DanssPair", "SignalPair");
tRand = new TChain("DanssRandom", "RandomPair");
tMuRand = new TChain("DanssMuRandom", "MuRandomPair");
prefix = "pair";
break;
case 1:
tSig = new TChain("MuonPair", "SignalPair");
tRand = new TChain("MuonRandom", "RandomPair");
tMuRand = new TChain("MuonMuRandom", "MuRandomPair");
prefix = "muon";
break;
default:
return;
}
info = new TChain("SumInfo", "Info");
for (i=0; i<num; i++) {
sprintf(str, "%s/%3.3dxxx/%s_%6.6d.root", root2dir, list[i]/1000, prefix, list[i]);
tSig->Add(str);
info->Add(str);
tRand->Add(str);
tMuRand->Add(str);
}
for (i=0; i<num; i++) {
info->GetEntry(i);
upTime += info->GetLeaf("gTime")->GetValue() / 125E6;
}
delete info;
gROOT->cd();
}
HPainter2::~HPainter2(void)
{
if (ClosefRes && fRes) fRes->Close();
delete tSig;
delete tRand;
}
void HPainter2::OpenFile(const char *name)
{
if (fRes) return;
fRes = new TFile(name, "UPDATE");
if (!fRes->IsOpen()) {
fRes = NULL;
} else {
ClosefRes = 1;
}
}
/*
Make list of runs for analysis
Return number of runs found, negative on error
int *list - allocated array to accept the run list
int size - size of allocated array
int mask - mask of possible conditions
int run_from, int run_to - runs range to consider
*/
int HPainter2::Make_file_list(int *list, int size, int mask, int run_from, int run_to)
{
int N;
FILE *f;
char str[1024];
const char *ptr;
int i, num, cond;
ptr = getenv("STAT_ALL");
if (!ptr) ptr = "stat_all.txt";
f = fopen(ptr, "rt");
if (!f) {
printf("Stat file %s not found: %m\n", ptr);
return -1;
}
N = 0;
for(i=0;;i++) {
ptr = fgets(str, sizeof(str), f);
if (!ptr || feof(f)) break;
ptr = strtok(str, " \t");
if (!isdigit(ptr[0])) continue;
num = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Strange record at line %d\n", i);
continue;
}
cond = strtol(ptr, NULL, 10);
if (num < run_from) continue;
if (num > run_to) break;
if (cond <= 0) continue;
if (mask & (1 << (cond - 1))) {
list[N] = num;
N++;
if (N >= size) break;
}
}
fclose(f);
return N;
}
/* Project histogram doing random background subtraction */
void HPainter2::Project(TH1 *hist, const char *what, TCut cut, int iZeroErrCorr)
{
TH1 *hSig; // Signal hist
TH1 *hRand; // Accidental background (e+ - n)
TH1 *hMuRand; // Accidental background (muon - pair)
TH1 *hDiff;
TCut ct;
char str[256];
char csig[1024];
char crand[1024];
char cmurand[1024];
char cdiff[1024];
if (!IsOpen()) return;
snprintf(csig, sizeof(csig), "%s-sig", hist->GetName());
snprintf(crand, sizeof(crand), "%s-rand", hist->GetName());
snprintf(cmurand, sizeof(cmurand), "%s-murand", hist->GetName());
snprintf(cdiff, sizeof(cdiff), "%s-diff", hist->GetName());
hSig = (TH1 *) hist->Clone(csig);
hRand = (TH1 *) hist->Clone(crand);
hMuRand = (TH1 *) hist->Clone(cmurand);
hDiff = (TH1 *) hist->Clone(cdiff);
hSig->SetYTitle("Events");
hRand->SetYTitle("Events");
hMuRand->SetYTitle("Events");
hDiff->SetYTitle("Events");
if (tBegin < tEnd) {
sprintf(str, "unixTime > %d && unixTime < %d", tBegin, tEnd);
ct = cut && str;
tSig->Project(csig, what, ct);
tRand->Project(crand, what, ct);
tMuRand->Project(cmurand, what, ct);
} else {
tSig->Project(csig, what, cut.GetTitle());
tRand->Project(crand, what, cut.GetTitle());
tMuRand->Project(cmurand, what, cut.GetTitle());
}
hSig->Sumw2();
hRand->Sumw2();
hMuRand->Sumw2();
if (iZeroErrCorr) { // No zero error correction for absolute counts
MakeNonZeroErrors(hSig);
MakeNonZeroErrors(hRand);
MakeNonZeroErrors(hMuRand);
}
hRand->Scale(1.0/NRANDOM);
hMuRand->Scale(1.0/NRANDOM);
hDiff->Add(hSig, hRand, 1.0, -1.0);
hist->Reset();
hist->Add(hDiff, 1000.0 / upTime); // mHz
if (fRes) {
fRes->cd();
hSig->Write();
hRand->Write();
hMuRand->Write(); // hMuRand is only calculated and saved. No subtraction here.
hDiff->Write();
}
// delete hSig;
// delete hRand;
// delete hDiff;
}
/* Project histogram doing random background subtraction
void HPainter2::Project(TH2 *hist, const char *what, TCut cut)
{
TH2 *hSig;
TH2 *hRand;
TH2 *hDiff;
TCut ct;
char str[256];
char csig[1024];
char crand[1024];
char cdiff[1024];
if (!IsOpen()) return;
snprintf(csig, sizeof(csig), "%s-sig", hist->GetName());
snprintf(crand, sizeof(crand), "%s-rand", hist->GetName());
snprintf(cdiff, sizeof(cdiff), "%s-diff", hist->GetName());
hSig = (TH2 *) hist->Clone(csig);
hRand = (TH2 *) hist->Clone(crand);
hDiff = (TH2 *) hist->Clone(cdiff);
hSig->SetZTitle("Events");
hRand->SetZTitle("Events");
hDiff->SetZTitle("Events");
if (tBegin < tEnd) {
sprintf(str, "unixTime > %d && unixTime < %d", tBegin, tEnd);
ct = cut && str;
tSig->Project(csig, what, ct);
tRand->Project(crand, what, ct);
} else {
tSig->Project(csig, what, cut.GetTitle());
tRand->Project(crand, what, cut.GetTitle());
}
hSig->Sumw2();
hRand->Sumw2();
MakeNonZeroErrors(hSig);
MakeNonZeroErrors(hRand);
hRand->Scale(1.0/NRANDOM);
hDiff->Add(hSig, hRand, 1.0, -1.0);
hist->Reset();
hist->Add(hDiff, 1000.0 / upTime); // mHz
if (fRes) {
fRes->cd();
hSig->Write();
hRand->Write();
hDiff->Write();
}
delete hSig;
delete hRand;
delete hDiff;
} */
void HPainter2::SetUpTime(unsigned int t0, unsigned int t1)
{
int i, N, S;
TH1S *h;
tBegin = t0;
tEnd = t1;
N = (t1 - t0) / 100;
h = (TH1S*) gROOT->FindObject("_tmp_utime");
if (h) delete h;
h = new TH1S("_tmp_utime", "unix time", N, tBegin, tBegin + 100*N);
tSig->Project("_tmp_utime", "unixTime");
S = 0;
for (i=0; i<N; i++) if (h->GetBinContent(i+1)) S++;
upTime = S*100;
}