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

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

Amelioration code thread monitoring pour tenir compte du mode fftshrink + calcul matrice saturation, cmv+Reza

File size: 25.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& logf)
158{
159 cout << " MonitorProc::procData1C() - NOT IMPLEMENTED -> Rc=67" << endl;
160 logf << " MonitorProc::procData1C() - NOT IMPLEMENTED -> Rc=67" << endl;
161 return 67;
162}
163
164/* --Methode-- */
165int MonitorProc::procData2C(ofstream& filog)
166{
167 BRPaqChecker pcheck[MAXNBFIB]; // Verification/comptage des paquets
168// Initialisation pour calcul FFT
169 uint_4 paqsz = memgr.PaqSize();
170 BRPaquet pq(paqsz);
171 TVector<r_4> vx(pq.DataSize()/2);
172 vx = (r_4)(0.);
173 TVector< complex<r_4> > cfour(pq.DataSize()/4+1); // composant TF
174
175 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
176 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
177
178 // Histo pour valeurs des bytes dans les paquets
179 Histo* phist[2*MAXNBFIB];
180 Histo2D* ph2[2*MAXNBFIB];
181 Histo* phexp[2*MAXNBFIB];
182 TMatrix<r_4>* psatur[2*MAXNBFIB];
183 for(int ifib=0; ifib<2*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<2*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 *psatur[lc] = 0.;
193 }
194 }
195 else {
196 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++)
197 phist[lc] = new Histo(-0.5,256.5,257);
198 }
199
200 TimeStamp ts;
201 char fname[512];
202 nfiles_ = 0;
203 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
204 if (stop_) break;
205 if (memgr.GetRunState() == MemZR_Stopped) break;
206
207 int mid = memgr.FindMemZoneId(mmact_); // MemZA_Proc
208 Byte* buffg = memgr.GetMemZone(mid);
209 if (buffg == NULL) {
210 cout << " MonitorProc::procData2C()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
211 break;
212 }
213 if ((step_>1)&&(kmz%step_ != 0)) {
214 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
215 continue;
216 }
217 sa_size_t lc=0;
218 Byte* fbuff[MAXNBFIB];
219 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) { // Boucle sur les fibres
220 fbuff[fib] = memgr.GetMemZone(mid,fib);
221 if (fbuff[fib] == NULL) { // cela ne devrait pas arriver
222 cout << " MonitorProc::procData2C()/ERROR memgr.GetMemZone(" << mid << "," << fib << ") -> NULL" << endl;
223 return 9;
224 }
225 }
226
227 // DBG-ATTENTION : freq 0 pour expo full FFT si reordonne
228 int idxexpo=(par_.fgfftshrink)?1023:0; // exposant FFT shrink 1024 freq, ou freq 0 pour full-FFT
229
230 cpaqok_ = (uint_1)0;
231 curfc_ = (uint_8)0;
232 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) {
233 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
234 BRPaquet paq(fbuff[fib]+i*paqsz, paqsz);
235 bool pqok=pcheck[fib].Check(paq,curfc_(fib,i)); // Verification du paquet / FrameCounter
236 if (!pqok) continue;
237 cpaqok_(fib,i) = 1;
238 sa_size_t lc=2*fib;
239 if (par_.fgdatafft) { // Traitement data de type FFT
240 TwoByteComplex* tbcp=paq.Data1C();
241
242 // exposant FFT
243 phexp[lc]->Add((r_4)tbcp[idxexpo].realD());
244
245 for(sa_size_t j=1; j<spectre_.NCols(); j++) {
246 phist[lc]->Add((r_4)tbcp[j].realD());
247 phist[lc]->Add((r_4)tbcp[j].imagD());
248 ph2[lc]->Add((r_4)tbcp[j].realD(), (r_4)tbcp[j].imagD());
249 // comptage saturation
250 int sir=0, sii=0; // saturation partie reelle, partie imaginaire
251 if (tbcp[j].realI()==-128) sir=-1;
252 else if (tbcp[j].realI()==127) sir=1;
253 if (tbcp[j].imagI()==-128) sii=-1;
254 else if (tbcp[j].imagI()==127) sii=1;
255 (*psatur[lc])(sir+1,sii+1)+=1.;
256
257 spectre_(lc,j) += Zmod2(tbcp[j]);
258 }
259 nzm_[lc]++;
260 tbcp=paq.Data2C(); lc++;
261 phexp[lc]->Add((r_4)tbcp[idxexpo].realD());
262 for(sa_size_t j=1; j<spectre_.NCols(); j++) {
263 phist[lc]->Add((r_4)tbcp[j].realD());
264 phist[lc]->Add((r_4)tbcp[j].imagD());
265 ph2[lc]->Add((r_4)tbcp[j].realD(), (r_4)tbcp[j].imagD());
266 // comptage saturation
267 int sir=0, sii=0; // saturation partie reelle, partie imaginaire
268 if (tbcp[j].realI()==-128) sir=-1;
269 else if (tbcp[j].realI()==127) sir=1;
270 if (tbcp[j].imagI()==-128) sii=-1;
271 else if (tbcp[j].imagI()==127) sii=1;
272 (*psatur[lc])(sir+1,sii+1)+=1.;
273
274 spectre_(lc,j) += Zmod2(tbcp[j]);
275 }
276 nzm_[lc]++;
277 }
278 else { // Traitement RawData
279 for(sa_size_t j=0; j<vx.Size(); j++) {
280 phist[lc]->Add((r_8)(*(paq.Data1()+j)));
281 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
282 }
283 fftwf_execute(plan);
284 // ffts_.FFTForward(vx, cfour_);
285 for(sa_size_t j=0; j<spectre_.NCols(); j++)
286 spectre_(lc,j) += Zmod2(cfour(j+1));
287 nzm_[lc]++; lc++;
288 for(sa_size_t j=0; j<vx.Size(); j++) {
289 phist[lc]->Add((r_8)(*(paq.Data2()+j)));
290 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
291 }
292 fftwf_execute(plan);
293 // ffts_.FFTForward(vx, cfour_);
294 for(sa_size_t j=0; j<spectre_.NCols(); j++)
295 spectre_(lc,j) += Zmod2(cfour(j+1));
296 nzm_[lc]++;
297 }
298 } // FIN de la boucle sur les paquets
299 } // Boucle sur les fibres
300 memgr.FreeMemZone(mid, mmsta_); // MemZS_Proc);
301 CheckFrameCounters();
302
303 nblocproc_ ++;
304 totnprocpaq_ += memgr.NbPaquets(); nprocpaq_ += memgr.NbPaquets();
305 bool fgnzm=true;
306 for(int lc=0; lc<2*memgr.NbFibres(); lc++)
307 if (nzm_[lc]<nmean_) fgnzm=false;
308
309 if (fgnzm) {
310 char buff[32];
311 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
312 spectre_.Row(lc) /= (r_4)nzm_[lc];
313 sprintf(buff,"NPaqMoy%d",(int)lc);
314 spectre_.Info()[buff] = nzm_[lc];
315 }
316 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
317 nfiles_++;
318 POutPersist po(fname);
319 po << PPFNameTag("spectre") << spectre_;
320 spectre_ = (r_4)(0.);
321 char buftag[32];
322 for(int lc=0; lc<2*memgr.NbFibres(); lc++) {
323 sprintf(buftag,"hvalV%d",(int)lc);
324 po << PPFNameTag(buftag) << (*phist[lc]);
325 phist[lc]->Zero();
326 if (par_.fgdatafft) { // data de type FFT
327 sprintf(buftag,"h2dV%d",(int)lc);
328 po << PPFNameTag(buftag) << (*ph2[lc]);
329 ph2[lc]->Zero();
330 sprintf(buftag,"hexpV%d",(int)lc);
331 po << PPFNameTag(buftag) << (*phexp[lc]);
332 phexp[lc]->Zero();
333 sprintf(buftag,"saturV%d",(int)lc);
334 po << PPFNameTag(buftag) << (*psatur[lc]);
335 cout << " Matrice Saturation Voie[" << lc << "] : " << (*psatur[lc]);
336 if (nzm_[lc]>0) {
337 (*psatur[lc]) /= ((r_4)nzm_[lc]*0.01);
338 cout << " Matrice Saturation Pourcentage Voie[" << lc << "] : " << (*psatur[lc]);
339 }
340 (*psatur[lc])=0.;
341 }
342 nzm_[lc]=0;
343 }
344 ts.SetNow();
345 // Calcul / impression fraction des paquets avec same-framecounter
346 int fracsame=0;
347 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
348 int totfracsame=0;
349 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
350 filog << ts << " : proc file (meanspectra) " << fname << endl;
351 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
352 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
353 << " -> " << fracsame << " % )" << endl;
354 cout << " MonitorProc::procData2C() " << ts << " : created file " << fname << endl;
355 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
356 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
357 << " -> " << fracsame << " % )" << endl;
358 nprocpaq_=npaqsamefc_=0;
359 }
360 } // Fin de boucle sur les kmz ( bloc MemZoneMgr a traiter )
361
362 bool fgnzm=false;
363 for(int lc=0; lc<2*memgr.NbFibres(); lc++)
364 if (nzm_[lc]>0) fgnzm=true;
365
366 if (fgnzm) {
367 char buff[32];
368 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
369 if (nzm_[lc]>0) spectre_.Row(lc) /= (r_4)nzm_[lc];
370 sprintf(buff,"NPaqMoy%d",(int)lc);
371 spectre_.Info()[buff] = nzm_[lc];
372 }
373 sprintf(fname,"%s/meanspec%d.ppf",path_.c_str(),(int)nfiles_);
374 POutPersist po(fname);
375 po << PPFNameTag("spectre") << spectre_;
376 spectre_ = (r_4)(0.);
377 char buftag[32];
378 for(int lc=0; lc<2*memgr.NbFibres(); lc++) {
379 sprintf(buftag,"hvalV%d",(int)lc);
380 po << PPFNameTag(buftag) << (*phist[lc]);
381 delete phist[lc];
382 if (par_.fgdatafft) { // data de type FFT
383 sprintf(buftag,"h2dV%d",(int)lc);
384 po << PPFNameTag(buftag) << (*ph2[lc]);
385 delete ph2[lc];
386 sprintf(buftag,"hexpV%d",(int)lc);
387 po << PPFNameTag(buftag) << (*phexp[lc]);
388 delete phexp[lc];
389 sprintf(buftag,"saturV%d",(int)lc);
390 po << PPFNameTag(buftag) << (*psatur[lc]);
391 cout << " Matrice Saturation Voie[" << lc << "] : " << (*psatur[lc]);
392 if (nzm_[lc]>0) {
393 (*psatur[lc]) /= ((r_4)nzm_[lc]*0.01);
394 cout << " Matrice Saturation Pourcentage Voie[" << lc << "] : " << (*psatur[lc]);
395 }
396 (*psatur[lc])=0.;
397 }
398 nzm_[lc]=0;
399 }
400 ts.SetNow();
401 // Calcul / impression fraction des paquets avec same-framecounter
402 int fracsame=0;
403 if (nprocpaq_>0) fracsame=100*npaqsamefc_/nprocpaq_;
404 int totfracsame=0;
405 if (totnprocpaq_>0) totfracsame=100*totnpaqsamefc_/totnprocpaq_;
406 filog << ts << " : proc file (meanspectra) " << fname << endl;
407 filog << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
408 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
409 << " -> " << fracsame << " % )" << endl;
410 cout << " MonitorProc::procData2C() " << ts << " : created file " << fname << endl;
411 cout << " NBlocProcessed=" << nblocproc_ << " NSameFC=" << totnpaqsamefc_ << " / " << totnprocpaq_
412 << " -> " << totfracsame << " % (LastPqs: " << npaqsamefc_ << " / " << nprocpaq_
413 << " -> " << fracsame << " % )" << endl;
414 }
415
416 for(sa_size_t lc=0; lc<2*memgr.NbFibres(); lc++) {
417 if (phist[lc]) delete phist[lc];
418 if (ph2[lc]) delete ph2[lc];
419 if (phexp[lc]) delete phexp[lc];
420 if (psatur[lc]) delete psatur[lc];
421 }
422
423 return 0;
424}
425
426/* --Methode-- */
427int MonitorProc::CheckFrameCounters()
428{
429 if (memgr.NbFibres()<2) {
430 npaqsamefc_++; totnpaqsamefc_++;
431 return 99;
432 }
433 sa_size_t pidx[MAXNBFIB];
434 sa_size_t maxidx=curfc_.NCols();
435 uint_8 cfc=0;
436 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
437 pidx[fib]=0;
438 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
439 }
440
441 bool fgsuite=true;
442 while (fgsuite) { // Boucle sur l'ensemble des paquets
443 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
444 if ((pidx[fib]>=maxidx)||(cpaqok_(fib,pidx[fib])==0)) { fgsuite=false; break; }
445 }
446 if (!fgsuite) break;
447 cfc=curfc_(0,pidx[0]);
448 bool fgsame=true;
449 for(uint_4 fib=1; fib<curfc_.NRows(); fib++) {
450 if (curfc_(fib,pidx[fib])!=cfc) {
451 fgsame=false;
452 if (curfc_(fib,pidx[fib]) > cfc) cfc=curfc_(fib,pidx[fib]);
453 }
454 }
455 if (fgsame) {
456 npaqsamefc_++; totnpaqsamefc_++;
457 for(uint_4 fib=0; fib<curfc_.NRows(); fib++) {
458 pidx[fib]++;
459 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
460 }
461 } // fin if (fgsame)
462 else { // else !fgsame
463 for(uint_4 fib=0; fib<memgr.NbFibres(); fib++) {
464 if (curfc_(fib,pidx[fib])<cfc) {
465 pidx[fib]++;
466 while((pidx[fib]<maxidx)&&(cpaqok_(fib,pidx[fib])==0)) pidx[fib]++;
467 }
468 }
469 } // fin de else !fgsame
470 } // Fin de while sur l'ensemble des paquets
471 return 0;
472}
473
474
475static struct sigaction act;
476//-------------------------------------------------------
477// Classe thread de traitement avec 1 voie par frame
478//-------------------------------------------------------
479
480void DataProcSignal(int s)
481{
482 cout <<"............................................... receive CtrlC" << endl;
483
484}
485
486DataProc::DataProc(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
487 : memgr(mem)
488{
489 nmax_ = nmax;
490 nmean_ = nmean;
491 step_ = step;
492 stop_ = false;
493 path_ = path;
494 act.sa_handler=DataProcSignal;
495}
496
497
498void DataProc::Stop()
499{
500 stop_=true;
501 // cout <<" DataProc::Stop ... > STOP " << endl;
502}
503
504void DataProc::run()
505{
506
507 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
508 // sigaction(SIGINT,&act,NULL);
509 setRC(1);
510 try {
511 TimeStamp ts;
512 cout << " DataProc::run() - Starting " << ts << " NMaxMemZones=" << nmax_
513 << " NMean=" << nmean_ << " Step=" << step_ << endl;
514 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
515 char fname[512];
516 sprintf(fname,"%s/proc.log",path_.c_str());
517 ofstream filog(fname);
518 filog << " DataProc::run() - starting log file " << ts << endl;
519 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
520
521// Initialisation pour clcul FFT
522 TVector< complex<r_4> > cfour; // composant TF
523 uint_4 paqsz = memgr.PaqSize();
524 BRPaquet pq(NULL, NULL, paqsz);
525 TVector<r_4> vx(pq.DataSize());
526 vx = (r_4)(0.);
527 FFTPackServer ffts;
528 ffts.FFTForward(vx, cfour);
529 TVector<r_4> spectre;
530 spectre.ReSize(cfour.Size());
531
532 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
533 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
534
535 uint_4 ifile = 0;
536 uint_4 nzm = 0;
537 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
538 if (stop_) break;
539 int mid = memgr.FindMemZoneId(MemZA_Proc);
540 Byte* buff = memgr.GetMemZone(mid);
541 if (buff == NULL) {
542 cout << " DataProc::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
543 setRC(2);
544 return;
545 }
546 BRPaquet paq0(NULL, buff, paqsz);
547 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
548 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
549 Byte min = 255;
550 Byte max = 0;
551
552 for(sa_size_t j=0; j<vx.Size(); j++)
553 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
554 fftwf_execute(plan);
555 // ffts_.FFTForward(vx, cfour_);
556 for(sa_size_t j=0; j<spectre.Size(); j++)
557 spectre(j) += Zmod2(cfour(j));
558 nzm++;
559 }
560 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
561 spectre /= (r_4)(nzm);
562 spectre.Info()["NPaqMoy"] = nzm;
563 sprintf(fname,"%s/spectre%d.ppf",path_.c_str(),(int)ifile);
564 POutPersist po(fname);
565 po << spectre;
566 spectre = (r_4)(0.);
567 nzm = 0; ifile++;
568 ts.SetNow();
569 filog << ts << " : proc file " << fname << endl;
570 cout << " DataProc::run() " << ts << " : created file " << fname << endl;
571 }
572
573 memgr.FreeMemZone(mid, MemZS_Proc);
574 }
575 }
576 catch (std::exception& exc) {
577 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
578 setRC(3);
579 return;
580 }
581 catch(...) {
582 cout << " DataProc::run()/catched unknown ... exception " << endl;
583 setRC(4);
584 return;
585 }
586 setRC(0);
587 return;
588}
589
590//---------------------------------------------------------------
591// Classe thread de traitement donnees ADC avec 2 voies par frame
592//---------------------------------------------------------------
593
594DataProc2C::DataProc2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
595 : memgr(mem)
596{
597 nmax_ = nmax;
598 nmean_ = nmean;
599 step_ = step;
600 stop_ = false;
601 path_ = path;
602 act.sa_handler=DataProcSignal;
603}
604void DataProc2C::Stop()
605{
606 stop_=true;
607 // cout <<" DataProc2C::Stop ... > STOP " << endl;
608}
609
610void DataProc2C::run()
611{
612 // sigaction(SIGINT,&act,NULL);
613 setRC(1);
614 try {
615 TimeStamp ts;
616 cout << " DataProc2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
617 << " NMean=" << nmean_ << " Step=" << step_ << endl;
618 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
619 char fname[512];
620 sprintf(fname,"%s/proc.log",path_.c_str());
621 ofstream filog(fname);
622 filog << " DataProc2C::run() - starting log file " << ts << endl;
623 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
624
625// Initialisation pour clcul FFT
626 TVector< complex<r_4> > cfour; // composant TF
627 uint_4 paqsz = memgr.PaqSize();
628 BRPaquet pq(NULL, NULL, paqsz);
629 TVector<r_4> vx(pq.DataSize()/2);
630 vx = (r_4)(0.);
631 FFTPackServer ffts;
632 ffts.FFTForward(vx, cfour);
633 TVector<r_4> spectreV1, spectreV2;
634 spectreV1.ReSize(cfour.Size());
635 spectreV2.ReSize(cfour.Size());
636
637 fftwf_plan plan = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
638 (fftwf_complex *)cfour.Data(), FFTW_ESTIMATE);
639
640 uint_4 ifile = 0;
641 uint_4 nzm = 0;
642 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
643 if (stop_) break;
644 int mid = memgr.FindMemZoneId(MemZA_Proc);
645 Byte* buff = memgr.GetMemZone(mid);
646 if (buff == NULL) {
647 cout << " DataProc2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
648 setRC(2);
649 return;
650 }
651 BRPaquet paq0(NULL, buff, paqsz);
652 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
653 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
654 Byte min = 255;
655 Byte max = 0;
656
657 for(sa_size_t j=0; j<vx.Size(); j++)
658 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
659 fftwf_execute(plan);
660 // ffts_.FFTForward(vx, cfour_);
661 for(sa_size_t j=0; j<spectreV1.Size(); j++)
662 spectreV1(j) += Zmod2(cfour(j));
663
664 for(sa_size_t j=0; j<vx.Size(); j++)
665 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
666 fftwf_execute(plan);
667 // ffts_.FFTForward(vx, cfour_);
668 for(sa_size_t j=0; j<spectreV2.Size(); j++)
669 spectreV2(j) += Zmod2(cfour(j));
670
671 nzm++;
672 }
673 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
674 spectreV1 /= (r_4)(nzm);
675 spectreV2 /= (r_4)(nzm);
676 spectreV1.Info()["NPaqMoy"] = nzm;
677 spectreV2.Info()["NPaqMoy"] = nzm;
678 {
679 sprintf(fname,"%s/spectre2C_%d.ppf",path_.c_str(),(int)ifile);
680 POutPersist po(fname);
681 po << PPFNameTag("specV1") << spectreV1;
682 po << PPFNameTag("specV2") << spectreV2;
683 }
684 spectreV1 = (r_4)(0.);
685 spectreV2 = (r_4)(0.);
686 nzm = 0; ifile++;
687 ts.SetNow();
688 filog << ts << " : proc file " << fname << endl;
689 cout << " DataProc2C::run() " << ts << " : created file " << fname << endl;
690 }
691
692 memgr.FreeMemZone(mid, MemZS_Proc);
693 }
694 }
695 catch (std::exception& exc) {
696 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
697 setRC(3);
698 return;
699 }
700 catch(...) {
701 cout << " DataProc2C::run()/catched unknown ... exception " << endl;
702 setRC(4);
703 return;
704 }
705 setRC(0);
706 return;
707}
708
709
710
711
712//---------------------------------------------------------------
713// Classe thread de traitement donnees FFT avec 2 voies par frame
714//---------------------------------------------------------------
715
716inline r_4 Zmod2TwoByte(TwoByteComplex z)
717{ return (z.realD()*z.realD()+z.imagD()*z.imagD()); }
718
719DataProcFFT2C::DataProcFFT2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean, uint_4 step, uint_4 nmax)
720 : memgr(mem)
721{
722 nmax_ = nmax;
723 nmean_ = nmean;
724 step_ = step;
725 stop_ = false;
726 path_ = path;
727 act.sa_handler=DataProcSignal;
728}
729void DataProcFFT2C::Stop()
730{
731 stop_=true;
732 // cout <<" DataProcFFT2C::Stop ... > STOP " << endl;
733}
734
735void DataProcFFT2C::run()
736{
737 // sigaction(SIGINT,&act,NULL);
738 setRC(1);
739 try {
740 TimeStamp ts;
741 cout << " DataProcFFT2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
742 << " NMean=" << nmean_ << " Step=" << step_ << endl;
743 cout << " DataProc::run()... - Output Data Path: " << path_ << endl;
744 char fname[512];
745 sprintf(fname,"%s/proc.log",path_.c_str());
746 ofstream filog(fname);
747 filog << " DataProcFFT2C::run() - starting log file " << ts << endl;
748 filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
749
750// Initialisation pour clcul FFT
751 TVector< complex<r_4> > cfour; // composant TF
752 uint_4 paqsz = memgr.PaqSize();
753 BRPaquet pq(NULL, NULL, paqsz);
754
755 TVector<r_4> spectreV1(pq.DataSize()/4+1), spectreV2(pq.DataSize()/4+1);
756
757 uint_4 ifile = 0;
758 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
759 if (stop_ ) break;
760 int mid = memgr.FindMemZoneId(MemZA_Proc);
761 Byte* buff = memgr.GetMemZone(mid);
762 if (buff == NULL) {
763 cout << " DataProcFFT2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
764 setRC(2);
765 return;
766 }
767 BRPaquet paq0(NULL, buff, paqsz);
768 uint_4 nzm = 0;
769 for(uint_4 i=0; i<memgr.NbPaquets(); i+=step_) {
770 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
771 Byte min = 255;
772 Byte max = 0;
773
774 TwoByteComplex* zz;
775 // Traitement Voie 1
776 zz = (TwoByteComplex*)paq.Data1();
777 spectreV1(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
778 for(sa_size_t j=1; j<spectreV1.Size()-1; j++) spectreV1(j) += Zmod2TwoByte(zz[j]);
779
780 spectreV1(spectreV1.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
781
782 // Traitement Voie 2
783 zz = (TwoByteComplex*)paq.Data2();
784 spectreV2(0) += zz[0].realD()*zz[0].realD(); // Composante continue, partie reelle uniquement
785 for(sa_size_t j=1; j<spectreV2.Size()-1; j++) spectreV2(j) += Zmod2TwoByte(zz[j]);
786
787 spectreV2(spectreV2.Size()-1) += zz[0].imagD()*zz[0].imagD(); // Freq. Nyquist a N/2
788
789 nzm++;
790 }
791 if ((nzm >= nmean_) ||(kmz==(nmax_-1))) {
792 spectreV1 /= (r_4)(nzm);
793 spectreV2 /= (r_4)(nzm);
794 spectreV1.Info()["NPaqMoy"] = nzm;
795 spectreV2.Info()["NPaqMoy"] = nzm;
796 {
797 sprintf(fname,"%s/spectreFFT2C_%d.ppf",path_.c_str(),(int)ifile);
798 POutPersist po(fname);
799 po << PPFNameTag("specV1") << spectreV1;
800 po << PPFNameTag("specV2") << spectreV2;
801 }
802 spectreV1 = (r_4)(0.);
803 spectreV2 = (r_4)(0.);
804 nzm = 0; ifile++;
805 ts.SetNow();
806 filog << ts << " : proc file " << fname << endl;
807 cout << " DataProcFFT2C::run() " << ts << " : created file " << fname << endl;
808 }
809
810 memgr.FreeMemZone(mid, MemZS_Proc);
811 }
812 }
813 catch (std::exception& exc) {
814 cout << " DataProc::run()/catched std::exception " << exc.what() << endl;
815 setRC(3);
816 return;
817 }
818 catch(...) {
819 cout << " DataProcFFT2C::run()/catched unknown ... exception " << endl;
820 setRC(4);
821 return;
822 }
823 setRC(0);
824 return;
825}
Note: See TracBrowser for help on using the repository browser.