1 | #include "sopnamsp.h"
|
---|
2 | #include "machdefs.h"
|
---|
3 | #include <iostream>
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <math.h>
|
---|
8 | #include <unistd.h>
|
---|
9 | #include "timing.h"
|
---|
10 | #include "histerr.h"
|
---|
11 | #include "hist2err.h"
|
---|
12 | #include "fitshisterr.h"
|
---|
13 |
|
---|
14 | int ObjectType(string nameh,string nameppf);
|
---|
15 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp=2,bool do_cov=false);
|
---|
16 | int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp=2,int ibinkt=-1,int ibinkz=-1);
|
---|
17 | void usage(void);
|
---|
18 | void usage(void)
|
---|
19 | {
|
---|
20 | cout
|
---|
21 | <<"cmvconcherr -w wtyp -n name_histoerr file1.ppf file2.ppf ..."<<endl
|
---|
22 | <<" name_histoerr: object name"<<endl
|
---|
23 | <<" wtyp: bit0 write ASCII, bit1 write PPF, bit2 write FITS (all=7)"<<endl
|
---|
24 | <<" -C : compute covariance for 1D spectrum"<<endl
|
---|
25 | <<" -Z ibinKt : compute covariance of line Pk(ibinKt,.) along Kz for 2D"<<endl
|
---|
26 | <<" -T ibinKz : compute covariance of line Pk(.,ibinKz) along Kt for 2D"<<endl
|
---|
27 | <<endl;
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | //---------------------------------------------------------------
|
---|
32 | int main(int narg,char *arg[])
|
---|
33 | {
|
---|
34 | // "hpkgen" "hpkgenf" "hpkrec" "hpkgen2" "hpkgenf2" "hpkrec2"
|
---|
35 | string nameh = "";
|
---|
36 | vector<string> ppfname;
|
---|
37 | int Write_Type = 2;
|
---|
38 | bool Do_Cov = false;
|
---|
39 | int ibinKt=-1, ibinKz=-1;
|
---|
40 |
|
---|
41 | // --- Decodage arguments
|
---|
42 | char c;
|
---|
43 | while((c = getopt(narg,arg,"hCn:w:Z:T:")) != -1) {
|
---|
44 | switch (c) {
|
---|
45 | case 'n' :
|
---|
46 | nameh = optarg;
|
---|
47 | break;
|
---|
48 | case 'w' :
|
---|
49 | sscanf(optarg,"%d",&Write_Type);
|
---|
50 | break;
|
---|
51 | case 'C' :
|
---|
52 | Do_Cov = true;
|
---|
53 | break;
|
---|
54 | case 'Z' :
|
---|
55 | sscanf(optarg,"%d",&ibinKt);
|
---|
56 | break;
|
---|
57 | case 'T' :
|
---|
58 | sscanf(optarg,"%d",&ibinKz);
|
---|
59 | break;
|
---|
60 | case 'h' :
|
---|
61 | default :
|
---|
62 | usage(); return -1;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | if(nameh.size()<=0 || optind>=narg) {usage(); return -1;}
|
---|
66 | for (int i=optind;i<narg;i++) ppfname.push_back(arg[i]);
|
---|
67 | cout<<"nameh = "<<nameh
|
---|
68 | <<" , number of file to be read = "<<ppfname.size()
|
---|
69 | <<" , write_type="<<Write_Type<<" , cov="<<Do_Cov<<endl;
|
---|
70 |
|
---|
71 | // --- Quel type d'objet ?
|
---|
72 | int obtyp = ObjectType(nameh,ppfname[0]);
|
---|
73 |
|
---|
74 | // --- lecture et moyenne des HistoErr ou Histo2DErr
|
---|
75 | int nread=0;
|
---|
76 | if(obtyp==1) nread = ConcatHistoErr(nameh,ppfname,Write_Type,Do_Cov);
|
---|
77 | else if(obtyp==2) nread = ConcatHisto2DErr(nameh,ppfname,Write_Type,ibinKt,ibinKz);
|
---|
78 | else {
|
---|
79 | cout<<"Type of object "<<nameh<<" not decoded"<<endl;
|
---|
80 | return -1;
|
---|
81 | }
|
---|
82 | cout<<"Number of file read: "<<nread<<endl;
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 | //---------------------------------------------------------------
|
---|
87 | int ObjectType(string nameh,string nameppf)
|
---|
88 | // retourne: 1 si HistoErr
|
---|
89 | // 2 si Histo2DErr,
|
---|
90 | // 0 si autre objet
|
---|
91 | // -1 si il n'y a pas d'objet de nom "nameh"
|
---|
92 | {
|
---|
93 | PInPersist pis(nameppf.c_str());
|
---|
94 |
|
---|
95 | bool found_tag = pis.GotoNameTag(nameh.c_str());
|
---|
96 | if(!found_tag) return -1;
|
---|
97 |
|
---|
98 | PPersist *pps = pis.ReadObject();
|
---|
99 | AnyDataObj *obj = pps->DataObj();
|
---|
100 | cout<<"Object type read from input PPF file : "<<typeid(*obj).name()<< endl;
|
---|
101 |
|
---|
102 | HistoErr *herr = dynamic_cast<HistoErr *>(obj);
|
---|
103 | if(herr) return 1;
|
---|
104 |
|
---|
105 | Histo2DErr *herr2 = dynamic_cast<Histo2DErr *>(obj);
|
---|
106 | if(herr2) return 2;
|
---|
107 |
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | //---------------------------------------------------------------
|
---|
112 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov)
|
---|
113 | {
|
---|
114 | if(ppfname.size()<=0) return 0;
|
---|
115 |
|
---|
116 | HistoErr *herrconc = NULL;
|
---|
117 | TVector<r_8> Tsum; TMatrix<r_8> Tsum2;
|
---|
118 | int nherr=0, nread=0, itest=0;
|
---|
119 | double sum=0., sum2=0., nsum=0;
|
---|
120 | for (unsigned int ifile=0;ifile<ppfname.size();ifile++) {
|
---|
121 | PInPersist pis(ppfname[ifile].c_str());
|
---|
122 | HistoErr herr;
|
---|
123 | pis.GetObject(herr,nameh);
|
---|
124 | cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="<<herr.NBins()<<endl;
|
---|
125 |
|
---|
126 | if(ifile==0) {
|
---|
127 | herrconc = new HistoErr(herr.XMin(),herr.XMax(),herr.NBins());
|
---|
128 | nherr = herr.NBins();
|
---|
129 | itest = int(herrconc->NBins()/5.+0.5);
|
---|
130 | if(do_cov) {Tsum.ReSize(nherr); Tsum=0.; Tsum2.ReSize(nherr,nherr); Tsum2=0.;}
|
---|
131 | } else if(nherr!=herr.NBins()) {
|
---|
132 | cout<<"BAD NUMBER OF BINS"<<endl;
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
|
---|
137 | <<" nmean="<<herr.NMean()<<endl;
|
---|
138 | nread++;
|
---|
139 | for(int i=0;i<nherr;i++) herrconc->AddBin(i,herr(i));
|
---|
140 | sum+=herr(itest); sum2+=herr(itest)*herr(itest); nsum++;
|
---|
141 |
|
---|
142 | if(do_cov) {
|
---|
143 | for(int i=0;i<nherr;i++) {
|
---|
144 | Tsum(i) += herr(i);
|
---|
145 | for(int j=0;j<nherr;j++) Tsum2(i,j) += herr(i)*herr(j);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 | herrconc->ToVariance();
|
---|
151 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
152 | cout<<"Test bin "<<itest<<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
|
---|
153 | cout<<" "<<" mean="<<(*herrconc)(itest)
|
---|
154 | <<" sigma^2="<<herrconc->Error2(itest)<<endl;
|
---|
155 |
|
---|
156 | // --- Covariance
|
---|
157 | if(do_cov && nread>1) {
|
---|
158 | Tsum /= nread;
|
---|
159 | Tsum2 /= nread;
|
---|
160 | for(int i=0;i<nherr;i++)
|
---|
161 | for(int j=0;j<nherr;j++) Tsum2(i,j) -= Tsum(i)*Tsum(j);
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --- ecriture
|
---|
165 | if(wrtyp&1) { // ecriture ASCII
|
---|
166 | string asname = "cmvconcherr.data";
|
---|
167 | herrconc->WriteASCII(asname);
|
---|
168 | }
|
---|
169 | if(wrtyp&2 || do_cov) { // ecriture PPF
|
---|
170 | string tagobs = "cmvconcherr.ppf";
|
---|
171 | POutPersist posobs(tagobs);
|
---|
172 | tagobs = "herrconc"; posobs.PutObject(*herrconc,tagobs);
|
---|
173 | if(do_cov) {
|
---|
174 | tagobs = "mean"; posobs.PutObject(Tsum,tagobs);
|
---|
175 | tagobs = "cov"; posobs.PutObject(Tsum2,tagobs);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | if(wrtyp&4) { // ecriture FITS
|
---|
179 | FitsInOutFile fio("!cmvconcherr.fits",FitsInOutFile::Fits_Create);
|
---|
180 | fio << *herrconc;
|
---|
181 | }
|
---|
182 |
|
---|
183 | delete herrconc;
|
---|
184 |
|
---|
185 | return nread;
|
---|
186 | }
|
---|
187 |
|
---|
188 | //---------------------------------------------------------------
|
---|
189 | int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp,int ibinkt,int ibinkz)
|
---|
190 | {
|
---|
191 | if(ppfname.size()<=0) return 0;
|
---|
192 |
|
---|
193 | Histo2DErr *herrconc = NULL;
|
---|
194 | TVector<r_8> Tsum_kz; TMatrix<r_8> Tsum2_kz;
|
---|
195 | TVector<r_8> Tsum_kt; TMatrix<r_8> Tsum2_kt;
|
---|
196 | int nherrx=0, nherry=0, nread=0, itestx=0, itesty=0;
|
---|
197 | double sum=0., sum2=0., nsum=0;
|
---|
198 | for (unsigned int ifile=0;ifile<ppfname.size();ifile++) {
|
---|
199 | PInPersist pis(ppfname[ifile].c_str());
|
---|
200 | Histo2DErr herr;
|
---|
201 | pis.GetObject(herr,nameh);
|
---|
202 | cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="
|
---|
203 | <<herr.NBinX()<<","<<herr.NBinY()<<endl;
|
---|
204 |
|
---|
205 | if(ifile==0) {
|
---|
206 | herrconc = new Histo2DErr(herr.XMin(),herr.XMax(),herr.NBinX()
|
---|
207 | ,herr.YMin(),herr.YMax(),herr.NBinY());
|
---|
208 | nherrx = herr.NBinX(); nherry = herr.NBinY();
|
---|
209 | itestx = int(herrconc->NBinX()/5.+0.5);
|
---|
210 | itesty = int(herrconc->NBinY()/5.+0.5);
|
---|
211 | if(ibinkt>=0) { // matrix de covariance pour une ligne Kz
|
---|
212 | if(ibinkt>=nherrx) ibinkt = nherrx-1;
|
---|
213 | Tsum_kz.ReSize(nherry); Tsum_kz=0.;
|
---|
214 | Tsum2_kz.ReSize(nherry,nherry); Tsum2_kz=0.;
|
---|
215 | }
|
---|
216 | if(ibinkz>=0) { // matrix de covariance pour une ligne Kt
|
---|
217 | if(ibinkz>=nherry) ibinkz = nherry-1;
|
---|
218 | Tsum_kt.ReSize(nherrx); Tsum_kt=0.;
|
---|
219 | Tsum2_kt.ReSize(nherrx,nherrx); Tsum2_kt=0.;
|
---|
220 | }
|
---|
221 | } else if(nherrx!=herr.NBinX() || nherry!=herr.NBinY()) {
|
---|
222 | cout<<"BAD NUMBER OF BINS"<<endl;
|
---|
223 | continue;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
|
---|
227 | <<" nmean="<<herr.NMean()<<endl;
|
---|
228 | nread++;
|
---|
229 | for(int i=0;i<nherrx;i++)
|
---|
230 | for(int j=0;j<nherry;j++) herrconc->AddBin(i,j,herr(i,j));
|
---|
231 | sum+=herr(itestx,itesty); sum2+=herr(itestx,itesty)*herr(itestx,itesty); nsum++;
|
---|
232 |
|
---|
233 | if(ibinkt>=0) {
|
---|
234 | for(int i=0;i<Tsum_kz.Size();i++) {
|
---|
235 | Tsum_kz(i) += herr(ibinkt,i);
|
---|
236 | for(int j=0;j<Tsum_kz.Size();j++) Tsum2_kz(i,j) += herr(ibinkt,i)*herr(ibinkt,j);
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if(ibinkz>=0) {
|
---|
241 | for(int i=0;i<Tsum_kt.Size();i++) {
|
---|
242 | Tsum_kt(i) += herr(i,ibinkz);
|
---|
243 | for(int j=0;j<Tsum_kt.Size();j++) Tsum2_kt(i,j) += herr(i,ibinkz)*herr(j,ibinkz);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|
248 | herrconc->ToVariance();
|
---|
249 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
250 | cout<<"Test bin "<<itestx<<","<<itesty
|
---|
251 | <<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
|
---|
252 | cout<<" "<<" mean="<<(*herrconc)(itestx,itesty)
|
---|
253 | <<" sigma^2="<<herrconc->Error2(itestx,itesty)<<endl;
|
---|
254 |
|
---|
255 | // --- Covariance
|
---|
256 | if(ibinkt>=0 && nread>1) {
|
---|
257 | Tsum_kz /= nread;
|
---|
258 | Tsum2_kz /= nread;
|
---|
259 | for(int i=0;i<Tsum_kz.Size();i++)
|
---|
260 | for(int j=0;j<Tsum_kz.Size();j++) Tsum2_kz(i,j) -= Tsum_kz(i)*Tsum_kz(j);
|
---|
261 | }
|
---|
262 | if(ibinkz>=0 && nread>1) {
|
---|
263 | Tsum_kt /= nread;
|
---|
264 | Tsum2_kt /= nread;
|
---|
265 | for(int i=0;i<Tsum_kt.Size();i++)
|
---|
266 | for(int j=0;j<Tsum_kt.Size();j++) Tsum2_kt(i,j) -= Tsum_kt(i)*Tsum_kt(j);
|
---|
267 | }
|
---|
268 |
|
---|
269 | // --- ecriture
|
---|
270 | if(wrtyp&1) { // ecriture ASCII
|
---|
271 | string asname = "cmvconcherr2.data";
|
---|
272 | herrconc->WriteASCII(asname);
|
---|
273 | }
|
---|
274 | if(wrtyp&2) { // ecriture PPF
|
---|
275 | string tagobs = "cmvconcherr2.ppf";
|
---|
276 | POutPersist posobs(tagobs);
|
---|
277 | tagobs = "herrconc2"; posobs.PutObject(*herrconc,tagobs);
|
---|
278 | if(ibinkt>=0) {
|
---|
279 | char str[32];
|
---|
280 | sprintf(str,"kzm%d",ibinkt);
|
---|
281 | posobs.PutObject(Tsum_kz,str);
|
---|
282 | sprintf(str,"kzc%d",ibinkt);
|
---|
283 | posobs.PutObject(Tsum2_kz,str);
|
---|
284 | }
|
---|
285 | if(ibinkz>=0) {
|
---|
286 | char str[32];
|
---|
287 | sprintf(str,"ktm%d",ibinkz);
|
---|
288 | posobs.PutObject(Tsum_kt,str);
|
---|
289 | sprintf(str,"ktc%d",ibinkz);
|
---|
290 | posobs.PutObject(Tsum2_kt,str);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | if(wrtyp&4) { // ecriture FITS
|
---|
294 | FitsInOutFile fio("!cmvconcherr2.fits",FitsInOutFile::Fits_Create);
|
---|
295 | fio << *herrconc;
|
---|
296 | }
|
---|
297 |
|
---|
298 | delete herrconc;
|
---|
299 |
|
---|
300 | return nread;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /*
|
---|
304 | ####
|
---|
305 | #### HistoErr 1D
|
---|
306 | ####
|
---|
307 | openppf cmvconcherr.ppf
|
---|
308 |
|
---|
309 | c++exec int i = 700; \
|
---|
310 | cout<<i<<" "<<herrconc.BinCenter(i)<<" "<<herrconc(i)<<" "<<herrconc.Error2(i)<<" "<<herrconc.NEntBin(i)<<endl;
|
---|
311 |
|
---|
312 | zone 2 2
|
---|
313 | disp herrconc "hbincont err"
|
---|
314 | disp herrconc "hbinerr"
|
---|
315 | disp herrconc "hbinent"
|
---|
316 |
|
---|
317 | zone
|
---|
318 | n/plot herrconc.val%log10(x) x>0 ! "connectpoints"
|
---|
319 | n/plot herrconc.val-sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
|
---|
320 | n/plot herrconc.val+sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
|
---|
321 |
|
---|
322 | n/plot herrconc.sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
|
---|
323 | n/plot herrconc.sqrt(err2)/val%log10(x) x>0&&err2>0&&val>0 ! "connectpoints"
|
---|
324 |
|
---|
325 | disp mean
|
---|
326 | imag cov
|
---|
327 |
|
---|
328 | del cor
|
---|
329 | c++exec \
|
---|
330 | TMatrix<r_8> cor(cov,false); cor = 0.; \
|
---|
331 | for(int i=0;i<cor.NRows();i++) { \
|
---|
332 | for(int j=0;j<cor.NCols();j++) { \
|
---|
333 | double v = cov(i,i)*cov(j,j); \
|
---|
334 | if(v<=0.) continue; \
|
---|
335 | cor(i,j) = cov(i,j)/sqrt(v); \
|
---|
336 | } } \
|
---|
337 | KeepObj(cor); \
|
---|
338 | cout<<"Matrice cor cree "<<endl;
|
---|
339 |
|
---|
340 | imag cor
|
---|
341 | n/plot cor.val%c r==500 ! "connectpoints"
|
---|
342 |
|
---|
343 | ####
|
---|
344 | #### Histo2DErr 2D
|
---|
345 | ####
|
---|
346 | openppf cmvconcherr2.ppf
|
---|
347 |
|
---|
348 | zone 2 2
|
---|
349 | imag herrconc2 "hbincont"
|
---|
350 | imag herrconc2 "hbinerr"
|
---|
351 | imag herrconc2 "hbinent"
|
---|
352 |
|
---|
353 | zone
|
---|
354 |
|
---|
355 | set mean kzm140
|
---|
356 | set cov kzc140
|
---|
357 |
|
---|
358 | set mean ktm32
|
---|
359 | set cov ktc32
|
---|
360 |
|
---|
361 | n/plot $mean.val%n ! ! "connectpoints"
|
---|
362 | disp $cov
|
---|
363 | n/plot $cov.val%c r==32 ! "connectpoints"
|
---|
364 |
|
---|
365 | set cor ${cov}cor
|
---|
366 | del $cor
|
---|
367 | c++exec \
|
---|
368 | TMatrix<r_8> $cor($cov,false); $cor = 0.; \
|
---|
369 | for(int i=0;i<$cor.NRows();i++) { \
|
---|
370 | for(int j=0;j<$cor.NCols();j++) { \
|
---|
371 | double v = $cov(i,i)*$cov(j,j); \
|
---|
372 | if(v<=0.) continue; \
|
---|
373 | $cor(i,j) = $cov(i,j)/sqrt(v); \
|
---|
374 | } } \
|
---|
375 | KeepObj($cor); \
|
---|
376 | cout<<"Matrice cor cree "<<endl;
|
---|
377 |
|
---|
378 | disp $cor
|
---|
379 | n/plot $cor.val%c r==32 ! "connectpoints"
|
---|
380 | */
|
---|