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

Last change on this file since 13710 was 13614, checked in by Jens Buss, 13 years ago
add removeRegionWithToFlatSlope
File size: 15.9 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 reg->maxVal = data[pos];
166 reg->maxPos = pos;
167 }
168 }
169 if (VerbosityLevel > 3) {
170 cout << "findAbsMaxInRegions - found Max at:"
171 << reg->maxPos << " with Value:" << reg->maxVal << endl;
172 }
173 }
174 return regions.size();
175}
176
177#include <algorithm>
178bool compMaxPosOfRegions (Region a, Region b) {
179 return (a.maxPos == b.maxPos);
180}
181
182size_t removeEqualMaxima(
183 vector<Region> &regions,
184 int VerbosityLevel)
185{
186 if (VerbosityLevel > 2){
187 cout << "removeEqualMaxima:" << endl;
188 cout << "region before contains:" << endl;
189 for (unsigned int i=0; i<regions.size(); i++){
190 cout << i <<"\t";
191 cout << regions[i].begin <<"\t";
192 cout << regions[i].end <<"\t";
193 cout << regions[i].maxPos <<"\t";
194 cout << regions[i].maxVal <<"\t";
195 cout << endl;
196 }
197 }
198
199 vector<Region>::iterator it;
200 it = unique (regions.begin(), regions.end() , compMaxPosOfRegions);
201 regions.resize( it - regions.begin() );
202
203 if (VerbosityLevel > 1){
204 cout << "region now contains:" << endl;
205 for (unsigned int i=0; i<regions.size(); i++){
206 cout << i <<"\t";
207 cout << regions[i].begin <<"\t";
208 cout << regions[i].end <<"\t";
209 cout << regions[i].maxPos <<"\t";
210 cout << regions[i].maxVal <<"\t";
211 cout << endl;
212 }
213 }
214 return regions.size();
215}
216
217size_t removeRegionOnFallingEdge(
218 vector<Region> &regions,
219 unsigned int FallingEdgeWidth,
220 int VerbosityLevel)
221{
222if ( FallingEdgeWidth < 1){
223 if (VerbosityLevel > 0){
224 cout << " removeRegionOnFallingEdge: FallingEdgeWidth < 1" << endl;
225 cout << " FallingEdgeWidth =" << FallingEdgeWidth << endl;
226 cout << "returning." << endl;
227 return regions.size();
228 }
229 }
230 if (regions.size() < 1){
231 if (VerbosityLevel > 3)
232 cout << "removeRegionOnFallingEdge: source vector empty." << endl;
233 return 0;
234 }
235
236 vector<Region>::reverse_iterator it = regions.rbegin();
237 while( it != regions.rend()-1 )
238 {
239 if (VerbosityLevel >10)
240 cout << it->maxPos << "\t" << (it+1)->maxPos << "\t" << it->maxPos - (it+1)->maxPos << endl;
241
242 if ( it->maxPos - (it+1)->maxPos < (int)FallingEdgeWidth) {
243 if (VerbosityLevel > 3)
244 cout << "erasing Region @ " << it->maxPos << endl;
245 regions.erase( --it.base() ) ;
246 ++it;
247 }else
248 ++it;
249 }
250
251 return regions.size();
252}
253
254
255size_t removeRegionWithMaxOnEdge(
256 vector<Region> &regions,
257 unsigned int EdgeWidth,
258 int VerbosityLevel)
259{
260 if (EdgeWidth < 1){
261 if (VerbosityLevel > 0){
262 cout << "removeReginWithMaximaOnEdge: EdgeWidth < 1" << endl;
263 cout << "EdgeWidth=" << EdgeWidth << endl;
264 cout << "returning." << endl;
265 return regions.size();
266 }
267 }
268
269 vector<Region>::iterator it = regions.begin();
270 while( it != regions.end() )
271 {
272// cout << it->begin << "\t";
273// cout << it->maxPos << "\t";
274// cout << it->end << "\t";
275// cout << it->maxVal << endl;
276
277 if (it->maxPos < (int)(it->begin+EdgeWidth)) {
278 if (VerbosityLevel > 3)
279 cout << "erasing Region(max@left edge) " << it->maxPos << endl;
280 it = regions.erase( it ) ;
281 //++it;
282 }else if (it->maxPos > (int)(it->end-EdgeWidth)) {
283 if (VerbosityLevel > 3)
284 cout << "erasing Region(max@right edge) " << it->maxPos << endl;
285 it = regions.erase( it ) ;
286 //++it;
287 }else
288 ++it;
289
290 }
291
292 return regions.size();
293}
294
295size_t removeMaximaBelow(
296 vector<Region> &regions,
297 float threshold,
298 int VerbosityLevel)
299{
300// if (threshold < 0){
301// if (VerbosityLevel > 0)
302// cout << "removeMaximaBelow: threshold < 0" << endl;
303// cout << "threshold=" << threshold << endl;
304// cout << "returning." << endl;
305// return regions.size();
306// }
307
308 vector<Region>::iterator it = regions.begin();
309 while( it != regions.end() )
310 {
311 if (it->maxVal < threshold ) {
312 if (VerbosityLevel > 3){
313 cout << "removing max " << it->maxVal << "\t";
314 cout << " @ " << it->maxPos << "\t";
315 cout << endl;
316 }
317 it = regions.erase( it ) ;
318 }else
319 ++it;
320 }
321
322 return regions.size();
323}
324
325size_t removeMaximaAbove(
326 vector<Region> &regions,
327 float threshold,
328 int VerbosityLevel)
329{
330 if (threshold < 0){
331 if (VerbosityLevel > 0)
332 cout << "removeMaximaAbove: threshold < 0" << endl;
333 cout << "threshold=" << threshold << endl;
334 cout << "returning." << endl;
335 return regions.size();
336 }
337
338 vector<Region>::iterator it = regions.begin();
339 while( it != regions.end() )
340 {
341 if (it->maxVal > threshold ) {
342 if (VerbosityLevel > 3){
343 cout << "removing max " << it->maxVal << "\t";
344 cout << " @ " << it->maxPos << "\t";
345 cout << endl;
346 }
347 it = regions.erase( it ) ;
348 }else
349 ++it;
350 }
351
352 return regions.size();
353}
354
355Region FindAbsMax(
356 vector<Region> &regions,
357 int VerbosityLevel)
358{
359 Region AbsMax;
360 AbsMax.begin = -1;
361 AbsMax.maxPos = -1;
362 AbsMax.end = -1;
363 AbsMax.maxVal=-numeric_limits<float>::max();
364 if (regions.size() < 1)
365 return AbsMax;
366
367 for (vector<Region>::iterator it = regions.begin(); it < regions.end(); ++it)
368 {
369 if (it->maxVal > AbsMax.maxVal ) {
370 AbsMax = *it;
371 }
372 }
373 if (VerbosityLevel > 5){
374 AbsMax.begin +=0;
375 }
376 return AbsMax;
377}
378
379////////////////
380////////////// old Version of the code
381/*
382 for (unsigned int sl = step; sl < input.size(); sl+=step){
383 if (input[sl] * input[sl-] < 0 || input[sl]==0.0 ){ // sign change --> zero crossing OR really zero
384
385 if ( (input[sl-1] - input[sl]) * edge < 0){ // this is the crossing the user wanted
386
387 // check if we go lower than a certain limit in the next few slices
388 for ( int lala=0; lala<post; lala++){
389 if (input[sl+lala] > 1.5) {
390 zeroPositions->push_back(sl);
391 sl += lala;
392 break;
393 }
394 }
395
396 } else if ( (input[sl-1] - input[sl]) * edge > 0){ // this is the zero x-ing the user did not want
397
398 // do nothing
399
400 } else { // sl and sl-1 are equal .. don't know waht do to...
401
402 }
403
404 }
405
406 } // end of loop over slices - between pre and post
407
408*/
409
410
411size_t findTimeOfHalfMaxLeft(
412 vector<Region> &regions,
413 vector<float> &data,
414 float baseline,
415 int beginRisingEdge,
416 int endRisingEdge,
417 int VerbosityLevel)
418{
419 float pulse_size = 0; //
420 float thr = 0;
421
422 //int begin = beginRisingEdge;
423 int end = endRisingEdge;
424 int counter = 0;
425 // local vars for line fitting
426 float xmean = 0.0;
427 float ymean = 0.0;
428 float cov = 0.0; // empiric covarianve between x and y
429 float var = 0.0; // empiric variance of x
430 // result of line fitting
431 float slope = 0.0;
432 float intercept = 0.0;
433 // result of this function -- the position of the half rising edge
434 float pos_of_thr_Xing = 0.0;
435 int result = 0;
436
437 vector<Region>::iterator reg;
438 for (reg = regions.begin() ; reg < regions.end() ; reg++){
439 counter = 0;
440 // check if linear falling edge is completely in pipeline
441 // if not --> delete
442 if ( reg->maxPos - end < 0 )
443 {
444 // delete this region from vector<Region>
445 reg = regions.erase( reg ) ;
446 --reg;
447 continue;
448 }
449
450 // The maximum was probably found using smoothed data,
451 // so I read the value at the position of the maximum from the data
452 // again. So I rely on a well defined maxPos.
453 if (VerbosityLevel > 1) cout << "## baseline: " << baseline << endl;
454 pulse_size = data[reg->maxPos] - baseline;
455 if (VerbosityLevel > 1) cout << "## pulse_size: " << pulse_size << endl;
456 thr = pulse_size / 2. + baseline;
457 if (VerbosityLevel > 1) cout << "## thr: " << thr << endl;
458
459 // I think 5 slices left of the max there begins the rising edge
460 // and I think the rising edge is about 6 slices long.
461 // but these numbers are input as parameters
462 // beginRisingEdge &
463 // endRisingEdge
464
465 // I fit a linear function to the falling edge
466 // ALARM check for out of range values...
467
468
469 for (int slice=reg->maxPos - beginRisingEdge;
470 slice > reg->maxPos - endRisingEdge; --slice)
471 {
472 xmean += slice;
473 ymean += data[slice];
474 counter++;
475 }
476// xmean /= beginRisingEdge - endRisingEdge;
477// ymean /= beginRisingEdge - endRisingEdge;
478 xmean /= counter;
479 ymean /= counter;
480 if (VerbosityLevel > 2) cout << "## xmean: " << xmean << endl;
481 if (VerbosityLevel > 2) cout << "## ymean: " << ymean << endl;
482
483 for (int slice=reg->maxPos - beginRisingEdge;
484 slice > reg->maxPos - endRisingEdge; --slice)
485 {
486 cov = (data[slice] - ymean) * (slice - xmean);
487 var = (slice - xmean) * (slice - xmean);
488 }
489 if (VerbosityLevel > 2) cout << "## cov: " << cov << endl;
490 if (VerbosityLevel > 2) cout << "## var: " << var << endl;
491
492 slope = cov / var;
493 intercept = ymean - slope * xmean;
494 // now calculate, where the fittet line crosses the threshold
495 pos_of_thr_Xing = (thr - intercept) / slope;
496 result = (int)(pos_of_thr_Xing + 0.5);
497
498
499 if (VerbosityLevel > 2)
500 {
501 cout << "findTimeOfHalfMaxLeft() is still in debugging phase:" << endl;
502 cout << "please edit the code in oder to suppress this output:" << endl;
503 cout << "threshold: " << thr << endl;
504 cout << "slope: " << slope << " [mV/timeslice]" << endl;
505 cout << "intercept: " << intercept << "[mV]" << endl;
506 cout << "position where line crosses threshold " << pos_of_thr_Xing << endl;
507 cout << "converted to slice number " << result << endl;
508
509 }
510
511 reg->halfRisingEdgePos = result;
512
513 reg->distanceEdgeToMax = reg->maxPos - result;
514 reg->interceptRisingEdge = intercept;
515 reg->slopeOfRisingEdge = slope;
516 }
517 return regions.size();
518}
519
520size_t removeRegionWithToFlatSlope(
521 vector<Region> &regions,
522 float minSlope,
523 int VerbosityLevel)
524{
525 vector<Region>::iterator it = regions.begin();
526 while( it != regions.end() )
527 {
528 if (it->slopeOfRisingEdge < minSlope ) {
529 if (VerbosityLevel > 3){
530 cout << "removing max " << it->maxVal << "\t";
531 cout << " @ " << it->maxPos << "\t";
532 cout << endl;
533 }
534 it = regions.erase( it ) ;
535 }else
536 ++it;
537 }
538
539 return regions.size();
540}
Note: See TracBrowser for help on using the repository browser.