source: Sophya/trunk/Cosmo/SimLSS/cmvconcherr.cc@ 3282

Last change on this file since 3282 was 3282, checked in by cmv, 18 years ago

calcul covariance pour une ligne Kz ou Kt 2D cmv 19/07/2007

File size: 10.2 KB
RevLine 
[3141]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"
[3150]11#include "hist2err.h"
12#include "fitshisterr.h"
[3141]13
[3150]14int ObjectType(string nameh,string nameppf);
[3282]15int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp=2,bool do_cov=false);
16int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp=2,int ibinkt=-1,int ibinkz=-1);
[3141]17void usage(void);
18void usage(void)
19{
[3150]20cout
21<<"cmvconcherr -w wtyp -n name_histoerr file1.ppf file2.ppf ..."<<endl
22<<" name_histoerr: object name"<<endl
[3193]23<<" wtyp: bit0 write ASCII, bit1 write PPF, bit2 write FITS (all=7)"<<endl
[3282]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;
[3141]28}
29
[3150]30
31//---------------------------------------------------------------
[3141]32int main(int narg,char *arg[])
33{
[3150]34 // "hpkgen" "hpkgenf" "hpkrec" "hpkgen2" "hpkgenf2" "hpkrec2"
35 string nameh = "";
[3141]36 vector<string> ppfname;
[3282]37 int Write_Type = 2;
[3193]38 bool Do_Cov = false;
[3282]39 int ibinKt=-1, ibinKz=-1;
[3141]40
41 // --- Decodage arguments
42 char c;
[3282]43 while((c = getopt(narg,arg,"hCn:w:Z:T:")) != -1) {
[3141]44 switch (c) {
45 case 'n' :
46 nameh = optarg;
47 break;
[3150]48 case 'w' :
49 sscanf(optarg,"%d",&Write_Type);
50 break;
[3193]51 case 'C' :
52 Do_Cov = true;
53 break;
[3282]54 case 'Z' :
55 sscanf(optarg,"%d",&ibinKt);
56 break;
57 case 'T' :
58 sscanf(optarg,"%d",&ibinKz);
59 break;
[3141]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]);
[3150]67 cout<<"nameh = "<<nameh
68 <<" , number of file to be read = "<<ppfname.size()
[3246]69 <<" , write_type="<<Write_Type<<" , cov="<<Do_Cov<<endl;
[3141]70
[3150]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;
[3193]76 if(obtyp==1) nread = ConcatHistoErr(nameh,ppfname,Write_Type,Do_Cov);
[3282]77 else if(obtyp==2) nread = ConcatHisto2DErr(nameh,ppfname,Write_Type,ibinKt,ibinKz);
[3150]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//---------------------------------------------------------------
87int 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//---------------------------------------------------------------
[3193]112int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov)
[3150]113{
114 if(ppfname.size()<=0) return 0;
115
[3141]116 HistoErr *herrconc = NULL;
[3193]117 TVector<r_8> Tsum; TMatrix<r_8> Tsum2;
[3150]118 int nherr=0, nread=0, itest=0;
[3141]119 double sum=0., sum2=0., nsum=0;
[3282]120 for (unsigned int ifile=0;ifile<ppfname.size();ifile++) {
[3141]121 PInPersist pis(ppfname[ifile].c_str());
122 HistoErr herr;
123 pis.GetObject(herr,nameh);
[3150]124 cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="<<herr.NBins()<<endl;
[3282]125
[3141]126 if(ifile==0) {
127 herrconc = new HistoErr(herr.XMin(),herr.XMax(),herr.NBins());
[3150]128 nherr = herr.NBins();
[3141]129 itest = int(herrconc->NBins()/5.+0.5);
[3193]130 if(do_cov) {Tsum.ReSize(nherr); Tsum=0.; Tsum2.ReSize(nherr,nherr); Tsum2=0.;}
[3150]131 } else if(nherr!=herr.NBins()) {
132 cout<<"BAD NUMBER OF BINS"<<endl;
133 continue;
[3141]134 }
[3282]135
[3150]136 if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
[3282]137 <<" nmean="<<herr.NMean()<<endl;
[3150]138 nread++;
[3141]139 for(int i=0;i<nherr;i++) herrconc->AddBin(i,herr(i));
[3154]140 sum+=herr(itest); sum2+=herr(itest)*herr(itest); nsum++;
[3193]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
[3141]149 }
[3150]150 herrconc->ToVariance();
[3141]151 if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
152 cout<<"Test bin "<<itest<<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
[3150]153 cout<<" "<<" mean="<<(*herrconc)(itest)
154 <<" sigma^2="<<herrconc->Error2(itest)<<endl;
[3141]155
[3193]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
[3150]164 // --- ecriture
165 if(wrtyp&1) { // ecriture ASCII
[3184]166 string asname = "cmvconcherr.data";
167 herrconc->WriteASCII(asname);
[3141]168 }
[3193]169 if(wrtyp&2 || do_cov) { // ecriture PPF
[3150]170 string tagobs = "cmvconcherr.ppf";
171 POutPersist posobs(tagobs);
172 tagobs = "herrconc"; posobs.PutObject(*herrconc,tagobs);
[3193]173 if(do_cov) {
174 tagobs = "mean"; posobs.PutObject(Tsum,tagobs);
175 tagobs = "cov"; posobs.PutObject(Tsum2,tagobs);
176 }
[3150]177 }
178 if(wrtyp&4) { // ecriture FITS
179 FitsInOutFile fio("!cmvconcherr.fits",FitsInOutFile::Fits_Create);
180 fio << *herrconc;
181 }
[3141]182
[3150]183 delete herrconc;
[3141]184
[3150]185 return nread;
186}
187
188//---------------------------------------------------------------
[3282]189int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp,int ibinkt,int ibinkz)
[3150]190{
191 if(ppfname.size()<=0) return 0;
192
193 Histo2DErr *herrconc = NULL;
[3282]194 TVector<r_8> Tsum_kz; TMatrix<r_8> Tsum2_kz;
195 TVector<r_8> Tsum_kt; TMatrix<r_8> Tsum2_kt;
[3150]196 int nherrx=0, nherry=0, nread=0, itestx=0, itesty=0;
197 double sum=0., sum2=0., nsum=0;
[3282]198 for (unsigned int ifile=0;ifile<ppfname.size();ifile++) {
[3150]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;
[3282]204
[3150]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);
[3282]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 }
[3150]221 } else if(nherrx!=herr.NBinX() || nherry!=herr.NBinY()) {
222 cout<<"BAD NUMBER OF BINS"<<endl;
223 continue;
224 }
[3282]225
[3150]226 if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
[3282]227 <<" nmean="<<herr.NMean()<<endl;
[3150]228 nread++;
229 for(int i=0;i<nherrx;i++)
[3154]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++;
[3282]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
[3150]247 }
248 herrconc->ToVariance();
[3154]249 if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
[3150]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
[3282]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
[3150]269 // --- ecriture
270 if(wrtyp&1) { // ecriture ASCII
[3184]271 string asname = "cmvconcherr2.data";
272 herrconc->WriteASCII(asname);
[3150]273 }
274 if(wrtyp&2) { // ecriture PPF
275 string tagobs = "cmvconcherr2.ppf";
276 POutPersist posobs(tagobs);
277 tagobs = "herrconc2"; posobs.PutObject(*herrconc,tagobs);
[3282]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 }
[3150]292 }
293 if(wrtyp&4) { // ecriture FITS
294 FitsInOutFile fio("!cmvconcherr2.fits",FitsInOutFile::Fits_Create);
295 fio << *herrconc;
296 }
297
[3141]298 delete herrconc;
299
[3150]300 return nread;
[3141]301}
302
303/*
[3282]304####
[3150]305#### HistoErr 1D
[3282]306####
[3150]307openppf cmvconcherr.ppf
[3141]308
[3154]309zone 2 2
[3150]310disp herrconc "hbincont err"
311disp herrconc "hbinerr"
312disp herrconc "hbinent"
[3141]313
[3154]314zone
[3141]315n/plot herrconc.val%log10(x) x>0 ! "connectpoints"
[3154]316n/plot herrconc.sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
[3141]317
[3154]318n/plot herrconc.sqrt(err2)/val%log10(x) x>0&&err2>0&&val>0 ! "connectpoints"
[3150]319
[3193]320disp mean
321imag cov
322
323del cor
324c++exec \
325TMatrix<r_8> cor(cov,false); cor = 0.; \
326for(int i=0;i<cor.NRows();i++) { \
327 for(int j=0;j<cor.NCols();j++) { \
328 double v = cov(i,i)*cov(j,j); \
329 if(v<=0.) continue; \
330 cor(i,j) = cov(i,j)/sqrt(v); \
331} } \
332KeepObj(cor); \
333cout<<"Matrice cor cree "<<endl;
334
335imag cor
336
[3282]337####
[3150]338#### Histo2DErr 2D
[3282]339####
[3150]340openppf cmvconcherr2.ppf
341
[3154]342zone 2 2
343imag herrconc2 "hbincont"
344imag herrconc2 "hbinerr"
345imag herrconc2 "hbinent"
[3282]346
347zone
348
349set mean kzm140
350set cov kzc140
351
352set mean ktm32
353set cov ktc32
354
355n/plot $mean.val%n ! ! "connectpoints"
356disp $cov
357n/plot $cov.val%c r==32 ! "connectpoints"
358
359set cor ${cov}cor
360del $cor
361c++exec \
362TMatrix<r_8> $cor($cov,false); $cor = 0.; \
363for(int i=0;i<$cor.NRows();i++) { \
364 for(int j=0;j<$cor.NCols();j++) { \
365 double v = $cov(i,i)*$cov(j,j); \
366 if(v<=0.) continue; \
367 $cor(i,j) = $cov(i,j)/sqrt(v); \
368} } \
369KeepObj($cor); \
370cout<<"Matrice cor cree "<<endl;
371
372disp $cor
373n/plot $cor.val%c r==32 ! "connectpoints"
[3141]374*/
Note: See TracBrowser for help on using the repository browser.