source: fact/tools/rootmacros/zerosearch.C@ 14942

Last change on this file since 14942 was 14815, checked in by Jens Buss, 12 years ago
whitespace
File size: 16.6 KB
Line 
1#include "Region.h"
2#include <iostream>
3
4#include "zerosearch.h"
5
6using namespace std;
7
8// searches for zero crossings in a given vector of floats
9// for zero crossings in falling edge
10// give edge = -1
11//
12//
13// returns pointer to vetctor of ints
14
15vector<Region> *zerosearch(
16 vector<float> &input,
17 int edge, // search on rising edge=1, -1:falling
18 unsigned int step, // search in steps of step
19 int VerbosityLevel
20){
21
22vector<Region> * ZeroPositions = new vector<Region>;
23Region currentRegion = {0, 0, 0, 0.0, 0, 0.0, 0.0, 0};
24 if (step < 1){
25 if (VerbosityLevel > 0)
26 cout << "zerosearch - stepsize=" << step
27 << " is smaller than 1. returning." << endl;
28 return ZeroPositions;
29 }
30 if (input.size() < step){
31 if (VerbosityLevel > 0)
32 cout << "zerosearch - input-vector.size()="<< input.size()
33 << " is smaller than stepsize=" << step
34 << "returning." << endl;
35 return ZeroPositions;
36 }
37
38 if (edge > 0) // search for zero-x-ings on the rising edge
39 {
40 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
41 {
42 if (input[sl] > 0.0) // can't be a rising 0-x-ing
43 {
44 continue;
45 }
46 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
47 {
48 currentRegion.begin = sl;
49 currentRegion.end = sl+step;
50 ZeroPositions->push_back(currentRegion);
51 }
52 }
53 }
54 else if (edge < 0) // search for zero-x-ings on the falling edge
55 {
56 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
57 {
58 if (input[sl] < 0.0) // can't be a falling 0-x-ing
59 {
60 continue;
61 }
62 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
63 {
64 currentRegion.begin = sl;
65 currentRegion.end = sl+step;
66 ZeroPositions->push_back(currentRegion);
67 }
68 }
69
70 }
71 else // search for zero-x-ings on both esges
72 {
73 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
74 {
75 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
76 {
77 currentRegion.begin = sl;
78 currentRegion.end = sl+step;
79 ZeroPositions->push_back(currentRegion);
80 }
81 }
82 }
83
84
85 return ZeroPositions;
86}
87
88size_t ShiftRegionBy(vector<Region> &src,
89 int Shift,
90 int VerbosityLevel)
91{
92 vector<Region>::iterator it;
93 // I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
94 if (VerbosityLevel > 10){
95 for (it = src.begin() ; it < src.end() ; it++){
96 cout << " it->begin="<< it->begin;
97 it->begin += Shift;
98 cout << "--> shifted="<< it->begin << endl;
99 cout << " it->end="<< it->end;
100 it->end += Shift;
101 cout << "--> shifted="<< it->end << endl;
102 cout << " it->maxPos="<< it->maxPos;
103 it->maxPos += Shift;
104 cout << "--> shifted="<< it->maxPos << endl;
105 }
106 return src.size();
107 }
108
109 for (it = src.begin() ; it < src.end() ; it++){
110 it->begin += Shift;
111 it->end += Shift;
112 it->maxPos += Shift;
113 }
114 return src.size();
115}
116
117size_t EnlargeRegion(vector<Region> &src,
118 int Left,
119 int Right,
120 int VerbosityLevel)
121{
122 vector<Region>::iterator it;
123 // I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
124 if (VerbosityLevel > 10){
125 for (it = src.begin() ; it < src.end() ; it++){
126 cout << " it->begin="<< it->begin;
127 it->begin -= Left;
128 cout << "--> enlarged="<< it->begin << endl;
129 cout << " it->end="<< it->end;
130 it->end += Right;
131 cout << "--> enlarged="<< it->end << endl;
132 }
133 return src.size();
134 }
135
136
137 for (it = src.begin() ; it < src.end() ; it++){
138 it->begin -= Left;
139 it->end += Right;
140 }
141 return src.size();
142
143}
144
145#include <limits>
146size_t findAbsMaxInRegions(
147 vector<Region> &regions,
148 vector<float> &data,
149 int VerbosityLevel)
150{
151 vector<Region>::iterator reg;
152 for (reg = regions.begin() ; reg < regions.end() ; reg++){
153 reg->maxVal=-numeric_limits<float>::max();
154 // 1st check if both ends of the region are in the data-vector
155 if (reg->begin < 0) reg->begin=0;
156 if ((unsigned int)reg->end > data.size()-1) reg->end=data.size()-1;
157
158 // TODO or TOTHINKOF:
159 // Region might overlap ... I don't care .. I just search
160 // in a later step Maxima at the same Positions can be removed...
161 // of course this means, some searches were needless ...
162 //cout << "searching from " << reg->begin << " to " << reg->end << endl;
163 for (unsigned int pos=reg->begin; pos <= (unsigned int)reg->end; pos++){
164 if (data[pos] > reg->maxVal)
165 {
166 reg->maxVal = data[pos];
167 reg->maxPos = pos;
168 }
169 }
170 if (VerbosityLevel > 3) {
171 cout << "findAbsMaxInRegions - found Max at:"
172 << reg->maxPos << " with Value:" << reg->maxVal << endl;
173 }
174 }
175 return regions.size();
176}
177
178#include <algorithm>
179bool compMaxPosOfRegions (Region a, Region b) {
180 return (a.maxPos == b.maxPos);
181}
182
183size_t removeEqualMaxima(
184 vector<Region> &regions,
185 int VerbosityLevel)
186{
187 if (VerbosityLevel > 2){
188 cout << "removeEqualMaxima:" << endl;
189 cout << "region before contains:" << endl;
190 for (unsigned int i=0; i<regions.size(); i++){
191 cout << i <<"\t";
192 cout << regions[i].begin <<"\t";
193 cout << regions[i].end <<"\t";
194 cout << regions[i].maxPos <<"\t";
195 cout << regions[i].maxVal <<"\t";
196 cout << endl;
197 }
198 }
199
200 vector<Region>::iterator it;
201 it = unique (regions.begin(), regions.end() , compMaxPosOfRegions);
202 regions.resize( it - regions.begin() );
203
204 if (VerbosityLevel > 1){
205 cout << "region now contains:" << endl;
206 for (unsigned int i=0; i<regions.size(); i++){
207 cout << i <<"\t";
208 cout << regions[i].begin <<"\t";
209 cout << regions[i].end <<"\t";
210 cout << regions[i].maxPos <<"\t";
211 cout << regions[i].maxVal <<"\t";
212 cout << endl;
213 }
214 }
215 return regions.size();
216}
217
218size_t removeRegionOnFallingEdge(
219 vector<Region> &regions,
220 unsigned int FallingEdgeWidth,
221 int VerbosityLevel)
222{
223 //throw exceptions
224 if ( FallingEdgeWidth < 1){
225 if (VerbosityLevel > 0){
226 cout << " removeRegionOnFallingEdge: FallingEdgeWidth < 1" << endl;
227 cout << " FallingEdgeWidth =" << FallingEdgeWidth << endl;
228 cout << "returning." << endl;
229 return regions.size();
230 }
231 }
232 //throw exceptions
233 if (regions.size() < 1){
234 if (VerbosityLevel > 3)
235 cout << "removeRegionOnFallingEdge: source vector empty." << endl;
236 return 0;
237 }
238
239 vector<Region>::reverse_iterator it = regions.rbegin();
240 while( it != regions.rend()-1 )
241 {
242 if (VerbosityLevel >10)
243 cout << it->maxPos << "\t" << (it+1)->maxPos << "\t" << it->maxPos - (it+1)->maxPos << endl;
244
245 if ( it->maxPos - (it+1)->maxPos < (int)FallingEdgeWidth) {
246 if (VerbosityLevel > 3)
247 cout << "erasing Region @ " << it->maxPos << endl;
248 regions.erase( --it.base() ) ;
249 ++it;
250 }else
251 ++it;
252 }
253
254 return regions.size();
255}
256
257
258size_t removeRegionWithMaxOnEdge(
259 vector<Region> &regions,
260 unsigned int EdgeWidth,
261 int VerbosityLevel)
262{
263 if (EdgeWidth < 1){
264 if (VerbosityLevel > 0){
265 cout << "removeRegionWithMaximaOnEdge: EdgeWidth < 1" << endl;
266 cout << "EdgeWidth=" << EdgeWidth << endl;
267 cout << "returning." << endl;
268 return regions.size();
269 }
270 }
271
272 vector<Region>::iterator it = regions.begin();
273 while( it != regions.end() )
274 {
275// cout << it->begin << "\t";
276// cout << it->maxPos << "\t";
277// cout << it->end << "\t";
278// cout << it->maxVal << endl;
279
280 if (it->maxPos < (int)(it->begin+EdgeWidth)) {
281 if (VerbosityLevel > 3)
282 cout << "erasing Region(max@left edge) " << it->maxPos << endl;
283 it = regions.erase( it ) ;
284 //++it;
285 }else if (it->maxPos > (int)(it->end-EdgeWidth)) {
286 if (VerbosityLevel > 3)
287 cout << "erasing Region(max@right edge) " << it->maxPos << endl;
288 it = regions.erase( it ) ;
289 //++it;
290 }else
291 ++it;
292
293 }
294
295 return regions.size();
296}
297
298size_t removeMaximaBelow(
299 vector<Region> &regions,
300 float threshold,
301 int VerbosityLevel)
302{
303// if (threshold < 0){
304// if (VerbosityLevel > 0)
305// cout << "removeMaximaBelow: threshold < 0" << endl;
306// cout << "threshold=" << threshold << endl;
307// cout << "returning." << endl;
308// return regions.size();
309// }
310
311 vector<Region>::iterator it = regions.begin();
312 while( it != regions.end() )
313 {
314 if (it->maxVal < threshold ) {
315 if (VerbosityLevel > 3){
316 cout << "removing max " << it->maxVal << "\t";
317 cout << " @ " << it->maxPos << "\t";
318 cout << endl;
319 }
320 it = regions.erase( it ) ;
321 }else
322 ++it;
323 }
324
325 return regions.size();
326}
327
328size_t removeMaximaAbove(
329 vector<Region> &regions,
330 float threshold,
331 int VerbosityLevel)
332{
333 if (threshold < 0){
334 if (VerbosityLevel > 0)
335 cout << "removeMaximaAbove: threshold < 0" << endl;
336 cout << "threshold=" << threshold << endl;
337 cout << "returning." << endl;
338 return regions.size();
339 }
340
341 vector<Region>::iterator it = regions.begin();
342 while( it != regions.end() )
343 {
344 if (it->maxVal > threshold ) {
345 if (VerbosityLevel > 3){
346 cout << "removing max " << it->maxVal << "\t";
347 cout << " @ " << it->maxPos << "\t";
348 cout << endl;
349 }
350 it = regions.erase( it ) ;
351 }else
352 ++it;
353 }
354
355 return regions.size();
356}
357
358Region FindAbsMax(
359 vector<Region> &regions,
360 int VerbosityLevel)
361{
362 Region AbsMax;
363 AbsMax.begin = -1;
364 AbsMax.maxPos = -1;
365 AbsMax.end = -1;
366 AbsMax.maxVal=-numeric_limits<float>::max();
367 if (regions.size() < 1)
368 return AbsMax;
369
370 for (vector<Region>::iterator it = regions.begin(); it < regions.end(); ++it)
371 {
372 if (it->maxVal > AbsMax.maxVal ) {
373 AbsMax = *it;
374 }
375 }
376 if (VerbosityLevel > 5){
377 AbsMax.begin +=0;
378 }
379 return AbsMax;
380}
381
382////////////////
383////////////// old Version of the code
384/*
385 for (unsigned int sl = step; sl < input.size(); sl+=step){
386 if (input[sl] * input[sl-] < 0 || input[sl]==0.0 ){ // sign change --> zero crossing OR really zero
387
388 if ( (input[sl-1] - input[sl]) * edge < 0){ // this is the crossing the user wanted
389
390 // check if we go lower than a certain limit in the next few slices
391 for ( int lala=0; lala<post; lala++){
392 if (input[sl+lala] > 1.5) {
393 zeroPositions->push_back(sl);
394 sl += lala;
395 break;
396 }
397 }
398
399 } else if ( (input[sl-1] - input[sl]) * edge > 0){ // this is the zero x-ing the user did not want
400
401 // do nothing
402
403 } else { // sl and sl-1 are equal .. don't know waht do to...
404
405 }
406
407 }
408
409 } // end of loop over slices - between pre and post
410
411*/
412
413
414size_t findTimeOfHalfMaxLeft(
415 vector<Region> &regions,
416 vector<float> &data,
417 float baseline,
418 int beginRisingEdge,
419 int endRisingEdge,
420 int VerbosityLevel)
421{
422 float pulse_size = 0; //
423 float thr = 0;
424
425 //int begin = beginRisingEdge;
426 int end = endRisingEdge;
427 int counter = 0;
428 // local vars for line fitting
429 float xmean = 0.0;
430 float ymean = 0.0;
431 float cov = 0.0; // empiric covarianve between x and y
432 float var = 0.0; // empiric variance of x
433 // result of line fitting
434 float slope = 0.0;
435 float intercept = 0.0;
436 // result of this function -- the position of the half rising edge
437 float pos_of_thr_Xing = 0.0;
438 int result = 0;
439
440 vector<Region>::iterator reg;
441 for (reg = regions.begin() ; reg < regions.end() ; reg++){
442 counter = 0;
443 // check if linear falling edge is completely in pipeline
444 // if not --> delete
445 if ( reg->maxPos - end < 0 )
446 {
447 // delete this region from vector<Region>
448 reg = regions.erase( reg ) ;
449 --reg;
450 continue;
451 }
452
453 // The maximum was probably found using smoothed data,
454 // so I read the value at the position of the maximum from the data
455 // again. So I rely on a well defined maxPos.
456 if (VerbosityLevel > 1) cout << "## baseline: " << baseline << endl;
457 pulse_size = data[reg->maxPos] - baseline;
458 if (VerbosityLevel > 1) cout << "## pulse_size: " << pulse_size << endl;
459 thr = pulse_size / 2. + baseline;
460 if (VerbosityLevel > 1) cout << "## thr: " << thr << endl;
461
462 // I think 5 slices left of the max there begins the rising edge
463 // and I think the rising edge is about 6 slices long.
464 // but these numbers are input as parameters
465 // beginRisingEdge &
466 // endRisingEdge
467
468 // I fit a linear function to the falling edge
469 // ALARM check for out of range values...
470
471 bool passed = false;
472 for (int slice=reg->maxPos;
473 slice > 1; --slice)
474 {
475 if ( data[slice] < 0.9*data[reg->maxPos] && !passed)
476 {
477 beginRisingEdge = reg->maxPos - slice;
478 passed = true;
479 }
480
481 if ( data[slice] < 0.1*data[reg->maxPos])
482 {
483 endRisingEdge = reg->maxPos - slice;
484 passed = false;
485 reg->lengthOfRisingEdge=endRisingEdge - beginRisingEdge;
486 break;
487 }
488 }
489
490
491 //calculate mean of x and y coordinate
492 for (int slice=reg->maxPos - beginRisingEdge;
493 slice > reg->maxPos - endRisingEdge; --slice)
494 {
495 xmean += slice;
496 ymean += data[slice];
497 counter++;
498 }
499// xmean /= beginRisingEdge - endRisingEdge;
500// ymean /= beginRisingEdge - endRisingEdge;
501 xmean /= counter;
502 ymean /= counter;
503
504 if (VerbosityLevel > 2) cout << "## xmean: " << xmean << endl;
505 if (VerbosityLevel > 2) cout << "## ymean: " << ymean << endl;
506
507 cov = 0.0;
508 var = 0.0;
509
510 for (int slice=reg->maxPos - beginRisingEdge;
511 slice > reg->maxPos - endRisingEdge; --slice)
512 {
513 cov += (data[slice] - ymean) * (slice - xmean);
514 var += (slice - xmean) * (slice - xmean);
515 }
516 if (VerbosityLevel > 2) cout << "## cov: " << cov << endl;
517 if (VerbosityLevel > 2) cout << "## var: " << var << endl;
518
519 slope = cov / var;
520 intercept = ymean - slope * xmean;
521
522 // now calculate, where the fittet line crosses the threshold
523 pos_of_thr_Xing = (thr - intercept) / slope;
524 result = (int)(pos_of_thr_Xing + 0.5);
525
526
527 if (VerbosityLevel > 2)
528 {
529 cout << "findTimeOfHalfMaxLeft() is still in debugging phase:" << endl;
530 cout << "please edit the code in oder to suppress this output:" << endl;
531 cout << "threshold: " << thr << endl;
532 cout << "slope: " << slope << " [mV/timeslice]" << endl;
533 cout << "intercept: " << intercept << "[mV]" << endl;
534 cout << "position where line crosses threshold " << pos_of_thr_Xing << endl;
535 cout << "converted to slice number " << result << endl;
536
537 }
538
539 reg->halfRisingEdgePos = result;
540
541 reg->distanceEdgeToMax = reg->maxPos - result;
542 reg->interceptRisingEdge = intercept;
543 reg->slopeOfRisingEdge = slope;
544 }
545 return regions.size();
546}
547
548size_t removeRegionWithToFlatSlope(
549 vector<Region> &regions,
550 float minSlope,
551 int VerbosityLevel)
552{
553 vector<Region>::iterator it = regions.begin();
554 while( it != regions.end() )
555 {
556 if (it->slopeOfRisingEdge < minSlope ) {
557 if (VerbosityLevel > 3){
558 cout << "removing max " << it->maxVal << "\t";
559 cout << " @ " << it->maxPos << "\t";
560 cout << endl;
561 }
562 it = regions.erase( it ) ;
563 }else
564 ++it;
565 }
566
567 return regions.size();
568}
Note: See TracBrowser for help on using the repository browser.