fast pQCD calculations for hadron-induced processes
fastNLOTools.h
1 #ifndef __fnlotools__
2 #define __fnlotools__
3 
4 #include <string>
5 #include <vector>
6 #include "fastnlotk/fastNLOConstants.h"
7 #include "speaker.h"
8 
9 namespace fastNLOTools {
10 
11 
13  template<typename T> int ReadVector( std::vector<T>& v, std::istream& table , double nevts = 1);
14  int ReadVector( std::vector<double>& v, std::istream& table , double nevts = 1);
15 
16  template<typename T> int ReadFlexibleVector(std::vector<T>& v, std::istream& table, int nProcLast=0 , double nevts = 1);
17  int ReadFlexibleVector( std::vector<std::string >& v, std::istream& table , int nProcLast = 0 , double nevts = 1 );
18  int ReadFlexibleVector( std::vector<double >& v, std::istream& table , int nProcLast = 0 , double nevts = 1 );
19  int ReadFlexibleVector( std::vector<int >& v, std::istream& table , int nProcLast = 0 , double nevts = 1 );
20  int ReadFlexibleVector( std::vector<unsigned long long >& v, std::istream& table , int nProcLast = 0 , double nevts = 1 );
21  int ReadUnused( std::istream& table );
22 
24  template<typename T> void ResizeFlexibleVector(std::vector<T>& v, const std::vector<T>& nom);
25  void ResizeFlexibleVector( std::vector<double >& v, const std::vector<double >& nom);
26  void ResizeFlexibleVector( std::vector<unsigned long long >& v, const std::vector<double >& nom);
27 
29  template<typename T> void ClearVector(std::vector<std::vector<T > >& v);
30  template<typename T> void ClearVector(std::vector<T>& v);
31 
32  // there are nicer options in c++11
33  void ResizeVector( fastNLO::v1d& v, int dim0 );
34  void ResizeVector( fastNLO::v2d& v, int dim0 , int dim1 );
35  void ResizeVector( fastNLO::v3d& v, int dim0 , int dim1, int dim2 );
36  void ResizeVector( fastNLO::v4d& v, int dim0 , int dim1, int dim2, int dim3 );
37  void ResizeVector( fastNLO::v5d& v, int dim0 , int dim1, int dim2, int dim3, int dim4 );
38  void ResizeVector( fastNLO::v6d& v, int dim0 , int dim1, int dim2, int dim3, int dim4, int dim5 );
39  void ResizeVector( fastNLO::v7d& v, int dim0 , int dim1, int dim2, int dim3, int dim4, int dim5, int dim6 );
40 
42  template<typename T> bool IsEmptyVector(const std::vector<std::vector<T > >& v);
43  template<typename T> bool IsEmptyVector(const std::vector<T>& v);
44 
48  template<typename T> int WriteVector( const std::vector<T>& v, std::ostream& table , double nevts=1 );
49  template<typename T> int _Write1DVectorByN( const std::vector<T>& v, std::ostream& table , double nevts );
50  template<typename T> int _Write1DVector( const std::vector<T>& v, std::ostream& table);
51  int WriteVector( const std::vector<double >& v, std::ostream& table , double nevts=1 );
52  int WriteVector( const std::vector<std::string >& v, std::ostream& table , double nevts=1 );
53  int WriteVector( const std::vector<int >& v, std::ostream& table , double nevts=1 ) ;
54  int WriteVector( const std::vector<unsigned long long >& v, std::ostream& table , double nevts=1 );
55 
56  template<typename T> int WriteFlexibleVector( const std::vector<T>& v, std::ostream& table, int nProcLast = 0, double nevts=1 );
57  int WriteFlexibleVector( const std::vector<double >& v, std::ostream& table, int nProcLast = 0 , double nevts=1 );
58  int WriteFlexibleVector( const std::vector<std::string >& v, std::ostream& table, int nProcLast = 0 , double nevts=1 );
59  int WriteFlexibleVector( const std::vector<int >& v, std::ostream& table, int nProcLast = 0 , double nevts=1 );
60  int WriteFlexibleVector( const std::vector<unsigned long long >& v, std::ostream& table, int nProcLast = 0 , double nevts=1 );
61 
63  template<typename T> void AddVectors( std::vector<T>& vSum, const std::vector<T>& vAdd, double w1 = 1, double w2 = 1 );
64  template<typename T> void _DoAddVectors( std::vector<T>& vSum, const std::vector<T>& vAdd, double w1 = 1, double w2 = 1 );
65  void AddVectors( std::vector<double >& vSum, const std::vector<double >& vAdd, double w1 = 1, double w2 = 1 ) ;
66  void AddVectors( std::vector<int >& vSum, const std::vector<int >& vAdd, double w1 = 1, double w2 = 1 ) ;
67  void AddVectors( std::vector<unsigned long long >& vSum, const std::vector<unsigned long long >& vAdd, double w1=1, double w2=1 ) ;
68 
70  void StripWhitespace(std::string& s);
71 
73  template<typename T> void PrintVector( const std::vector<T>& v, std::string name, std::string prefix="");
74 
76  bool CheckVersion(int version);
77  //bool CheckVersion(const std::string& version) {return CheckVersion((int)std::stoi(version));} ; //!< check version and exit if failed.
78  bool ReadMagicNo(std::istream& table);
79  void PutBackMagicNo(std::istream& table);
80 
81 };
82 
83 
84 
85 //________________________________________________________________________________________________________________
86 // Reading functions
87 template<typename T>
88 int fastNLOTools::ReadVector( std::vector<T>& v, std::istream& table , double nevts){
91  int nn = 0;
92  for( unsigned int i=0 ; i<v.size() ; i++ ){
93  nn += ReadVector(v[i],table, nevts);
94  }
95  return nn;
96 };
97 
98 
99 template<typename T>
100 int fastNLOTools::ReadFlexibleVector(std::vector<T>& v, std::istream& table, int nProcLast, double nevts ){
101  int nn = 0;
102  int size = 0;
103  table >> size; nn++;
104  v.resize(size);
105  for(unsigned int i0=0;i0<v.size();i0++){
106  nn += ReadFlexibleVector(v[i0],table,nProcLast,nevts);
107  }
108  return nn;
109 };
110 
111 //________________________________________________________________________________________________________________
112 // Resizing functions
113 template<typename T>
114 void fastNLOTools::ResizeFlexibleVector(std::vector<T>& v, const std::vector<T>& nom) {
115  v.resize(nom.size());
116  for (unsigned int i = 0 ; i<v.size() ; i++) {
117  ResizeFlexibleVector(v[i],nom[i]);
118  }
119 };
120 
121 
122 //________________________________________________________________________________________________________________
123 // Clearing
124 template<typename T>
125 void fastNLOTools::ClearVector(std::vector<std::vector<T > >& v) {
126  for (unsigned int i = 0 ; i<v.size() ; i++) {
127  ClearVector(v[i]);
128  }
129 };
130 
131 template<typename T>
132 void fastNLOTools::ClearVector(std::vector<T >& v) {
133  for (unsigned int i = 0 ; i<v.size() ; i++) {
134  v[i]=0;
135  }
136 };
137 
138 
139 //________________________________________________________________________________________________________________
140 // Check if std::vector is empty
141 template<typename T>
142 bool fastNLOTools::IsEmptyVector(const std::vector<std::vector<T > >& v){
144  if ( v.empty() ) return true;
145  for (unsigned int i = 0 ; i<v.size() ; i++) {
146  if ( !IsEmptyVector(v[i]) ) return false;
147  }
148  return true;
149 }
150 
151 template<typename T>
152 bool fastNLOTools::IsEmptyVector(const std::vector<T>& v){
154  if ( v.empty() ) return true;
155  for (unsigned int i = 0 ; i<v.size() ; i++) {
156  if ( v[i] != 0 ) return false;
157  }
158  return true;
159 }
160 
161 
162 //________________________________________________________________________________________________________________
163 // Writing functions
164 template<typename T>
165 int fastNLOTools::WriteVector( const std::vector<T>& v, std::ostream& table , double nevts) {
167  int nn = 0;
168  for(unsigned int i=0;i<v.size();i++)
169  nn += WriteVector( v[i] , table , nevts );
170  return nn;
171 }
172 
173 template<typename T>
174 int fastNLOTools::_Write1DVectorByN( const std::vector<T>& v, std::ostream& table , double nevts) {
175  if( nevts == 0) return -1000;
176  for(unsigned int i0=0;i0<v.size();i0++)
177  table << v[i0] / nevts << fastNLO::sep;
178  return v.size();
179 }
180 
181 template<typename T>
182 int fastNLOTools::_Write1DVector( const std::vector<T>& v, std::ostream& table ) {
183  for(unsigned int i0=0;i0<v.size();i0++)
184  table << v[i0] << fastNLO::sep;
185  return v.size();
186 }
187 
188 
189 template<typename T>
190 int fastNLOTools::WriteFlexibleVector( const std::vector<T>& v, std::ostream& table, int nProcLast , double nevts ) {
191  if ( nevts == 0 ) {
192  say::error["fastNLOTools::WriteFlexibleVector"]<<"Cannot divide by zero."<<std::endl;
193  return -1000;
194  }
195  int nn = 1;
196  table << v.size() << fastNLO::sep;
197  for(unsigned int i0=0;i0<v.size();i0++){
198  nn += WriteFlexibleVector( v[i0] , table , nProcLast , nevts );
199  }
200  return nn;
201 };
202 
203 
204 //________________________________________________________________________________________________________________
205 // Adding functions
206 template<typename T>
207 void fastNLOTools::AddVectors( std::vector<T>& vSum, const std::vector<T>& vAdd, double w1, double w2 ) {
211  if ( vSum.size() != vAdd.size() ) {
212  say::error["fastNLOTools::AddVectors"]
213  <<"Cannot add tables with different size. s1="
214  <<vSum.size()<<", s2="<<vAdd.size()<<std::endl;
215  return;
216  }
217  for ( unsigned int i = 0 ; i<vSum.size() ; i++ )
218  AddVectors( vSum[i], vAdd[i], w1 , w2 );
219 }
220 
221 template<typename T>
222 void fastNLOTools::_DoAddVectors( std::vector<T>& vSum, const std::vector<T>& vAdd, double w1, double w2 ) {
224  if ( vSum.size() != vAdd.size() ) {
225  say::error["fastNLOTools::_DoAddVectors"]
226  <<"Cannot add tables with different size. s1="
227  <<vSum.size()<<", s2="<<vAdd.size()<<std::endl;
228  return;
229  }
230  if ( w1==1. && w2==1. )
231  for ( unsigned int i = 0 ; i<vSum.size() ; i++ )
232  vSum[i] += vAdd[i];
233  else
234  for ( unsigned int i = 0 ; i<vSum.size() ; i++ )
235  vSum[i] = w1*vSum[i] + w2*vAdd[i];
236 }
237 
238 template<typename T>
239 void fastNLOTools::PrintVector( const std::vector<T>& v, std::string name, std::string prefix){
240  std::cout<<" "<<prefix<<" "<<name<<std::endl;
241  for(unsigned int i=0;i<v.size();i++){
242  std::cout<<" "<<prefix<<" "<<i<<"\t"<<v[i]<<std::endl;
243  }
244 }
245 
246 
247 #endif
Definition: fastNLOTools.cc:11