source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/packages/simulation/detector/tools/src/OADBConverter.cc @ 117

Last change on this file since 117 was 117, checked in by moretto, 11 years ago

ESAF version compilable on mac OS

File size: 12.1 KB
Line 
1// ESAF : Euso Simulation and Analysis Framework
2// $Id: OADBConverter.cc 1856 2005-05-17 21:48:30Z thea $
3// A.Thea, J.Watts created Mar, 22 2004
4
5#include "OADBConverter.hh"
6#include "OADBHeader.hh"
7#include "OADBPhotons.hh"
8#include <string> 
9#include <vector> 
10#include <Riostream.h>
11
12ClassImp(OADBConverter)
13   
14// ctor
15OADBConverter::OADBConverter( OADBTree *t ) : fPhotons(NULL) {
16    // init tree
17    fTree = t;
18    fHeader = &(t->GetHeader());
19    fTree->SetBranchAddress("photons", &fPhotons);
20   
21    // length of the keywords
22    fKeyLength = 16;
23
24    fBinFile = new fstream;
25   
26}
27
28// dtor
29OADBConverter::~OADBConverter() {
30    delete fBinFile;
31}
32
33// convert oatree into a binary ESAF-readable file
34void OADBConverter::SaveBinary( const char *path ) {
35
36    //ofstream fBinFile;
37    fBinFile->open( path, ios::binary | ios::out );
38    if ( !(fBinFile->is_open()) )
39        cout << Form("SaveBinary: Unable to open %s",path) << endl;
40
41    WriteHeader();
42   
43    Int_t iEntry(0);
44
45    InsertBegin(kPhotons);
46    for( Int_t iWl(0); iWl < fHeader->GetNumWavelengths(); iWl++ ) {
47        // be sure to have photons loaded before getting the wlid
48        fTree->GetEntry(iEntry);
49        InsertBegin(kWavelength);
50        Int_t wlId = fPhotons->GetWavelengthId(); 
51        fBinFile->write((char*)&wlId,sizeof(Int_t));
52
53        for( Int_t iAng(0); iAng < fHeader->GetNumAngles(); iAng++ ){
54           fTree->GetEntry(iEntry++);
55           WritePhotons(); 
56        } 
57        InsertEnd(kWavelength);
58    }
59    InsertEnd(kPhotons);
60   
61    fBinFile->close();
62
63}
64
65// checks binary
66void OADBConverter::CheckBinary( const char *path ) {
67    fBinFile->open( path, ios::in | ios::binary ); 
68
69    Read();
70
71    fBinFile->close();
72}
73
74string OADBConverter::KeyWord( OAEKeyType type ) {
75
76    string buffer;
77    switch ( type ) {
78        case kBegin:
79            buffer = "BEGIN"; 
80            break;
81        case kEnd:
82            buffer = "END"; 
83            break;
84        case kHeader:
85            buffer = "HEADER"; 
86            break;
87        case kPhotons:
88            buffer = "PHOTONS"; 
89            break;
90        case kWavelength:
91            buffer = "WAVELENGTH"; 
92            break;
93        case kAngle:
94            buffer = "ANGLE"; 
95            break;
96        default:
97            buffer.resize(0);
98            break;
99    }
100    return buffer.c_str();
101}
102
103// insert begin keyword
104void OADBConverter::InsertBegin( OAEKeyType type ) {
105    string dummy = "BEGIN";
106    dummy+=KeyWord(type);
107    dummy.resize(fKeyLength);
108    fBinFile->write(dummy.c_str(), fKeyLength);
109}
110
111// insert end keyword
112void OADBConverter::InsertEnd( OAEKeyType type ) {
113    string dummy = "END";
114    dummy+=KeyWord(type);
115    dummy.resize(fKeyLength);
116    fBinFile->write(dummy.c_str(), fKeyLength);
117}
118
119// append header to fBinFile
120void OADBConverter::WriteHeader() {
121
122    Int_t num;
123    string comment;
124    comment  = "# ESAF - Euso Simulation and Analysis Framework\n";
125    comment += "# OpticsAnalysisModule Photon Database File\n";
126    comment += "# The header contains the following informations:\n";
127    comment += "# TODO";
128    size_t commentSize = comment.size();
129
130    // insert step, radius, entrance Z, exit Z
131    size_t dataSize = 10;
132    Float_t data[dataSize];
133    data[0]=fHeader->GetXYStep();
134    data[1]=fHeader->GetOpticsRadius();
135    data[2]=fHeader->GetEntranceDiscRadius();
136    data[3]=fHeader->GetFirstLensBottom();
137    data[4]=fHeader->GetFirstLensTop();
138    data[5]=fHeader->GetSecondLensBottom();
139    data[6]=fHeader->GetSecondLensTop();
140    data[7]=fHeader->GetEntranceZ();
141    data[8]=fHeader->GetExitZ();
142    data[9]=fHeader->GetPosZ();
143   
144    size_t headSize =  sizeof(char)*commentSize
145                      +sizeof(size_t)
146                      +sizeof(Int_t)*(5+fHeader->GetNumRows())
147                      +sizeof(Float_t)*(dataSize+fHeader->GetNumWavelengths()
148                           +fHeader->GetNumAngles());
149
150    InsertBegin( kHeader );
151    fBinFile->write((char*)&headSize, sizeof(size_t));
152    fBinFile->write((char*)&commentSize, sizeof(size_t));
153    fBinFile->write(comment.c_str(), comment.size());
154   
155    fBinFile->write((char*)&dataSize, sizeof(size_t));
156    fBinFile->write((char*)data, sizeof(Float_t)*dataSize);
157
158    // insert Wavelengths
159    num = fHeader->GetNumWavelengths();
160    fBinFile->write((char*)&num, sizeof(Int_t));
161    fBinFile->write((char*)fHeader->GetWavelengths(), sizeof(Float_t)*num);
162    // inser Angles
163    num = fHeader->GetNumAngles();
164    fBinFile->write((char*)&num, sizeof(Int_t));
165    fBinFile->write((char*)fHeader->GetAngles(), sizeof(Float_t)*num);
166
167    // insert Positions
168    num=fHeader->GetNumPos();
169    fBinFile->write((char*)&num, sizeof(Int_t));
170    num=fHeader->GetNumRows();
171    fBinFile->write((char*)&num, sizeof(Int_t));
172    fBinFile->write((char*)fHeader->GetRows(), sizeof(Int_t)*num);
173
174    InsertEnd( kHeader );
175
176}
177
178// append a bunch of photons to fBinFile
179void OADBConverter::WritePhotons() {
180    if (!fPhotons) {
181        cout << "WritePhotons: invalid pointer" << endl;
182        return;
183    }
184    if ( fPhotons->GetSize() != fHeader->GetNumPos() ) {
185        cout << "WritePhotons: mismatch between header and fPhotons" << endl;
186        return;
187    }
188    InsertBegin( kAngle );
189    Int_t angleId = fPhotons->GetAngleId(); 
190    Float_t ph[5];
191
192    fBinFile->write((char*)&angleId,sizeof(Int_t));
193    for(Int_t i(0); i<fPhotons->GetSize(); i++) {
194        ph[0]=fPhotons->GetThroughput()[i];
195        ph[1]=fPhotons->GetXOut()[i];
196        ph[2]=fPhotons->GetYOut()[i];
197        ph[3]=fPhotons->GetThetaOut()[i];
198        ph[4]=fPhotons->GetPhiOut()[i];
199
200        fBinFile->write((char*)ph,sizeof(Float_t)*5);
201    }
202
203    InsertEnd( kAngle );
204}
205
206// reads the header of the binary file and dispays its contents
207void OADBConverter::Read() {
208    Float_t *values(NULL);
209    char buffer[512];
210   
211    fBinFile->read(buffer, fKeyLength);
212
213    if ( (KeyWord(kBegin)+KeyWord(kHeader)) == buffer)
214        cout << "Header found" << endl;
215   
216    size_t headSize;
217    fBinFile->read((char*)&headSize, sizeof(size_t));
218    cout << "header " << headSize << " bytes long" << endl;
219   
220    // check the end of the header to be where it should
221    streampos sp = fBinFile->tellg();
222    fBinFile->seekg(headSize, ios::cur);
223
224    fBinFile->read(buffer, fKeyLength);
225    if ( (KeyWord(kEnd)+KeyWord(kHeader)) == buffer)
226        cout << "End Header found" << endl;
227    else {
228        cout << "End Header missing or misplaced. Exiting..." << endl;
229        return;   
230    }
231   
232    fBinFile->seekg(sp);
233
234    // read comment
235    size_t commentSize;
236    char* comment;
237
238    fBinFile->read((char*)&commentSize, sizeof(size_t));
239    cout << "comment size: " << commentSize << endl;
240    comment = new char[commentSize];
241    fBinFile->read((char*)comment, commentSize);
242    cout << comment << endl;
243    delete [] comment;
244
245    Int_t numData;
246    fBinFile->read((char*)&numData, sizeof(Int_t));
247    values = new Float_t[numData];
248    fBinFile->read((char*)values, sizeof(Float_t)*numData);
249    cout << "XYStep = " <<  values[0] << endl;
250    cout << "OpticsRadius = " << values[1] << endl;
251    cout << "EntranceDiscRadius = " << values[2] << endl;
252    cout << "FirstLensBottom = " << values[3] << endl;
253    cout << "FirstLensTop = " << values[4] << endl;
254    cout << "SecondLensBottom = " << values[5] << endl;
255    cout << "SecondLensTop = " << values[6] << endl;
256    cout << "EntranceZ = " << values[7] << endl;
257    cout << "ExitZ = " << values[8] << endl;
258    cout << "PosZ = " << values[9] << endl;
259    delete [] values;
260
261    // read wls
262    Int_t numWls;
263    fBinFile->read((char*)&numWls, sizeof(Int_t));
264    cout << numWls << " wls found" << endl;
265
266    values = new Float_t[numWls];
267    fBinFile->read((char*)values, sizeof(Float_t)*numWls);
268    for(Int_t i(0); i<numWls; i++) 
269        cout << i << "  " << values[i] << endl;
270    delete [] values;
271   
272    // read angles
273    Int_t numAngles;
274    fBinFile->read((char*)&numAngles, sizeof(Int_t));
275    cout << numAngles << " angles found" << endl;
276
277    values = new Float_t[numAngles];
278    fBinFile->read((char*)values, sizeof(Float_t)*numAngles);
279    for(Int_t i(0); i<numAngles; i++) 
280        cout << i << "  " << values[i] << endl;
281    delete [] values;
282
283    // positions
284    Int_t numPos;
285    fBinFile->read((char*)&numPos, sizeof(Int_t));
286    cout << numPos << " positions found" << endl;
287
288    Int_t rows;
289    Int_t *cols(NULL);
290    fBinFile->read((char*)&rows, sizeof(Int_t));
291    cout << rows << " rows found" << endl;
292
293    Int_t posCheck(0);
294    cols = new Int_t[rows];
295    fBinFile->read((char*)cols, sizeof(Float_t)*rows);
296    for(Int_t i(0); i<rows; i++) {
297        cout << i << "  " << cols[i] << endl;
298        posCheck+=cols[i];
299    }
300    if ( numPos != posCheck ) {
301        cout << "mismatch between number of columns and total " << flush;
302        cout << "number of positions in the header. Exiting..." << endl; 
303    }
304
305    fBinFile->read(buffer, fKeyLength);
306    if ( (KeyWord(kEnd)+KeyWord(kHeader)) == buffer)
307        cout << "Header closed" << endl;
308
309
310    fBinFile->read(buffer, fKeyLength);
311    if ( (KeyWord(kBegin)+KeyWord(kPhotons)) == buffer)
312        cout << "Photon list found" << endl;
313    else {
314        cout << "Photon missing or misplaced. Exiting" << endl;
315        return;
316    }
317
318    // calculate the length of the photons' list
319    size_t angleSize = sizeof(char)*(2*fKeyLength)+sizeof(Int_t)+sizeof(Float_t)*(5*numPos);
320    size_t wlsize = sizeof(char)*(2*fKeyLength)+sizeof(Int_t)+angleSize*numAngles;
321    size_t phSize = wlsize*numWls;
322
323    cout << "Calculated list size " << phSize << endl;
324   
325    // check the end of the list to be where it should
326    sp = fBinFile->tellg();
327    fBinFile->seekg(phSize, ios::cur);
328
329    fBinFile->read(buffer, fKeyLength);
330    if ( (KeyWord(kEnd)+KeyWord(kPhotons)) == buffer)
331        cout << "End Photons found" << endl;
332    else {
333        cout << "End Photons missing or misplaced. Exiting..." << endl;
334        return;   
335    }
336    fBinFile->seekg(sp);
337   
338    // checking all the key positions
339    vector< vector<streampos> > keyMap;
340    for ( Int_t iWl(0); iWl < numWls; iWl++) {
341        // check the wl key
342        fBinFile->read(buffer, fKeyLength);
343        if ( (KeyWord(kBegin)+KeyWord(kWavelength)) != buffer){
344            cout << buffer << endl;
345            cout << "Wavelength keyword " << iWl << " not found. Exiting" << endl;   
346            return;
347        }
348       
349        // check wl id
350        Int_t wlId;
351        fBinFile->read((char*)&wlId, sizeof(Int_t));
352        if ( wlId != iWl){
353            cout << "Wavelength id mismatch: wlId = " << wlId << "   iWl = " << iWl << endl;   
354            return;
355        }
356       
357        cout << " wlId = " << wlId << endl; 
358        vector<streampos> dummy;
359        for ( Int_t iAng(0); iAng < numAngles; iAng++) {
360            // check the angle key
361            fBinFile->read(buffer, fKeyLength);
362            if ( (KeyWord(kBegin)+KeyWord(kAngle)) != buffer) {
363                cout << "Angle keyword " << iAng << " not found. Exiting" << endl;   
364                return;
365            }
366
367            // check angle id
368            Int_t angleId;
369            fBinFile->read((char*)&angleId, sizeof(Int_t));
370            if ( angleId != iAng){
371                cout << " id mismatch: angleId = " << angleId << "   iAng = " << iAng << endl;   
372                return;
373            }
374           
375            cout << " angleId = " << angleId << endl; 
376            // keep the pos of the first ph
377            streampos cur = fBinFile->tellg();
378           
379            // move at the end
380            fBinFile->seekg(sizeof(Float_t)*5*numPos,ios::cur);
381           
382            // check the end key
383            fBinFile->read(buffer, fKeyLength);
384            if ( (KeyWord(kEnd)+KeyWord(kAngle)) != buffer) {
385                cout << buffer << endl;
386                cout << "End Angle keyword " << iAng << " not found. Exiting" << endl;   
387                return;
388            }
389
390            // save the streamer position of the first ph
391            dummy.push_back(cur);
392        }
393       
394        // check the end key
395        fBinFile->read(buffer, fKeyLength);
396        if ( (KeyWord(kEnd)+KeyWord(kWavelength)) != buffer) {
397            cout << "End Wavelength keyword " << iWl << " not found. Exiting" << endl;   
398            return;
399        }
400       
401        keyMap.push_back(dummy); 
402    }
403    delete [] cols;
404}
Note: See TracBrowser for help on using the repository browser.