JHUGen MELA  JHUGen v7.5.6, MELA v2.4.2
Matrix element calculations as used in JHUGen.
MELALinearInterpFunc.cc
Go to the documentation of this file.
1 #include "MELALinearInterpFunc.h"
2 #include <cmath>
3 #include "Riostream.h"
4 #include "TMath.h"
5 
6 using namespace TMath;
7 using namespace RooFit;
8 using namespace std;
9 using namespace TNumericUtil;
10 
11 
13 
15 RooAbsReal(),
16 verbosity(MELALinearInterpFunc::kSilent),
17 useFloor(true), floorEval(0), floorInt(0),
18 rangeXmin(1), rangeXmax(-1),
19 theXVar("theXVar", "theXVar", this),
20 FcnList("FcnList", "FcnList", this),
21 leafDepsList("leafDepsList", "leafDepsList", this)
22 {}
23 
25  const char* name,
26  const char* title
27  ) :
28  RooAbsReal(name, title),
29  verbosity(MELALinearInterpFunc::kSilent),
30  useFloor(true), floorEval(0), floorInt(0),
31  rangeXmin(1), rangeXmax(-1),
32  theXVar("theXVar", "theXVar", this),
33  FcnList("FcnList", "FcnList", this),
34  leafDepsList("leafDepsList", "leafDepsList", this)
35 {}
36 
38  const char* name,
39  const char* title,
40  RooAbsReal& inXVar,
41  const std::vector<MELALinearInterpFunc::T>& inXList,
42  const RooArgList& inFcnList,
43  Bool_t inUseFloor,
44  T inFloorEval,
45  T inFloorInt
46  ) :
47  RooAbsReal(name, title),
48  verbosity(MELALinearInterpFunc::kSilent),
49  useFloor(inUseFloor), floorEval(inFloorEval), floorInt(inFloorInt),
50  rangeXmin(1), rangeXmax(-1),
51  XList(inXList),
52  theXVar("theXVar", "theXVar", this, inXVar),
53  FcnList("FcnList", "FcnList", this),
54  leafDepsList("leafDepsList", "leafDepsList", this)
55 {
56  RooArgSet leafset;
57  if (!dynamic_cast<RooRealVar*>(theXVar.absArg())){
58  RooArgSet tmpdeps;
59  theXVar.absArg()->leafNodeServerList(&tmpdeps, 0, true);
60  leafset.add(tmpdeps);
61  }
62 
63  TIterator* iter = inFcnList.createIterator();
64  RooAbsArg* absarg;
65  while ((absarg = (RooAbsArg*)iter->Next())){
66  if (!dynamic_cast<RooAbsReal*>(absarg)){
67  coutE(InputArguments) << "ERROR::MELALinearInterpFunc(" << GetName() << ") function " << absarg->GetName() << " is not of type RooAbsReal" << endl;
68  assert(0);
69  }
70  FcnList.add(*absarg);
71 
72  if (!dynamic_cast<RooRealVar*>(absarg)){
73  RooArgSet tmpdeps;
74  absarg->leafNodeServerList(&tmpdeps, 0, true);
75  leafset.add(tmpdeps);
76  }
77  }
78  delete iter;
79 
80  iter = leafset.createIterator();
81  while ((absarg = (RooAbsArg*)iter->Next())){ if (dynamic_cast<RooRealVar*>(absarg)) leafDepsList.add(*absarg); }
82  delete iter;
83 
84  if (FcnList.getSize()!=(int)XList.size()){
85  coutE(InputArguments) << "MELALinearInterpFunc ERROR::MELALinearInterpFunc(" << GetName() << ") input XList size not the same as FcnList size!" << endl;
86  assert(0);
87  }
88 }
89 
91  const MELALinearInterpFunc& other,
92  const char* name
93  ) :
94  RooAbsReal(other, name),
95  verbosity(other.verbosity),
96  useFloor(other.useFloor), floorEval(other.floorEval), floorInt(other.floorInt),
97  rangeXmin(other.rangeXmin), rangeXmax(other.rangeXmax),
98  XList(other.XList),
99  theXVar("theXVar", this, other.theXVar),
100  FcnList("FcnList", this, other.FcnList),
101  leafDepsList("leafDepsList", this, other.leafDepsList)
102 {}
103 
107 void MELALinearInterpFunc::doFloor(Bool_t flag){ useFloor = flag; }
108 
110  Int_t bin=-1;
111  MELALinearInterpFunc::T valj, valjpo;
112  Int_t np;
113  vector<MELALinearInterpFunc::T> const* coord=&XList;
114  np=npoints();
115 
116  if (np<=1) bin=0;
117  else{
118  valjpo = coord->at(0);
119  for (Int_t j=0; j<np-1; j++){
120  valj = coord->at(j);
121  valjpo = coord->at(j+1);
122  if (val<valjpo && val>=valj){ bin=j; break; }
123  }
124  if (bin==-1 && val>=valjpo) bin=np-2;
125  else if (bin==-1) bin=0;
126  }
127 
128  return bin;
129 }
131  if (bin>=0){
132  vector<MELALinearInterpFunc::T> const* coord=&XList;
133  MELALinearInterpFunc::T diff = 1;
134  if (Int_t(coord->size())>(bin+1)) diff = coord->at(bin+1)-coord->at(bin);
135  if (fabs(diff)>MELALinearInterpFunc::T(0)) return MELALinearInterpFunc::T(1)/diff;
136  else return 0;
137  }
138  else return 1;
139 }
141  const Int_t bin = getWhichBin(val);
142  vector<MELALinearInterpFunc::T> const* coord=&XList;
144  return (val - coord->at(bin))*kappa;
145 }
146 
147 MELALinearInterpFunc::T MELALinearInterpFunc::interpolateFcn(Int_t code, const char* rangeName)const{
148  const Int_t xprime = 101;
149  const bool intAlongX=(code>0 && code%xprime==0);
150 
152  Int_t coderem = (intAlongX ? code/xprime : code);
153  if (coderem==1) coderem=0;
154 
156  if (intAlongX) cout << "MELALinearInterpFunc(" << GetName() << ")::interpolateFcn integrates using code = " << code << ", coderem = " << coderem << endl;
157  else cout << "MELALinearInterpFunc(" << GetName() << ")::interpolateFcn evaluates using code = " << code << ", coderem = " << coderem << endl;
158  }
159 
160  // Get bins
161  Int_t xbin=-1, xbinmin=-1, xbinmax=-1;
162  MELALinearInterpFunc::T tx=0, txmin=0, txmax=0;
163  if (!intAlongX){ // Case to just compute the value at x
164  if (!testRangeValidity(theXVar)) return 0;
165  xbin = getWhichBin(theXVar);
166  tx = getTVar(theXVar);
167  }
168  else{ // Case to integrate along x
169  MELALinearInterpFunc::T coordmin = theXVar.min(rangeName); cropValueForRange(coordmin);
170  MELALinearInterpFunc::T coordmax = theXVar.max(rangeName); cropValueForRange(coordmax);
171  xbinmin = getWhichBin(coordmin);
172  txmin = getTVar(coordmin);
173  xbinmax = getWhichBin(coordmax);
174  txmax = getTVar(coordmax);
175  }
176 
177  int nxbins = npoints()-1;
178  if (nxbins==0){
180  if (coderem==0) fcnval = dynamic_cast<const RooAbsReal*>(FcnList.at(0))->getVal();
181  else fcnval = dynamic_cast<const RooAbsReal*>(FcnList.at(0))->analyticalIntegral(coderem, rangeName);
182  res = fcnval;
183  if (intAlongX) res *= (txmax-txmin);
184  }
185  else{
186  for (int ix=0; ix<nxbins; ix++){
187  if (
188  (xbin>=0 && ix!=xbin)
189  ||
190  (xbinmin>=0 && xbinmax>=xbinmin && !(xbinmin<=ix && ix<=xbinmax))
191  ) continue;
192 
193  MELALinearInterpFunc::T txlow=0, txhigh=1;
194  if (intAlongX){
195  if (ix==xbinmin) txlow=txmin;
196  if (ix==xbinmax) txhigh=txmax;
197  }
198  else txhigh=tx;
199 
200  MELALinearInterpFunc::T fcnval[2]={ 0 };
201  if (coderem==0){
202  for (unsigned int j=0; j<2; j++) fcnval[j] = dynamic_cast<const RooAbsReal*>(FcnList.at(ix+j))->getVal();
203  }
204  else{
205  for (unsigned int j=0; j<2; j++) fcnval[j] = dynamic_cast<const RooAbsReal*>(FcnList.at(ix+j))->analyticalIntegral(coderem, rangeName);
206  }
207 
209  if (intAlongX) segval += (fcnval[1] - fcnval[0])*(pow(txhigh, 2)-pow(txlow, 2))/2.;
210  else segval += (fcnval[1] - fcnval[0])*txhigh;
211  res += segval;
212 
214  << "MELALinearInterpFunc(" << GetName() << ")::interpolateFcn evaluated bin " << ix
215  << " with txlow = " << txlow << ", txhigh = " << txhigh << ", fcnval[0] = " << fcnval[0] << ", fcnval[1] = " << fcnval[1]
216  << endl;
217  }
218  }
219 
220  return res;
221 }
223  Double_t value = interpolateFcn(0);
224  if (useFloor && value<floorEval){
225  if (verbosity>=MELALinearInterpFunc::kError) coutE(Eval) << "MELALinearInterpFunc ERROR::MELALinearInterpFunc(" << GetName() << ") evaluation returned " << value << " at x = " << theXVar << endl;
226  value = floorEval;
227  }
228  if (verbosity==MELALinearInterpFunc::kVerbose){ cout << "MELALinearInterpFunc(" << GetName() << ")::evaluate = " << value << " at x = " << theXVar << endl; }
229  return value;
230 }
231 Int_t MELALinearInterpFunc::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName)const{
232  if (_forceNumInt) return 0;
233 
234  const Int_t xprime = 101;
235  Int_t code=1;
236 
237  RooArgSet Xdeps, Fcndeps;
238  RooRealVar* rrv_x = dynamic_cast<RooRealVar*>(theXVar.absArg());
239  if (rrv_x==0) theXVar.absArg()->leafNodeServerList(&Xdeps, 0, true);
240 
241  for (int ix=0; ix<FcnList.getSize(); ix++){
242  RooArgSet tmpdeps;
243  RooAbsReal const* fcn = dynamic_cast<const RooAbsReal*>(FcnList.at(ix));
244  fcn->leafNodeServerList(&tmpdeps, 0, true);
245  Fcndeps.add(tmpdeps);
246  if (ix==0){
247  RooArgSet mirrorvars;
248  TIterator* iter = allVars.createIterator();
249  RooAbsArg* var;
250  while ((var = (RooAbsArg*)iter->Next())){
251  RooRealVar* rrv_tmp = dynamic_cast<RooRealVar*>(var);
252  if (rrv_tmp==0) continue;
253  bool isFound=false;
254  if (rrv_x!=0){
255  isFound = (rrv_tmp==rrv_x);
256  }
257  else{
258  TIterator* iter_deps = Xdeps.createIterator();
259  RooAbsArg* var_deps;
260  while ((var_deps = (RooAbsArg*)iter_deps->Next())){
261  RooRealVar* depvar = dynamic_cast<RooRealVar*>(var_deps);
262  if (depvar==0) continue;
263  if (depvar==rrv_tmp){ isFound=true; break; }
264  }
265  delete iter_deps;
266  }
267  if (!isFound) mirrorvars.add(*var);
268  }
269  delete iter;
270 
271  Int_t codetmp = fcn->getAnalyticalIntegral(mirrorvars, analVars, rangeName);
272  if (codetmp!=0) code *= codetmp;
273  }
274  }
275 
276  if (rrv_x!=0){
277  if (Fcndeps.find(*rrv_x)==0){
278  if (matchArgs(allVars, analVars, theXVar)) code *= xprime;
279  }
280  }
281 
282  if (code==1) code=0;
284  cout << "MELALinearInterpFunc(" << GetName() << ")::getAnalyticalIntegral code = " << code << endl;
285  allVars.Print("v");
286  analVars.Print("v");
287  }
288  return code;
289 }
290 Double_t MELALinearInterpFunc::analyticalIntegral(Int_t code, const char* rangeName)const{
291  Double_t value = interpolateFcn(code, rangeName);
292  if (useFloor && value<floorInt){
293  if (verbosity>=MELALinearInterpFunc::kError) coutE(Integration) << "MELALinearInterpFunc ERROR::MELALinearInterpFunc(" << GetName() << ") integration returned " << value << " for code = " << code << endl;
294  value = floorInt;
295  }
296  if (verbosity==MELALinearInterpFunc::kVerbose){ cout << "MELALinearInterpFunc(" << GetName() << ")::analyticalIntegral = " << value << " for code = " << code << endl; }
297  return value;
298 }
299 
301  const T* range[2];
302  range[0] = &rangeXmin;
303  range[1] = &rangeXmax;
304  return (*(range[0])>*(range[1]) || (val>=*(range[0]) && val<=*(range[1])));
305 }
306 void MELALinearInterpFunc::setRangeValidity(const T valmin, const T valmax){
307  T* range[2];
308  range[0] = &rangeXmin;
309  range[1] = &rangeXmax;
310  *(range[0])=valmin;
311  *(range[1])=valmax;
312 }
314  if (testRangeValidity(val)) return;
315  const T* range[2];
316  range[0] = &rangeXmin;
317  range[1] = &rangeXmax;
318  if (val<*(range[0])) val = *(range[0]);
319  if (val>*(range[1])) val = *(range[1]);
320 }
MELALinearInterpFunc::kVerbose
@ kVerbose
Definition: MELALinearInterpFunc.h:20
MELALinearInterpFunc::FcnList
RooListProxy FcnList
Definition: MELALinearInterpFunc.h:64
MELALinearInterpFunc::verbosity
VerbosityLevel verbosity
Definition: MELALinearInterpFunc.h:54
MELALinearInterpFunc::setVerbosity
void setVerbosity(VerbosityLevel flag)
Definition: MELALinearInterpFunc.cc:104
value
pymela::gHIGGS_KAPPA value("gHIGGS_KAPPA_TILDE", pymela::gHIGGS_KAPPA_TILDE) .value("SIZE_HQQ"
MELALinearInterpFunc::getAnalyticalIntegral
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Definition: MELALinearInterpFunc.cc:231
MELALinearInterpFunc::evaluate
Double_t evaluate() const
Definition: MELALinearInterpFunc.cc:222
TNumericUtil::KahanAccumulator
Definition: MELAAccumulators.h:27
MELALinearInterpFunc::leafDepsList
RooListProxy leafDepsList
Definition: MELALinearInterpFunc.h:65
MELALinearInterpFunc::setRangeValidity
void setRangeValidity(const T valmin, const T valmax)
Definition: MELALinearInterpFunc.cc:306
MELALinearInterpFunc::setIntFloor
void setIntFloor(T val)
Definition: MELALinearInterpFunc.cc:106
MELALinearInterpFunc::getWhichBin
Int_t getWhichBin(const T &val) const
Definition: MELALinearInterpFunc.cc:109
MELALinearInterpFunc::cropValueForRange
void cropValueForRange(T &val) const
Definition: MELALinearInterpFunc.cc:313
MELALinearInterpFunc::rangeXmax
T rangeXmax
Definition: MELALinearInterpFunc.h:60
ClassImp
ClassImp(MELALinearInterpFunc) MELALinearInterpFunc
Definition: MELALinearInterpFunc.cc:12
MELALinearInterpFunc::MELALinearInterpFunc
MELALinearInterpFunc()
modparameters::kappa
complex(8), public kappa
Definition: mod_Parameters.F90:882
MELALinearInterpFunc::getTVar
T getTVar(const T &val) const
Definition: MELALinearInterpFunc.cc:140
TNumericUtil
Definition: MELAAccumulators.h:6
MELALinearInterpFunc::theXVar
RooRealProxy theXVar
Definition: MELALinearInterpFunc.h:63
MELALinearInterpFunc::doFloor
void doFloor(Bool_t flag)
Definition: MELALinearInterpFunc.cc:107
MELALinearInterpFunc::npoints
unsigned int npoints() const
Definition: MELALinearInterpFunc.h:67
MELALinearInterpFunc::floorEval
T floorEval
Definition: MELALinearInterpFunc.h:56
MELALinearInterpFunc::rangeXmin
T rangeXmin
Definition: MELALinearInterpFunc.h:59
MELALinearInterpFunc::VerbosityLevel
VerbosityLevel
Definition: MELALinearInterpFunc.h:17
MELALinearInterpFunc.h
MELALinearInterpFunc::T
Float_t T
Definition: MELALinearInterpFunc.h:15
MELALinearInterpFunc::setEvalFloor
void setEvalFloor(T val)
Definition: MELALinearInterpFunc.cc:105
dd_global::cout
integer cout
Definition: DD_global.F90:21
MELALinearInterpFunc::floorInt
T floorInt
Definition: MELALinearInterpFunc.h:57
MELALinearInterpFunc::XList
std::vector< T > XList
Definition: MELALinearInterpFunc.h:62
MELALinearInterpFunc::analyticalIntegral
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Definition: MELALinearInterpFunc.cc:290
MELALinearInterpFunc::useFloor
Bool_t useFloor
Definition: MELALinearInterpFunc.h:55
MELALinearInterpFunc::kError
@ kError
Definition: MELALinearInterpFunc.h:19
MELALinearInterpFunc::getKappa
T getKappa(const Int_t &bin) const
Definition: MELALinearInterpFunc.cc:130
MELALinearInterpFunc::interpolateFcn
T interpolateFcn(Int_t code, const char *rangeName=0) const
Definition: MELALinearInterpFunc.cc:147
MELALinearInterpFunc::testRangeValidity
Bool_t testRangeValidity(const T &val) const
Definition: MELALinearInterpFunc.cc:300
MELALinearInterpFunc
Definition: MELALinearInterpFunc.h:13