-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhittree.cpp
153 lines (138 loc) · 3.59 KB
/
hittree.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <TFile.h>
#include <TLeaf.h>
#include <TTree.h>
//#define MASK ((1L<<42) - 1)
#define MASK ((1L<<45) - 1)
#define MAXTRIG 4000000
/* Open data file either directly or via zcat etc, depending on the extension */
FILE* OpenDataFile(const char *fname)
{
char cmd[1024];
FILE *f;
if (strstr(fname, ".bz2")) {
sprintf(cmd, "bzcat %s", fname);
f = popen(cmd, "r");
} else if (strstr(fname, ".gz")) {
sprintf(cmd, "zcat %s", fname);
f = popen(cmd, "r");
} else if (strstr(fname, ".xz")) {
sprintf(cmd, "xzcat %s", fname);
f = popen(cmd, "r");
} else {
f = fopen(fname, "rb");
}
return f;
}
int main(int argc, char **argv)
{
int i, j, K, N;
long long gtA;
long long gtB;
struct RawStruct {
unsigned short PmtCnt;
unsigned short VetoCnt;
unsigned short SiPmCnt;
};
struct RawStruct Raw;
struct RawArrayStruct {
long long tag;
struct RawStruct data;
};
struct RawArrayStruct *RawArray;
char str[1024];
char str_copy[1024];
char *ptr;
if (argc < 3) {
printf("Add tree with hit information to a file\n");
printf("Usage: %s root_file hit_file\n", argv[0]);
return 10;
}
if (access(argv[1], W_OK | R_OK)) {
printf("File %s either does not exist or can not be written: %m\n", argv[1]);
return 20;
}
if (access(argv[2], R_OK)) {
printf("File %s either does not exist or can not be read: %m\n", argv[2]);
return 30;
}
RawArray = (struct RawArrayStruct *) malloc(MAXTRIG * sizeof(struct RawArrayStruct));
if (!RawArray) {
printf("No memory: %m\n");
return 35;
}
FILE *fIn = OpenDataFile(argv[2]);
if (!fIn) {
printf("Something wrong with file: %s\n", argv[2]);
free(RawArray);
return 40;
}
K = 0;
for (;;) {
ptr = fgets(str, sizeof(str), fIn);
if (!ptr) break; // EOF
strcpy(str_copy, str);
ptr = strtok(str, " \t");
if (!ptr) continue;
if (!isdigit(ptr[0])) continue;
RawArray[K].tag = MASK & strtoll(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string at %d: %s [file %s]\n", i, str_copy, argv[2]);
continue;
}
RawArray[K].data.PmtCnt = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string at %d: %s [file %s]\n", i, str_copy, argv[2]);
continue;
}
RawArray[K].data.VetoCnt = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string at %d: %s [file %s]\n", i, str_copy, argv[2]);
continue;
}
RawArray[K].data.SiPmCnt = strtol(ptr, NULL, 10);
K++;
}
pclose(fIn);
TFile *f = new TFile(argv[1], "UPDATE");
TTree *evt = (TTree *) f->Get("DanssEvent");
if (!evt) {
printf("Something wrong with file: %s\n", argv[1]);
return 50;
}
N = evt->GetEntries();
TTree *t = new TTree("RawHits", "RawHits");
t->Branch("RawHits", &Raw, "PmtCnt/s:VetoCnt/s:SiPmCnt/s");
j = 0;
for(i=0; i<N; i++) {
evt->GetEntry(i);
gtA = MASK & (evt->GetLeaf("globalTime"))->GetValueLong64();
memset(&Raw, 0, sizeof(Raw));
for (;j<K;j++) {
gtB = RawArray[j].tag;
if (gtB == gtA) break;
}
if (gtB != gtA) { // try to start from the beginning
for (j=0;j<K;j++) {
gtB = RawArray[j].tag;
if (gtB == gtA) break;
}
}
if (gtB == gtA) { // trigger found
memcpy(&Raw, &RawArray[j].data, sizeof(struct RawStruct));
} else { // trigger is somehow missing
printf("Trigger %d at %Ld not found [file %s]\n", i, gtA, argv[2]);
}
t->Fill();
}
t->Write();
f->Close();
free(RawArray);
return 0;
}