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

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

write Histo2DErr into ASCII file cmv 13/02/2007

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