1 | // Utilisation de SOPHYA pour faciliter les tests ...
|
---|
2 | #include "sopnamsp.h"
|
---|
3 | #include "machdefs.h"
|
---|
4 |
|
---|
5 | /* ----------------------------------------------------------
|
---|
6 | Projet BAORadio - (C) LAL/IRFU 2008-2011
|
---|
7 |
|
---|
8 | Programme de lecture des fichiers DUMP ADC du CRT (Pittsburgh)
|
---|
9 | pour calcul de spectres / correlations
|
---|
10 | R. Ansari, C. Magneville - LAL/Irfu - Juin 2011
|
---|
11 | ---------------------------------------------------------- */
|
---|
12 |
|
---|
13 | // include standard c/c++
|
---|
14 | #include <math.h>
|
---|
15 | #include <stdio.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 |
|
---|
19 | #include <iostream>
|
---|
20 | #include <string>
|
---|
21 |
|
---|
22 | #include "pexceptions.h"
|
---|
23 | #include "tvector.h"
|
---|
24 | #include "fioarr.h"
|
---|
25 | // #include "tarrinit.h"
|
---|
26 | #include "ntuple.h"
|
---|
27 | #include "datatable.h"
|
---|
28 | #include "histinit.h"
|
---|
29 | #include "matharr.h"
|
---|
30 | #include "timestamp.h"
|
---|
31 | #include "fftwserver.h"
|
---|
32 | #include "fftpserver.h"
|
---|
33 | #include "utilarr.h"
|
---|
34 | #include "histats.h"
|
---|
35 |
|
---|
36 | // include sophya mesure ressource CPU/memoire ...
|
---|
37 | #include "resusage.h"
|
---|
38 | #include "ctimer.h"
|
---|
39 | #include "timing.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | //--------------------------- Fonctions de ce fichier -------------------
|
---|
43 | int Usage(bool fgshort=true);
|
---|
44 | int DecodeProc(int narg, char* arg[]);
|
---|
45 | int ProcessADCFiles(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep, int jf1, int jf2, int nfreq);
|
---|
46 | int ComputeCorrel(TMatrix< complex<r_4> >& cfour, TMatrix< complex<r_8> >& correl, TMatrix< complex<r_8> >& autocorrel);
|
---|
47 | int ADCFiles2Histo(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep);
|
---|
48 |
|
---|
49 | //------------------------------------------------------------------------------------------------------------
|
---|
50 |
|
---|
51 | /* --Fonction-- */
|
---|
52 | int Usage(bool fgshort)
|
---|
53 | {
|
---|
54 | cout << " --- corrcrtadc.cc : Read CRT-ADC dump files and process to produce correlation matrices \n"
|
---|
55 | << " Usage: corrcrtadc -corr/-hval InOutPath OutFile [AdcIds] [Imin,Imax,step]\n" << endl;
|
---|
56 | if (fgshort) {
|
---|
57 | cout << " corrcrtadc -h for detailed instructions" << endl;
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 | cout << " -corr/-hval : Compute correlations or Fill histograms for ADC sample values \n"
|
---|
61 | << " InOutPath : Input/Output directory name \n"
|
---|
62 | << " ADCIds : ADC Id's to be processed (Example: 1,2,3,4, default=0,1) \n"
|
---|
63 | << " OutFile : Output PPF file name \n"
|
---|
64 | << " Imin,Imax,IStep: Input ADC dump files sequence number \n"
|
---|
65 | << " FileNames=InOutPath/adcdumpNJFII.fits Imin<=II<=Imax II+=IStep , J=ADCIds \n" << endl;
|
---|
66 | // << " NumFreq1,NumFreq2,NBinFreq: Freq Zone and number of frequency bins for ntuple" << endl;
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | //----------------------------------------------------
|
---|
71 | //----------------------------------------------------
|
---|
72 | int main(int narg, char* arg[])
|
---|
73 | {
|
---|
74 | if ((narg>1)&&(strcmp(arg[1],"-h")==0)) return Usage(false);
|
---|
75 | if (narg<4) return Usage(true);
|
---|
76 | HiStatsInitiator _inia;
|
---|
77 | // TArrayInitiator _inia;
|
---|
78 |
|
---|
79 | int rc = 0;
|
---|
80 | try {
|
---|
81 | ResourceUsage resu;
|
---|
82 | DecodeProc(narg-1, arg+1);
|
---|
83 | resu.Update();
|
---|
84 | cout << resu;
|
---|
85 | }
|
---|
86 | catch (PException& exc) {
|
---|
87 | cerr << " corrcrtadc.cc catched PException " << exc.Msg() << endl;
|
---|
88 | rc = 77;
|
---|
89 | }
|
---|
90 | catch (std::exception& sex) {
|
---|
91 | cerr << "\n corrcrtadc.cc std::exception :"
|
---|
92 | << (string)typeid(sex).name() << "\n msg= "
|
---|
93 | << sex.what() << endl;
|
---|
94 | rc = 78;
|
---|
95 | }
|
---|
96 | catch (...) {
|
---|
97 | cerr << " corrcrtadc.cc catched unknown (...) exception " << endl;
|
---|
98 | rc = 79;
|
---|
99 | }
|
---|
100 |
|
---|
101 | cout << ">>>> corrcrtadc.cc ------- END ----------- RC=" << rc << endl;
|
---|
102 | return rc;
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | //--------------------------------------------------------------------
|
---|
108 | // Traitement fichiers produits par vismfib (V Nov09)
|
---|
109 | /* --Fonction-- */
|
---|
110 | int DecodeProc(int narg, char* arg[])
|
---|
111 | {
|
---|
112 | // Decodage des arguments et traitement
|
---|
113 | string popt = arg[0];
|
---|
114 | bool fillhis=false;
|
---|
115 | if (popt=="-hval") fillhis=true;
|
---|
116 | string inoutpath = arg[1];
|
---|
117 | string outname = arg[2];
|
---|
118 | int aids[4]={0,1,-1,-1};
|
---|
119 | if (narg>3) sscanf(arg[3],"%d,%d,%d,%d",aids,aids+1,aids+2,aids+3);
|
---|
120 | vector<int> adcids;
|
---|
121 | for(int ii=0;ii<4;ii++)
|
---|
122 | if((aids[ii]>=0)&&(aids[ii]<16)) adcids.push_back(aids[ii]);
|
---|
123 | int imin=0;
|
---|
124 | int imax=0;
|
---|
125 | int istep=1;
|
---|
126 | if (narg>4) sscanf(arg[4],"%d,%d,%d",&imin,&imax,&istep);
|
---|
127 | int jf1=0;
|
---|
128 | int jf2=0;
|
---|
129 | int nfreq=0;
|
---|
130 | if (narg>5)
|
---|
131 | sscanf(arg[5],"%d,%d,%d",&jf1,&jf2,&nfreq);
|
---|
132 |
|
---|
133 | cout << " ----- corrcrtadc/DecodeProc - Start " << ((fillhis)? " Fill ADC-val histogram" : " Compute Correlation") << endl;
|
---|
134 | cout << " InOutPath= " << inoutpath << " ADCIds: " ;
|
---|
135 | for(size_t ii=0; ii<adcids.size(); ii++) cout << adcids[ii] << " , ";
|
---|
136 | cout << " (NbADC=" << adcids.size() << ") OutFileName= " << outname
|
---|
137 | << " IMin,Max,Step=" << imin << "," << imax << "," << istep << endl;
|
---|
138 | // << "Frequency num range JF=" << jf1 << "," << jf2 << "," << nfreq << " ------- " << endl;
|
---|
139 | int rc=0;
|
---|
140 | if (fillhis) rc = ADCFiles2Histo(inoutpath, adcids, outname, imin, imax, istep);
|
---|
141 | else rc=ProcessADCFiles(inoutpath, adcids, outname, imin, imax, istep, jf1, jf2, nfreq);
|
---|
142 |
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Taille des fichiers 4 voies = 1024*1024*128 ===> 32 x 1024 x 1024 samples / voie
|
---|
147 | // On lit par paquet de 4096 = 4 x 1024 / voie --> ADCRDBLKSZ=16x1024=16384
|
---|
148 | #define ADCFLEN 4096
|
---|
149 | #define ADCRDBLKSZ 16384
|
---|
150 | #define ADCNREAD 8192
|
---|
151 |
|
---|
152 | static int PRTMODULO = 1000;
|
---|
153 |
|
---|
154 | // Pour traitement (calcul FFT et visibilites (ProcA) 1 fibre, 2 voies RAW)
|
---|
155 | /* --Fonction-- */
|
---|
156 | int ProcessADCFiles(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep, int jf1, int jf2, int nfreq)
|
---|
157 | {
|
---|
158 | Timer tm("ProcessADCFiles");
|
---|
159 |
|
---|
160 | #define NFREQUSED 2040
|
---|
161 | #define NFREQUSESTART 5
|
---|
162 | #define NFREBIN 4
|
---|
163 |
|
---|
164 | sa_size_t NFREQREBIN = NFREQUSED/NFREBIN;
|
---|
165 | sa_size_t NBADC = adcids.size();
|
---|
166 |
|
---|
167 | sa_size_t NCHAN = 4*NBADC;
|
---|
168 | TMatrix< complex<r_4> > cfour(NCHAN, NFREQUSED);
|
---|
169 | sa_size_t NCORREL = NCHAN*(NCHAN+1)/2;
|
---|
170 | TMatrix< complex<r_8> > correl(NCORREL,NFREQUSED);
|
---|
171 | TMatrix< complex<r_8> > autocorrel(NCHAN,NFREQUSED);
|
---|
172 | double nsumcor=0.;
|
---|
173 |
|
---|
174 | vector<FILE*> fip;
|
---|
175 | vector<int> adcchans;
|
---|
176 | for(size_t ka=0; ka<NBADC; ka++) {
|
---|
177 | fip.push_back(NULL);
|
---|
178 | for(size_t ica=0; ica<4; ica++)
|
---|
179 | adcchans.push_back(adcids[ka]*4+ica);
|
---|
180 | }
|
---|
181 |
|
---|
182 | FFTPackServer ffts(false);
|
---|
183 |
|
---|
184 | TVector<r_4> sigadc(ADCFLEN);
|
---|
185 | TVector< complex<r_4> > foursig;
|
---|
186 |
|
---|
187 | char fname[512];
|
---|
188 |
|
---|
189 | signed char rdbuff[ADCRDBLKSZ];
|
---|
190 | for(int ifile=imin; ifile<=imax; ifile+=istep) {
|
---|
191 | for(int ka=0; ka<NBADC; ka++) {
|
---|
192 | sprintf(fname, "%s/adcdumpN%dF%d.bd",inoutpath.c_str(),adcids[ka],ifile);
|
---|
193 | fip[ka]=fopen(fname,"rb");
|
---|
194 | if (fip[ka]==NULL) {
|
---|
195 | cout << "ProcessADCFiles/ERROR opening file " << fname << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
|
---|
196 | throw IOExc(" ADC dump file open error!");
|
---|
197 | }
|
---|
198 | else
|
---|
199 | cout << "ProcessADCFiles/Info - file " << fname << " opened ... " << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
|
---|
200 | }
|
---|
201 |
|
---|
202 | for(int m=0; m<ADCNREAD; m++) {
|
---|
203 | sa_size_t irc=0;
|
---|
204 | for(int ka=0; ka<NBADC; ka++) {
|
---|
205 | fread(rdbuff,1,ADCRDBLKSZ,fip[ka]);
|
---|
206 | for(int jac=0; jac<4; jac++) {
|
---|
207 | for(sa_size_t ls=0; ls<ADCFLEN; ls++) sigadc(ls)=(float)rdbuff[ls*4+jac];
|
---|
208 | ffts.FFTForward(sigadc, foursig);
|
---|
209 | cfour.Row(irc)=foursig(Range(NFREQUSESTART,NFREQUSESTART+NFREQUSED-1)).Transpose(); irc++;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | ComputeCorrel(cfour, correl, autocorrel);
|
---|
213 | nsumcor++;
|
---|
214 | if (m%PRTMODULO==0) cout << " ProcessADCFiles/Info Done read+FFT+correl m=" << m << " / Max=" << ADCNREAD << endl;
|
---|
215 | }
|
---|
216 | for(int ka=0; ka<NBADC; ka++) fclose(fip[ka]);
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | correl /= nsumcor;
|
---|
221 | autocorrel /= nsumcor;
|
---|
222 |
|
---|
223 | TMatrix< complex<r_8> > correbin(NCORREL,NFREQREBIN);
|
---|
224 | sa_size_t icc=0;
|
---|
225 | for(sa_size_t i=0; i<correbin.NCols(); i++) {
|
---|
226 | for(sa_size_t j=0; j<NFREBIN; j++) {
|
---|
227 | correbin.Column(i) += correl.Column(icc);
|
---|
228 | icc++;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | sprintf(fname, "%s/%s",inoutpath.c_str(),outname.c_str());
|
---|
233 | cout << "ProcessADCFiles: Opening file " << fname << " for writing correlation matrix" << endl;
|
---|
234 | POutPersist po(fname);
|
---|
235 | po << PPFNameTag("correlmtx") << correl;
|
---|
236 | po << PPFNameTag("acorrmtx") << autocorrel;
|
---|
237 | po << PPFNameTag("rebincormtx") << correbin;
|
---|
238 |
|
---|
239 | cout << "ProcessADCFiles: finished FFT + correlation computing " << endl;
|
---|
240 |
|
---|
241 | cout << " --------------- Channel/Correlation number association --------------- " << endl;
|
---|
242 | cout << " CorreNumber- axb (MtxRow) : (Ca, Cb) -- ADCChan(c,d) " << endl;
|
---|
243 | cout << " ----------------------------------------------------------------------- " << endl;
|
---|
244 | sa_size_t jcr=0;
|
---|
245 | for(sa_size_t j1=0; j1<cfour.NRows(); j1++) {
|
---|
246 | for(sa_size_t j2=j1; j2<cfour.NRows(); j2++) {
|
---|
247 | cout << jcr << " : (" << j1 << "," << j2 << ") -> ADCChans (" << adcchans[j1] << "," << adcchans[j2] << ")" << endl;
|
---|
248 | jcr++;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | cout << " ----------------------------------------------------------------------- " << endl;
|
---|
252 |
|
---|
253 | return 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /* --Fonction-- */
|
---|
257 | int ComputeCorrel(TMatrix< complex<r_4> >& cfour, TMatrix< complex<r_8> >& correl, TMatrix< complex<r_8> >& autocorrel)
|
---|
258 | {
|
---|
259 | sa_size_t jcr=0;
|
---|
260 | for(sa_size_t j1=0; j1<cfour.NRows(); j1++) {
|
---|
261 | for(sa_size_t j2=j1; j2<cfour.NRows(); j2++) {
|
---|
262 | for(sa_size_t i=0; i<cfour.NCols(); i++)
|
---|
263 | correl(jcr,i) += complex<r_8> ( cfour(j1,i)*conj(cfour(j2,i)) );
|
---|
264 | if (j1==j2) autocorrel.Row(j1)=correl.Row(jcr);
|
---|
265 | jcr++;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* --Fonction-- */
|
---|
272 | int ADCFiles2Histo(string& inoutpath, vector<int>& adcids, string& outname, int imin, int imax, int istep)
|
---|
273 | {
|
---|
274 | Timer tm("ADCFiles2Histo");
|
---|
275 |
|
---|
276 | sa_size_t NBADC = adcids.size();
|
---|
277 |
|
---|
278 | sa_size_t NCHAN = 4*NBADC;
|
---|
279 | vector<Histo *> vhist;
|
---|
280 | for(int ka=0; ka<NCHAN; ka++)
|
---|
281 | vhist.push_back(new Histo(-128.5,128.5,257) );
|
---|
282 |
|
---|
283 | vector<FILE*> fip;
|
---|
284 | for(size_t ka=0; ka<NBADC; ka++) fip.push_back(NULL);
|
---|
285 |
|
---|
286 | char fname[512];
|
---|
287 |
|
---|
288 | signed char rdbuff[ADCRDBLKSZ];
|
---|
289 | for(int ifile=imin; ifile<=imax; ifile+=istep) {
|
---|
290 | for(int ka=0; ka<NBADC; ka++) {
|
---|
291 | sprintf(fname, "%s/adcdumpN%dF%d.bd",inoutpath.c_str(),adcids[ka],ifile);
|
---|
292 | fip[ka]=fopen(fname,"rb");
|
---|
293 | if (fip[ka]==NULL) {
|
---|
294 | cout << "ADCFiles2Histo/ERROR opening file " << fname << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
|
---|
295 | throw IOExc(" ADC dump file open error!");
|
---|
296 | }
|
---|
297 | else
|
---|
298 | cout << "ADCFiles2Histo/Info - file " << fname << " opened ... " << " AdcId=" << adcids[ka] << " I=" << ifile << endl;
|
---|
299 | }
|
---|
300 |
|
---|
301 | for(int m=0; m<ADCNREAD; m++) {
|
---|
302 | sa_size_t irc=0;
|
---|
303 | for(int ka=0; ka<NBADC; ka++) {
|
---|
304 | fread(rdbuff,1,ADCRDBLKSZ,fip[ka]);
|
---|
305 | for(int jac=0; jac<4; jac++) {
|
---|
306 | for(sa_size_t ls=0; ls<ADCFLEN; ls++) vhist[irc]->Add((r_8)rdbuff[ls*4+jac]);
|
---|
307 | irc++;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | if (m%PRTMODULO==0) cout << " ADCFiles2Histo/Info Done FillHist m=" << m << " / Max=" << ADCNREAD << endl;
|
---|
311 | }
|
---|
312 | for(int ka=0; ka<NBADC; ka++) fclose(fip[ka]);
|
---|
313 | }
|
---|
314 |
|
---|
315 | sprintf(fname, "%s/%s",inoutpath.c_str(),outname.c_str());
|
---|
316 | cout << "ADCFiles2Histo: Opening file " << fname << " for writing ADC value histograms " << endl;
|
---|
317 | POutPersist po(fname);
|
---|
318 | char nametag[64];
|
---|
319 | for(size_t ka=0; ka<NCHAN; ka++) {
|
---|
320 | sprintf(nametag,"adcvalC%d",ka);
|
---|
321 | po << PPFNameTag(nametag) << *(vhist[ka]);
|
---|
322 | }
|
---|
323 | for(size_t ka=0; ka<NCHAN; ka++) delete vhist[ka];
|
---|
324 |
|
---|
325 | cout << "ADCFiles2Histo: finished ADC value histogram fill " << endl;
|
---|
326 | return 0;
|
---|
327 | }
|
---|