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

Last change on this file since 12365 was 12306, checked in by neise, 13 years ago
added removeMaximaAbove method
File size: 8.3 KB
Line 
1#include "Region.h"
2#include <vector>
3
4// searches for zero crossings in a given vector of floats
5// for zero crossings in falling edge
6// give edge = -1
7//
8//
9// returns pointer to vetctor of ints
10
11vector<Region> *zerosearch(
12 vector<float> &input,
13 int edge, // search for transitions on rising edge=1, -1:falling
14 unsigned int step, // search in steps of step
15 int VerbosityLevel
16){
17
18vector<Region> * ZeroPositions = new vector<Region>;
19Region currentRegion = {0, 0, 0, 0.0};
20 if (step < 1){
21 if (VerbosityLevel > 0)
22 cout << "zerosearch - stepsize=" << step
23 << " is smaller than 1. returning." << endl;
24 return ZeroPositions;
25 }
26 if (input.size() < step){
27 if (VerbosityLevel > 0)
28 cout << "zerosearch - input-vector.size()="<< input.size()
29 << " is smaller than stepsize=" << step
30 << "returning." << endl;
31 return ZeroPositions;
32 }
33
34 if (edge > 0) // search for zero-x-ings on the rising edge
35 {
36 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
37 {
38 if (input[sl] > 0.0) // can't be a rising 0-x-ing
39 {
40 continue;
41 }
42 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
43 {
44 currentRegion.begin = sl;
45 currentRegion.end = sl+step;
46 ZeroPositions->push_back(currentRegion);
47 }
48 }
49 }
50 else if (edge < 0) // search for zero-x-ings on the falling edge
51 {
52 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
53 {
54 if (input[sl] < 0.0) // can't be a falling 0-x-ing
55 {
56 continue;
57 }
58 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
59 {
60 currentRegion.begin = sl;
61 currentRegion.end = sl+step;
62 ZeroPositions->push_back(currentRegion);
63 }
64 }
65
66 }
67 else // search for zero-x-ings on both esges
68 {
69 for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
70 {
71 if (input[sl] * input[sl+step] <= 0.0) // 0-x-ing or both are 0
72 {
73 currentRegion.begin = sl;
74 currentRegion.end = sl+step;
75 ZeroPositions->push_back(currentRegion);
76 }
77 }
78 }
79
80
81 return ZeroPositions;
82}
83
84size_t ShiftRegionBy(vector<Region> &src,
85 int Shift,
86 int VerbosityLevel)
87{
88vector<Region>::iterator it;
89 // I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
90 if (VerbosityLevel > 10){
91 for (it = src.begin() ; it < src.end() ; it++){
92 cout << " it->begin="<< it->begin;
93 it->begin += Shift;
94 cout << "--> shifted="<< it->begin << endl;
95 cout << " it->end="<< it->end;
96 it->end += Shift;
97 cout << "--> shifted="<< it->end << endl;
98 cout << " it->maxPos="<< it->maxPos;
99 it->maxPos += Shift;
100 cout << "--> shifted="<< it->maxPos << endl;
101 }
102 return src.size();
103 }
104
105 for (it = src.begin() ; it < src.end() ; it++){
106 it->begin += Shift;
107 it->end += Shift;
108 it->maxPos += Shift;
109 }
110 return src.size();
111}
112
113size_t EnlargeRegion(vector<Region> &src,
114 int Left,
115 int Right,
116 int VerbosityLevel)
117{
118vector<Region>::iterator it;
119 // I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
120 if (VerbosityLevel > 10){
121 for (it = src.begin() ; it < src.end() ; it++){
122 cout << " it->begin="<< it->begin;
123 it->begin -= Left;
124 cout << "--> enlarged="<< it->begin << endl;
125 cout << " it->end="<< it->end;
126 it->end += Right;
127 cout << "--> enlarged="<< it->end << endl;
128 }
129 return src.size();
130 }
131
132
133 for (it = src.begin() ; it < src.end() ; it++){
134 it->begin -= Left;
135 it->end += Right;
136 }
137 return src.size();
138
139}
140
141#include <limits>
142size_t findAbsMaxInRegions(
143 vector<Region> &regions,
144 vector<float> &data,
145 int VerbosityLevel)
146{
147vector<Region>::iterator reg;
148 for (reg = regions.begin() ; reg < regions.end() ; reg++){
149 reg->maxVal=-numeric_limits<float>::max();
150 // 1st check if both ends of the region are in the data-vector
151 if (reg->begin < 0) reg->begin=0;
152 if ((unsigned int)reg->end > data.size()-1) reg->end=data.size()-1;
153
154 // TODO or TOTHINKOF:
155 // Region might overlap ... I don't care .. I just search
156 // in a later step Maxima at the same Positions can be removed...
157 // of course this means, some searches were needless ...
158 //cout << "searching from " << reg->begin << " to " << reg->end << endl;
159 for (unsigned int pos=reg->begin; pos <= (unsigned int)reg->end; pos++){
160 if (data[pos] > reg->maxVal){
161 reg->maxVal = data[pos];
162 reg->maxPos = pos;
163 }
164 }
165 if (VerbosityLevel > 3) {
166 cout << "findAbsMaxInRegions - found Max at:"
167 << reg->maxPos << " with Value:" << reg->maxVal << endl;
168 }
169 }
170 return regions.size();
171}
172
173#include <algorithm>
174bool compMaxPosOfRegions (Region a, Region b) {
175 return (a.maxPos == b.maxPos);
176}
177
178size_t removeEqualMaxima(
179 vector<Region> &regions,
180 int VerbosityLevel)
181{
182 if (VerbosityLevel > 2){
183 cout << "removeEqualMaxima:" << endl;
184 cout << "region before contains:" << endl;
185 for (unsigned int i=0; i<regions.size(); i++){
186 cout << i <<"\t";
187 cout << regions[i].begin <<"\t";
188 cout << regions[i].end <<"\t";
189 cout << regions[i].maxPos <<"\t";
190 cout << regions[i].maxVal <<"\t";
191 cout << endl;
192 }
193 }
194
195 vector<Region>::iterator it;
196 it = unique (regions.begin(), regions.end() , compMaxPosOfRegions);
197 regions.resize( it - regions.begin() );
198
199 if (VerbosityLevel > 1){
200 cout << "region now contains:" << endl;
201 for (unsigned int i=0; i<regions.size(); i++){
202 cout << i <<"\t";
203 cout << regions[i].begin <<"\t";
204 cout << regions[i].end <<"\t";
205 cout << regions[i].maxPos <<"\t";
206 cout << regions[i].maxVal <<"\t";
207 cout << endl;
208 }
209 }
210 return regions.size();
211}
212
213
214size_t removeReginWithMaximaOnEdge(
215 vector<Region> &regions,
216 unsigned int EdgeWidth,
217 int VerbosityLevel)
218{
219 if (EdgeWidth < 1){
220 if (VerbosityLevel > 0)
221 cout << "removeReginWithMaximaOnEdge: EdgeWidth < 1" << endl;
222 cout << "EdgeWidth=" << EdgeWidth << endl;
223 cout << "returning." << endl;
224 return regions.size();
225 }
226
227 vector<Region>::iterator it = regions.begin();
228 while( it != regions.end() )
229 {
230// cout << it->begin << "\t";
231// cout << it->maxPos << "\t";
232// cout << it->end << "\t";
233// cout << it->maxVal << endl;
234
235 if (it->maxPos < (int)(it->begin+EdgeWidth)) {
236 if (VerbosityLevel > 3)
237 cout << "erasing Region(max@left edge) " << it->maxPos << endl;
238 it = regions.erase( it ) ;
239 //++it;
240 }else if (it->maxPos > (int)(it->end-EdgeWidth)) {
241 if (VerbosityLevel > 3)
242 cout << "erasing Region(max@right edge) " << it->maxPos << endl;
243 it = regions.erase( it ) ;
244 //++it;
245 }else
246 ++it;
247
248 }
249
250 return regions.size();
251}
252
253size_t removeMaximaBelow(
254 vector<Region> &regions,
255 float threshold,
256 int VerbosityLevel)
257{
258 if (threshold < 0){
259 if (VerbosityLevel > 0)
260 cout << "removeMaximaBelow: threshold < 0" << endl;
261 cout << "threshold=" << threshold << endl;
262 cout << "returning." << endl;
263 return regions.size();
264 }
265
266 vector<Region>::iterator it = regions.begin();
267 while( it != regions.end() )
268 {
269 if (it->maxVal < threshold ) {
270 if (VerbosityLevel > 3){
271 cout << "removing max " << it->maxVal << "\t";
272 cout << " @ " << it->maxPos << "\t";
273 cout << endl;
274 }
275 it = regions.erase( it ) ;
276 }else
277 ++it;
278 }
279
280 return regions.size();
281}
282
283size_t removeMaximaAbove(
284 vector<Region> &regions,
285 float threshold,
286 int VerbosityLevel)
287{
288 if (threshold < 0){
289 if (VerbosityLevel > 0)
290 cout << "removeMaximaAbove: threshold < 0" << endl;
291 cout << "threshold=" << threshold << endl;
292 cout << "returning." << endl;
293 return regions.size();
294 }
295
296 vector<Region>::iterator it = regions.begin();
297 while( it != regions.end() )
298 {
299 if (it->maxVal > threshold ) {
300 if (VerbosityLevel > 3){
301 cout << "removing max " << it->maxVal << "\t";
302 cout << " @ " << it->maxPos << "\t";
303 cout << endl;
304 }
305 it = regions.erase( it ) ;
306 }else
307 ++it;
308 }
309
310 return regions.size();
311}
312
313////////////////
314////////////// old Version of the code
315/*
316 for (unsigned int sl = step; sl < input.size(); sl+=step){
317 if (input[sl] * input[sl-] < 0 || input[sl]==0.0 ){ // sign change --> zero crossing OR really zero
318
319 if ( (input[sl-1] - input[sl]) * edge < 0){ // this is the crossing the user wanted
320
321 // check if we go lower than a certain limit in the next few slices
322 for ( int lala=0; lala<post; lala++){
323 if (input[sl+lala] > 1.5) {
324 zeroPositions->push_back(sl);
325 sl += lala;
326 break;
327 }
328 }
329
330 } else if ( (input[sl-1] - input[sl]) * edge > 0){ // this is the zero x-ing the user did not want
331
332 // do nothing
333
334 } else { // sl and sl-1 are equal .. don't know waht do to...
335
336 }
337
338 }
339
340 } // end of loop over slices - between pre and post
341
342*/
Note: See TracBrowser for help on using the repository browser.