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

Last change on this file since 3200 was 3193, checked in by cmv, 19 years ago

petis changements cmv 21/03/2007

File size: 7.6 KB
Line 
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
14int ObjectType(string nameh,string nameppf);
15int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov);
16int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp);
17void usage(void);
18void usage(void)
19{
20cout
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}
26
27
28//---------------------------------------------------------------
29int main(int narg,char *arg[])
30{
31 // "hpkgen" "hpkgenf" "hpkrec" "hpkgen2" "hpkgenf2" "hpkrec2"
32 string nameh = "";
33 vector<string> ppfname;
34 int Write_Type = 3;
35 bool Do_Cov = false;
36
37 // --- Decodage arguments
38 char c;
39 while((c = getopt(narg,arg,"hCn:w:")) != -1) {
40 switch (c) {
41 case 'n' :
42 nameh = optarg;
43 break;
44 case 'w' :
45 sscanf(optarg,"%d",&Write_Type);
46 break;
47 case 'C' :
48 Do_Cov = true;
49 break;
50 case 'h' :
51 default :
52 usage(); return -1;
53 }
54 }
55 if(nameh.size()<=0 || optind>=narg) {usage(); return -1;}
56 for (int i=optind;i<narg;i++) ppfname.push_back(arg[i]);
57 cout<<"nameh = "<<nameh
58 <<" , number of file to be read = "<<ppfname.size()
59 <<" , write_type="<<Write_Type<<endl;
60
61 // --- Quel type d'objet ?
62 int obtyp = ObjectType(nameh,ppfname[0]);
63
64 // --- lecture et moyenne des HistoErr ou Histo2DErr
65 int nread=0;
66 if(obtyp==1) nread = ConcatHistoErr(nameh,ppfname,Write_Type,Do_Cov);
67 else if(obtyp==2) nread = ConcatHisto2DErr(nameh,ppfname,Write_Type);
68 else {
69 cout<<"Type of object "<<nameh<<" not decoded"<<endl;
70 return -1;
71 }
72 cout<<"Number of file read: "<<nread<<endl;
73 return 0;
74}
75
76//---------------------------------------------------------------
77int ObjectType(string nameh,string nameppf)
78// retourne: 1 si HistoErr
79// 2 si Histo2DErr,
80// 0 si autre objet
81// -1 si il n'y a pas d'objet de nom "nameh"
82{
83 PInPersist pis(nameppf.c_str());
84
85 bool found_tag = pis.GotoNameTag(nameh.c_str());
86 if(!found_tag) return -1;
87
88 PPersist *pps = pis.ReadObject();
89 AnyDataObj *obj = pps->DataObj();
90 cout<<"Object type read from input PPF file : "<<typeid(*obj).name()<< endl;
91
92 HistoErr *herr = dynamic_cast<HistoErr *>(obj);
93 if(herr) return 1;
94
95 Histo2DErr *herr2 = dynamic_cast<Histo2DErr *>(obj);
96 if(herr2) return 2;
97
98 return 0;
99}
100
101//---------------------------------------------------------------
102int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov)
103{
104 if(ppfname.size()<=0) return 0;
105
106 HistoErr *herrconc = NULL;
107 TVector<r_8> Tsum; TMatrix<r_8> Tsum2;
108 int nherr=0, nread=0, itest=0;
109 double sum=0., sum2=0., nsum=0;
110 for (int ifile=0;ifile<ppfname.size();ifile++) {
111 PInPersist pis(ppfname[ifile].c_str());
112 HistoErr herr;
113 pis.GetObject(herr,nameh);
114 cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="<<herr.NBins()<<endl;
115 if(ifile==0) {
116 herrconc = new HistoErr(herr.XMin(),herr.XMax(),herr.NBins());
117 nherr = herr.NBins();
118 itest = int(herrconc->NBins()/5.+0.5);
119 if(do_cov) {Tsum.ReSize(nherr); Tsum=0.; Tsum2.ReSize(nherr,nherr); Tsum2=0.;}
120 } else if(nherr!=herr.NBins()) {
121 cout<<"BAD NUMBER OF BINS"<<endl;
122 continue;
123 }
124 if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
125 <<" nmean="<<herr.NMean()<<endl;
126 nread++;
127 for(int i=0;i<nherr;i++) herrconc->AddBin(i,herr(i));
128 sum+=herr(itest); sum2+=herr(itest)*herr(itest); nsum++;
129
130 if(do_cov) {
131 for(int i=0;i<nherr;i++) {
132 Tsum(i) += herr(i);
133 for(int j=0;j<nherr;j++) Tsum2(i,j) += herr(i)*herr(j);
134 }
135 }
136
137 }
138 herrconc->ToVariance();
139 if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
140 cout<<"Test bin "<<itest<<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
141 cout<<" "<<" mean="<<(*herrconc)(itest)
142 <<" sigma^2="<<herrconc->Error2(itest)<<endl;
143
144 // --- Covariance
145 if(do_cov && nread>1) {
146 Tsum /= nread;
147 Tsum2 /= nread;
148 for(int i=0;i<nherr;i++)
149 for(int j=0;j<nherr;j++) Tsum2(i,j) -= Tsum(i)*Tsum(j);
150 }
151
152 // --- ecriture
153 if(wrtyp&1) { // ecriture ASCII
154 string asname = "cmvconcherr.data";
155 herrconc->WriteASCII(asname);
156 }
157 if(wrtyp&2 || do_cov) { // ecriture PPF
158 string tagobs = "cmvconcherr.ppf";
159 POutPersist posobs(tagobs);
160 tagobs = "herrconc"; posobs.PutObject(*herrconc,tagobs);
161 if(do_cov) {
162 tagobs = "mean"; posobs.PutObject(Tsum,tagobs);
163 tagobs = "cov"; posobs.PutObject(Tsum2,tagobs);
164 }
165 }
166 if(wrtyp&4) { // ecriture FITS
167 FitsInOutFile fio("!cmvconcherr.fits",FitsInOutFile::Fits_Create);
168 fio << *herrconc;
169 }
170
171 delete herrconc;
172
173 return nread;
174}
175
176//---------------------------------------------------------------
177int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp)
178{
179 if(ppfname.size()<=0) return 0;
180
181 Histo2DErr *herrconc = NULL;
182 int nherrx=0, nherry=0, nread=0, itestx=0, itesty=0;
183 double sum=0., sum2=0., nsum=0;
184 for (int ifile=0;ifile<ppfname.size();ifile++) {
185 PInPersist pis(ppfname[ifile].c_str());
186 Histo2DErr herr;
187 pis.GetObject(herr,nameh);
188 cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="
189 <<herr.NBinX()<<","<<herr.NBinY()<<endl;
190 if(ifile==0) {
191 herrconc = new Histo2DErr(herr.XMin(),herr.XMax(),herr.NBinX()
192 ,herr.YMin(),herr.YMax(),herr.NBinY());
193 nherrx = herr.NBinX(); nherry = herr.NBinY();
194 itestx = int(herrconc->NBinX()/5.+0.5);
195 itesty = int(herrconc->NBinY()/5.+0.5);
196 } else if(nherrx!=herr.NBinX() || nherry!=herr.NBinY()) {
197 cout<<"BAD NUMBER OF BINS"<<endl;
198 continue;
199 }
200 if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
201 <<" nmean="<<herr.NMean()<<endl;
202 nread++;
203 for(int i=0;i<nherrx;i++)
204 for(int j=0;j<nherry;j++) herrconc->AddBin(i,j,herr(i,j));
205 sum+=herr(itestx,itesty); sum2+=herr(itestx,itesty)*herr(itestx,itesty); nsum++;
206 }
207 herrconc->ToVariance();
208 if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
209 cout<<"Test bin "<<itestx<<","<<itesty
210 <<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
211 cout<<" "<<" mean="<<(*herrconc)(itestx,itesty)
212 <<" sigma^2="<<herrconc->Error2(itestx,itesty)<<endl;
213
214 // --- ecriture
215 if(wrtyp&1) { // ecriture ASCII
216 string asname = "cmvconcherr2.data";
217 herrconc->WriteASCII(asname);
218 }
219 if(wrtyp&2) { // ecriture PPF
220 string tagobs = "cmvconcherr2.ppf";
221 POutPersist posobs(tagobs);
222 tagobs = "herrconc2"; posobs.PutObject(*herrconc,tagobs);
223 }
224 if(wrtyp&4) { // ecriture FITS
225 FitsInOutFile fio("!cmvconcherr2.fits",FitsInOutFile::Fits_Create);
226 fio << *herrconc;
227 }
228
229 delete herrconc;
230
231 return nread;
232}
233
234/*
235#### HistoErr 1D
236openppf cmvconcherr.ppf
237
238zone 2 2
239disp herrconc "hbincont err"
240disp herrconc "hbinerr"
241disp herrconc "hbinent"
242
243zone
244n/plot herrconc.val%log10(x) x>0 ! "connectpoints"
245n/plot herrconc.sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
246
247n/plot herrconc.sqrt(err2)/val%log10(x) x>0&&err2>0&&val>0 ! "connectpoints"
248
249disp mean
250imag cov
251
252del cor
253c++exec \
254TMatrix<r_8> cor(cov,false); cor = 0.; \
255for(int i=0;i<cor.NRows();i++) { \
256 for(int j=0;j<cor.NCols();j++) { \
257 double v = cov(i,i)*cov(j,j); \
258 if(v<=0.) continue; \
259 cor(i,j) = cov(i,j)/sqrt(v); \
260} } \
261KeepObj(cor); \
262cout<<"Matrice cor cree "<<endl;
263
264imag cor
265
266#### Histo2DErr 2D
267openppf cmvconcherr2.ppf
268
269zone 2 2
270imag herrconc2 "hbincont"
271imag herrconc2 "hbinerr"
272imag herrconc2 "hbinent"
273*/
Note: See TracBrowser for help on using the repository browser.