[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] | 14 | int ObjectType(string nameh,string nameppf);
|
---|
[3193] | 15 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov);
|
---|
[3150] | 16 | int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp);
|
---|
[3141] | 17 | void usage(void);
|
---|
| 18 | void usage(void)
|
---|
| 19 | {
|
---|
[3150] | 20 | cout
|
---|
| 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
|
---|
| 24 | <<" -C : compute covariance for 1D spectrum"<<endl;
|
---|
[3141] | 25 | }
|
---|
| 26 |
|
---|
[3150] | 27 |
|
---|
| 28 | //---------------------------------------------------------------
|
---|
[3141] | 29 | int main(int narg,char *arg[])
|
---|
| 30 | {
|
---|
[3150] | 31 | // "hpkgen" "hpkgenf" "hpkrec" "hpkgen2" "hpkgenf2" "hpkrec2"
|
---|
| 32 | string nameh = "";
|
---|
[3141] | 33 | vector<string> ppfname;
|
---|
[3150] | 34 | int Write_Type = 3;
|
---|
[3193] | 35 | bool Do_Cov = false;
|
---|
[3141] | 36 |
|
---|
| 37 | // --- Decodage arguments
|
---|
| 38 | char c;
|
---|
[3193] | 39 | while((c = getopt(narg,arg,"hCn:w:")) != -1) {
|
---|
[3141] | 40 | switch (c) {
|
---|
| 41 | case 'n' :
|
---|
| 42 | nameh = optarg;
|
---|
| 43 | break;
|
---|
[3150] | 44 | case 'w' :
|
---|
| 45 | sscanf(optarg,"%d",&Write_Type);
|
---|
| 46 | break;
|
---|
[3193] | 47 | case 'C' :
|
---|
| 48 | Do_Cov = true;
|
---|
| 49 | break;
|
---|
[3141] | 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]);
|
---|
[3150] | 57 | cout<<"nameh = "<<nameh
|
---|
| 58 | <<" , number of file to be read = "<<ppfname.size()
|
---|
[3246] | 59 | <<" , write_type="<<Write_Type<<" , cov="<<Do_Cov<<endl;
|
---|
[3141] | 60 |
|
---|
[3150] | 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;
|
---|
[3193] | 66 | if(obtyp==1) nread = ConcatHistoErr(nameh,ppfname,Write_Type,Do_Cov);
|
---|
[3150] | 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 | //---------------------------------------------------------------
|
---|
| 77 | int 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 | //---------------------------------------------------------------
|
---|
[3193] | 102 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp,bool do_cov)
|
---|
[3150] | 103 | {
|
---|
| 104 | if(ppfname.size()<=0) return 0;
|
---|
| 105 |
|
---|
[3141] | 106 | HistoErr *herrconc = NULL;
|
---|
[3193] | 107 | TVector<r_8> Tsum; TMatrix<r_8> Tsum2;
|
---|
[3150] | 108 | int nherr=0, nread=0, itest=0;
|
---|
[3141] | 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);
|
---|
[3150] | 114 | cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="<<herr.NBins()<<endl;
|
---|
[3141] | 115 | if(ifile==0) {
|
---|
| 116 | herrconc = new HistoErr(herr.XMin(),herr.XMax(),herr.NBins());
|
---|
[3150] | 117 | nherr = herr.NBins();
|
---|
[3141] | 118 | itest = int(herrconc->NBins()/5.+0.5);
|
---|
[3193] | 119 | if(do_cov) {Tsum.ReSize(nherr); Tsum=0.; Tsum2.ReSize(nherr,nherr); Tsum2=0.;}
|
---|
[3150] | 120 | } else if(nherr!=herr.NBins()) {
|
---|
| 121 | cout<<"BAD NUMBER OF BINS"<<endl;
|
---|
| 122 | continue;
|
---|
[3141] | 123 | }
|
---|
[3150] | 124 | if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
|
---|
| 125 | <<" nmean="<<herr.NMean()<<endl;
|
---|
| 126 | nread++;
|
---|
[3141] | 127 | for(int i=0;i<nherr;i++) herrconc->AddBin(i,herr(i));
|
---|
[3154] | 128 | sum+=herr(itest); sum2+=herr(itest)*herr(itest); nsum++;
|
---|
[3193] | 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 |
|
---|
[3141] | 137 | }
|
---|
[3150] | 138 | herrconc->ToVariance();
|
---|
[3141] | 139 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
| 140 | cout<<"Test bin "<<itest<<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
|
---|
[3150] | 141 | cout<<" "<<" mean="<<(*herrconc)(itest)
|
---|
| 142 | <<" sigma^2="<<herrconc->Error2(itest)<<endl;
|
---|
[3141] | 143 |
|
---|
[3193] | 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 |
|
---|
[3150] | 152 | // --- ecriture
|
---|
| 153 | if(wrtyp&1) { // ecriture ASCII
|
---|
[3184] | 154 | string asname = "cmvconcherr.data";
|
---|
| 155 | herrconc->WriteASCII(asname);
|
---|
[3141] | 156 | }
|
---|
[3193] | 157 | if(wrtyp&2 || do_cov) { // ecriture PPF
|
---|
[3150] | 158 | string tagobs = "cmvconcherr.ppf";
|
---|
| 159 | POutPersist posobs(tagobs);
|
---|
| 160 | tagobs = "herrconc"; posobs.PutObject(*herrconc,tagobs);
|
---|
[3193] | 161 | if(do_cov) {
|
---|
| 162 | tagobs = "mean"; posobs.PutObject(Tsum,tagobs);
|
---|
| 163 | tagobs = "cov"; posobs.PutObject(Tsum2,tagobs);
|
---|
| 164 | }
|
---|
[3150] | 165 | }
|
---|
| 166 | if(wrtyp&4) { // ecriture FITS
|
---|
| 167 | FitsInOutFile fio("!cmvconcherr.fits",FitsInOutFile::Fits_Create);
|
---|
| 168 | fio << *herrconc;
|
---|
| 169 | }
|
---|
[3141] | 170 |
|
---|
[3150] | 171 | delete herrconc;
|
---|
[3141] | 172 |
|
---|
[3150] | 173 | return nread;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | //---------------------------------------------------------------
|
---|
| 177 | int 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++)
|
---|
[3154] | 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++;
|
---|
[3150] | 206 | }
|
---|
| 207 | herrconc->ToVariance();
|
---|
[3154] | 208 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
[3150] | 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
|
---|
[3184] | 216 | string asname = "cmvconcherr2.data";
|
---|
| 217 | herrconc->WriteASCII(asname);
|
---|
[3150] | 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 |
|
---|
[3141] | 229 | delete herrconc;
|
---|
| 230 |
|
---|
[3150] | 231 | return nread;
|
---|
[3141] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /*
|
---|
[3150] | 235 | #### HistoErr 1D
|
---|
| 236 | openppf cmvconcherr.ppf
|
---|
[3141] | 237 |
|
---|
[3154] | 238 | zone 2 2
|
---|
[3150] | 239 | disp herrconc "hbincont err"
|
---|
| 240 | disp herrconc "hbinerr"
|
---|
| 241 | disp herrconc "hbinent"
|
---|
[3141] | 242 |
|
---|
[3154] | 243 | zone
|
---|
[3141] | 244 | n/plot herrconc.val%log10(x) x>0 ! "connectpoints"
|
---|
[3154] | 245 | n/plot herrconc.sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
|
---|
[3141] | 246 |
|
---|
[3154] | 247 | n/plot herrconc.sqrt(err2)/val%log10(x) x>0&&err2>0&&val>0 ! "connectpoints"
|
---|
[3150] | 248 |
|
---|
[3193] | 249 | disp mean
|
---|
| 250 | imag cov
|
---|
| 251 |
|
---|
| 252 | del cor
|
---|
| 253 | c++exec \
|
---|
| 254 | TMatrix<r_8> cor(cov,false); cor = 0.; \
|
---|
| 255 | for(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 | } } \
|
---|
| 261 | KeepObj(cor); \
|
---|
| 262 | cout<<"Matrice cor cree "<<endl;
|
---|
| 263 |
|
---|
| 264 | imag cor
|
---|
| 265 |
|
---|
[3150] | 266 | #### Histo2DErr 2D
|
---|
| 267 | openppf cmvconcherr2.ppf
|
---|
| 268 |
|
---|
[3154] | 269 | zone 2 2
|
---|
| 270 | imag herrconc2 "hbincont"
|
---|
| 271 | imag herrconc2 "hbinerr"
|
---|
| 272 | imag herrconc2 "hbinent"
|
---|
[3141] | 273 | */
|
---|