source: Sophya/trunk/AddOn/TAcq/racquproc.cc@ 3944

Last change on this file since 3944 was 3944, checked in by ansari, 15 years ago

Correction bug xnt_ non alloue ds BRMeanSpecCalculator, Reza 4/02/2011

File size: 33.7 KB
Line 
1#include "racquproc.h"
2
3/* ----------------------------------------
4 Projet BAORadio --- LAL
5 2008 - 2010
6------------------------------------------- */
7
8#include <stdlib.h>
9#include <unistd.h>
10#include <fstream>
11#include <signal.h>
12
13#include "pexceptions.h"
14#include "tvector.h"
15#include "fioarr.h"
16#include "timestamp.h"
17#include "fftpserver.h"
18#include "fftwserver.h"
19#include "histos.h"
20#include "histos2.h"
21
22#include "FFTW/fftw3.h"
23
24
25#include "pciewrap.h"
26#include "brpaqu.h"
27#include "minifits.h"
28
29/* Fonction, module^2 des nombres complexes */
30static inline r_4 Zmod2(complex<r_4> z)
31{ return (z.real()*z.real()+z.imag()*z.imag()); }
32static inline r_8 Zmod2(complex<r_8> z)
33{ return (z.real()*z.real()+z.imag()*z.imag()); }
34static inline r_4 Zmod2(TwoByteComplex z)
35{ return ((r_4)z.realB()*(r_4)z.realB()+(r_4)z.imagB()*(r_4)z.imagB()); }
36
37//---------------------------------------------------------------------
38// Classe thread de traitement/monitoring multifibre Raw/FFT
39//---------------------------------------------------------------------
40/* --Methode-- */
41MonitorProc::MonitorProc(RAcqMemZoneMgr& mem)
42 : memgr(mem)
43{
44 BRAcqConfig bpar;
45 par_.Set(bpar.GetParams());
46 nmax_ = par_.nmaxProc;
47 if (nmax_==0) nmax_=par_.MaxNbBlocs();
48 nmean_ = par_.nmeanProc;
49 step_ = par_.stepProc;
50 stop_ = false;
51 path_ = bpar.OutputDirectory();
52 nfiles_ = 0;
53 nblocproc_ = 0;
54 nprocpaq_=0;
55 npaqsamefc_=0;
56 totnprocpaq_=0;
57 totnpaqsamefc_=0;
58 curfc_.SetSize(memgr.NbFibres(), memgr.NbPaquets());
59 cpaqok_.SetSize(memgr.NbFibres(), memgr.NbPaquets());
60 SetMemZAction();
61}
62
63/* --Methode-- */
64MonitorProc::~MonitorProc()
65{
66 // cout << " **** DBG ***** MonitorProc::~MonitorProc() " << endl;
67}
68
69/* --Methode-- */
70MemZStatus MonitorProc::SetMemZAction(MemZaction mmact)
71{
72 mmact_=mmact;
73 mmsta_=MemZS_Proc;
74 switch (mmact) {
75 case MemZA_ProcA:
76 mmsta_=MemZS_ProcA;
77 break;
78 case MemZA_ProcB:
79 mmsta_=MemZS_ProcB;
80 break;
81 case MemZA_ProcC:
82 mmsta_=MemZS_ProcC;
83 break;
84 case MemZA_ProcD:
85 mmsta_=MemZS_ProcD;
86 break;
87 case MemZA_ProcE:
88 mmsta_=MemZS_ProcE;
89 break;
90 case MemZA_ProcF:
91 mmsta_=MemZS_ProcF;
92 break;
93 default:
94 mmact_=MemZA_Proc;
95 mmsta_=MemZS_Proc;
96 break;
97 }
98 return mmsta_;
99}
100
101/* --Methode-- */
102void MonitorProc::Stop()
103{
104 stop_=true;
105}
106
107/* --Methode-- */
108void MonitorProc::run()
109{
110 setRC(1);
111 int rc=0;
112 try {
113 TimeStamp ts;
114 cout << " MonitorProc::run() - Starting " << ts << " NMaxMemZones=" << nmax_
115 << " NMean=" << nmean_ << " Step=" << step_ << endl;
116 cout << " MonitorProc::run()... - Output Data Path: " << path_ << endl;
117 char fname[512];
118 sprintf(fname,"%s/monproc.log",path_.c_str());
119 ofstream filog(fname);
120 filog << " MonitorProc::run() - starting log file " << ts << endl;
121 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
122 uint_4 paqsz = memgr.PaqSize();
123 BRPaquet pq(paqsz);
124 if (par_.fgsinglechannel) {
125 spectre_.SetSize(memgr.NbFibres(), pq.DataSize()/2);
126 for(int kc=0; kc<memgr.NbFibres(); kc++) nzm_.push_back(0);
127 rc=procData1C(filog);
128 }
129 else {
130 spectre_.SetSize(2*memgr.NbFibres(), pq.DataSize()/4);
131 for(int kc=0; kc<2*memgr.NbFibres(); kc++) nzm_.push_back(0);
132 rc=procData2C(filog);
133 }
134 cout << " ---- MonitorProc::run()/End NBlocProcessed=" << nblocproc_
135 << " NFiles=" << nfiles_ << " Rc=" << rc << endl;
136 ts.SetNow();
137 filog << " ---- MonitorProc::run()/End " << ts << endl;
138 filog << " --------- MonitorProc::run()/End NBlocProcessed=" << nblocproc_
139 << " NFiles=" << nfiles_ << " Rc=" << rc << endl;
140 }
141 catch (std::exception& exc) {
142 cout << " MonitorProc::run()/catched std::exception " << exc.what() << endl;
143 setRC(98);
144 return;
145 }
146 catch(...) {
147 cout << " MonitorProc::run()/catched unknown ... exception " << endl;
148 setRC(99);
149 return;
150 }
151
152 setRC(rc);
153 return;
154}
155
156/* --Methode-- */
157int MonitorProc::procData1C(ofstream& filog)
158{
159 /*
160 if (par_.fgdatafft) {
161 cout << " MonitorProc::procData1C() - NOT IMPLEMENTED for 1 channel FFT data -> Rc=67" << endl;
162 filog << " MonitorProc::procData1C() - NOT IMPLEMENTED for 1 channel FFT data -> Rc=67" << endl;
163 return 67;
164 }
165 */
166 BRPaqChecker pcheck[MAXNBFIB]; // Verification/comptage des paquets
167// Initialisation pour calcul FFT
168 uint_4 paqsz = memgr.PaqSize();
169 BRPaquet pq(paqsz);
170 TVector<r_4> vx(pq.DataSize());
171 vx = (r_4)(0.);
172 TVector< complex<r_4> > cfour(pq.DataSize()/2+1); // composant TF
173
174 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
175 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
176
177 // Histo pour valeurs des bytes dans les paquets
178 Histo* phist[MAXNBFIB];
179 Histo2D* ph2[MAXNBFIB];
180 Histo* phexp[MAXNBFIB];
181 TMatrix<r_4>* psatur[MAXNBFIB];
182 TVector<uint_4>* pvsatfreq[MAXNBFIB];
183 for(int ifib=0; ifib<MAXNBFIB; ifib++) {
184 phist[ifib]=NULL; ph2[ifib]=NULL; phexp[ifib]=NULL; psatur[ifib]=NULL;
185 }
186 if (par_.fgdatafft) { // data de type FFT
187 for(sa_size_t lc=0; lc<memgr.NbFibres(); lc++) {
188 phist[lc] = new Histo(-128.5,+128.5,257);
189 ph2[lc] = new Histo2D(-128.5,+128.5,257, -128.5,+128.5,257);
190 phexp[lc] = new Histo(-128.5,+128.5,257);
191 psatur[lc] = new TMatrix<r_4>(3,3);
192 pvsatfreq[lc] = new TVector<uint_4>(spectre_.NCols());
193 *psatur[lc] = 0.;
194 }
195 }
196 else {
197 for(sa_size_t lc=0; lc<memgr.NbFibres(); lc++)
198 phist[lc] = new Histo(-0.5,256.5,257);
199 }
200
201 TimeStamp ts;
202 char fname[512];
203 nfiles_ = 0;
204 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
205 if (stop_) break;
206 if (memgr.GetRunState() == MemZR_Stopped) break;
207
208 int mid = memgr.FindMemZoneId(mmact_); // MemZA_Proc
209 Byte* buffg = memgr.GetMemZone(mid);
210 if (buffg == NULL) {
211 cout << " MonitorProc::procData1C()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
212 break;
213 }
214 if ((step_>1)&&(kmz%step_ != 0)) {
215 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
216 continue;
217 }
218 sa_size_t lc=0;
219 Byte* fbuff[MAXNBFIB];
220 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) { // Boucle sur les fibres
221 fbuff[fib] = memgr.GetMemZone(mid,fib);
222 if (fbuff[fib] == NULL) { // cela ne devrait pas arriver
223 cout << " MonitorProc::procData1C()/ERROR memgr.GetMemZone(" << mid << "," << fib << ") -> NULL" << endl;
224 return 9;
225 }
226 }
227
228 // DBG-ATTENTION : freq 0 pour expo full FFT si reordonne
229 // int idxexpo=(par_.fgfftshrink)?1023:0; // exposant FFT shrink 1024 freq, ou freq 0 pour full-FFT
230 int idxexpo=0;
231
232 cpaqok_ = (uint_1)0;
233 curfc_ = (uint_8)0;
234 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) {
235 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
236 BRPaquet paq(fbuff[fib]+i*paqsz, paqsz);
237 bool pqok=pcheck[fib].Check(paq,curfc_(fib,i)); // Verification du paquet / FrameCounter
238 if (!pqok) continue;
239 cpaqok_(fib,i) = 1;
240 sa_size_t lc=fib;
241 if (par_.fgdatafft) { // Traitement data de type FFT
242 TwoByteComplex* tbcp=paq.Data1C();
243
244 // exposant FFT
245 phexp[lc]->Add((r_4)tbcp[idxexpo].realD());
246
247 for(sa_size_t j=1; j<spectre_.NCols(); j++) {
248 phist[lc]->Add((r_4)tbcp[j].realD());
249 phist[lc]->Add((r_4)tbcp[j].imagD());
250 ph2[lc]->Add((r_4)tbcp[j].realD(), (r_4)tbcp[j].imagD());
251 // comptage saturation
252 int sir=0, sii=0; // saturation partie reelle, partie imaginaire
253 if (tbcp[j].realI()==-128) sir=-1;
254 else if (tbcp[j].realI()==127) sir=1;
255 if (tbcp[j].imagI()==-128) sii=-1;
256 else if (tbcp[j].imagI()==127) sii=1;
257 (*psatur[lc])(sir+1,sii+1)+=1.;
258 if ((sir!=0)||(sii!=0)) (*pvsatfreq[lc])(j)+=1;
259 spectre_(lc,j) += Zmod2(tbcp[j]);
260 }
261 nzm_[lc]++;
262 }
263 else { // Traitement RawData
264 for(sa_size_t j=0; j<vx.Size(); j++) {
265 phist[lc]->Add((r_8)(*(paq.Data1()+j)));
266 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
267 }
268 fftwf_execute(plan);
269 // ffts_.FFTForward(vx, cfour_);
270 for(sa_size_t j=0; j<spectre_.NCols(); j++)
271 spectre_(lc,j) += Zmod2(cfour(j+1));
272 nzm_[lc]++; lc++;
273 }
274 } // FIN de la boucle sur les paquets
275 } // Boucle sur les fibres
276 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
277 CheckFrameCounters();
278
279 nblocproc_ ++;
280 totnprocpaq_ += memgr.NbPaquets(); nprocpaq_ += memgr.NbPaquets();
281 bool fgnzm=true;
282 for(int lc=0; lc<memgr.NbFibres(); lc++)
283 if (nzm_[lc]<nmean_) fgnzm=false;
284
285 if (fgnzm) {
286 char buff[32];
287 for(sa_size_t lc=0; lc<memgr.NbFibres(); lc++) {
288 spectre_.Row(lc) /= (r_4)nzm_[lc];
289 sprintf(buff,"NPaqMoy%d",(int)lc);
290 spectre_.Info()[buff] = nzm_[lc];
291 }
292 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
293 nfiles_++;
294 POutPersist po(fname);
295 po << PPFNameTag("spectre") << spectre_;
296 spectre_ = (r_4)(0.);
297 char buftag[32];
298 for(int lc=0; lc<memgr.NbFibres(); lc++) {
299 sprintf(buftag,"hvalV%d",(int)lc);
300 po << PPFNameTag(buftag) << (*phist[lc]);
301 phist[lc]->Zero();
302 if (par_.fgdatafft) { // data de type FFT
303 sprintf(buftag,"h2dV%d",(int)lc);
304 po << PPFNameTag(buftag) << (*ph2[lc]);
305 ph2[lc]->Zero();
306 sprintf(buftag,"hexpV%d",(int)lc);
307 po << PPFNameTag(buftag) << (*phexp[lc]);
308 phexp[lc]->Zero();
309 sprintf(buftag,"saturV%d",(int)lc);
310 po << PPFNameTag(buftag) << (*psatur[lc]);
311 (*psatur[lc])=0.;
312 sprintf(buftag,"satfreqV%d",(int)lc);
313 po << PPFNameTag(buftag) << (*pvsatfreq[lc]);
314 (*pvsatfreq[lc])=0;
315 }
316 nzm_[lc]=0;
317 }
318 ts.SetNow();
319 // Calcul / impression fraction des paquets avec same-framecounter
320 int fracsame=0;
321 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
322 int totfracsame=0;
323 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
324 filog << ts << " : proc file (meanspectra) " << fname << endl;
325 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
326 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
327 << " -> " << fracsame << " % )" << endl;
328 cout << " MonitorProc::procData1C() " << ts << " : created file " << fname << endl;
329 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
330 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
331 << " -> " << fracsame << " % )" << endl;
332 nprocpaq_=npaqsamefc_=0;
333 }
334 } // Fin de boucle sur les kmz ( bloc MemZoneMgr a traiter )
335
336 bool fgnzm=false;
337 for(int lc=0; lc<memgr.NbFibres(); lc++)
338 if (nzm_[lc]>0) fgnzm=true;
339
340 if (fgnzm) {
341 char buff[32];
342 for(sa_size_t lc=0; lc<memgr.NbFibres(); lc++) {
343 if (nzm_[lc]>0) spectre_.Row(lc) /= (r_4)nzm_[lc];
344 sprintf(buff,"NPaqMoy%d",(int)lc);
345 spectre_.Info()[buff] = nzm_[lc];
346 }
347 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
348 POutPersist po(fname);
349 po << PPFNameTag("spectre") << spectre_;
350 spectre_ = (r_4)(0.);
351 char buftag[32];
352 for(int lc=0; lc<memgr.NbFibres(); lc++) {
353 sprintf(buftag,"hvalV%d",(int)lc);
354 po << PPFNameTag(buftag) << (*phist[lc]);
355 delete phist[lc];
356 if (par_.fgdatafft) { // data de type FFT
357 sprintf(buftag,"h2dV%d",(int)lc);
358 po << PPFNameTag(buftag) << (*ph2[lc]);
359 delete ph2[lc];
360 sprintf(buftag,"hexpV%d",(int)lc);
361 po << PPFNameTag(buftag) << (*phexp[lc]);
362 delete phexp[lc];
363 sprintf(buftag,"saturV%d",(int)lc);
364 po << PPFNameTag(buftag) << (*psatur[lc]);
365 sprintf(buftag,"satfreqV%d",(int)lc);
366 po << PPFNameTag(buftag) << (*pvsatfreq[lc]);
367 delete psatur[lc];
368 delete pvsatfreq[lc];
369 }
370 nzm_[lc]=0;
371 }
372 ts.SetNow();
373 // Calcul / impression fraction des paquets avec same-framecounter
374 int fracsame=0;
375 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
376 int totfracsame=0;
377 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
378 filog << ts << " : proc file (meanspectra) " << fname << endl;
379 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
380 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
381 << " -> " << fracsame << " % )" << endl;
382 cout << " MonitorProc::procData1C() " << ts << " : created file " << fname << endl;
383 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
384 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
385 << " -> " << fracsame << " % )" << endl;
386 }
387 return 0;
388}
389
390/* --Methode-- */
391int MonitorProc::procData2C(ofstream& filog)
392{
393 BRPaqChecker pcheck[MAXNBFIB]; // Verification/comptage des paquets
394// Initialisation pour calcul FFT
395 uint_4 paqsz = memgr.PaqSize();
396 BRPaquet pq(paqsz);
397 TVector<r_4> vx(pq.DataSize()/2);
398 vx = (r_4)(0.);
399 TVector< complex<r_4> > cfour(pq.DataSize()/4+1); // composant TF
400
401 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
402 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
403
404 // Histo pour valeurs des bytes dans les paquets
405 Histo* phist[2*MAXNBFIB];
406 Histo2D* ph2[2*MAXNBFIB];
407 Histo* phexp[2*MAXNBFIB];
408 TMatrix<r_4>* psatur[2*MAXNBFIB];
409 TVector<uint_4>* pvsatfreq[2*MAXNBFIB];
410 for(int ifib=0; ifib<2*MAXNBFIB; ifib++) {
411 phist[ifib]=NULL; ph2[ifib]=NULL; phexp[ifib]=NULL; psatur[ifib]=NULL;
412 }
413 if (par_.fgdatafft) { // data de type FFT
414 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
415 phist[lc] = new Histo(-128.5,+128.5,257);
416 ph2[lc] = new Histo2D(-128.5,+128.5,257, -128.5,+128.5,257);
417 phexp[lc] = new Histo(-128.5,+128.5,257);
418 psatur[lc] = new TMatrix<r_4>(3,3);
419 pvsatfreq[lc] = new TVector<uint_4>(spectre_.NCols());
420 *psatur[lc] = 0.;
421 }
422 }
423 else {
424 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++)
425 phist[lc] = new Histo(-0.5,256.5,257);
426 }
427
428 TimeStamp ts;
429 char fname[512];
430 nfiles_ = 0;
431 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
432 if (stop_) break;
433 if (memgr.GetRunState() == MemZR_Stopped) break;
434
435 int mid = memgr.FindMemZoneId(mmact_); // MemZA_Proc
436 Byte* buffg = memgr.GetMemZone(mid);
437 if (buffg == NULL) {
438 cout << " MonitorProc::procData2C()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
439 break;
440 }
441 if ((step_>1)&&(kmz%step_ != 0)) {
442 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
443 continue;
444 }
445 sa_size_t lc=0;
446 Byte* fbuff[MAXNBFIB];
447 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) { // Boucle sur les fibres
448 fbuff[fib] = memgr.GetMemZone(mid,fib);
449 if (fbuff[fib] == NULL) { // cela ne devrait pas arriver
450 cout << " MonitorProc::procData2C()/ERROR memgr.GetMemZone(" << mid << "," << fib << ") -> NULL" << endl;
451 return 9;
452 }
453 }
454
455 // DBG-ATTENTION : freq 0 pour expo full FFT si reordonne
456 int idxexpo=(par_.fgfftshrink)?1023:0; // exposant FFT shrink 1024 freq, ou freq 0 pour full-FFT
457
458 cpaqok_ = (uint_1)0;
459 curfc_ = (uint_8)0;
460 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) {
461 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
462 BRPaquet paq(fbuff[fib]+i*paqsz, paqsz);
463 bool pqok=pcheck[fib].Check(paq,curfc_(fib,i)); // Verification du paquet / FrameCounter
464 if (!pqok) continue;
465 cpaqok_(fib,i) = 1;
466 sa_size_t lc=2*fib;
467 if (par_.fgdatafft) { // Traitement data de type FFT
468 TwoByteComplex* tbcp=paq.Data1C();
469
470 // exposant FFT
471 phexp[lc]->Add((r_4)tbcp[idxexpo].realD());
472
473 for(sa_size_t j=1; j<spectre_.NCols(); j++) {
474 phist[lc]->Add((r_4)tbcp[j].realD());
475 phist[lc]->Add((r_4)tbcp[j].imagD());
476 ph2[lc]->Add((r_4)tbcp[j].realD(), (r_4)tbcp[j].imagD());
477 // comptage saturation
478 int sir=0, sii=0; // saturation partie reelle, partie imaginaire
479 if (tbcp[j].realI()==-128) sir=-1;
480 else if (tbcp[j].realI()==127) sir=1;
481 if (tbcp[j].imagI()==-128) sii=-1;
482 else if (tbcp[j].imagI()==127) sii=1;
483 (*psatur[lc])(sir+1,sii+1)+=1.;
484 if ((sir!=0)||(sii!=0)) (*pvsatfreq[lc])(j)+=1;
485 spectre_(lc,j) += Zmod2(tbcp[j]);
486 }
487 nzm_[lc]++;
488 tbcp=paq.Data2C(); lc++;
489 phexp[lc]->Add((r_4)tbcp[idxexpo].realD());
490 for(sa_size_t j=1; j<spectre_.NCols(); j++) {
491 phist[lc]->Add((r_4)tbcp[j].realD());
492 phist[lc]->Add((r_4)tbcp[j].imagD());
493 ph2[lc]->Add((r_4)tbcp[j].realD(), (r_4)tbcp[j].imagD());
494 // comptage saturation
495 int sir=0, sii=0; // saturation partie reelle, partie imaginaire
496 if (tbcp[j].realI()==-128) sir=-1;
497 else if (tbcp[j].realI()==127) sir=1;
498 if (tbcp[j].imagI()==-128) sii=-1;
499 else if (tbcp[j].imagI()==127) sii=1;
500 (*psatur[lc])(sir+1,sii+1)+=1.;
501 if ((sir!=0)||(sii!=0)) (*pvsatfreq[lc])(j)+=1;
502
503 spectre_(lc,j) += Zmod2(tbcp[j]);
504 }
505 nzm_[lc]++;
506 }
507 else { // Traitement RawData
508 for(sa_size_t j=0; j<vx.Size(); j++) {
509 phist[lc]->Add((r_8)(*(paq.Data1()+j)));
510 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
511 }
512 fftwf_execute(plan);
513 // ffts_.FFTForward(vx, cfour_);
514 for(sa_size_t j=0; j<spectre_.NCols(); j++)
515 spectre_(lc,j) += Zmod2(cfour(j+1));
516 nzm_[lc]++; lc++;
517 for(sa_size_t j=0; j<vx.Size(); j++) {
518 phist[lc]->Add((r_8)(*(paq.Data2()+j)));
519 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
520 }
521 fftwf_execute(plan);
522 // ffts_.FFTForward(vx, cfour_);
523 for(sa_size_t j=0; j<spectre_.NCols(); j++)
524 spectre_(lc,j) += Zmod2(cfour(j+1));
525 nzm_[lc]++;
526 }
527 } // FIN de la boucle sur les paquets
528 } // Boucle sur les fibres
529 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
530 CheckFrameCounters();
531
532 nblocproc_ ++;
533 totnprocpaq_ += memgr.NbPaquets(); nprocpaq_ += memgr.NbPaquets();
534 bool fgnzm=true;
535 for(int lc=0; lc<2*memgr.NbFibres(); lc++)
536 if (nzm_[lc]<nmean_) fgnzm=false;
537
538 if (fgnzm) {
539 char buff[32];
540 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
541 spectre_.Row(lc) /= (r_4)nzm_[lc];
542 sprintf(buff,"NPaqMoy%d",(int)lc);
543 spectre_.Info()[buff] = nzm_[lc];
544 }
545 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
546 nfiles_++;
547 POutPersist po(fname);
548 po << PPFNameTag("spectre") << spectre_;
549 spectre_ = (r_4)(0.);
550 char buftag[32];
551 for(int lc=0; lc<2*memgr.NbFibres(); lc++) {
552 sprintf(buftag,"hvalV%d",(int)lc);
553 po << PPFNameTag(buftag) << (*phist[lc]);
554 phist[lc]->Zero();
555 if (par_.fgdatafft) { // data de type FFT
556 sprintf(buftag,"h2dV%d",(int)lc);
557 po << PPFNameTag(buftag) << (*ph2[lc]);
558 ph2[lc]->Zero();
559 sprintf(buftag,"hexpV%d",(int)lc);
560 po << PPFNameTag(buftag) << (*phexp[lc]);
561 phexp[lc]->Zero();
562 sprintf(buftag,"saturV%d",(int)lc);
563 po << PPFNameTag(buftag) << (*psatur[lc]);
564 (*psatur[lc])=0.;
565 sprintf(buftag,"satfreqV%d",(int)lc);
566 po << PPFNameTag(buftag) << (*pvsatfreq[lc]);
567 (*pvsatfreq[lc])=0;
568 }
569 nzm_[lc]=0;
570 }
571 ts.SetNow();
572 // Calcul / impression fraction des paquets avec same-framecounter
573 int fracsame=0;
574 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
575 int totfracsame=0;
576 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
577 filog << ts << " : proc file (meanspectra) " << fname << endl;
578 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
579 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
580 << " -> " << fracsame << " % )" << endl;
581 cout << " MonitorProc::procData2C() " << ts << " : created file " << fname << endl;
582 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
583 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
584 << " -> " << fracsame << " % )" << endl;
585 nprocpaq_=npaqsamefc_=0;
586 }
587 } // Fin de boucle sur les kmz ( bloc MemZoneMgr a traiter )
588
589 bool fgnzm=false;
590 for(int lc=0; lc<2*memgr.NbFibres(); lc++)
591 if (nzm_[lc]>0) fgnzm=true;
592
593 if (fgnzm) {
594 char buff[32];
595 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
596 if (nzm_[lc]>0) spectre_.Row(lc) /= (r_4)nzm_[lc];
597 sprintf(buff,"NPaqMoy%d",(int)lc);
598 spectre_.Info()[buff] = nzm_[lc];
599 }
600 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
601 POutPersist po(fname);
602 po << PPFNameTag("spectre") << spectre_;
603 spectre_ = (r_4)(0.);
604 char buftag[32];
605 for(int lc=0; lc<2*memgr.NbFibres(); lc++) {
606 sprintf(buftag,"hvalV%d",(int)lc);
607 po << PPFNameTag(buftag) << (*phist[lc]);
608 delete phist[lc];
609 if (par_.fgdatafft) { // data de type FFT
610 sprintf(buftag,"h2dV%d",(int)lc);
611 po << PPFNameTag(buftag) << (*ph2[lc]);
612 delete ph2[lc];
613 sprintf(buftag,"hexpV%d",(int)lc);
614 po << PPFNameTag(buftag) << (*phexp[lc]);
615 delete phexp[lc];
616 sprintf(buftag,"saturV%d",(int)lc);
617 po << PPFNameTag(buftag) << (*psatur[lc]);
618 sprintf(buftag,"satfreqV%d",(int)lc);
619 po << PPFNameTag(buftag) << (*pvsatfreq[lc]);
620 delete psatur[lc];
621 delete pvsatfreq[lc];
622 }
623 nzm_[lc]=0;
624 }
625 ts.SetNow();
626 // Calcul / impression fraction des paquets avec same-framecounter
627 int fracsame=0;
628 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
629 int totfracsame=0;
630 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
631 filog << ts << " : proc file (meanspectra) " << fname << endl;
632 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
633 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
634 << " -> " << fracsame << " % )" << endl;
635 cout << " MonitorProc::procData2C() " << ts << " : created file " << fname << endl;
636 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
637 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
638 << " -> " << fracsame << " % )" << endl;
639 }
640 return 0;
641}
642
643/* --Methode-- */
644int MonitorProc::CheckFrameCounters()
645{
646 if (memgr.NbFibres()<2) {
647 npaqsamefc_++; totnpaqsamefc_++;
648 return 99;
649 }
650 sa_size_t pidx[MAXNBFIB];
651 sa_size_t maxidx=curfc_.NCols();
652 uint_8 cfc=0;
653 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
654 pidx[fib]=0;
655 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
656 }
657
658 bool fgsuite=true;
659 while (fgsuite) { // Boucle sur l'ensemble des paquets
660 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
661 if ((pidx[fib]>=maxidx)||(cpaqok_(fib,pidx[fib])==0)) { fgsuite=false; break; }
662 }
663 if (!fgsuite) break;
664 cfc=curfc_(0,pidx[0]);
665 bool fgsame=true;
666 for(uint_4 fib=1; fib<curfc_.NRows(); fib++) {
667 if (curfc_(fib,pidx[fib])!=cfc) {
668 fgsame=false;
669 if (curfc_(fib,pidx[fib]) > cfc) cfc=curfc_(fib,pidx[fib]);
670 }
671 }
672 if (fgsame) {
673 npaqsamefc_++; totnpaqsamefc_++;
674 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
675 pidx[fib]++;
676 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
677 }
678 } // fin if (fgsame)
679 else { // else !fgsame
680 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) {
681 if (curfc_(fib,pidx[fib])<cfc) {
682 pidx[fib]++;
683 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
684 }
685 }
686 } // fin de else !fgsame
687 } // Fin de while sur l'ensemble des paquets
688 return 0;
689}
690
691
692static struct sigaction act;
693//-------------------------------------------------------
694// Classe thread de traitement avec 1 voie par frame
695//-------------------------------------------------------
696
697void DataProcSignal(int s)
698{
699 cout <<"............................................... receive CtrlC" << endl;
700
701}
702
703DataProc::DataProc(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
704 : memgr(mem)
705{
706 nmax_ = nmax;
707 nmean_ = nmean;
708 step_ = step;
709 stop_ = false;
710 path_ = path;
711 act.sa_handler=DataProcSignal;
712}
713
714
715void DataProc::Stop()
716{
717 stop_=true;
718 // cout <<" DataProc::Stop ... > STOP " << endl;
719}
720
721void DataProc::run()
722{
723
724 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
725 // sigaction(SIGINT,&act,NULL);
726 setRC(1);
727 try {
728 TimeStamp ts;
729 cout << " DataProc::run() - Starting " << ts << " NMaxMemZones=" << nmax_
730 << " NMean=" << nmean_ << " Step=" << step_ << endl;
731 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
732 char fname[512];
733 sprintf(fname,"%s/proc.log",path_.c_str());
734 ofstream filog(fname);
735 filog << " DataProc::run() - starting log file " << ts << endl;
736 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
737
738// Initialisation pour clcul FFT
739 TVector< complex<r_4> > cfour; // composant TF
740 uint_4 paqsz = memgr.PaqSize();
741 BRPaquet pq(NULL, NULL, paqsz);
742 TVector<r_4> vx(pq.DataSize());
743 vx = (r_4)(0.);
744 FFTPackServer ffts;
745 ffts.FFTForward(vx, cfour);
746 TVector<r_4> spectre;
747 spectre.ReSize(cfour.Size());
748
749 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
750 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
751
752 uint_4 ifile = 0;
753 uint_4 nzm = 0;
754 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
755 if (stop_) break;
756 int mid = memgr.FindMemZoneId(MemZA_Proc);
757 Byte* buff = memgr.GetMemZone(mid);
758 if (buff == NULL) {
759 cout << " DataProc::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
760 setRC(2);
761 return;
762 }
763 BRPaquet paq0(NULL, buff, paqsz);
764 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
765 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
766 Byte min = 255;
767 Byte max = 0;
768
769 for(sa_size_t j=0; j<vx.Size(); j++)
770 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
771 fftwf_execute(plan);
772 // ffts_.FFTForward(vx, cfour_);
773 for(sa_size_t j=0; j<spectre.Size(); j++)
774 spectre(j) += Zmod2(cfour(j));
775 nzm++;
776 }
777 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
778 spectre /= (r_4)(nzm);
779 spectre.Info()["NPaqMoy"] = nzm;
780 sprintf(fname,"%s/spectre%d.ppf",path_.c_str(),(int)ifile);
781 POutPersist po(fname);
782 po << spectre;
783 spectre = (r_4)(0.);
784 nzm = 0; ifile++;
785 ts.SetNow();
786 filog << ts << " : proc file " << fname << endl;
787 cout << " DataProc::run() " << ts << " : created file " << fname << endl;
788 }
789
790 memgr.FreeMemZone(mid, MemZS_Proc);
791 }
792 }
793 catch (std::exception& exc) {
794 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
795 setRC(3);
796 return;
797 }
798 catch(...) {
799 cout << " DataProc::run()/catched unknown ... exception " << endl;
800 setRC(4);
801 return;
802 }
803 setRC(0);
804 return;
805}
806
807//---------------------------------------------------------------
808// Classe thread de traitement donnees ADC avec 2 voies par frame
809//---------------------------------------------------------------
810
811DataProc2C::DataProc2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
812 : memgr(mem)
813{
814 nmax_ = nmax;
815 nmean_ = nmean;
816 step_ = step;
817 stop_ = false;
818 path_ = path;
819 act.sa_handler=DataProcSignal;
820}
821void DataProc2C::Stop()
822{
823 stop_=true;
824 // cout <<" DataProc2C::Stop ... > STOP " << endl;
825}
826
827void DataProc2C::run()
828{
829 // sigaction(SIGINT,&act,NULL);
830 setRC(1);
831 try {
832 TimeStamp ts;
833 cout << " DataProc2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
834 << " NMean=" << nmean_ << " Step=" << step_ << endl;
835 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
836 char fname[512];
837 sprintf(fname,"%s/proc.log",path_.c_str());
838 ofstream filog(fname);
839 filog << " DataProc2C::run() - starting log file " << ts << endl;
840 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
841
842// Initialisation pour clcul FFT
843 TVector< complex<r_4> > cfour; // composant TF
844 uint_4 paqsz = memgr.PaqSize();
845 BRPaquet pq(NULL, NULL, paqsz);
846 TVector<r_4> vx(pq.DataSize()/2);
847 vx = (r_4)(0.);
848 FFTPackServer ffts;
849 ffts.FFTForward(vx, cfour);
850 TVector<r_4> spectreV1, spectreV2;
851 spectreV1.ReSize(cfour.Size());
852 spectreV2.ReSize(cfour.Size());
853
854 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
855 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
856
857 uint_4 ifile = 0;
858 uint_4 nzm = 0;
859 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
860 if (stop_) break;
861 int mid = memgr.FindMemZoneId(MemZA_Proc);
862 Byte* buff = memgr.GetMemZone(mid);
863 if (buff == NULL) {
864 cout << " DataProc2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
865 setRC(2);
866 return;
867 }
868 BRPaquet paq0(NULL, buff, paqsz);
869 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
870 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
871 Byte min = 255;
872 Byte max = 0;
873
874 for(sa_size_t j=0; j<vx.Size(); j++)
875 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
876 fftwf_execute(plan);
877 // ffts_.FFTForward(vx, cfour_);
878 for(sa_size_t j=0; j<spectreV1.Size(); j++)
879 spectreV1(j) += Zmod2(cfour(j));
880
881 for(sa_size_t j=0; j<vx.Size(); j++)
882 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
883 fftwf_execute(plan);
884 // ffts_.FFTForward(vx, cfour_);
885 for(sa_size_t j=0; j<spectreV2.Size(); j++)
886 spectreV2(j) += Zmod2(cfour(j));
887
888 nzm++;
889 }
890 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
891 spectreV1 /= (r_4)(nzm);
892 spectreV2 /= (r_4)(nzm);
893 spectreV1.Info()["NPaqMoy"] = nzm;
894 spectreV2.Info()["NPaqMoy"] = nzm;
895 {
896 sprintf(fname,"%s/spectre2C_%d.ppf",path_.c_str(),(int)ifile);
897 POutPersist po(fname);
898 po << PPFNameTag("specV1") << spectreV1;
899 po << PPFNameTag("specV2") << spectreV2;
900 }
901 spectreV1 = (r_4)(0.);
902 spectreV2 = (r_4)(0.);
903 nzm = 0; ifile++;
904 ts.SetNow();
905 filog << ts << " : proc file " << fname << endl;
906 cout << " DataProc2C::run() " << ts << " : created file " << fname << endl;
907 }
908
909 memgr.FreeMemZone(mid, MemZS_Proc);
910 }
911 }
912 catch (std::exception& exc) {
913 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
914 setRC(3);
915 return;
916 }
917 catch(...) {
918 cout << " DataProc2C::run()/catched unknown ... exception " << endl;
919 setRC(4);
920 return;
921 }
922 setRC(0);
923 return;
924}
925
926
927
928
929//---------------------------------------------------------------
930// Classe thread de traitement donnees FFT avec 2 voies par frame
931//---------------------------------------------------------------
932
933inline r_4 Zmod2TwoByte(TwoByteComplex z)
934{ return (z.realD()*z.realD()+z.imagD()*z.imagD()); }
935
936DataProcFFT2C::DataProcFFT2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
937 : memgr(mem)
938{
939 nmax_ = nmax;
940 nmean_ = nmean;
941 step_ = step;
942 stop_ = false;
943 path_ = path;
944 act.sa_handler=DataProcSignal;
945}
946void DataProcFFT2C::Stop()
947{
948 stop_=true;
949 // cout <<" DataProcFFT2C::Stop ... > STOP " << endl;
950}
951
952void DataProcFFT2C::run()
953{
954 // sigaction(SIGINT,&act,NULL);
955 setRC(1);
956 try {
957 TimeStamp ts;
958 cout << " DataProcFFT2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
959 << " NMean=" << nmean_ << " Step=" << step_ << endl;
960 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
961 char fname[512];
962 sprintf(fname,"%s/proc.log",path_.c_str());
963 ofstream filog(fname);
964 filog << " DataProcFFT2C::run() - starting log file " << ts << endl;
965 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
966
967// Initialisation pour clcul FFT
968 TVector< complex<r_4> > cfour; // composant TF
969 uint_4 paqsz = memgr.PaqSize();
970 BRPaquet pq(NULL, NULL, paqsz);
971
972 TVector<r_4> spectreV1(pq.DataSize()/4+1), spectreV2(pq.DataSize()/4+1);
973
974 uint_4 ifile = 0;
975 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
976 if (stop_ ) break;
977 int mid = memgr.FindMemZoneId(MemZA_Proc);
978 Byte* buff = memgr.GetMemZone(mid);
979 if (buff == NULL) {
980 cout << " DataProcFFT2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
981 setRC(2);
982 return;
983 }
984 BRPaquet paq0(NULL, buff, paqsz);
985 uint_4 nzm = 0;
986 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
987 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
988 Byte min = 255;
989 Byte max = 0;
990
991 TwoByteComplex* zz;
992 // Traitement Voie 1
993 zz = (TwoByteComplex*)paq.Data1();
994 spectreV1(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
995 for(sa_size_t j=1; j<spectreV1.Size()-1; j++) spectreV1(j) += Zmod2TwoByte(zz[j]);
996
997 spectreV1(spectreV1.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
998
999 // Traitement Voie 2
1000 zz = (TwoByteComplex*)paq.Data2();
1001 spectreV2(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
1002 for(sa_size_t j=1; j<spectreV2.Size()-1; j++) spectreV2(j) += Zmod2TwoByte(zz[j]);
1003
1004 spectreV2(spectreV2.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
1005
1006 nzm++;
1007 }
1008 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
1009 spectreV1 /= (r_4)(nzm);
1010 spectreV2 /= (r_4)(nzm);
1011 spectreV1.Info()["NPaqMoy"] = nzm;
1012 spectreV2.Info()["NPaqMoy"] = nzm;
1013 {
1014 sprintf(fname,"%s/spectreFFT2C_%d.ppf",path_.c_str(),(int)ifile);
1015 POutPersist po(fname);
1016 po << PPFNameTag("specV1") << spectreV1;
1017 po << PPFNameTag("specV2") << spectreV2;
1018 }
1019 spectreV1 = (r_4)(0.);
1020 spectreV2 = (r_4)(0.);
1021 nzm = 0; ifile++;
1022 ts.SetNow();
1023 filog << ts << " : proc file " << fname << endl;
1024 cout << " DataProcFFT2C::run() " << ts << " : created file " << fname << endl;
1025 }
1026
1027 memgr.FreeMemZone(mid, MemZS_Proc);
1028 }
1029 }
1030 catch (std::exception& exc) {
1031 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
1032 setRC(3);
1033 return;
1034 }
1035 catch(...) {
1036 cout << " DataProcFFT2C::run()/catched unknown ... exception " << endl;
1037 setRC(4);
1038 return;
1039 }
1040 setRC(0);
1041 return;
1042}
Note: See TracBrowser for help on using the repository browser.