1 | #include "machdefs.h"
|
---|
2 | #include "toimanager.h"
|
---|
3 | #include "pexceptions.h"
|
---|
4 | #include "ntuple.h"
|
---|
5 | #include "ktoibad.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | ////////////////////////////////////////////////////////////////////////
|
---|
9 | // Flag des sample hors dynamique
|
---|
10 | ////////////////////////////////////////////////////////////////////////
|
---|
11 |
|
---|
12 | FlagOutOfRange::FlagOutOfRange(r_8 vmin,r_8 vmax,uint_8 flag)
|
---|
13 | : VMin(vmin), VMax(vmax), flgBad(flag)
|
---|
14 | {
|
---|
15 | if(VMax<VMin) {
|
---|
16 | cout<<"FlagOutOfRange::run() - Bad range interval "<<VMin<<" , "<<VMax<<endl;
|
---|
17 | throw ParmError("FlagOutOfRange::FlagOutOfRange() - Bad range interval");
|
---|
18 | }
|
---|
19 | }
|
---|
20 |
|
---|
21 | FlagOutOfRange::~FlagOutOfRange()
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 | void FlagOutOfRange::Print(ostream & os)
|
---|
26 | {
|
---|
27 | os<<"FlagOutOfRange::Print Range: ["<<VMin<<","<<VMax<<"] flgBad="<<flgBad<<endl;
|
---|
28 | }
|
---|
29 |
|
---|
30 | void FlagOutOfRange::init()
|
---|
31 | {
|
---|
32 | cout << "FlagOutOfRange::init" << endl;
|
---|
33 | declareInput("DataIn"); // input index 0
|
---|
34 | declareOutput("DataOut"); // output index 0
|
---|
35 | }
|
---|
36 |
|
---|
37 | void FlagOutOfRange::run()
|
---|
38 | {
|
---|
39 | long snb = getMinIn();
|
---|
40 | long sne = getMaxIn();
|
---|
41 |
|
---|
42 | if(snb>sne) {
|
---|
43 | cout<<"FlagOutOfRange::run() - Bad sample interval"<<snb<<" , "<<sne<<endl;
|
---|
44 | throw ParmError("FlagOutOfRange::run() - Bad sample interval");
|
---|
45 | }
|
---|
46 | if(!checkInputTOIIndex(0)) {
|
---|
47 | cout<<"FlagOutOfRange::run() - Input TOI (DataIn) not connected! "<<endl;
|
---|
48 | throw ParmError("FlagOutOfRange::run() Input TOI (DataIn) not connected!");
|
---|
49 | }
|
---|
50 | if(!checkOutputTOIIndex(0)) {
|
---|
51 | cout<<"FlagOutOfRange::run() - Output TOI (DataOut) not connected! "<<endl;
|
---|
52 | throw ParmError("FlagOutOfRange::run() Output TOI (DataOut) not connected!");
|
---|
53 | }
|
---|
54 |
|
---|
55 | //---------------------------------------------------------
|
---|
56 | try {
|
---|
57 |
|
---|
58 | cout<<"FlagOutOfRange::run(): SampleNum de "<<snb<<" a "<<sne<<endl;
|
---|
59 | uint_4 nbad=0;
|
---|
60 |
|
---|
61 | for(int_4 sn=snb;sn<=sne;sn++) {
|
---|
62 | r_8 bolo; uint_8 fgbolo=0;
|
---|
63 | getData(0,sn,bolo,fgbolo);
|
---|
64 | if(bolo<VMin || bolo>VMax) { // Pixel out of range
|
---|
65 | nbad++;
|
---|
66 | fgbolo |= flgBad;
|
---|
67 | }
|
---|
68 | putData(0,sn,bolo,fgbolo);
|
---|
69 | }
|
---|
70 | cout<<"FlagOutOfRange::run(): Fin de boucle: nbad="<<nbad
|
---|
71 | <<" / tot="<<sne-snb+1<<endl;
|
---|
72 |
|
---|
73 | //---------------------------------------------------------
|
---|
74 | } catch (PException & exc) {
|
---|
75 | cout<<"FlagOutOfRange: Catched Exception "<<(string)typeid(exc).name()
|
---|
76 | <<"\n .... Msg= "<<exc.Msg()<<endl;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | ////////////////////////////////////////////////////////////////////////
|
---|
84 | // Recherche des glitches :
|
---|
85 | // INPUT:
|
---|
86 | // lg : longeur maxi d'un glitch (impair, si pair lg++)
|
---|
87 | // doit etre suffisamment long pour que les glitches soient <lg
|
---|
88 | // suffisamment court pour ne pas tuer la galaxie
|
---|
89 | // lm : longueur de la fenetre glissante (de chaque cote)
|
---|
90 | // <---lm---><--lg--><---lm--->
|
---|
91 | // doit etre suffisamment long pour avoir une bonne stabilite
|
---|
92 | // des calculs (a0,a1) et suffisamment court pour ne pas
|
---|
93 | // tuer la galaxie.
|
---|
94 | // nsg : nombre de sigmas pour valider un glitch
|
---|
95 | ////////////////////////////////////////////////////////////////////////
|
---|
96 |
|
---|
97 | FlagGlitch::FlagGlitch(uint_4 lg,uint_4 lm,r_8 nsg)
|
---|
98 | : mLP(0), mLPsnb(-1), mLPsne(-1)
|
---|
99 | {
|
---|
100 | // Mise en forme des arguments
|
---|
101 | if(nsg<=0) nsg = 3.;
|
---|
102 | if(lg<=0) lg=3; if(lg%2==0) lg++;
|
---|
103 | if(lm<3) lm = 5*lg;
|
---|
104 |
|
---|
105 | LGlitch = lg;
|
---|
106 | LSide = lm;
|
---|
107 | NSCut = nsg;
|
---|
108 | Slpb[0] = Slpb[1] = NULL;
|
---|
109 | DoNotLookAt();
|
---|
110 | SetFlag();
|
---|
111 | SetBuffUpd();
|
---|
112 | }
|
---|
113 |
|
---|
114 | FlagGlitch::~FlagGlitch()
|
---|
115 | {
|
---|
116 | for(int i=0;i<2;i++) {if(Slpb[i]) delete Slpb[i]; Slpb[i]=NULL;}
|
---|
117 | }
|
---|
118 |
|
---|
119 | void FlagGlitch::Print(ostream & os)
|
---|
120 | {
|
---|
121 | os<<"FlagGlitch::Print"
|
---|
122 | <<" LGlitch="<<LGlitch<<" LSide="<<LSide<<" NSCut="<<NSCut
|
---|
123 | <<" flgNotLookAt="<<flgNotLookAt<<" flgGlitch="<<flgGlitch
|
---|
124 | <<" BuffUpdate="<<BuffUpdate<<endl;
|
---|
125 | }
|
---|
126 |
|
---|
127 | ////////////////////////////////////////////////////////////////////////
|
---|
128 | void FlagGlitch::init()
|
---|
129 | {
|
---|
130 | if(mLP) cout << "FlagGlitch::init" << endl;
|
---|
131 | declareInput("DataIn"); // input index 0
|
---|
132 | declareOutput("DataOut"); // output index 0
|
---|
133 |
|
---|
134 | for(int i=0;i<2;i++)
|
---|
135 | Slpb[i] = new SLinParBuff(LSide,BuffUpdate,0.,0.,true);
|
---|
136 |
|
---|
137 | int_4 lbuff = 2*LSide + LGlitch +10;
|
---|
138 | if(neededHistory<lbuff) setNeededHistory(lbuff);
|
---|
139 | }
|
---|
140 |
|
---|
141 | void FlagGlitch::run()
|
---|
142 | {
|
---|
143 | long snb = getMinIn();
|
---|
144 | long sne = getMaxIn();
|
---|
145 |
|
---|
146 | if(snb>sne) {
|
---|
147 | cout<<"FlagGlitch::run() - Bad sample interval"<<snb<<" , "<<sne<<endl;
|
---|
148 | throw ParmError("FlagGlitch::run() - Bad sample interval");
|
---|
149 | }
|
---|
150 | if(!checkInputTOIIndex(0)) {
|
---|
151 | cout<<"FlagGlitch::run() - Input TOI (DataIn) not connected! "<<endl;
|
---|
152 | throw ParmError("FlagGlitch::run() Input TOI (DataIn) not connected!");
|
---|
153 | }
|
---|
154 | if(!checkOutputTOIIndex(0)) {
|
---|
155 | cout<<"FlagGlitch::run() - Output TOI (DataOut) not connected! "<<endl;
|
---|
156 | throw ParmError("FlagGlitch::run() Output TOI (DataOut) not connected!");
|
---|
157 | }
|
---|
158 |
|
---|
159 | //---------------------------------------------------------
|
---|
160 | try {
|
---|
161 |
|
---|
162 | char* nament_[8] = {"s","bol","sg","sd","slg","sld","ypg","ypd"};
|
---|
163 | r_4 xnt_[8];
|
---|
164 | NTuple nt_(8,nament_);
|
---|
165 | int intuple = mLP/10;
|
---|
166 |
|
---|
167 | if(mLP) cout<<"FlagGlitch::run(): SampleNum de "<<snb<<" a "<<sne<<endl;
|
---|
168 | uint_4 nflag=0;
|
---|
169 | r_8 nsg = NSCut;
|
---|
170 | int_4 lg = LGlitch;
|
---|
171 | int_4 lgs2 = (lg-1)/2;
|
---|
172 | int_4 lm = LSide;
|
---|
173 | int_4 lmmin = lm/2; if(lmmin<2) lmmin=2;
|
---|
174 |
|
---|
175 | for(int_4 sn=snb;sn<=sne;sn++) {
|
---|
176 | int_4 lp = (mLPsnb<=sn && sn<=mLPsne) ? mLP%10: 0;
|
---|
177 | r_8 bolog; uint_8 fgbolog=0;
|
---|
178 |
|
---|
179 | getData(0,sn,bolog,fgbolog);
|
---|
180 | if(fgbolog & flgNotLookAt) {
|
---|
181 | putData(0,sn,bolog,fgbolog);
|
---|
182 | continue;
|
---|
183 | }
|
---|
184 |
|
---|
185 | // i
|
---|
186 | // <---lm---><--lg--><---lm--->
|
---|
187 | // | c=0 | | c=1 |
|
---|
188 | // ideb | | ifin
|
---|
189 | // ifin ideb
|
---|
190 | r_8 bolo; uint_8 fgbolo=0;
|
---|
191 | int_4 ifill[2] = {sn-lgs2-1,sn+lgs2+lm};
|
---|
192 | if(lp>1) printf(">>>> sn=%d v=(%f,%ld) ifill=[%d,%d]\n"
|
---|
193 | ,sn,bolog,fgbolog,ifill[0],ifill[1]);
|
---|
194 |
|
---|
195 | if(ifill[0]<snb) {
|
---|
196 | putData(0,sn,bolog,fgbolog);
|
---|
197 | continue;
|
---|
198 | }
|
---|
199 | getData(0,ifill[0],bolo,fgbolo);
|
---|
200 | if(fgbolo&flgNotLookAt) Slpb[0]->Pop();
|
---|
201 | else Slpb[0]->Push((r_8)ifill[0],bolo);
|
---|
202 | if((int_4)Slpb[0]->NPoints()<lmmin) {
|
---|
203 | putData(0,sn,bolog,fgbolog);
|
---|
204 | continue;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if(ifill[1]<=sne) {
|
---|
208 | getData(0,ifill[1],bolo,fgbolo);
|
---|
209 | if(fgbolo&flgNotLookAt) Slpb[1]->Pop();
|
---|
210 | else Slpb[1]->Push((r_8)ifill[1],bolo);
|
---|
211 | } else Slpb[1]->Pop();
|
---|
212 | if((int_4)Slpb[1]->NPoints()<lmmin) {
|
---|
213 | putData(0,sn,bolog,fgbolog);
|
---|
214 | continue;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // Calcul des valeurs pour decider
|
---|
218 | double s[2],slin[2],ypred[2];
|
---|
219 | for(int c=0;c<2;c++) {
|
---|
220 | r_8 mean,a0,a1;
|
---|
221 | s[c] = Slpb[c]->Compute(mean);
|
---|
222 | slin[c] = Slpb[c]->Compute(a0,a1);
|
---|
223 | ypred[c] = a0+a1*sn;
|
---|
224 | if(lp>1) printf("... c=%d m=%f s=%f ypred=%f slin=%f (%d)\n"
|
---|
225 | ,c,mean,s[c],ypred[c],slin[c],Slpb[c]->NPoints());
|
---|
226 | if(slin[c]<0. || s[c]<0.) {
|
---|
227 | putData(0,sn,bolog,fgbolog);
|
---|
228 | continue;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if(intuple>0) {
|
---|
233 | xnt_[0] = sn; xnt_[1] = bolog;
|
---|
234 | xnt_[2] = s[0]; xnt_[3] = s[1];
|
---|
235 | xnt_[4] = slin[0]; xnt_[5] = slin[1];
|
---|
236 | xnt_[6] = ypred[0]; xnt_[7] = ypred[1];
|
---|
237 | nt_.Fill(xnt_);
|
---|
238 | }
|
---|
239 |
|
---|
240 | // Si les predictions ne sont pas compatibles (sig), pas de deglitch
|
---|
241 | r_8 yy = ypred[1]-ypred[0];
|
---|
242 | r_8 sy = nsg*sqrt(s[0]*s[0]+s[1]*s[1]);
|
---|
243 | if(lp>1) printf("... yy=|%f| > sy=%f ???\n",yy,sy);
|
---|
244 | if(fabs(yy) > sy) {
|
---|
245 | putData(0,sn,bolog,fgbolog);
|
---|
246 | continue;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // Si la valeur est entre les 2 predictions, pas de deglitch
|
---|
250 | r_8 dp0 = ypred[0];
|
---|
251 | r_8 dp1 = ypred[1];
|
---|
252 | if(ypred[1]<ypred[0]) {dp0 = ypred[1]; dp1 = ypred[0];}
|
---|
253 | if(lp>1) printf("... %f dans [%f,%f] ???\n",bolog,dp0,dp1);
|
---|
254 | if(dp0<bolog && bolog<dp1) {
|
---|
255 | putData(0,sn,bolog,fgbolog);
|
---|
256 | continue;
|
---|
257 | }
|
---|
258 |
|
---|
259 | // A + de N sigmas (lin) des 2 predictions
|
---|
260 | dp0 = fabs(bolog-ypred[0]); slin[0] *= nsg;
|
---|
261 | dp1 = fabs(bolog-ypred[1]); slin[1] *= nsg;
|
---|
262 | if(lp>1) printf("... dp0=%f > %f et dp1=%f > %f ???\n"
|
---|
263 | ,dp0,slin[0],dp1,slin[1]);
|
---|
264 | if(dp0 < slin[0] || dp1 < slin[1]) {
|
---|
265 | putData(0,sn,bolog,fgbolog);
|
---|
266 | continue;
|
---|
267 | }
|
---|
268 |
|
---|
269 | // C'est un glitch
|
---|
270 | nflag++;
|
---|
271 | fgbolog |= flgGlitch;
|
---|
272 | if(lp>1)
|
---|
273 | printf("......... Glitch en sn=%d ... %d glitches found\n",sn,nflag);
|
---|
274 | putData(0,sn,bolog,fgbolog);
|
---|
275 |
|
---|
276 | }
|
---|
277 | if(mLP) cout<<"FlagGlitch::run(): Fin de boucle: nflag="<<nflag
|
---|
278 | <<" / "<<sne-snb+1<<endl;
|
---|
279 |
|
---|
280 | if(intuple>0) {
|
---|
281 | char *fileout_ = "flagglitch.ppf";
|
---|
282 | string tag_ = "nfg";
|
---|
283 | POutPersist pos_(fileout_); pos_.PutObject(nt_,tag_);
|
---|
284 | }
|
---|
285 |
|
---|
286 | //---------------------------------------------------------
|
---|
287 | } catch (PException & exc) {
|
---|
288 | cout<<"FlagGlitch: Catched Exception "<<(string)typeid(exc).name()
|
---|
289 | <<"\n .... Msg= "<<exc.Msg()<<endl;
|
---|
290 | }
|
---|
291 |
|
---|
292 | return;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | ////////////////////////////////////////////////////////////////////////
|
---|
297 | // Flag des sample autour d'un sample deja flaggue :
|
---|
298 | // INPUT:
|
---|
299 | // lm : longueur a flagguer de part et d'autre du mauvais sample
|
---|
300 | // <---lm---><bad_sample><---lm--->
|
---|
301 | ////////////////////////////////////////////////////////////////////////
|
---|
302 |
|
---|
303 | FlagAroundFlag::FlagAroundFlag(uint_4 lm,uint_8 flgs)
|
---|
304 | {
|
---|
305 | // Mise en forme des arguments
|
---|
306 | LSide = lm;
|
---|
307 |
|
---|
308 | SetFlag();
|
---|
309 | SetFlagAroundFlag(flgs);
|
---|
310 | SetLimits();
|
---|
311 | }
|
---|
312 |
|
---|
313 | FlagAroundFlag::~FlagAroundFlag()
|
---|
314 | {
|
---|
315 | }
|
---|
316 |
|
---|
317 | void FlagAroundFlag::Print(ostream & os)
|
---|
318 | {
|
---|
319 | os<<"FlagAroundFlag::Print"
|
---|
320 | <<" LSide="<<LSide
|
---|
321 | <<" flgSample="<<flgSample<<" flgAround="<<flgAround<<endl
|
---|
322 | <<" ... Range: ["<<VMin<<","<<VMax<<"]"<<" flgBad="<<flgBad<<endl;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void FlagAroundFlag::init()
|
---|
326 | {
|
---|
327 | cout << "FlagAroundFlag::init" << endl;
|
---|
328 | declareInput("DataIn"); // input index 0
|
---|
329 | declareOutput("DataOut"); // output index 0
|
---|
330 |
|
---|
331 | int_4 lbuff = 2*LSide +10;
|
---|
332 | if(neededHistory<lbuff) setNeededHistory(lbuff);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void FlagAroundFlag::run()
|
---|
336 | {
|
---|
337 | long snb = getMinIn();
|
---|
338 | long sne = getMaxIn();
|
---|
339 |
|
---|
340 | if(snb>sne) {
|
---|
341 | cout<<"FlagAroundFlag::run() - Bad sample interval"<<snb<<" , "<<sne<<endl;
|
---|
342 | throw ParmError("FlagAroundFlag::run() - Bad sample interval");
|
---|
343 | }
|
---|
344 | if(!checkInputTOIIndex(0)) {
|
---|
345 | cout<<"FlagAroundFlag::run() - Input TOI (DataIn) not connected! "<<endl;
|
---|
346 | throw ParmError("FlagAroundFlag::run() Input TOI (DataIn) not connected!");
|
---|
347 | }
|
---|
348 | if(!checkOutputTOIIndex(0)) {
|
---|
349 | cout<<"FlagAroundFlag::run() - Output TOI (DataOut) not connected! "<<endl;
|
---|
350 | throw ParmError("FlagAroundFlag::run() Output TOI (DataOut) not connected!");
|
---|
351 | }
|
---|
352 |
|
---|
353 | //---------------------------------------------------------
|
---|
354 | try {
|
---|
355 |
|
---|
356 | cout<<"FlagAroundFlag::run(): SampleNum de "<<snb<<" a "<<sne<<endl;
|
---|
357 | uint_4 nflag=0, nflagged=0, nflagalready=0, nbad=0;
|
---|
358 |
|
---|
359 | // Un peu lourdingue! On pourrait optimiser!
|
---|
360 | // Si on ne tue pas (LSide=0), alors juste un transfert de donnees
|
---|
361 | for(int_4 sn=snb;sn<=sne;sn++) {
|
---|
362 | r_8 bolo; uint_8 fgbolo=0;
|
---|
363 | getData(0,sn,bolo,fgbolo);
|
---|
364 | if(VMin<=VMax && (bolo<VMin || bolo>VMax)) { // Pixel out of range
|
---|
365 | nbad++;
|
---|
366 | fgbolo |= flgBad;
|
---|
367 | } else if(fgbolo&flgSample) { // Pixel flaggue flgSample
|
---|
368 | nflag++;
|
---|
369 | } else if(fgbolo&flgAround) { // Pixel deja flagge around?
|
---|
370 | nflagalready++;
|
---|
371 | } else if(LSide>0) { // Pixel a flagguer?
|
---|
372 | int_4 ideb = sn-LSide, ifin = sn+LSide;
|
---|
373 | for(int_4 i=ideb;i<=ifin;i++) { // Y a t-il un "mauvais" pixel autour?
|
---|
374 | if(i<snb || i>sne || i==sn) continue;
|
---|
375 | r_8 b; uint_8 fg=0;
|
---|
376 | getData(0,i,b,fg);
|
---|
377 | if(fg&flgSample) {fgbolo |= flgAround; nflagged++; break;}
|
---|
378 | }
|
---|
379 | }
|
---|
380 | putData(0,sn,bolo,fgbolo);
|
---|
381 | }
|
---|
382 | cout<<"FlagAroundFlag::run(): Fin de boucle: nflag="<<nflag
|
---|
383 | <<" nflagged="<<nflagged<<" (already="<<nflagalready<<")"<<endl
|
---|
384 | <<" ...... bad="<<nbad<<" / tot="<<sne-snb+1<<endl;
|
---|
385 |
|
---|
386 | //---------------------------------------------------------
|
---|
387 | } catch (PException & exc) {
|
---|
388 | cout<<"FlagAroundFlag: Catched Exception "<<(string)typeid(exc).name()
|
---|
389 | <<"\n .... Msg= "<<exc.Msg()<<endl;
|
---|
390 | }
|
---|
391 |
|
---|
392 | return;
|
---|
393 | }
|
---|