1 | // ESAF : Euso Simulation and Analysis Framework |
---|
2 | // $Id: NumbersFileParser.hh 2804 2008-10-09 12:10:06Z biktem $ |
---|
3 | // Author: Daniel De Marco Jan, 28 2002 |
---|
4 | |
---|
5 | |
---|
6 | /***************************************************************************** |
---|
7 | * ESAF: Euso Simulation and Analysis Framework * |
---|
8 | * * |
---|
9 | * Id: NumbersFileParser * |
---|
10 | * Package: Base * |
---|
11 | * Coordinator: Marco.Pallavicini * |
---|
12 | * * |
---|
13 | *****************************************************************************/ |
---|
14 | |
---|
15 | #ifndef __NUMBERSFILEPARSER_HH__ |
---|
16 | #define __NUMBERSFILEPARSER_HH__ |
---|
17 | |
---|
18 | #include <string> |
---|
19 | #include <map> |
---|
20 | #include <vector> |
---|
21 | #include <iostream> |
---|
22 | #include <fstream> |
---|
23 | #include <stdexcept> |
---|
24 | #include <zlib.h> |
---|
25 | |
---|
26 | #include "euso.hh" |
---|
27 | #include "EsafMsgSource.hh" |
---|
28 | |
---|
29 | //////////////////////////////////////////////////////////////////////////////// |
---|
30 | // // |
---|
31 | // NumbersFileParser // |
---|
32 | // // |
---|
33 | // Parser of numbers data files // |
---|
34 | // // |
---|
35 | //////////////////////////////////////////////////////////////////////////////// |
---|
36 | |
---|
37 | class NumbersFileParser : public EsafMsgSource { |
---|
38 | public: |
---|
39 | enum Coding {ascii, gzip}; |
---|
40 | // types of file coding that NumberFileParser can handle |
---|
41 | |
---|
42 | NumbersFileParser(const string &fn, size_t ncol, Coding = ascii); |
---|
43 | // constructor |
---|
44 | |
---|
45 | virtual ~NumbersFileParser(); |
---|
46 | // destructor |
---|
47 | |
---|
48 | vector<Double_t> &GetCol(size_t col); |
---|
49 | // columns |
---|
50 | |
---|
51 | vector<Double_t> GetRow(size_t row); |
---|
52 | // rows |
---|
53 | |
---|
54 | Double_t GetNum(size_t row, size_t col); |
---|
55 | // get element |
---|
56 | |
---|
57 | inline void SetUnit(Double_t unit, size_t col); |
---|
58 | // set colum unit of measure |
---|
59 | |
---|
60 | inline Double_t GetUnit(size_t col) const; |
---|
61 | // get colum unit of measure |
---|
62 | |
---|
63 | protected: |
---|
64 | // access to number of lines and columns for child class |
---|
65 | inline Int_t GetNumLines() { return fNumbers[0].size(); } |
---|
66 | inline size_t GetNumColumns() const { return fNumCol; } |
---|
67 | |
---|
68 | private: |
---|
69 | void ProcessLine( string ); |
---|
70 | // Helper function: gets numbers from a line |
---|
71 | |
---|
72 | vector<Double_t> *fNumbers; |
---|
73 | // a single vector for the whole matrix |
---|
74 | |
---|
75 | Double_t *fUnits; |
---|
76 | |
---|
77 | string fFileName; |
---|
78 | Coding fFileType; |
---|
79 | size_t fNumCol; |
---|
80 | |
---|
81 | ClassDef(NumbersFileParser,0) |
---|
82 | |
---|
83 | }; |
---|
84 | |
---|
85 | // |
---|
86 | // inline functions |
---|
87 | // |
---|
88 | |
---|
89 | // Get a column |
---|
90 | inline vector<Double_t> &NumbersFileParser::GetCol(size_t col) { |
---|
91 | if ( col >= fNumCol ) |
---|
92 | Msg(EsafMsg::Panic) << "Invalid column number reqeusted to "+fFileName << MsgDispatch; |
---|
93 | |
---|
94 | return fNumbers[col]; |
---|
95 | } |
---|
96 | |
---|
97 | // Get an element |
---|
98 | //fNumCol = 17 |
---|
99 | inline Double_t NumbersFileParser::GetNum(size_t row, size_t col) { |
---|
100 | if ( col > fNumCol ) |
---|
101 | Msg(EsafMsg::Panic) << "Invalid column requested to "+fFileName << MsgDispatch; |
---|
102 | |
---|
103 | if ( row>fNumbers[col].size() ) { |
---|
104 | Msg(EsafMsg::Panic) << "Invalid row requsted to "+fFileName << MsgDispatch; |
---|
105 | } |
---|
106 | return (fNumbers[col])[row]; |
---|
107 | } |
---|
108 | |
---|
109 | // Set colum unit of measure |
---|
110 | inline void NumbersFileParser::SetUnit(Double_t unit, size_t col) { |
---|
111 | if ( col >= fNumCol ) |
---|
112 | Msg(EsafMsg::Panic) << "SetUnit: Column number out of range "<< MsgDispatch; |
---|
113 | |
---|
114 | Double_t ratio = unit/fUnits[col]; |
---|
115 | |
---|
116 | vector<Double_t>::iterator it; |
---|
117 | for (it=fNumbers[col].begin(); it != fNumbers[col].end(); it++) |
---|
118 | (*it) *= ratio; |
---|
119 | |
---|
120 | fUnits[col] = unit; |
---|
121 | } |
---|
122 | |
---|
123 | // Get colum unit of measure |
---|
124 | inline Double_t NumbersFileParser::GetUnit(size_t col) const { |
---|
125 | if ( col >= fNumCol ) |
---|
126 | Msg(EsafMsg::Panic) << "GetUnit: Column number out of range "<< MsgDispatch; |
---|
127 | |
---|
128 | return fUnits[col]; |
---|
129 | } |
---|
130 | |
---|
131 | #endif /* __NUMBERSFILEPARSER_HH__ */ |
---|
132 | |
---|