1 | // $Id: EusoMapping.cc 2863 2011-02-18 11:00:44Z biktem $ |
---|
2 | // Author: M. Pallavicini 8 2003 |
---|
3 | |
---|
4 | /***************************************************************************** |
---|
5 | * ESAF: Euso Simulation and Analysis Framework * |
---|
6 | * * |
---|
7 | * Id: EusoMapping * |
---|
8 | * Package: Optics * |
---|
9 | * Coordinator: Alessandro.Thea * |
---|
10 | * * |
---|
11 | *****************************************************************************/ |
---|
12 | |
---|
13 | //______________________________________________________________________________ |
---|
14 | // |
---|
15 | // EusoMapping |
---|
16 | // ========== |
---|
17 | // |
---|
18 | // Class containing the angle-pixel map. |
---|
19 | // Every single detector config MUST have its own map. |
---|
20 | // Map is affected by: |
---|
21 | // OpticalSystem |
---|
22 | // OpticalAdaptor |
---|
23 | // PmtGeometry |
---|
24 | // FocalSurface (layout) |
---|
25 | // |
---|
26 | |
---|
27 | #include "EusoMapping.hh" |
---|
28 | #include "NumbersFileParser.hh" |
---|
29 | #include "ERunParameters.hh" |
---|
30 | #include "EsafSys.hh" |
---|
31 | #include "Config.hh" |
---|
32 | #include <string> |
---|
33 | #include "TBenchmark.h" |
---|
34 | #include "TSystem.h" |
---|
35 | |
---|
36 | ClassImp(EusoMapping) |
---|
37 | |
---|
38 | EusoMapping *EusoMapping::fMe = 0; |
---|
39 | |
---|
40 | //______________________________________________________________________________ |
---|
41 | EusoMapping::EusoMapping():EsafConfigurable() { |
---|
42 | // ctor |
---|
43 | |
---|
44 | fMapFile = Conf()->GetStrPath("EusoMapping.fMapFile"); |
---|
45 | fMapCacheFile = Conf()->GetStrPath("EusoMapping.fMapCacheFile"); |
---|
46 | if ( Conf()->GetStr("EusoMapping.fUseCache") == "yes" ) fUseCache = kTRUE; |
---|
47 | else fUseCache=kFALSE; |
---|
48 | |
---|
49 | fAnglePixelMap.resize(0); |
---|
50 | |
---|
51 | if ( fUseCache ) |
---|
52 | if ( CheckCacheFile() ) { |
---|
53 | Msg(EsafMsg::Info) << "Reading Pixel Map from cache... "; |
---|
54 | LoadBinary(); |
---|
55 | Msg(EsafMsg::Info) << "Done." << MsgDispatch; |
---|
56 | } else { |
---|
57 | Msg(EsafMsg::Info) << "Reading Pixel Map..."; |
---|
58 | LoadAscii(); |
---|
59 | Msg(EsafMsg::Info) << "Done." << MsgDispatch; |
---|
60 | SaveBinary(); |
---|
61 | } |
---|
62 | else { |
---|
63 | Msg(EsafMsg::Info) << "Reading Pixel Map..."; |
---|
64 | LoadAscii(); |
---|
65 | Msg(EsafMsg::Info) << "Done." << MsgDispatch; |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | //______________________________________________________________________________ |
---|
71 | EusoMapping::~EusoMapping() { |
---|
72 | // dtor |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | //______________________________________________________________________________ |
---|
77 | EusoMapping* EusoMapping::Get() { |
---|
78 | // instance |
---|
79 | |
---|
80 | if ( fMe == 0 ) |
---|
81 | fMe = new EusoMapping(); |
---|
82 | return fMe; |
---|
83 | } |
---|
84 | |
---|
85 | //______________________________________________________________________________ |
---|
86 | Double_t EusoMapping::GetThetaFOV( ChannelUniqueId uid) { |
---|
87 | // returns theta in field-of-view for a given unique channel id |
---|
88 | |
---|
89 | if( uid > 0 && uid <= GetPixelsNumber() ) |
---|
90 | return fAnglePixelMap[uid-1].fThetaFOV; |
---|
91 | else |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | //______________________________________________________________________________ |
---|
96 | Double_t EusoMapping::GetSigmaThetaFOV( ChannelUniqueId uid) { |
---|
97 | // returns theta in field-of-view for a given unique channel id |
---|
98 | |
---|
99 | if( uid > 0 && uid <= GetPixelsNumber() ) |
---|
100 | return fAnglePixelMap[uid-1].fSigmaThetaFOV; |
---|
101 | else |
---|
102 | return -1; |
---|
103 | } |
---|
104 | |
---|
105 | //______________________________________________________________________________ |
---|
106 | Double_t EusoMapping::GetPhiFOV( ChannelUniqueId uid) { |
---|
107 | // returns phi in field-of-view for a given unique channel id |
---|
108 | |
---|
109 | if( uid > 0 && uid <= GetPixelsNumber() ) |
---|
110 | return fAnglePixelMap[uid-1].fPhiFOV; |
---|
111 | |
---|
112 | else |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | //______________________________________________________________________________ |
---|
117 | Double_t EusoMapping::GetSigmaPhiFOV( ChannelUniqueId uid) { |
---|
118 | // returns phi spread in field-of-view for a given unique channel id |
---|
119 | |
---|
120 | if( uid > 0 && uid <= GetPixelsNumber() ) |
---|
121 | return fAnglePixelMap[uid-1].fSigmaPhiFOV; |
---|
122 | else |
---|
123 | return -1; |
---|
124 | } |
---|
125 | //______________________________________________________________________________ |
---|
126 | Bool_t EusoMapping::CheckCacheFile() { |
---|
127 | // check if aux file matches the source |
---|
128 | |
---|
129 | Long_t id, flags, modtime; |
---|
130 | Long64_t size; |
---|
131 | |
---|
132 | // look for existing binary |
---|
133 | if ( gSystem->GetPathInfo(fMapCacheFile.c_str(), &id, &size, &flags, &modtime) == 1 ) |
---|
134 | // Binary auxilar file doesn't exist |
---|
135 | return kFALSE; |
---|
136 | |
---|
137 | // look for original map file |
---|
138 | if ( gSystem->GetPathInfo(fMapFile.c_str(), &id, &size, &flags, &modtime) == 1 ) |
---|
139 | // Binary auxilar file doesn't exist |
---|
140 | throw runtime_error("EusoMapping: "+fMapFile+" doesn't exists"); |
---|
141 | |
---|
142 | // open auxilar file |
---|
143 | ifstream fcache(fMapCacheFile.c_str()); |
---|
144 | if ( !(fcache.is_open()) ) |
---|
145 | throw runtime_error("EusoMapping: Unable to open "+fMapCacheFile); |
---|
146 | |
---|
147 | |
---|
148 | Long_t stats[4]; |
---|
149 | fcache.read((char*)stats,sizeof(Long_t)*4); |
---|
150 | |
---|
151 | fcache.close(); |
---|
152 | |
---|
153 | /* Debug printout |
---|
154 | * cout << "Id\t" << id << " --> " << stats[0] << endl; |
---|
155 | * cout << "Size\t" << size << " --> " << stats[1] << endl; |
---|
156 | * cout << "Flags\t" << flags << " --> " << stats[2] << endl; |
---|
157 | * cout << "ModTime\t" << modtime << " --> " << stats[3] << endl; |
---|
158 | */ |
---|
159 | |
---|
160 | Bool_t check = // Id skipped since it changes if the same file is used on |
---|
161 | // different machined that share the same disk |
---|
162 | // ( id == stats[0] ) && |
---|
163 | //FIXME: disabled due to a bug in GetPathInfo in root 4.00.04 |
---|
164 | ( size == stats[1] ) && |
---|
165 | ( flags == stats[2] ) && |
---|
166 | ( modtime == stats[3] ); |
---|
167 | return check; |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | //______________________________________________________________________________ |
---|
173 | void EusoMapping::LoadAscii(){ |
---|
174 | // read raw data from the final file of map |
---|
175 | |
---|
176 | |
---|
177 | Int_t n_pixels, chid, lastchid; |
---|
178 | |
---|
179 | //firstpixel is chid=1 |
---|
180 | chid = lastchid = 0; |
---|
181 | |
---|
182 | NumbersFileParser nf(fMapFile.c_str(), 5, NumbersFileParser::gzip); |
---|
183 | |
---|
184 | n_pixels = nf.GetCol(0).size(); |
---|
185 | |
---|
186 | Pixel p; |
---|
187 | for(Int_t i(0); i < n_pixels; i++){ |
---|
188 | // build new pixel |
---|
189 | |
---|
190 | vector<Double_t> row = nf.GetRow(i); |
---|
191 | chid = (Int_t)row[0]; |
---|
192 | |
---|
193 | if(chid!=lastchid+1) |
---|
194 | cout<< Form("Warning: pixel number %d missing!",lastchid+1)<<endl; |
---|
195 | |
---|
196 | lastchid = chid; |
---|
197 | |
---|
198 | // store values in EPixel |
---|
199 | p.fThetaFOV = row[1]; |
---|
200 | p.fSigmaThetaFOV = row[2]; |
---|
201 | p.fPhiFOV = row[3]; |
---|
202 | p.fSigmaPhiFOV = row[4]; |
---|
203 | |
---|
204 | fAnglePixelMap.push_back(p); |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | //______________________________________________________________________________ |
---|
210 | void EusoMapping::SaveBinary() { |
---|
211 | |
---|
212 | string dummy = fMapCacheFile; |
---|
213 | cout << "Saving cache map to " << dummy << endl; |
---|
214 | fstream fcache(dummy.c_str(), ios::out | ios::binary | ios::trunc); |
---|
215 | |
---|
216 | // get stats of fMapFile to save into fMapCacheFile |
---|
217 | Long_t stats[4]; |
---|
218 | Long64_t lsize; |
---|
219 | |
---|
220 | if ( gSystem->GetPathInfo(fMapFile.c_str(), stats, |
---|
221 | &lsize, stats+2, stats+3) == 1 ) |
---|
222 | throw runtime_error("EusoMapping: Unable to get stats for "+fMapFile); |
---|
223 | |
---|
224 | stats[1] = (Long_t)lsize; |
---|
225 | // save stats as first info in bin file |
---|
226 | fcache.write((char*)&stats,sizeof(Long_t)*4); |
---|
227 | |
---|
228 | // get number of pixels and put it into the bin file |
---|
229 | Long_t npixel = (Long_t)fAnglePixelMap.size(); |
---|
230 | fcache.write((char*)&npixel,sizeof(Float_t)); |
---|
231 | |
---|
232 | Float_t buffer[4]; |
---|
233 | |
---|
234 | for(Long_t i(0); i < npixel; i++){ |
---|
235 | buffer[0] = fAnglePixelMap[i].fThetaFOV; |
---|
236 | buffer[1] = fAnglePixelMap[i].fSigmaThetaFOV; |
---|
237 | buffer[2] = fAnglePixelMap[i].fPhiFOV; |
---|
238 | buffer[3] = fAnglePixelMap[i].fSigmaPhiFOV; |
---|
239 | fcache.write((char*)buffer,sizeof(Float_t)*4); |
---|
240 | } |
---|
241 | fcache.close(); |
---|
242 | } |
---|
243 | |
---|
244 | //______________________________________________________________________________ |
---|
245 | void EusoMapping::LoadBinary() { |
---|
246 | |
---|
247 | fstream fcache(fMapCacheFile.c_str(), ios::in | ios::binary); |
---|
248 | |
---|
249 | // extract the stats |
---|
250 | Long_t stats[4]; |
---|
251 | fcache.read((char*)&stats,sizeof(Long_t)*4); |
---|
252 | |
---|
253 | // get the number of pixels |
---|
254 | Long_t npixels; |
---|
255 | fcache.read((char*)&npixels,sizeof(Long_t)); |
---|
256 | |
---|
257 | Float_t buffer[4]; |
---|
258 | |
---|
259 | Pixel p; |
---|
260 | for(Int_t i(0); i < npixels; i++){ |
---|
261 | // build new pixel |
---|
262 | |
---|
263 | // get data |
---|
264 | fcache.read((char*)buffer,sizeof(Float_t)*4); |
---|
265 | |
---|
266 | p.fThetaFOV = buffer[0]; |
---|
267 | p.fSigmaThetaFOV = buffer[1]; |
---|
268 | p.fPhiFOV = buffer[2]; |
---|
269 | p.fSigmaPhiFOV = buffer[3]; |
---|
270 | |
---|
271 | fAnglePixelMap.push_back(p); |
---|
272 | } |
---|
273 | fcache.close(); |
---|
274 | } |
---|