[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);
|
---|
| 15 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp);
|
---|
| 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
|
---|
| 23 | <<" wtyp: bit0 write ASCII, bit1 write PPF, bit2 write FITS (all=7)"<<endl;
|
---|
[3141] | 24 | }
|
---|
| 25 |
|
---|
[3150] | 26 |
|
---|
| 27 | //---------------------------------------------------------------
|
---|
[3141] | 28 | int main(int narg,char *arg[])
|
---|
| 29 | {
|
---|
[3150] | 30 | // "hpkgen" "hpkgenf" "hpkrec" "hpkgen2" "hpkgenf2" "hpkrec2"
|
---|
| 31 | string nameh = "";
|
---|
[3141] | 32 | vector<string> ppfname;
|
---|
[3150] | 33 | int Write_Type = 3;
|
---|
[3141] | 34 |
|
---|
| 35 | // --- Decodage arguments
|
---|
| 36 | char c;
|
---|
[3150] | 37 | while((c = getopt(narg,arg,"hn:w:")) != -1) {
|
---|
[3141] | 38 | switch (c) {
|
---|
| 39 | case 'n' :
|
---|
| 40 | nameh = optarg;
|
---|
| 41 | break;
|
---|
[3150] | 42 | case 'w' :
|
---|
| 43 | sscanf(optarg,"%d",&Write_Type);
|
---|
| 44 | break;
|
---|
[3141] | 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]);
|
---|
[3150] | 52 | cout<<"nameh = "<<nameh
|
---|
| 53 | <<" , number of file to be read = "<<ppfname.size()
|
---|
| 54 | <<" , write_type="<<Write_Type<<endl;
|
---|
[3141] | 55 |
|
---|
[3150] | 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 | //---------------------------------------------------------------
|
---|
| 72 | int 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 | //---------------------------------------------------------------
|
---|
| 97 | int ConcatHistoErr(string nameh,vector<string> ppfname,int wrtyp)
|
---|
| 98 | {
|
---|
| 99 | if(ppfname.size()<=0) return 0;
|
---|
| 100 |
|
---|
[3141] | 101 | HistoErr *herrconc = NULL;
|
---|
[3150] | 102 | int nherr=0, nread=0, itest=0;
|
---|
[3141] | 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);
|
---|
[3150] | 108 | cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="<<herr.NBins()<<endl;
|
---|
[3141] | 109 | if(ifile==0) {
|
---|
| 110 | herrconc = new HistoErr(herr.XMin(),herr.XMax(),herr.NBins());
|
---|
[3150] | 111 | nherr = herr.NBins();
|
---|
[3141] | 112 | itest = int(herrconc->NBins()/5.+0.5);
|
---|
[3150] | 113 | } else if(nherr!=herr.NBins()) {
|
---|
| 114 | cout<<"BAD NUMBER OF BINS"<<endl;
|
---|
| 115 | continue;
|
---|
[3141] | 116 | }
|
---|
[3150] | 117 | if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
|
---|
| 118 | <<" nmean="<<herr.NMean()<<endl;
|
---|
| 119 | nread++;
|
---|
[3141] | 120 | for(int i=0;i<nherr;i++) herrconc->AddBin(i,herr(i));
|
---|
[3154] | 121 | sum+=herr(itest); sum2+=herr(itest)*herr(itest); nsum++;
|
---|
[3141] | 122 | }
|
---|
[3150] | 123 | herrconc->ToVariance();
|
---|
[3141] | 124 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
| 125 | cout<<"Test bin "<<itest<<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
|
---|
[3150] | 126 | cout<<" "<<" mean="<<(*herrconc)(itest)
|
---|
| 127 | <<" sigma^2="<<herrconc->Error2(itest)<<endl;
|
---|
[3141] | 128 |
|
---|
[3150] | 129 | // --- ecriture
|
---|
| 130 | if(wrtyp&1) { // ecriture ASCII
|
---|
| 131 | FILE * fdata = fopen("cmvconcherr.data","w");
|
---|
| 132 | fprintf(fdata,"# x <mean> variance\n");
|
---|
| 133 | fprintf(fdata,"%.17e %.17e %d %d\n",herrconc->XMin(),herrconc->XMax()
|
---|
| 134 | ,herrconc->NBins(),herrconc->NMean());
|
---|
[3141] | 135 | for(int i=0;i<herrconc->NBins();i++) {
|
---|
[3150] | 136 | double x = herrconc->BinCenter(i);
|
---|
| 137 | fprintf(fdata,"%e %e %e\n",x,(*herrconc)(i),herrconc->Error2(i));
|
---|
[3141] | 138 | }
|
---|
| 139 | fclose(fdata);
|
---|
| 140 | }
|
---|
[3150] | 141 | if(wrtyp&2) { // ecriture PPF
|
---|
| 142 | string tagobs = "cmvconcherr.ppf";
|
---|
| 143 | POutPersist posobs(tagobs);
|
---|
| 144 | tagobs = "herrconc"; posobs.PutObject(*herrconc,tagobs);
|
---|
| 145 | }
|
---|
| 146 | if(wrtyp&4) { // ecriture FITS
|
---|
| 147 | FitsInOutFile fio("!cmvconcherr.fits",FitsInOutFile::Fits_Create);
|
---|
| 148 | fio << *herrconc;
|
---|
| 149 | }
|
---|
[3141] | 150 |
|
---|
[3150] | 151 | delete herrconc;
|
---|
[3141] | 152 |
|
---|
[3150] | 153 | return nread;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //---------------------------------------------------------------
|
---|
| 157 | int ConcatHisto2DErr(string nameh,vector<string> ppfname,int wrtyp)
|
---|
| 158 | {
|
---|
| 159 | if(ppfname.size()<=0) return 0;
|
---|
| 160 |
|
---|
| 161 | Histo2DErr *herrconc = NULL;
|
---|
| 162 | int nherrx=0, nherry=0, nread=0, itestx=0, itesty=0;
|
---|
| 163 | double sum=0., sum2=0., nsum=0;
|
---|
| 164 | for (int ifile=0;ifile<ppfname.size();ifile++) {
|
---|
| 165 | PInPersist pis(ppfname[ifile].c_str());
|
---|
| 166 | Histo2DErr herr;
|
---|
| 167 | pis.GetObject(herr,nameh);
|
---|
| 168 | cout<<ifile<<" : "<<ppfname[ifile]<<" nbin="
|
---|
| 169 | <<herr.NBinX()<<","<<herr.NBinY()<<endl;
|
---|
| 170 | if(ifile==0) {
|
---|
| 171 | herrconc = new Histo2DErr(herr.XMin(),herr.XMax(),herr.NBinX()
|
---|
| 172 | ,herr.YMin(),herr.YMax(),herr.NBinY());
|
---|
| 173 | nherrx = herr.NBinX(); nherry = herr.NBinY();
|
---|
| 174 | itestx = int(herrconc->NBinX()/5.+0.5);
|
---|
| 175 | itesty = int(herrconc->NBinY()/5.+0.5);
|
---|
| 176 | } else if(nherrx!=herr.NBinX() || nherry!=herr.NBinY()) {
|
---|
| 177 | cout<<"BAD NUMBER OF BINS"<<endl;
|
---|
| 178 | continue;
|
---|
| 179 | }
|
---|
| 180 | if(herr.NMean()!=1) cout<<"ATTENTION: file="<<ppfname[ifile]
|
---|
| 181 | <<" nmean="<<herr.NMean()<<endl;
|
---|
| 182 | nread++;
|
---|
| 183 | for(int i=0;i<nherrx;i++)
|
---|
[3154] | 184 | for(int j=0;j<nherry;j++) herrconc->AddBin(i,j,herr(i,j));
|
---|
| 185 | sum+=herr(itestx,itesty); sum2+=herr(itestx,itesty)*herr(itestx,itesty); nsum++;
|
---|
[3150] | 186 | }
|
---|
| 187 | herrconc->ToVariance();
|
---|
[3154] | 188 | if(nsum>0) {sum/=nsum; sum2/=nsum; sum2-=sum*sum;}
|
---|
[3150] | 189 | cout<<"Test bin "<<itestx<<","<<itesty
|
---|
| 190 | <<" mean="<<sum<<" sigma^2="<<sum2<<" nsum="<<nsum<<endl;
|
---|
| 191 | cout<<" "<<" mean="<<(*herrconc)(itestx,itesty)
|
---|
| 192 | <<" sigma^2="<<herrconc->Error2(itestx,itesty)<<endl;
|
---|
| 193 |
|
---|
| 194 | // --- ecriture
|
---|
| 195 | if(wrtyp&1) { // ecriture ASCII
|
---|
| 196 | cout<<"ERROR: No ASCII writing implemented"<<endl;
|
---|
| 197 | }
|
---|
| 198 | if(wrtyp&2) { // ecriture PPF
|
---|
| 199 | string tagobs = "cmvconcherr2.ppf";
|
---|
| 200 | POutPersist posobs(tagobs);
|
---|
| 201 | tagobs = "herrconc2"; posobs.PutObject(*herrconc,tagobs);
|
---|
| 202 | }
|
---|
| 203 | if(wrtyp&4) { // ecriture FITS
|
---|
| 204 | FitsInOutFile fio("!cmvconcherr2.fits",FitsInOutFile::Fits_Create);
|
---|
| 205 | fio << *herrconc;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[3141] | 208 | delete herrconc;
|
---|
| 209 |
|
---|
[3150] | 210 | return nread;
|
---|
[3141] | 211 | }
|
---|
| 212 |
|
---|
| 213 | /*
|
---|
[3150] | 214 | #### HistoErr 1D
|
---|
| 215 | openppf cmvconcherr.ppf
|
---|
[3141] | 216 |
|
---|
[3154] | 217 | zone 2 2
|
---|
[3150] | 218 | disp herrconc "hbincont err"
|
---|
| 219 | disp herrconc "hbinerr"
|
---|
| 220 | disp herrconc "hbinent"
|
---|
[3141] | 221 |
|
---|
[3154] | 222 | zone
|
---|
[3141] | 223 | n/plot herrconc.val%log10(x) x>0 ! "connectpoints"
|
---|
[3154] | 224 | n/plot herrconc.sqrt(err2)%log10(x) x>0&&err2>0 ! "connectpoints same red"
|
---|
[3141] | 225 |
|
---|
[3154] | 226 | n/plot herrconc.sqrt(err2)/val%log10(x) x>0&&err2>0&&val>0 ! "connectpoints"
|
---|
[3150] | 227 |
|
---|
| 228 | #### Histo2DErr 2D
|
---|
| 229 | openppf cmvconcherr2.ppf
|
---|
| 230 |
|
---|
[3154] | 231 | zone 2 2
|
---|
| 232 | imag herrconc2 "hbincont"
|
---|
| 233 | imag herrconc2 "hbinerr"
|
---|
| 234 | imag herrconc2 "hbinent"
|
---|
[3141] | 235 | */
|
---|