-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertUSBWCRates2root8ChannelsBin.cc
258 lines (239 loc) · 9.1 KB
/
convertUSBWCRates2root8ChannelsBin.cc
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
///////////////////////////////////////////////////////////////////////
// Date: Tue Nov 3 17:45:25 CET 2015 //
// Autor: Leonid Burmistrov //
// Program description: Convertion of the binary data into the root //
// format. Initioal binary data file produced //
// by the single board 8 channel //
// USB-WaveCatcher (software version 2.4.11) //
// and 2.4.18 //
///////////////////////////////////////////////////////////////////////
//root
#include <TH1D.h>
#include <TStyle.h>
#include <TString.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TTree.h>
#include <TMath.h>
//C, C++
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const Int_t nCh = 8;
const Int_t nMaxDataFiles = 100000;
//const Int_t nPointMax = 100000;
//const Int_t nSegmentsMax = 100000;//per file
void convertUSBWCRates2root8ChannelsBin(TString dataFileList,
TString datPreff,
TString rootFileN);
int main(int argc, char *argv[] ){
if(argc == 4){
TString dataFileList = argv[1];
TString datPreff = argv[2];
TString rootFileN = argv[3];
cout<<endl
<<" File containing the list of input data files: "<<dataFileList<<endl
<<" Path of the directory containing these input files: "<<datPreff<<endl
<<" Name of the output ROOT file: "<<rootFileN<<endl<<endl;
convertUSBWCRates2root8ChannelsBin(dataFileList,
datPreff,
rootFileN);
}
else{
cout<<endl
<<" ERROR ---> in input arguments "<<endl
<<" [1] : name of a text file containing the list of binary files with rates to be converted to ROOT"<<endl
<<" [2] : path of the directory containing the input USBWC files"<<endl
<<" [3] : name of the output ROOT file"<<endl;
}
return 0;
}
void convertUSBWCRates2root8ChannelsBin(TString dataFileList,
TString datPreff,
TString rootFileN){
FILE *pFile;
long totFileSizeByte;
long currentPositionByte;
char *buffer;
TString buffTmp;
int EventNumber;
double EpochTime;
unsigned int Year;
unsigned int Month;
unsigned int Day;
unsigned int Hour;
unsigned int Minute;
unsigned int Second;
unsigned int Millisecond;
int channel;
int EventIDsamIndex;
float RawTriggerRate[nCh];
float floatR;
Int_t EventNumber_usbwc;
Double_t EpochTime_usbwc;
Int_t Year_usbwc;
Int_t Month_usbwc;
Int_t Day_usbwc;
Int_t Hour_usbwc;
Int_t Minute_usbwc;
Int_t Second_usbwc;
Int_t Millisecond_usbwc;
Float_t RawTriggerRate_usbwc[nCh];
Int_t nCh_usbwc = nCh;
TFile *hfile = new TFile( rootFileN, "RECREATE", "Data", 1);
if (hfile->IsZombie()) {
cout << "PROBLEM with the initialization of the output ROOT ntuple "
<< rootFileN << ": check that the path is correct!!!"
<< endl;
exit(-1);
}
TTree *tree = new TTree("T", "Data Tree");
hfile->SetCompressionLevel(2);
tree->SetAutoSave(1000000);
// Create new event
TTree::SetBranchStyle(0);
//Event
tree->Branch("EventNumber_usbwc",&EventNumber_usbwc, "EventNumber_usbwc/I");
tree->Branch("EpochTime_usbwc",&EpochTime_usbwc, "EpochTime_usbwc/D");
tree->Branch("Year_usbwc",&Year_usbwc, "Year_usbwc/I");
tree->Branch("Month_usbwc",&Month_usbwc, "Month_usbwc/I");
tree->Branch("Day_usbwc",&Day_usbwc, "Day_usbwc/I");
tree->Branch("Hour_usbwc",&Hour_usbwc, "Hour_usbwc/I");
tree->Branch("Minute_usbwc",&Minute_usbwc, "Minute_usbwc/I");
tree->Branch("Second_usbwc",&Second_usbwc, "Second_usbwc/I");
tree->Branch("Millisecond_usbwc",&Millisecond_usbwc, "Millisecond_usbwc/I");
tree->Branch("nCh_usbwc",&nCh_usbwc, "nCh_usbwc/I");
tree->Branch("RawTriggerRate_usbwc", RawTriggerRate_usbwc, "RawTriggerRate_usbwc[nCh_usbwc]/F");
Int_t i,j;
ifstream indataFileList;
string mot;
TString fileN;
indataFileList.open(dataFileList.Data());
assert(indataFileList.is_open());
while (indataFileList >> mot){
fileN = datPreff + "/" + mot;
pFile = fopen (fileN.Data(), "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
cout<<" ---> File to convert : "<<fileN<<endl;
// obtain file size:
fseek (pFile , 0 , SEEK_END);
totFileSizeByte = ftell (pFile);
rewind (pFile);
//cout<<"totFileSizeByte = "<<totFileSizeByte<<endl;
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*totFileSizeByte);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
//result = fread (buffer,1,lSize,pFile);
currentPositionByte = 0;
fread (buffer,1,206,pFile);
currentPositionByte = currentPositionByte + 206;
//for(j = 0;j<206;j++){
//cout<<setw(5)<<j<<setw(5)<<buffer[j]<<endl;
//}
buffTmp = buffer[204];
if( buffTmp != "="){
cout<<" ERROR --> Wrong format"<<endl
<<" : buffer[204] != ="<<endl;
cout<<std::hex<<(int)buffer[204] << '\n';
cout<<std::dec;
//cout<<10<< '\n';
assert(0);
}
while(currentPositionByte<totFileSizeByte){
//for(j = 0;j<100;j++){
currentPositionByte += fread (&EventNumber,1,4,pFile);
currentPositionByte += fread (&EpochTime,1,8,pFile);
currentPositionByte += fread (&Year,1,4,pFile);
currentPositionByte += fread (&Month,1,4,pFile);
currentPositionByte += fread (&Day,1,4,pFile);
currentPositionByte += fread (&Hour,1,4,pFile);
currentPositionByte += fread (&Minute,1,4,pFile);
currentPositionByte += fread (&Second,1,4,pFile);
currentPositionByte += fread (&Millisecond,1,4,pFile);
EventNumber_usbwc = EventNumber;
EpochTime_usbwc = EpochTime;
Year_usbwc = Year;
Month_usbwc = Month;
Day_usbwc = Day;
Hour_usbwc = Hour;
Minute_usbwc = Minute;
Second_usbwc = Second;
Millisecond_usbwc = Millisecond;
for(i = 0;i<nCh;i++){
currentPositionByte += fread (&floatR,1,4,pFile); RawTriggerRate[i] = floatR;
RawTriggerRate_usbwc[i] = RawTriggerRate[i];
}
//cout<<"EventNumber "<<EventNumber<<endl;
//cout<<"totFileSizeByte "<<totFileSizeByte<<endl
//<<"currentPositionByte "<<currentPositionByte<<endl;
//for(i = 0;i<nCh;i++){
//cout<<"RawTriggerRate[i] "<<RawTriggerRate[i]<<endl;
//}
//printf ("EpochTime %13.3f \n", EpochTime);
//cout<<"Year "<<Year<<endl
// <<"Month "<<Month<<endl
// <<"Day "<<Day<<endl
// <<"Hour "<<Hour<<endl
// <<"Minute "<<Minute<<endl
// <<"Second "<<Second<<endl
// <<"Millisecond "<<Millisecond<<endl;
tree->Fill();
}
// terminate
fclose (pFile);
free (buffer);
}
indataFileList.close();
hfile = tree->GetCurrentFile();
hfile->Write();
hfile->Close();
}
/*
if(AcqParams.SaveRateRunInFile)
{
sprintf(line,"=== Rates [Hz] of: ");
for(channel = 0; channel < SystemParams.NbOfFeChannels + SystemParams.NbOfDigitalChannels; channel++)
{
if(SystemParams.ChannelMask[channel] == ENABLED)
{
sprintf(lineBuf,"Ch%d ", channel);
strcat(line, lineBuf);
}
}
if(AcqParams.EnableRateLinearCombination == TRUE)
{
sprintf(lineBuf,"Combined ch");
strcat(line, lineBuf);
}
strcat(line, "===\n");
fputs(line,FileForSavingRateRunPtr);
}
fwrite (&EventNumber, sizeof(int), 1, FileForSavingRateRunPtr);
fwrite (&EpochTime, sizeof(double), 1, FileForSavingRateRunPtr);
fwrite (&Year, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
fwrite (&Month, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
fwrite (&Day, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
fwrite (&Hour, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
fwrite (&Minute, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
fwrite (&Second, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
uint_millisecond =(unsigned int)Millisecond;
fwrite (&uint_millisecond, sizeof(unsigned int), 1, FileForSavingRateRunPtr);
for(samIndex = 0; samIndex < SystemParams.NbOfFeSamBlocks; samIndex++)
{
for(channel = 0; channel <NB_OF_CHANNELS_IN_SAMBLOCK; channel ++)
{
if(SystemParams.ChannelMask[samIndex*NB_OF_CHANNELS_IN_SAMBLOCK+channel] == ENABLED)
{
fwrite(&RawTriggerRate[channel+2*samIndex], sizeof(float), 1, FileForSavingRateRunPtr);
}
}
}
if(AcqParams.EnableRateLinearCombination == TRUE)
fwrite(&RawTriggerRate[SystemParams.NbOfFeSamBlocks + SystemParams.NbOfDigitalSamBlocks], sizeof(float), 1, FileForSavingRateRunPtr);
*/