Index: fact/tools/rootmacros/zerosearch.C
===================================================================
--- fact/tools/rootmacros/zerosearch.C	(revision 12257)
+++ fact/tools/rootmacros/zerosearch.C	(revision 12258)
@@ -1,2 +1,3 @@
+#include "Region.h"
 #include <vector>
 
@@ -8,18 +9,288 @@
 // returns pointer to vetctor of ints
 
-vector<int> *zerosearch(vector<float> &input, int edge =-1 , int pre=10, int post=30 ){
-
+vector<Region> *zerosearch(
+	vector<float> &input,
+	int edge,			// search for transitions on rising edge=1, -1:falling
+	unsigned int step,			// search in steps of step
+	int VerbosityLevel
+){
+
+vector<Region> * ZeroPositions = new vector<Region>;
+Region currentRegion = {0, 0, 0, 0.0};
+	if (step < 1){
+		if (VerbosityLevel > 0)
+			cout << "zerosearch - stepsize=" << step
+				<< " is smaller than 1. returning." << endl;
+		return ZeroPositions;
+	}
+	if (input.size() < step){
+		if (VerbosityLevel > 0)
+			cout << "zerosearch - input-vector.size()="<< input.size()
+			<< " is smaller than stepsize=" << step
+			<< "returning." << endl;
+		return ZeroPositions;
+	}
 	
-	vector<int> * zeroPositions = new vector<int>;
+	if (edge > 0) // search for zero-x-ings on the rising edge
+	{
+		for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
+		{
+			if (input[sl] > 0.0) // can't be a rising 0-x-ing
+			{
+				continue;
+			}
+			if (input[sl] * input[sl+step] <= 0.0)  // 0-x-ing or both are 0
+			{
+				currentRegion.begin	= sl;
+				currentRegion.end	= sl+step;
+				ZeroPositions->push_back(currentRegion);
+			}
+		}
+	}
+	else if (edge < 0) // search for zero-x-ings on the falling edge
+	{
+		for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
+		{
+			if (input[sl] < 0.0) // can't be a falling 0-x-ing
+			{
+				continue;
+			}
+			if (input[sl] * input[sl+step] <= 0.0)  // 0-x-ing or both are 0
+			{
+				currentRegion.begin	= sl;
+				currentRegion.end	= sl+step;
+				ZeroPositions->push_back(currentRegion);
+			}
+		}
+
+	}
+	else // search for zero-x-ings on both esges
+	{
+		for (unsigned int sl = 0 ; sl < (input.size()-step) ; sl+=step)
+		{
+			if (input[sl] * input[sl+step] <= 0.0)  // 0-x-ing or both are 0
+			{
+				currentRegion.begin	= sl;
+				currentRegion.end	= sl+step;
+				ZeroPositions->push_back(currentRegion);
+			}
+		}
+	}
+
 	
-	for (int sl =pre ; sl < input.size()-post; sl++){
-//cout << "sl:" << sl << endl;
-		if (input[sl] * input[sl-1] < 0 || input[sl]==0.0 ){ // sign change --> zero crossing OR really zero
-
-			if ( (input[sl-1] - input[sl]) * edge < 0){ // this is the crossing the user wanted 
-				
+	return ZeroPositions;
+}
+
+size_t ShiftRegionBy(vector<Region> &src,
+	int Shift,
+	int VerbosityLevel)
+{
+vector<Region>::iterator it;
+	// I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
+	if (VerbosityLevel > 10){
+		for (it = src.begin() ; it < src.end() ; it++){
+			cout << " it->begin="<< it->begin;
+			it->begin += Shift;
+			cout << "--> shifted="<< it->begin << endl;
+			cout << " it->end="<< it->end;
+			it->end += Shift;
+			cout << "--> shifted="<< it->end << endl;
+			cout << " it->maxPos="<< it->maxPos;
+			it->maxPos += Shift;
+			cout << "--> shifted="<< it->maxPos << endl;
+		}
+		return src.size();
+	}
+
+	for (it = src.begin() ; it < src.end() ; it++){
+		it->begin += Shift;
+		it->end += Shift;
+		it->maxPos += Shift;
+	}
+	return src.size();
+}
+
+size_t EnlargeRegion(vector<Region> &src,
+	int Left,
+	int Right,
+	int VerbosityLevel)
+{
+vector<Region>::iterator it;
+	// I copy code here ... not good, but I hope nobody is giving VL > 10 ever...
+	if (VerbosityLevel > 10){
+		for (it = src.begin() ; it < src.end() ; it++){
+			cout << " it->begin="<< it->begin;
+			it->begin -= Left;
+			cout << "--> enlarged="<< it->begin << endl;
+			cout << " it->end="<< it->end;
+			it->end += Right;
+			cout << "--> enlarged="<< it->end << endl;
+		}
+		return src.size();
+	}
+
+
+		for (it = src.begin() ; it < src.end() ; it++){
+		it->begin -= Left;
+		it->end += Right;
+	}
+	return src.size();
+
+}
+
+#include <limits>
+size_t findAbsMaxInRegions(
+	vector<Region> &regions,
+	vector<float> &data,
+	int VerbosityLevel)
+{
+vector<Region>::iterator reg;
+	for (reg = regions.begin() ; reg < regions.end() ; reg++){
+		reg->maxVal=-numeric_limits<float>::max();
+		// 1st check if both ends of the region are in the data-vector
+		if (reg->begin < 0) reg->begin=0;
+		if ((unsigned int)reg->end > data.size()-1) reg->end=data.size()-1;
+
+		// TODO or TOTHINKOF:
+		// Region might overlap ... I don't care .. I just search
+		// in a later step Maxima at the same Positions can be removed...
+		// of course this means, some searches were needless ...
+		//cout << "searching from " << reg->begin << " to " << reg->end << endl;
+		for (unsigned int pos=reg->begin; pos <= (unsigned int)reg->end; pos++){
+			if (data[pos] > reg->maxVal){
+				reg->maxVal = data[pos];
+				reg->maxPos = pos;
+			}
+		}
+		if (VerbosityLevel > 3) {
+			cout << "findAbsMaxInRegions - found Max at:"
+				<< reg->maxPos << " with Value:" << reg->maxVal << endl;
+		}
+	}
+	return regions.size();
+}
+
+#include <algorithm>
+bool compMaxPosOfRegions (Region a, Region b) {
+  return (a.maxPos == b.maxPos);
+}
+
+size_t removeEqualMaxima(
+	vector<Region> &regions,
+	int VerbosityLevel)
+{
+	if (VerbosityLevel > 2){
+		cout << "removeEqualMaxima:" << endl;
+		cout << "region before contains:" << endl;
+		for (unsigned int i=0; i<regions.size(); i++){
+			cout << i <<"\t";
+			cout << regions[i].begin <<"\t";
+			cout << regions[i].end <<"\t";
+			cout << regions[i].maxPos <<"\t";
+			cout << regions[i].maxVal <<"\t";
+			cout << endl;
+		}
+	}
+
+	vector<Region>::iterator it;
+	it = unique (regions.begin(), regions.end() , compMaxPosOfRegions);
+	regions.resize( it - regions.begin() );
+
+	if (VerbosityLevel > 1){
+		cout << "region now contains:" << endl;
+		for (unsigned int i=0; i<regions.size(); i++){
+			cout << i <<"\t";
+			cout << regions[i].begin <<"\t";
+			cout << regions[i].end <<"\t";
+			cout << regions[i].maxPos <<"\t";
+			cout << regions[i].maxVal <<"\t";
+			cout << endl;
+		}
+	}
+	return regions.size();
+}
+
+
+size_t removeReginWithMaximaOnEdge(
+	vector<Region> &regions,
+	unsigned int EdgeWidth,
+	int VerbosityLevel)
+{
+	if (EdgeWidth < 1){
+		if (VerbosityLevel > 0)
+			cout << "removeReginWithMaximaOnEdge: EdgeWidth < 1" << endl;
+			cout << "EdgeWidth=" << EdgeWidth << endl;
+			cout << "returning." << endl;
+		return regions.size();
+	}
+
+	vector<Region>::iterator it = regions.begin();
+	while( it != regions.end() )
+	{
+//	cout << it->begin << "\t";
+//	cout << it->maxPos << "\t";
+//	cout << it->end << "\t";
+//	cout << it->maxVal << endl;
+
+		if (it->maxPos < (int)(it->begin+EdgeWidth)) {
+			if (VerbosityLevel > 3)
+				cout << "erasing Region(max@left edge) " << it->maxPos << endl;
+			it = regions.erase( it ) ;
+			//++it;
+		}else if (it->maxPos > (int)(it->end-EdgeWidth)) {
+			if (VerbosityLevel > 3)
+				cout << "erasing Region(max@right edge) " << it->maxPos << endl;
+			it = regions.erase( it ) ;
+			//++it;
+		}else
+			++it;
+
+	}
+
+	return regions.size();
+}
+
+size_t removeMaximaBelow(
+	vector<Region> &regions,
+	float threshold,
+	int VerbosityLevel)
+{
+	if (threshold < 0){
+		if (VerbosityLevel > 0)
+			cout << "removeMaximaBelow: threshold < 0" << endl;
+			cout << "threshold=" << threshold << endl;
+			cout << "returning." << endl;
+		return regions.size();
+	}
+
+	vector<Region>::iterator it = regions.begin();
+	while( it != regions.end() )
+	{
+		if (it->maxVal < threshold ) {
+			if (VerbosityLevel > 3){
+				cout << "removing max " << it->maxVal << "\t";
+				cout << " @ " << it->maxPos << "\t";
+				cout << endl;
+			}
+			it = regions.erase( it ) ;
+		}else
+			++it;
+	}
+
+	return regions.size();
+}
+
+
+////////////////
+////////////// old Version of the code
+/*
+	for (unsigned int sl = step; sl < input.size(); sl+=step){
+		if (input[sl] * input[sl-] < 0 || input[sl]==0.0 ){ // sign change --> zero crossing OR really zero
+
+			if ( (input[sl-1] - input[sl]) * edge < 0){ // this is the crossing the user wanted
+
 				// check if we go lower than a certain limit in the next few slices
 				for ( int lala=0; lala<post; lala++){
-					if (input[sl+lala] < -1.5) {
+					if (input[sl+lala] > 1.5) {
 						zeroPositions->push_back(sl);
 						sl += lala;
@@ -33,12 +304,10 @@
 
 			} else { // sl and sl-1 are equal .. don't know waht do to...
-	
-			}		
-
-		} 
+
+			}
+
+		}
 
 	} // end of loop over slices - between pre and post
 
-	
-	return zeroPositions;
-}
+*/
Index: fact/tools/rootmacros/zerosearch.h
===================================================================
--- fact/tools/rootmacros/zerosearch.h	(revision 12258)
+++ fact/tools/rootmacros/zerosearch.h	(revision 12258)
@@ -0,0 +1,44 @@
+// file zerosearch.h
+#ifndef __ZEROSEARCH_H
+#define __ZEROSEARCH_H
+
+#include "Region.h"
+
+vector<Region> *zerosearch(
+	vector<float> &input,
+	int edge = 1,			// search for transitions on rising edge=1, -1:falling
+	unsigned int step = 4,			// search in steps of step
+	int VerbosityLevel = 1  // 1 means ... just normal warings are output.
+);
+
+size_t ShiftRegionBy(vector<Region> &src,
+	int Shift,
+	int VerbosityLevel=0);
+
+size_t EnlargeRegion(vector<Region> &src,
+	int Left,
+	int Right,
+	int VerbosityLevel=0);
+
+size_t findAbsMaxInRegions(
+	vector<Region> &regions,
+	vector<float> &data,
+	int VerbosityLevel=0);
+
+bool compMaxPosOfRegions (Region a, Region b);
+
+size_t removeEqualMaxima(
+	vector<Region> &regions,
+	int VerbosityLevel=0);
+
+size_t removeReginWithMaximaOnEdge(
+	vector<Region> &regions,
+	unsigned int EdgeWidth=3,
+	int VerbosityLevel=0);
+
+size_t removeMaximaBelow(
+	vector<Region> &regions,
+	float threshold = 2.0,
+	int VerbosityLevel=0);
+
+#endif
