-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevt2root_NSCL11_mADC.C
353 lines (289 loc) · 9.56 KB
/
evt2root_NSCL11_mADC.C
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
/////////////////////////////////////////////////////////////////////////////////////
// ROOT script: evt2root_NSCL11_mADC.C
// See readme.md for general instructions.
// Adopted & tested for the NSCLDAQ11 version.
//
// Nabin, ddc, DSG, KTM et.al. // December 2015.
//
// Inputs & Comments are Welcome! --Nabin
/////////////////////////////////////////////////////////////////////////////////////
//C and C++ libraries
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include <unistd.h>
//ROOT libraries
#include <TFile.h>
#include <TTree.h>
#include <TROOT.h>
#include <TH1I.h>
#include <TH2I.h>
#include <TObjArray.h>
//Detectors' libraries
#include "2016_detclass.h"
//#include "configfile.h"
//#include "SimpleInPipe.h"
#include "VM_Module.hpp"
//#include "VM_BaseClass.hpp"
using namespace std;
//////////////////////////////////////////////////////////////////////////////////////
void ReadPhysicsBuffer();
const int BufferWords = 13328;
const int BufferBytes = BufferWords*2;
const int unsigned buflen = 26656;
char buffer[buflen];
const string files_list = "evt_files.lst";
// Global variables
TFile* fileR;
TTree* DataTree;
TObjArray* RootObjects;
Int_t run;
// Set VME module names and positions
CAEN_ADC* caen_adc1 = new CAEN_ADC("First ADC", 2);
CAEN_TDC* caen_tdc1 = new CAEN_TDC("First TDC", 5);
float CalParamF[128][3];
float CalParamB[128][3];
int unsigned Nevents;
int unsigned TotEvents=0;
int unsigned words;
unsigned short *point,*epoint;
Int_t EventCounter = 0;
int unsigned EOB_NEvents=0;
int unsigned ASICsCounter=0;
int unsigned CAENCounter=0;
TH2I* ADC_vs_Chan;
TH2I* TDC_vs_Chan;
//- Detectors' classes --------------------------------------------------------
CAENHit ADC;
CAENHit TDC;
Int_t TAC;
Int_t FP1;
Int_t FP2;
Int_t Scint1;
Int_t Scint2;
////////////////////////////////////////////////////////
//- Main function -------------------------------------------------------------
int evt2root_NSCL11_mADC() {
gROOT->Reset();
cout << "==============================================================================" <<endl;
cout << "evt2root: Takes .evt files from a list and converts the data into ROOT format." <<endl;
cout << "==============================================================================" <<endl;
int unsigned type;
UInt_t BufferType = 0;
int unsigned Nbuffers=0;
int BufferPhysics = 0;
int runNum;
ifstream ListEVT;
string OutputROOTFile;
string aux;
char *ROOTFile;
ListEVT.open(files_list.c_str());
if (!ListEVT.is_open()) {
cout << "*** Error: could not open " << files_list << endl;
return 0;
}
else {
cout << "File " << files_list << " opened." <<endl;
ListEVT >> aux >> aux >> aux >> OutputROOTFile;
}
//- ROOT objects' definitions -------------------------------------------------
// ROOT output file
ROOTFile = new char [OutputROOTFile.size()+1];
strcpy (ROOTFile, OutputROOTFile.c_str());
fileR = new TFile(ROOTFile,"RECREATE");
// Data Tree
DataTree = new TTree("DataTree","DataTree");
DataTree->Branch("ADC.Nhits",&ADC.Nhits,"ADCNhits/I");
DataTree->Branch("ADC.ID",ADC.ID,"ID[ADCNhits]/I");
DataTree->Branch("ADC.ChNum",ADC.ChNum,"ChNum[ADCNhits]/I");
DataTree->Branch("ADC.Data",ADC.Data,"Data[ADCNhits]/I");
DataTree->Branch("TDC.Nhits",&TDC.Nhits,"TDCNhits/I");
DataTree->Branch("TDC.ID",TDC.ID,"ID[TDCNhits]/I");
DataTree->Branch("TDC.ChNum",TDC.ChNum,"ChNum[TDCNhits]/I");
DataTree->Branch("TDC.Data",TDC.Data,"Data[TDCNhits]/I");
DataTree->Branch("TAC",&TAC,"TAC/I");
DataTree->Branch("FP1",&FP1,"FP1/I");
DataTree->Branch("FP2",&FP2,"FP2/I");
DataTree->Branch("Scint1",&Scint1,"Scint1/I");
DataTree->Branch("Scint2",&Scint2,"Scint2/I");
// Histograms
Int_t xbins=288;
Int_t ybins=4096;
ADC_vs_Chan = new TH2I("ADC_vs_Chan","",xbins,0,xbins,ybins,0,ybins);
TDC_vs_Chan = new TH2I("TDC_vs_Chan","",xbins,0,xbins,ybins,0,ybins);
//List of root objects.
RootObjects = new TObjArray();
RootObjects->Add(DataTree);
RootObjects->Add(ADC_vs_Chan);
RootObjects->Add(TDC_vs_Chan);
string data_dir = "";
//Check if this file exists.
if (!ListEVT.is_open()) {
cout << "*** Error: could not open " << files_list << endl;
return 0;
}
else {
// cout << "File " << files_list << " opened." <<endl;
ListEVT >> aux >> aux >> data_dir;
}
ifstream evtfile;
bool fileProblem = 0;
bool endOfRun = 0;
cout << "Loop over evt files " <<endl; //debug
int run_number;
int nseg;
ListEVT >> run_number;
run=run_number;
//Loop over files in the data file list.
while(!ListEVT.eof()) {
if (evtfile.is_open()) cout << " * Problem previous file not closed!" << endl;
endOfRun=0;
fileProblem = 0;
nseg=0;
for(int seg_number=0;seg_number<3;seg_number++) {
string name = data_dir + Form("run-%.4d-%.2d.evt",run_number,seg_number);
//open evt file
evtfile.clear();
evtfile.open(name.c_str(),ios::binary);
if(seg_number==0) {//should be true for all files in list
cout << " Data file: " << name << endl;
if (evtfile.bad()) cout << " ** Bad evt file." << endl;
if (evtfile.fail()) cout << " ** Fail evt file" << endl;
if (!evtfile) {
cout << " Could not open evt file" << endl;
//return 1;
}
else {
cout << " Converting data ..." << endl;
nseg++;
}
}
else {//should only be true for multi-segment files; limit output in case of single-segment
if(evtfile.good()) {
cout << " Data file: " << name << endl;
cout << " Converting data ..." << endl;
nseg++;
}
}
////-----------------------------------------------------------------------------
for (;;) {
evtfile.read(buffer,8);
evtfile.read(buffer+8,*(unsigned int*)buffer-8);
//--ddc daq11, the data starts right after the body subheader, which should be zero.
if( *(unsigned int*)(buffer+8) > 0 ) {
cout << " unexpected subheader... " << (unsigned int*)(buffer+8) << endl;
}
if (!evtfile) {
//this could be a bad file or the file is subdivided into parts
break;
}
point = ((unsigned short*)buffer) + 6;
Nbuffers++;
epoint = point;
Nevents = 1;
string TitleStr;
BufferType = *(unsigned int*)(buffer+4);
switch(BufferType) {
case 0x1E : type=1;
break;
case 0x01 : type=11;
break;
case 0x02 : type=12;
break;
default: type=0;
}
switch(type) {
case 11:
runNum = *(epoint+8);
cout << " Run number = "<< runNum << endl;
if(runNum!=run)
cout << " Expected run number = " << run << endl;
break;
case 12:
break;
case 2:
break;
case 1:
BufferPhysics++;
ReadPhysicsBuffer();
break; //end of physics buffer
}//end switch(type)
} //end for(;;) over evtfile
////---------------------------------------------------------------------------------
evtfile.close();
evtfile.clear(); // clear event status in case we had a bad file
}//end of segment loop
if(nseg>1)
printf(" %d segments found\n",nseg);
ListEVT >> run_number;
}
cout << setprecision(3);
cout << "Total buffers = " << Nbuffers << endl;
cout << " Physics buffers = " << BufferPhysics << " (" <<100.0*BufferPhysics/Nbuffers<< "\% of total buffers)"<< endl;
cout << " Number of events based on buffer headers: " << TotEvents << endl;
cout << " Number of events based on event counter: " << EventCounter << endl;
RootObjects->Write();
fileR->Close();
return 1;
}//end of evt2root
////////////////////////////////////////////////////////////////////////////
// Function where the root objects are filled.
////////////////////////////////////////////////////////////////////////////
void ReadPhysicsBuffer() {
Nevents = 1;
TotEvents += Nevents;
for (unsigned int ievent=0;ievent<Nevents;ievent++) {
ADC.ResetCAENHit();
TDC.ResetCAENHit();
//create pointer inside of each event
unsigned short * fpoint = epoint;
words = *fpoint++;
caen_adc1->Reset();
caen_tdc1->Reset();
caen_adc1->Unpack(fpoint); if(fpoint>epoint + words + 1) break;
caen_tdc1->Unpack(fpoint); if(fpoint>epoint + words + 1) break;
epoint += words+1; // This skips the rest of the event
///////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------
for(int i=0;i<32;i++) {
ADC.ID[ADC.Nhits] = 1;
ADC.ChNum[ADC.Nhits] =i;
ADC.Data[ADC.Nhits++] = (Int_t) caen_adc1->fChValue[i];
ADC_vs_Chan->Fill(i,caen_adc1->fChValue[i]);
if(run>50 && run <70) {
if(EventCounter==0 && ievent==0 && i==0)
cout << " Sorting data into TAC (1), Delay (2), Scint (2)" << endl;
switch(i) {
case 0 :
TAC=(Int_t) caen_adc1->fChValue[i];
break;
case 1 :
FP1=(Int_t) caen_adc1->fChValue[i];
break;
case 2 :
FP2=(Int_t) caen_adc1->fChValue[i];
break;
case 3 :
Scint1=(Int_t) caen_adc1->fChValue[i];
break;
case 4 :
Scint2=(Int_t) caen_adc1->fChValue[i];
break;
}
}
}
//---------------------------------------------------
for(int i=0;i<8;i++) {
TDC.ID[TDC.Nhits] = 1;
TDC.ChNum[TDC.Nhits] =i;
TDC.Data[TDC.Nhits++] = (Int_t) caen_tdc1->fChValue[i];
TDC_vs_Chan->Fill(i,caen_tdc1->fChValue[i]);
}
EventCounter++;
DataTree->Fill();
}//end for over events
}//end of void ReadPhysicsBuffer()
/////////////////////////////////////////////////////////////////