1 | // $Id: NumbersFileParser.cc 2956 2011-06-28 18:54:32Z mabl $ |
---|
2 | // ESAF : Euso Simulation and Analysis Framework |
---|
3 | // Author: Daniel De Marco Jan, 28 2002 |
---|
4 | |
---|
5 | /***************************************************************************** |
---|
6 | * ESAF: Euso Simulation and Analysis Framework * |
---|
7 | * * |
---|
8 | * Id: NumbersFileParser * |
---|
9 | * Package: Base * |
---|
10 | * Coordinator: Marco.Pallavicini * |
---|
11 | * * |
---|
12 | *****************************************************************************/ |
---|
13 | |
---|
14 | //_____________________________________________________________________________ |
---|
15 | // |
---|
16 | // Parser of number data files |
---|
17 | // =========================== |
---|
18 | // |
---|
19 | // Parser of numbers data files |
---|
20 | // |
---|
21 | |
---|
22 | #include "NumbersFileParser.hh" |
---|
23 | #include "utils.hh" |
---|
24 | #include <math.h> |
---|
25 | |
---|
26 | ClassImp(NumbersFileParser) |
---|
27 | |
---|
28 | //______________________________________________________________________________ |
---|
29 | NumbersFileParser::~NumbersFileParser() { |
---|
30 | // |
---|
31 | // Destructor |
---|
32 | // |
---|
33 | |
---|
34 | delete [] fNumbers; |
---|
35 | delete [] fUnits; |
---|
36 | } |
---|
37 | |
---|
38 | //______________________________________________________________________________ |
---|
39 | NumbersFileParser::NumbersFileParser(const string &fn, size_t ncol, Coding ft): |
---|
40 | fFileName(fn), fFileType(ft), fNumCol(ncol){ |
---|
41 | // |
---|
42 | // Constructor |
---|
43 | // |
---|
44 | |
---|
45 | fNumbers = new vector<Double_t>[fNumCol]; |
---|
46 | fUnits = new Double_t[fNumCol]; |
---|
47 | |
---|
48 | for ( size_t i(0); i<fNumCol; i++) { |
---|
49 | fUnits[i] = 1; |
---|
50 | } |
---|
51 | |
---|
52 | if ( ft == gzip ) { |
---|
53 | // open gzip file |
---|
54 | gzFile zF(0); |
---|
55 | |
---|
56 | // check if map file or gzipped version exists |
---|
57 | if ( (zF = gzopen( fn.c_str(), "r" )) == 0 ) |
---|
58 | Msg(EsafMsg::Panic) << "Unable to open file "+fn << MsgDispatch; |
---|
59 | |
---|
60 | const size_t max_size=500; |
---|
61 | Char_t line[max_size]; |
---|
62 | string dummy; |
---|
63 | |
---|
64 | while(gzgets(zF, line, max_size) != Z_NULL ){ |
---|
65 | dummy = line; |
---|
66 | |
---|
67 | const size_t row_size = dummy.size(); |
---|
68 | |
---|
69 | if(row_size == max_size-1) |
---|
70 | Msg(EsafMsg::Panic) << "Row exceeds buffer in file "+fFileName << MsgDispatch; |
---|
71 | |
---|
72 | ProcessLine(dummy); |
---|
73 | } |
---|
74 | |
---|
75 | gzclose(zF); |
---|
76 | } else if ( ft == ascii ) { |
---|
77 | // open ascii file |
---|
78 | ifstream in(fFileName.c_str()); |
---|
79 | if ( !in ) Msg(EsafMsg::Panic) << "Can't open file "+fFileName << MsgDispatch; |
---|
80 | |
---|
81 | string dummy; |
---|
82 | while(getline(in, dummy)) |
---|
83 | ProcessLine(dummy); |
---|
84 | |
---|
85 | in.close(); |
---|
86 | } else { |
---|
87 | Msg(EsafMsg::Panic) << "Unable to handle "+fFileName << MsgDispatch; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | //______________________________________________________________________________ |
---|
92 | void NumbersFileParser::ProcessLine(string line) { |
---|
93 | // |
---|
94 | // Helper function: gets numbers from a line |
---|
95 | // |
---|
96 | |
---|
97 | size_t pos; |
---|
98 | |
---|
99 | // remove newline |
---|
100 | line.erase(line.find_last_not_of("\n\r")+1); |
---|
101 | |
---|
102 | // erase comments |
---|
103 | pos = line.find('#'); |
---|
104 | if ( pos != string::npos ) |
---|
105 | line.erase(pos); |
---|
106 | |
---|
107 | // swallow spaces from both sides |
---|
108 | line = trim(line); |
---|
109 | |
---|
110 | // skip blank lines |
---|
111 | if ( line.empty() ) |
---|
112 | return; |
---|
113 | |
---|
114 | // we expect something like that: |
---|
115 | // number number number ... |
---|
116 | |
---|
117 | size_t col = 0; |
---|
118 | while ( !line.empty() ) { |
---|
119 | if(++col > fNumCol) |
---|
120 | Msg(EsafMsg::Panic) << col << " instead of " << fNumCol |
---|
121 | << " columns found in "+fFileName << MsgDispatch; |
---|
122 | |
---|
123 | // get value |
---|
124 | const string value=line.substr(0, line.find_first_of("\t ")); |
---|
125 | |
---|
126 | // convert to Double_t |
---|
127 | Double_t v = toDouble(value); |
---|
128 | if ( isnan(v) ) |
---|
129 | FatalError(value+"is not a number"); |
---|
130 | fNumbers[col-1].push_back(v); |
---|
131 | |
---|
132 | // erase used value |
---|
133 | line.erase(0, line.find_first_of("\t ")); |
---|
134 | |
---|
135 | // since the right side of the string is already trimed, only trim the right side |
---|
136 | line=ltrim(line); |
---|
137 | } |
---|
138 | |
---|
139 | if ( col != 0 && col != fNumCol ) |
---|
140 | Msg(EsafMsg::Panic) << col << " instead of " << fNumCol |
---|
141 | << " columns found in "+fFileName << MsgDispatch; |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | //______________________________________________________________________________ |
---|
146 | vector<Double_t> NumbersFileParser::GetRow(size_t row) { |
---|
147 | // |
---|
148 | // Get a copy of a row |
---|
149 | // |
---|
150 | |
---|
151 | if ( row > fNumbers[0].size()) |
---|
152 | Msg(EsafMsg::Panic) << "Invalid row requested to "+fFileName << endl; |
---|
153 | |
---|
154 | vector<Double_t> dummy; |
---|
155 | |
---|
156 | for(size_t i=0; i<fNumCol; i++) |
---|
157 | dummy.push_back(fNumbers[i][row]); |
---|
158 | |
---|
159 | return dummy; |
---|
160 | } |
---|