-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharge.cpp
More file actions
383 lines (303 loc) · 13.1 KB
/
charge.cpp
File metadata and controls
383 lines (303 loc) · 13.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* Build charge, pedestal, and average waveform histograms
from oscilloscope data in .hdf5 format
* * *
* input params:
* argv[1]: text file listing .hdf5 for analysis
* argv[2]: name of output root file to write histograms to
* argv[3]: length of the pedestal window
* argv[4]: length of the integration window
* argv[5]: scope channel for analysis
* * *
*/
#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include <vector>
// Root Libraries
#include <TROOT.h>
#include <TFile.h>
#include <TMath.h>
#include <TCanvas.h>
#include <TH1.h>
// HDF5 Library
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
using namespace std;
const int RANK_OUT = 2; // Data Rank
struct DataCluster{
DataSet *dataset; // Dataset pointer
DataSpace dataspace; // DataSet's DataSpace
DataSpace memspace; // MemSpace Object for Data Extraction
hsize_t offset[RANK_OUT]; // Data Extraction Parameters...
hsize_t count[RANK_OUT];
hsize_t offset_out[RANK_OUT];
hsize_t count_out[RANK_OUT];
unsigned long trace_length; // Length of a Scope Trace
unsigned long n_traces; // Number of traces in DataSet
char * data_out; // Pointer to Data Buffer
};
typedef struct DataCluster DataCluster;
// Initialize Datacluster
DataCluster * Init_Data(DataSet *dataset);
// For each trace, read in the dataset
int Read_Trace(DataCluster *datacluster, unsigned long trace_index);
// Return the voltage of the sample
double getVoltage(DataCluster *datacluster, double pedestal, double dy, int i);
// Return the integrated charge over the signal window
double getCharge(float pedestal_window, float signal_window, DataCluster *datacluster, double pedestal, double dy, double dx);
// Return the integral of the charge histogram above 5pc
void chargeIntegral(TH1F* charges_signal);
// Beautify charge plot
void prettyPlot(TH1F* h);
void printHelp();
const int termination_ohms = 50;
int main (int argc, char* argv[])
{
if(argc==2 && strcmp(argv[1],"-h")==0){
printHelp();
return 0;
}
else if(argc < 6){
cout << "Invalid command, use -h for help." << endl;
return 0;
}
try{
string channel = argv[5]; // analysis channel
// Attribute Variables
Attribute horiz_interval;
Attribute vertical_gain;
// Variables for time binning and veritcal resolution
double dx,dy;
// Variables for avg wfm histogram
float trace_count = 0.0;
std::vector<float> waveform_voltage;
// ROOT histograms
TH1F *pedestals = new TH1F("Pedestal","",5000,0.0,2);
// This is the binning for scintillator data
TH1F *charges_signal = new TH1F("Charge","",700,-5.0,800);
// For reading in data
string filename;
H5File file;
DataSet dataset;
// Load in the datafiles from a txt file
ifstream ifs (argv[1] , ifstream::in);
ifs >> filename;
while (ifs.good()){
// Open HDF5 File, then HDF5 DataSet and Read in Attributes
file.openFile(filename, H5F_ACC_RDONLY);
ifs >> filename;
dataset = file.openDataSet(channel);
horiz_interval = dataset.openAttribute("horiz_interval");
vertical_gain = dataset.openAttribute("vertical_gain");
horiz_interval.read(PredType::NATIVE_DOUBLE, &dx);
vertical_gain.read(PredType::NATIVE_DOUBLE, &dy);
// Initialize the datacluster
DataCluster * datacluster = Init_Data(&dataset);
unsigned long window_length = datacluster->trace_length;
/* User inputs */
float pedestal_window = atoi(argv[3]); // pedestal window
float signal_window = atoi(argv[4]); // signal window
cout << "-------------------------------------------------------" << endl;
cout << "Analyzing file: " << filename << endl;
// Print the waveform parameters to screen
cout << "Waveform parameters for: " << channel << endl;
cout << " * * * * * * * * * * * * * * * * * * * " << endl;
cout << "Horizontal resolution: " << dx << " ns" << endl;
cout << "Verticle resolution: " << dy << " V" << endl;
cout << "Trace length: " << window_length*dx*1e9 << " ns" << endl;
cout << "Pedestal window: " << "0 - " << pedestal_window << " ns, "
<< "or 0 - " << pedestal_window/(dx*1e9) << " samples." << endl;
cout << "Integration window: " << pedestal_window << " - "
<< (pedestal_window + signal_window) << " ns, "
<< "or " << pedestal_window/(dx*1e9) << " - "
<< (pedestal_window + signal_window)/(dx*1e9) << " samples." << endl;
cout << "-------------------------------------------------------" << endl;
// Loop over every trace
for (unsigned int j = 0; j < datacluster->n_traces; j++){
cout << "Analyzing trace " << j+1 << " out of "
<<datacluster->n_traces<< " total traces " <<'\r';
cout.flush();
// Read in the data
Read_Trace(datacluster,j);
double pedestal = TMath::Mean (pedestal_window, datacluster->data_out)*dy; // baseline (V)
unsigned int window_length = datacluster->trace_length; // trace length
if(j == 0){ // resize once
waveform_voltage.resize(window_length);
}
// integrated charge (pC)
double charge = getCharge(pedestal_window, signal_window, datacluster, pedestal, dy, dx);
// build waveform for debugging and avg wvm array
for(unsigned int i = 0; i < window_length; i++){
waveform_voltage.at(i) += getVoltage(datacluster, pedestal, dy, i);
}
charges_signal->Fill(charge);
pedestals->Fill(pedestal);
trace_count++;
}
file.close(); // close hdf5 file
}
ifs.close(); // close file stream
// Build the average waveform
TH1F* avg_wfm = new TH1F("avg_wfvm","",waveform_voltage.size(),0,waveform_voltage.size()*dx*1e9);
for(unsigned int i = 0; i < waveform_voltage.size(); i++){
avg_wfm->SetBinContent(i, waveform_voltage[i]/trace_count);
}
chargeIntegral(charges_signal);
prettyPlot(charges_signal);
// Output Histograms to File
if (argc > 2){
TFile f(argv[2],"new");
pedestals->Write();
charges_signal->Write();
avg_wfm->Write();
}
// clean-up
delete pedestals;
delete charges_signal;
delete avg_wfm;
} // end of try block
// catch failure caused by the H5File operations
catch(FileIException error){
error.printError();
return -1;
}
// catch failure caused by the DataSet operations
catch(DataSetIException error){
error.printError();
return -1;
}
// catch failure caused by the DataSpace operations
catch(DataSpaceIException error){
error.printError();
return -1;
}
// catch failure caused by the DataSpace operations
catch(DataTypeIException error){
error.printError();
return -1;
}
return 0; // successfully terminated
}
/* Datacluster method */
DataCluster * Init_Data(DataSet *dataset){
DataCluster * datacluster = new DataCluster[1];
/* Get dataspace of the dataset. */
datacluster->dataset = dataset;
datacluster->dataspace = datacluster->dataset->getSpace();
/* Get the dimension size of each dimension
in the dataspace and display them. */
hsize_t dims_out[2];
datacluster->dataspace.getSimpleExtentDims(dims_out, NULL);
datacluster->trace_length = (unsigned long)(dims_out[1]);
datacluster->n_traces = (unsigned long)(dims_out[0]);
// Data Buffer
datacluster->data_out = new char[datacluster->trace_length]; // Data is size char
for (unsigned long i = 0; i < datacluster->trace_length; i++) datacluster->data_out[i]= 0;
/* Define hyperslab in the dataset. */
datacluster->offset[0] = 0;
datacluster->offset[1] = 0;
datacluster->count[0] = 1;
datacluster->count[1] = datacluster->trace_length;
datacluster->dataspace.selectHyperslab( H5S_SELECT_SET, datacluster->count, datacluster->offset );
/* Define the memory dataspace. */
hsize_t dimsm[2]; /* memory space dimensions */
dimsm[0] = dims_out[0];
dimsm[1] = dims_out[1];
datacluster->memspace = DataSpace( RANK_OUT, dimsm );
/* Define memory hyperslab. */
datacluster->offset_out[0] = 0;
datacluster->offset_out[1] = 0;
datacluster->count_out[0] = 1;
datacluster->count_out[1] = datacluster->trace_length;
datacluster->memspace.selectHyperslab( H5S_SELECT_SET, datacluster->count_out, datacluster->offset_out );
return datacluster;
}
/* Method: Read_Trace(DataCluster *datacluster, unsigned long trace_index)
* Updates a DataCluster datacluster so that its buffer contains trace number trace_index */
int Read_Trace(DataCluster *datacluster, unsigned long trace_index){
datacluster->offset[0]= (hsize_t)trace_index;
datacluster->dataspace.selectHyperslab( H5S_SELECT_SET, datacluster->count, datacluster->offset );
datacluster->memspace.selectHyperslab( H5S_SELECT_SET, datacluster->count_out, datacluster->offset_out );
datacluster->dataset->read( datacluster->data_out, PredType::NATIVE_CHAR, datacluster->memspace, datacluster->dataspace );
return 0;
}
// Returns voltage of the sample
double getVoltage(DataCluster *datacluster, double pedestal, double dy, int i){
float voltage = ((float)datacluster->data_out[i]*dy-pedestal);
return voltage;
}
// Returns charge (pC) integrated over the signal window
double getCharge(float pedestal_window, float signal_window, DataCluster *datacluster, double pedestal, double dy, double dx){
double charge = 0.0;
// Charge integration starts at the end of the pedestal window
for(int i = pedestal_window/(dx*1e9); i < (pedestal_window + signal_window)/(dx*1e9); i++){
float voltage = ((float)datacluster->data_out[i]*dy-pedestal);
charge+=(voltage*((-1000.0*dx*1e9)/termination_ohms)); // in pC
}
return charge;
}
// Charge-weighted integral of the charge histogram
// Compare against LAB+PPO standard
void chargeIntegral(TH1F* charges_signal){
bool degassed = false;
// Degassed light yield
const float lab_ppo_ly = 1.99509e+07;
//const float lab_ppo_ly = 1.83045e+07;
// Integration range
TAxis *axis = charges_signal->GetXaxis();
int bmin = axis->FindBin(5.0);
int bmax = axis->FindBin(800.0);
double x=0.0,y=0.0,err=0.0,integral=0.0,terror=0.0;
double endpoint = 0.0;
double bin_content_end = 10.0;
// Charge-weighted integral of charge histogram
for(int i = bmin; i < bmax; i++){
x = axis->GetBinCenter(i);
y = charges_signal->GetBinContent(i);
err = charges_signal->GetBinError(i);
if(y >= bin_content_end){
endpoint = x;
}
integral += x*y;
terror += x*err;
}
// Print the integral of the charge distribtuion above some noise threshold
cout << endl;
cout << "-------------------------------------------------------" << endl;
if(degassed){
cout << "Comparing to degassed scintillator light yield." << endl;
}
else{
cout << "Comparing to gassed scintillator light yield." << endl;
}
cout << "Weighted integral above " << 5.0 << "pC is " << integral << endl;
cout << "End point (last bin with content > " << bin_content_end << ") is " << endpoint << " pC " << endl;
cout << "Relative light yield is: " << integral/lab_ppo_ly << " +/- " << terror/lab_ppo_ly << endl;
cout << "-------------------------------------------------------" << endl;
TCanvas *c = new TCanvas;
charges_signal->Draw();
c->Update();
}
// Beautify charge plot
void prettyPlot(TH1F* h){
TAxis *xaxis = h->GetXaxis();
TAxis *yaxis = h->GetYaxis();
xaxis->SetTitleFont(132);
xaxis->SetLabelFont(132);
xaxis->SetTitle("Charge (pC)");
yaxis->SetTitleFont(132);
yaxis->SetLabelFont(132);
yaxis->SetTitle("Counts");
h->SetLineColor(kBlack);
}
void printHelp(){
cout << "Command lines arguments (all are required):" << endl;
cout << "[1] Text file listing .hdf5 files for analysis." << endl;
cout << "[2] Output root file." << endl;
cout << "[3] Pedestal window length, in samples." << endl;
cout << "[4] Integration winodw length, in samples." << endl;
cout << "[5] Name of channel to analyze." << endl;
}