source: Sophya/trunk/AddOn/TAcq/brproc.cc@ 3645

Last change on this file since 3645 was 3645, checked in by ansari, 16 years ago

Traitement 4 canaux ds brproc.cc, mcrd.cc , mais il reste des problemes - Reza 01/06/2009

File size: 13.6 KB
Line 
1#include "racquproc.h"
2
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <fstream>
7#include <signal.h>
8
9#include "pexceptions.h"
10#include "tvector.h"
11#include "fioarr.h"
12#include "timestamp.h"
13#include "ctimer.h"
14#include "fftpserver.h"
15#include "fftwserver.h"
16
17#include "FFTW/fftw3.h"
18
19
20#include "pciewrap.h"
21#include "brpaqu.h"
22#include "brproc.h"
23
24
25//---------------------------------------------------------------
26// Classe thread de traitement donnees ADC avec 2 voies par frame
27//---------------------------------------------------------------
28
29/* --Methode-- */
30BRProcARaw2C::BRProcARaw2C(RAcqMemZoneMgr& mem, string& path, uint_4 nmean,
31 uint_4 nmax, bool fgnotrl, int card)
32 : memgr(mem)
33{
34 nmax_ = nmax;
35 nmean_ = nmean;
36 stop_ = false;
37 path_ = path;
38 fgnotrl_ = fgnotrl;
39 card_ = card;
40}
41
42/* --Methode-- */
43void BRProcARaw2C::Stop()
44{
45 stop_=true;
46 // cout <<" BRProcARaw2C::Stop ... > STOP " << endl;
47}
48
49
50static inline r_4 Zmod2(complex<r_4> z)
51{ return (z.real()*z.real()+z.imag()*z.imag()); }
52
53static inline string card2name_(int card)
54{
55 if (card==2) return " (Chan3,4) ";
56 else return " (Chan1,2) ";
57}
58/* --Methode-- */
59void BRProcARaw2C::run()
60{
61 setRC(1);
62 try {
63 Timer tm("BRProcARaw2C", false);
64 TimeStamp ts;
65 BRPaqChecker pcheck(~fgnotrl_); // Verification/comptage des paquets
66
67 size_t totnbytesout = 0;
68 size_t totnbytesproc = 0;
69
70 cout << " BRProcARaw2C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
71 << " NMean=" << nmean_ << card2name_(card_) << endl;
72 cout << " BRProcARaw2C::run()... - Output Data Path: " << path_ << endl;
73 char fname[512];
74// sprintf(fname,"%s/proc.log",path_.c_str());
75// ofstream filog(fname);
76// filog << " BRProcARaw2C::run() - starting log file " << ts << endl;
77// filog << " ... NMaxMemZones=" << nmax_ << " NMean=" << nmean_ << " Step=" << step_ << endl;
78
79// Initialisation pour calcul FFT
80 TVector< complex<r_4> > cfour1; // composant TF
81 uint_4 paqsz = memgr.PaqSize();
82 uint_4 procpaqsz = memgr.ProcPaqSize();
83 BRPaquet pq(NULL, NULL, paqsz);
84 TVector<r_4> vx(pq.DataSize()/2);
85 vx = (r_4)(0.);
86 FFTPackServer ffts;
87 ffts.FFTForward(vx, cfour1);
88 TVector< complex<r_4> > cfour2(cfour1.Size());
89
90 TVector<r_4> spectreV1(cfour1.Size());
91 TVector<r_4> spectreV2(cfour1.Size());
92 TVector< complex<r_4> > visiV12( cfour1.Size() );
93
94
95 fftwf_plan plan1 = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
96 (fftwf_complex*)cfour1.Data(), FFTW_ESTIMATE);
97 fftwf_plan plan2 = fftwf_plan_dft_r2c_1d(vx.Size(), vx.Data(),
98 (fftwf_complex*)cfour2.Data(), FFTW_ESTIMATE);
99
100 uint_4 ifile = 0;
101 uint_4 nzm = 0;
102 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
103 if (stop_) break;
104 int mid = memgr.FindMemZoneId(MemZA_ProcA);
105 Byte* buff = memgr.GetMemZone(mid);
106 if (buff == NULL) {
107 cout << " BRProcARaw2C::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
108 break;
109 }
110 Byte* procbuff = memgr.GetProcMemZone(mid);
111 if (procbuff == NULL) {
112 cout << " BRProcARaw2C::run()/ERROR memgr.GetProcMemZone(" << mid << ") -> NULL" << endl;
113 break;
114 }
115
116 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
117 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
118 if (!pcheck.Check(paq)) continue; // on ne traite que les paquets OK
119
120// Traitement voie 1
121 for(sa_size_t j=0; j<vx.Size(); j++)
122 vx(j) = (r_4)(*(paq.Data1()+j))-127.5;
123// fftwf_complex* coeff1 = (fftwf_complex*)(procbuff+i*procpaqsz);
124 fftwf_execute(plan1);
125// complex<r_4>* zp1 = (complex<r_4>*)(coeff1);
126// ffts.FFTForward(vx, cfour1);
127 for(sa_size_t j=0; j<spectreV1.Size(); j++)
128 spectreV1(j) += Zmod2(cfour1(j));
129 memcpy(procbuff+i*procpaqsz, cfour1.Data(), sizeof(complex<r_4>)*cfour1.Size());
130// Traitement voie 2
131 for(sa_size_t j=0; j<vx.Size(); j++)
132 vx(j) = (r_4)(*(paq.Data2()+j))-127.5;
133
134 fftwf_execute(plan2);
135
136 for(sa_size_t j=0; j<spectreV2.Size(); j++)
137 spectreV2(j) += Zmod2(cfour2(j)); // Zmod2(zp2[j]);
138 memcpy(procbuff+i*procpaqsz+procpaqsz/2, cfour2.Data(), sizeof(complex<r_4>)*cfour2.Size());
139
140// Calcul correlation (visibilite V1 * V2)
141 for(sa_size_t j=0; j<visiV12.Size(); j++)
142 visiV12(j)+=cfour1(j)*conj(cfour2(j));
143// for(sa_size_t j=0; j<visiV12.Size(); j++) visiV12(j)+=zp1[j]*zp2[j];
144 nzm++;
145 totnbytesproc += paq.DataSize();
146 totnbytesout += (2*sizeof(complex<r_4>)*cfour1.Size());
147
148 } // Fin de boucle sur les paquets d'une zone
149 if ((nzm >= nmean_) || ((kmz==(nmax_-1))&&(nzm>1))) {
150 spectreV1 /= (r_4)(nzm);
151 spectreV2 /= (r_4)(nzm);
152
153 visiV12 /= complex<r_4>((r_4)nzm, 0.);
154
155 spectreV1.Info()["NPaqMoy"] = nzm;
156 spectreV2.Info()["NPaqMoy"] = nzm;
157 visiV12.Info()["NPaqMoy"] = nzm;
158 {
159 sprintf(fname,"%s_%d.ppf",path_.c_str(),(int)ifile);
160 POutPersist po(fname);
161 string tag1="specV1";
162 string tag2="specV2";
163 string tag12="visiV12";
164 if (card_==2) {
165 tag1 = "specV3";
166 tag2 = "specV4";
167 tag12="visiV34";
168 }
169 po << PPFNameTag(tag1) << spectreV1;
170 po << PPFNameTag(tag2) << spectreV2;
171 po << PPFNameTag(tag12) << visiV12;
172 }
173 spectreV1 = (r_4)(0.);
174 spectreV2 = (r_4)(0.);
175 visiV12 = complex<r_4>(0., 0.);
176 nzm = 0; ifile++;
177// ts.SetNow();
178// filog << ts << " : proc file " << fname << endl;
179 cout << " BRProcARaw2C::run() created file " << fname << card2name_(card_) << endl;
180 }
181
182 memgr.FreeMemZone(mid, MemZS_ProcA);
183 } // Fin de boucle sur les zones a traiter
184 cout << " ------------ BRProcARaw2C::run() END " << card2name_(card_)
185 << " ------------ " << endl;
186 ts.SetNow();
187 tm.SplitQ();
188 cout << " TotalProc= " << totnbytesproc/(1024*1024) << " MBytes, rate= "
189 << (double)(totnbytesproc)/1024./tm.PartialElapsedTimems() << " MB/s"
190 << " ProcDataOut=" << totnbytesout/(1024*1024) << " MB" << endl;
191 cout << pcheck;
192 cout << " BRProcARaw2C::run()/Timing: " << card2name_(card_) << endl;
193 tm.Print();
194 cout << " ---------------------------------------------------------- " << endl;
195
196 }
197 catch (PException& exc) {
198 cout << " BRProcARaw2C::run()/catched PException " << exc.Msg() << endl;
199 setRC(3);
200 return;
201 }
202 catch(...) {
203 cout << " BRProcARaw2C::run()/catched unknown ... exception " << endl;
204 setRC(4);
205 return;
206 }
207 setRC(0);
208 return;
209}
210
211//---------------------------------------------------------------------
212// Classe thread de traitement 2 x 2 voies/frames (Apres BRProcARaw2C)
213//---------------------------------------------------------------------
214
215/* --Methode-- */
216BRProcBRaw4C::BRProcBRaw4C(RAcqMemZoneMgr& mem1, RAcqMemZoneMgr& mem2,
217 string& path, uint_4 nmean, uint_4 nmax, bool fgnotrl)
218 : memgr1(mem1), memgr2(mem2)
219{
220 nmax_ = nmax;
221 nmean_ = nmean;
222 stop_ = false;
223 path_ = path;
224 fgnotrl_ = fgnotrl;
225}
226
227/* --Methode-- */
228void BRProcBRaw4C::Stop()
229{
230 stop_=true;
231 // cout <<" BRProcBRaw4C::Stop ... > STOP " << endl;
232}
233
234
235/* --Methode-- */
236void BRProcBRaw4C::run()
237{
238 setRC(1);
239 try {
240 Timer tm("BRProcBRaw4C", false);
241 TimeStamp ts;
242 BRPaqChecker pcheck1(~fgnotrl_); // Verification/comptage des paquets
243 BRPaqChecker pcheck2(~fgnotrl_); // Verification/comptage des paquets
244
245 size_t totnbytesout = 0;
246 size_t totnbytesproc = 0;
247
248 cout << " BRProcBRaw4C::run() - Starting " << ts << " NMaxMemZones=" << nmax_
249 << " NMean=" << nmean_ << endl;
250 cout << " BRProcBRaw4C::run()... - Output Data Path: " << path_ << endl;
251
252 uint_4 paqsz = memgr1.PaqSize();
253 uint_4 procpaqsz = memgr1.ProcPaqSize();
254 if ((paqsz != memgr2.PaqSize())||(procpaqsz!= memgr2.ProcPaqSize())) {
255 cout << "BRProcBRaw4C::run()/ERROR : different paquet size -> stop \n ...(PaqSz1="
256 << paqsz << " Sz2=" << memgr2.PaqSize() << " ProcPaqSz1="
257 << procpaqsz << " Sz2=" << memgr2.ProcPaqSize() << " )" << endl;
258 setRC(9);
259 return;
260 }
261
262 TVector< complex<r_4> > cfour; // composant TF
263 BRPaquet pq(NULL, NULL, paqsz);
264 TVector<r_4> vx(pq.DataSize()/2);
265 vx = (r_4)(0.);
266 FFTPackServer ffts;
267 ffts.FFTForward(vx, cfour);
268
269 TVector< complex<r_4> > visiV13( cfour.Size() );
270 TVector< complex<r_4> > visiV14( cfour.Size() );
271 TVector< complex<r_4> > visiV23( cfour.Size() );
272 TVector< complex<r_4> > visiV24( cfour.Size() );
273
274 uint_4 nzm = 0;
275 uint_4 totnoksfc = 0;
276 uint_4 totnokpaq = 0;
277 uint_4 totnpaq = 0;
278 uint_4 ifile = 0;
279 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
280 uint_4 noksfc = 0;
281 uint_4 nokpaq = 0;
282 if (stop_) break;
283 int mid1 = memgr1.FindMemZoneId(MemZA_ProcB);
284 Byte* buff1 = memgr1.GetMemZone(mid1);
285 if (buff1 == NULL) {
286 cout << " BRProcBRaw4C::run()/ERROR memgr.GetMemZone(" << mid1 << ") -> NULL" << endl;
287 break;
288 }
289 Byte* procbuff1 = memgr1.GetProcMemZone(mid1);
290 if (procbuff1 == NULL) {
291 cout << " BRProcBRaw4C::run()/ERROR memgr.GetProcMemZone(" << mid1 << ") -> NULL" << endl;
292 break;
293 }
294 int mid2 = memgr2.FindMemZoneId(MemZA_ProcB);
295 Byte* buff2 = memgr2.GetMemZone(mid2);
296 if (buff1 == NULL) {
297 cout << " BRProcBRaw4C::run()/ERROR memgr.GetMemZone(" << mid2 << ") -> NULL" << endl;
298 break;
299 }
300 Byte* procbuff2 = memgr2.GetProcMemZone(mid2);
301 if (procbuff2 == NULL) {
302 cout << " BRProcBRaw4C::run()/ERROR memgr.GetProcMemZone(" << mid2 << ") -> NULL" << endl;
303 break;
304 }
305 uint_4 i1,i2;
306 i1=i2=0;
307 while((i1<memgr1.NbPaquets())&&(i2<memgr2.NbPaquets())) {
308 BRPaquet paq1(NULL, buff1+i1*paqsz, paqsz);
309 BRPaquet paq2(NULL, buff2+i2*paqsz, paqsz);
310 totnpaq++;
311// cout << " DBG["<<kmz<<"] i1,i2=" << i1 <<","<<i2<<" FC1,FC2=" <<paq1.FrameCounter()
312//<<","<<paq2.FrameCounter()<<endl;
313 // on ne traite que les paquets OK
314 if (!pcheck1.Check(paq1)) { i1++; continue; }
315 if (!pcheck2.Check(paq2)) { i2++; continue; }
316 nokpaq++;
317 if (paq1.FrameCounter()<paq2.FrameCounter()) { i1++; continue; }
318 if (paq2.FrameCounter()<paq1.FrameCounter()) { i2++; continue; }
319// cout << " DBG["<<kmz<<"]OKOK i1,i2=" << i1 <<","<<i2<<" FC1,FC2=" <<paq1.FrameCounter()
320// <<","<<paq2.FrameCounter()<<endl;
321
322 // Les deux framecounters sont identiques ...
323 noksfc++;
324 complex<r_4>* zp1 = (complex<r_4>*)(procbuff1+i1*procpaqsz);
325 complex<r_4>* zp2 = (complex<r_4>*)(procbuff1+i1*procpaqsz+procpaqsz/2);
326 complex<r_4>* zp3 = (complex<r_4>*)(procbuff2+i2*procpaqsz);
327 complex<r_4>* zp4 = (complex<r_4>*)(procbuff2+i2*procpaqsz+procpaqsz/2);
328 for(sa_size_t j=0; j<visiV13.Size(); j++) {
329 visiV13(j)+=zp1[j]*conj(zp3[j]);
330 visiV14(j)+=zp1[j]*conj(zp4[j]);
331 visiV23(j)+=zp2[j]*conj(zp3[j]);
332 visiV24(j)+=zp2[j]*conj(zp4[j]);
333 }
334 nzm++; i1++; i2++;
335 totnbytesproc += 2*paq1.DataSize();
336 } // Fin de boucle sur les paquets d'une zone
337 if ((nzm >= nmean_) || ((kmz==(nmax_-1))&&(nzm>1))) {
338 visiV13 /= complex<r_4>((r_4)nzm, 0.);
339 visiV14 /= complex<r_4>((r_4)nzm, 0.);
340 visiV23 /= complex<r_4>((r_4)nzm, 0.);
341 visiV24 /= complex<r_4>((r_4)nzm, 0.);
342 visiV13.Info()["NPaqMoy"] = nzm;
343 visiV14.Info()["NPaqMoy"] = nzm;
344 visiV23.Info()["NPaqMoy"] = nzm;
345 visiV24.Info()["NPaqMoy"] = nzm;
346 char fname[512];
347 {
348 sprintf(fname,"%s_%d.ppf",path_.c_str(),(int)ifile);
349 POutPersist po(fname);
350 po << PPFNameTag("visiV13") << visiV13;
351 po << PPFNameTag("visiV14") << visiV14;
352 po << PPFNameTag("visiV23") << visiV23;
353 po << PPFNameTag("visiV24") << visiV24;
354 }
355 visiV13 = complex<r_4>(0., 0.);
356 visiV14 = complex<r_4>(0., 0.);
357 visiV23 = complex<r_4>(0., 0.);
358 visiV24 = complex<r_4>(0., 0.);
359 nzm = 0;
360// ts.SetNow();
361// filog << ts << " : proc file " << fname << endl;
362 cout << " BRProcBRaw4C::run() created file " << fname << endl;
363 }
364 memgr1.FreeMemZone(mid1, MemZS_ProcB);
365 memgr2.FreeMemZone(mid2, MemZS_ProcB);
366 double okfrac = (nokpaq>1)?((double)noksfc/(double)nokpaq*100.):0.;
367 cout << "BRProcBRaw2C ["<<kmz<<"] NOKPaq=" << nokpaq << " NSameFC=" << noksfc
368 << " (" << okfrac << " %)" << endl;
369 totnokpaq += nokpaq;
370 totnoksfc += noksfc;
371 } // Fin de boucle sur les zones a traiter
372 cout << " ------------------ BRProcBRaw4C::run() END ----------------- " << endl;
373 ts.SetNow();
374 tm.SplitQ();
375 cout << " TotalProc= " << totnbytesproc/(1024*1024) << " MBytes, rate= "
376 << (double)(totnbytesproc)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;
377 double totokfrac = (totnokpaq>1)?((double)totnoksfc/(double)totnokpaq*100.):0.;
378 cout << " NOkPaq1,2=" << totnokpaq << " /TotNPaq=" << totnpaq << " TotNSameFC="
379 << totnoksfc << " (" << totokfrac << " %)" << endl;
380// cout << pcheck1;
381// cout << pcheck2;
382 cout << " BRProcBRaw4C::run()/Timing: \n";
383 tm.Print();
384 cout << " ---------------------------------------------------------- " << endl;
385}
386 catch (PException& exc) {
387 cout << " BRProcBRaw4C::run()/catched PException " << exc.Msg() << endl;
388 setRC(3);
389 return;
390 }
391 catch(...) {
392 cout << " BRProcBRaw4C::run()/catched unknown ... exception " << endl;
393 setRC(4);
394 return;
395 }
396 setRC(0);
397 return;
398}
399
400
Note: See TracBrowser for help on using the repository browser.