Skip to content

Commit

Permalink
Merge pull request cms-sw#117 from nucleosynthesis/nckw-add-cuts-to-R…
Browse files Browse the repository at this point in the history
…ooSpline

Add cuts to RooSplineND
  • Loading branch information
nucleosynthesis committed Apr 9, 2014
2 parents aca0fe0 + 21efd83 commit 46b53a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
5 changes: 4 additions & 1 deletion interface/RooSplineND.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
#include "TDecompSparse.h"
#include "TVectorD.h"
#include "TTree.h"
#include "TEventList.h"
#include "TAxis.h"
#include "TGraph.h"
#include "TDirectory.h"
#include "RooRealVar.h"

#include <map>
#include <vector>
#include <string>

/**********************************************************************
Original Author -- Nicholas Wardle
Expand Down Expand Up @@ -58,7 +61,7 @@ class RooSplineND : public RooAbsReal {

public:
RooSplineND() : ndim_(0),M_(0),eps_(3.) {}
RooSplineND(const char *name, const char *title, RooArgList &vars, TTree *tree, const char* fName="f", double eps=3. ) ;
RooSplineND(const char *name, const char *title, RooArgList &vars, TTree *tree, const char* fName="f", double eps=3., std::string cutstring="" ) ;
RooSplineND(const RooSplineND& other, const char *name) ;
RooSplineND(const char *name, const char *title, const RooListProxy &vars, int ndim, int M, double eps, std::vector<double> &w, std::map<int,std::vector<double> > &map, std::map<int,std::pair<double,double> > & ,double,double) ;
~RooSplineND() ;
Expand Down
57 changes: 37 additions & 20 deletions src/RooSplineND.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "../interface/RooSplineND.h"

RooSplineND::RooSplineND(const char *name, const char *title, RooArgList &vars, TTree *tree, const char *fName, double eps) :
RooSplineND::RooSplineND(const char *name, const char *title, RooArgList &vars, TTree *tree, const char *fName, double eps, std::string cutstring) :
RooAbsReal(name,title),
vars_("vars","Variables", this)
{
ndim_ = vars.getSize();
M_ = tree->GetEntries();
int nentries = tree->GetEntries();

// Get Selection list
tree->Draw(">>cutlist",cutstring.c_str());
TEventList *keep_points = (TEventList*)gDirectory->Get("cutlist");
M_ = keep_points->GetN();

std::cout << "RooSplineND -- Num Dimensions == " << ndim_ <<std::endl;
std::cout << "RooSplineND -- Num Samples == " << M_ << std::endl;
double *b_map = new double(ndim_);

float *b_map = new float(ndim_);

RooAbsReal *rIt;
TIterator *iter = vars.createIterator(); int it_c=0;
Expand All @@ -21,25 +27,33 @@ RooSplineND::RooSplineND(const char *name, const char *title, RooArgList &vars,
tree->SetBranchAddress(rIt->GetName(),&b_map[it_c]);
it_c++;
}
// Assume the function val (yi) branch is f
double F;

float F;
tree->SetBranchAddress(fName,&F);
std::vector<double> F_vec;

// Run through tree and store points
for (int i=0;i<M_;i++){
// Run through tree and store points if selected
int nselect=0;
for (int i=0;i<nentries;i++){
if ( !(keep_points->Contains(i)) ) continue;
tree->GetEntry(i);
//std::cout <<"Adding point " << i << ", ";
for (int k=0;k<ndim_;k++){
double cval = b_map[k];
if (cval < r_map[k].first) r_map[k].first=cval;
if (cval > r_map[k].second) r_map[k].second=cval;
v_map[k][i] = cval;
float cval = b_map[k];
//if (cval < r_map[k].first) r_map[k].first=cval;
//if (cval > r_map[k].second) r_map[k].second=cval;
v_map[k][nselect] = (double)cval;
//std::cout << "x" << k << "=" << cval << ", ";
}
//std::cout << "F=" << F << std::endl;
F_vec.push_back(F);
nselect++;
}

//std::cout << "... N(selected) = " << nselect << std::endl;
keep_points->Reset();
tree->SetEventList(0);
// Try to re-scale axis to even out dimensions.
axis_pts_ = TMath::Power(M_,1./ndim_);
// axis_pts_ = TMath::Power(M_,1./ndim_);
eps_= eps;
calculateWeights(F_vec);
delete b_map;
Expand Down Expand Up @@ -130,6 +144,7 @@ TGraph * RooSplineND::getGraph(const char *xvar, double step){
//_____________________________________________________________________________
void RooSplineND::calculateWeights(std::vector<double> &f){

std::cout << "RooSplineND -- Solving for Weights" << std::endl;
if (M_==0) {
w_mean = 0;
w_rms = 1;
Expand All @@ -149,11 +164,10 @@ void RooSplineND::calculateWeights(std::vector<double> &f){
}

TVectorD weights(M_);
for (int i=0;i<M_;i++){
weights[i]=(double)f[i];
}
for (int i=0;i<M_;i++) weights[i]=f[i];

TDecompQRH decomp(fMatrix);
//TDecompQRH decomp(fMatrix);
TDecompChol decomp(fMatrix);
std::cout << "RooSplineND -- Solving for Weights" << std::endl;

decomp.Solve(weights); // Solution now in weights
Expand All @@ -175,7 +189,8 @@ double RooSplineND::getDistSquare(int i, int j){
for (int k=0;k<ndim_;k++){
double v_i = v_map[k][i];
double v_j = v_map[k][j];
double dk = axis_pts_*(v_i-v_j)/(r_map[k].second-r_map[k].first);
//double dk = axis_pts_*(v_i-v_j)/(r_map[k].second-r_map[k].first);
double dk = (v_i-v_j);
//std::cout << "dimension - " << k << ", Values at pts " << i <<"," << j << " are "<<v_i<< ","<<v_j<< " Distance " <<dk<<std::endl;
D += dk*dk;
}
Expand All @@ -189,7 +204,8 @@ double RooSplineND::getDistFromSquare(int i) const{
double v_i = v_map[k][i];
RooAbsReal *v = (RooAbsReal*)vars_.at(k);
double v_j = v->getVal();
double dk = axis_pts_*(v_i-v_j)/(r_map[k].second-r_map[k].first);
//double dk = axis_pts_*(v_i-v_j)/(r_map[k].second-r_map[k].first);
double dk = (v_i-v_j);
D += dk*dk;
}
return D; // only ever use square of distance!
Expand All @@ -198,7 +214,8 @@ double RooSplineND::getDistFromSquare(int i) const{
//_____________________________________________________________________________
double RooSplineND::radialFunc(double d2, double eps) const{
double expo = (d2/(eps*eps));
double retval = 1./(1+(TMath::Power(expo,1.5)));
//double retval = 1./(1+(TMath::Power(expo,1.5)));
double retval = TMath::Exp(-1*expo);
return retval;
}
//_____________________________________________________________________________
Expand Down

0 comments on commit 46b53a2

Please sign in to comment.