-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeHkTree.cxx
218 lines (185 loc) · 5.45 KB
/
makeHkTree.cxx
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
#include <cstdio>
#include <fstream>
#include <iostream>
#include <zlib.h>
#include <libgen.h>
#include "TTree.h"
#include "TChain.h"
#include "TFile.h"
#include "TSystem.h"
#include "TTreeIndex.h"
#define HACK_FOR_ROOT
#include "simpleStructs.h"
#include "RawHk.h"
#include "CalibratedHk.h"
void processHk();
void processHkCal();
void calcSlopesAndIntercepts();
void calibrateHk();
void makeHkTree(char *calInName, char *rawInName, char *outName);
//Global Variable
CalibratedHk *goodHkPtr=0;
RawHk *rawHkPtr=0;
RawHk *calHkPtr=0;
RawHk *avzHkPtr=0;
HkDataStruct_t theHk;
TFile *theFile;
TTree *hkTree=0;
TTree *hkRawTree=0;
TTree *hkCalTree=0;
TTree *hkAvzTree=0;
char rootFileName[FILENAME_MAX];
int doneInit=0;
int doneInitCal=0;
int runNumber;
UInt_t realTime;
int main(int argc, char **argv) {
if(argc<4) {
std::cout << "Usage: " << basename(argv[0]) << " <cal file list> <raw file list> <outfile>" << std::endl;
return -1;
}
makeHkTree(argv[1],argv[2],argv[3]);
return 0;
}
void makeHkTree(char *calInName, char *rawInName, char *outName) {
strncpy(rootFileName,outName,FILENAME_MAX);
{
std::ifstream PosFile(calInName);
int numBytes=0;
char fileName[180];
int error=0;
int counter=0;
while(PosFile >> fileName) {
const char *subDir = gSystem->DirName(fileName);
const char *subSubDir = gSystem->DirName(subDir);
const char *rawDir= gSystem->DirName(subSubDir);
const char *hkDir = gSystem->DirName(rawDir);
const char *houseDir = gSystem->DirName(hkDir);
const char *runDir = gSystem->DirName(houseDir);
const char *justRun = gSystem->BaseName(runDir);
// std::cout << justRun << std::endl;
sscanf(justRun,"run%d",&runNumber);
if(counter%100==0)
std::cout << fileName << std::endl;
counter++;
gzFile infile = gzopen (fileName, "rb");
for(int i=0;i<1000;i++) {
numBytes=gzread(infile,&theHk,sizeof(HkDataStruct_t));
if(numBytes!=sizeof(HkDataStruct_t)) {
error=1;
break;
}
processHkCal();
}
gzclose(infile);
// if(error) break;
}
if(hkCalTree)
hkCalTree->AutoSave();
if(hkAvzTree)
hkAvzTree->AutoSave();
}
{
std::ifstream PosFile(rawInName);
int numBytes=0;
char fileName[180];
int error=0;
int counter=0;
while(PosFile >> fileName) {
const char *subDir = gSystem->DirName(fileName);
const char *subSubDir = gSystem->DirName(subDir);
const char *rawDir= gSystem->DirName(subSubDir);
const char *hkDir = gSystem->DirName(rawDir);
const char *houseDir = gSystem->DirName(hkDir);
const char *runDir = gSystem->DirName(houseDir);
const char *justRun = gSystem->BaseName(runDir);
// std::cout << justRun << std::endl;
sscanf(justRun,"run%d",&runNumber);
if(counter%100==0)
std::cout << fileName << std::endl;
counter++;
gzFile infile = gzopen (fileName, "rb");
for(int i=0;i<1000;i++) {
numBytes=gzread(infile,&theHk,sizeof(HkDataStruct_t));
if(numBytes!=sizeof(HkDataStruct_t)) {
error=1;
break;
}
processHk();
}
gzclose(infile);
// if(error) break;
}
if(hkTree)
hkTree->AutoSave();
if(hkRawTree)
hkRawTree->AutoSave();
// theFile->Close();
}
doneInit=0;
doneInitCal=0;
}
void processHk() {
static Long64_t currentCalEntry=-1;
static Long64_t currentAvzEntry=-1;
if(!doneInit) {
if(!theFile)
theFile = new TFile(rootFileName,"UPDATE");
hkTree = new TTree("hkTree","Tree of Anita Housekeepings");
hkTree->Branch("hk","CalibratedHk",&goodHkPtr);
hkRawTree= new TTree("hkRawTree","Raw housekeeping");
hkRawTree->Branch("hk","RawHk",&rawHkPtr);
if(hkCalTree)
hkCalTree->BuildIndex("payloadTime","payloadTimeUs");
if(hkAvzTree)
hkAvzTree->BuildIndex("payloadTime","payloadTimeUs");
doneInit=1;
}
if(hkCalTree) {
Long64_t bestCalEntry=
hkCalTree->GetEntryNumberWithBestIndex(theHk.unixTime,theHk.unixTimeUs);
if(bestCalEntry<0) bestCalEntry=0;
if(bestCalEntry!=currentCalEntry) {
currentCalEntry=bestCalEntry;
hkCalTree->GetEntry(currentCalEntry);
}
}
if(hkAvzTree) {
Long64_t bestAvzEntry=
hkAvzTree->GetEntryNumberWithBestIndex(theHk.unixTime,theHk.unixTimeUs);
if(bestAvzEntry<0) bestAvzEntry=0;
if(bestAvzEntry!=currentAvzEntry) {
currentAvzEntry=bestAvzEntry;
hkAvzTree->GetEntry(currentAvzEntry);
}
}
if(rawHkPtr) delete rawHkPtr;
rawHkPtr = new RawHk(runNumber,theHk.unixTime,&theHk);
if(goodHkPtr) delete goodHkPtr;
if(hkCalTree && hkAvzTree)
goodHkPtr = new CalibratedHk(rawHkPtr,avzHkPtr,calHkPtr);
else
goodHkPtr = new CalibratedHk(rawHkPtr,0,0);
hkTree->Fill();
hkRawTree->Fill();
}
void processHkCal() {
if(!doneInitCal) {
theFile = new TFile(rootFileName,"RECREATE");
hkAvzTree = new TTree("hkAvzTree","Tree of Anita Housekeeping Calibration");
hkAvzTree->Branch("hk","RawHk",&avzHkPtr);
hkCalTree = new TTree("hkCalTree","Tree of Anita Housekeeping Calibration");
hkCalTree->Branch("hk","RawHk",&calHkPtr);
doneInitCal=1;
}
if(theHk.ip320.code==IP320_CAL) {
if(calHkPtr) delete calHkPtr;
calHkPtr = new RawHk(runNumber,theHk.unixTime,&theHk);
hkCalTree->Fill();
}
else if(theHk.ip320.code==IP320_AVZ) {
if(avzHkPtr) delete avzHkPtr;
avzHkPtr = new RawHk(runNumber,theHk.unixTime,&theHk);
hkAvzTree->Fill();
}
}