ROOTPWA
wset.h
Go to the documentation of this file.
1 
2 //
3 // Copyright 2009 Sebastian Neubert
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #include <fstream>
23 #include <iostream>
24 #include <iterator>
25 #include <vector>
26 #include <string>
27 #include <map>
28 #include <set>
29 #include "TString.h"
30 #include "TRandom3.h"
31 
32 using namespace std;
33 
34 // helper class
35 class wsetentry {
36 public:
37  TString name;
38  double threshold;
39  unsigned int index; // in waveset
40 
41  void operator =(const wsetentry& w){
42  name=w.name; threshold=w.threshold; index=w.index;
43  }
44 
45  friend bool operator <(const wsetentry& lhs,const wsetentry& rhs);
46  friend bool operator ==(const wsetentry& lhs,const wsetentry& rhs);
47  friend bool operator !=(const wsetentry& lhs,const wsetentry& rhs);
48  bool operator <(const wsetentry& e){return this->index<e.index;}
49  bool operator ==(const wsetentry& e){return this->name==e.name;}
50  bool operator !=(const wsetentry& e){return !(*this==e);}
51 };
52 
53 
54 
55 bool operator <(const wsetentry& lhs,const wsetentry& rhs){
56  return lhs.index<rhs.index;
57 }
58 bool operator ==(const wsetentry& lhs,const wsetentry& rhs){
59  return lhs.name==rhs.name;
60 }
61 bool operator !=(const wsetentry& lhs,const wsetentry& rhs){
62  return !(lhs==rhs);
63 }
64 
65 set<wsetentry>::iterator findByName(set<wsetentry>& aset, wsetentry entry){
66  set<wsetentry>::iterator sit=aset.begin();
67  while(sit!= aset.end()){
68  set<wsetentry>::iterator sit2=sit;
69  sit++;
70  if(entry.name==sit2->name)return sit2;
71  }
72  return aset.end();
73 }
74 
75 void removeFromSet(set<wsetentry>& aset, wsetentry entry){
76  set<wsetentry>::iterator sit=findByName(aset,entry);
77  if(sit!=aset.end())aset.erase(sit);
78 }
79 
80 
82 void readWavelist(set<wsetentry>& result, const TString& input){
83  ifstream file(input.Data());
84  string line;
85  unsigned int index=0;
86  while(file.good()){
87  getline(file,line);
88  unsigned int pos=line.find(" ");
89  string name=line.substr(0,pos);
90  if(name.length()<2)continue;
91  double thres;
92  if(pos<line.length()){
93  thres=atof(line.substr(pos,line.length()).c_str());
94  } else thres=0;
95  if(line.length()>1){
96  wsetentry entry;
97  entry.name=name;entry.threshold=thres;
98  entry.index=index++;
99  result.insert(entry);
100  }
101  }
102 }
103 
104 set<wsetentry>::iterator
105 randomEntry(set<wsetentry>& myset, unsigned int start=0){
106  if(start>=myset.size()){
107  cerr<<" Requesting randomEntry " << start <<" out of range!" << endl;
108  throw;
109  }
110  unsigned int x=start;
111  if(start<myset.size()-1){
112  unsigned int n=myset.size()-start;
113  if(n!=0)x=gRandom->Integer(n)+start;
114  }
115  //cerr << "x=" <<x << endl;
116  set<wsetentry>::iterator it=myset.begin();
117  for(unsigned i=0;i<x;++i)++it;
118  return it;
119 }