Lit::LikeModule Class Reference

#include <LikeModule.h>

List of all members.

Public Types

typedef std::map< int,
std::vector< double > > 
VarMap
typedef std::map< short, double > ProbMap

Public Member Functions

 LikeModule ()
 ~LikeModule ()
bool Read (const std::string &filepath, int nevent=0, const std::string &treename="events")
bool Read (TDirectory *dir, int nevent=0, const std::string &treename="events")
bool Write (const std::string &filepath, const std::string &treename="events")
void Write (TDirectory *dir, const std::string &treename="events")
void Add (const Event &event)
bool Fill (unsigned short optimize_depth=7, const std::string &option="")
bool Find (Event event, unsigned int nfind=100) const
bool Find (unsigned int nfind, const std::string &option) const
const EventVecGetEventVec () const
const ResultGetResult () const
const VarMapGetVarMap () const
const std::map< int, double > & GetMetric () const
void Print () const
void Print (std::ostream &os) const

Private Member Functions

NodeBalance (unsigned int bdepth)
void ComputeMetric (unsigned int ifrac=10)
const Event Scale (const Event &event) const

Private Attributes

unsigned int fDimn
NodefTree
std::map< int, double > fVarScale
Result fResult
std::map< short, unsigned int > fCount
EventVec fEvent
VarMap fVar

Detailed Description

Definition at line 18 of file LikeModule.h.


Member Typedef Documentation

typedef std::map<short, double> Lit::LikeModule::ProbMap

Definition at line 23 of file LikeModule.h.

typedef std::map<int, std::vector<double> > Lit::LikeModule::VarMap

Definition at line 22 of file LikeModule.h.


Constructor & Destructor Documentation

Lit::LikeModule::LikeModule (  ) 

Definition at line 22 of file LikeModule.cxx.

00023    :fDimn(0),
00024     fTree(0)
00025 {
00026 }

Lit::LikeModule::~LikeModule (  ) 

Definition at line 29 of file LikeModule.cxx.

References fTree.

00030 {
00031    if(fTree)
00032    {
00033       delete fTree;
00034    }
00035 }


Member Function Documentation

void Lit::LikeModule::Add ( const Event event  ) 

Definition at line 147 of file LikeModule.cxx.

References fCount, fDimn, fEvent, fTree, fVar, Lit::Event::GetNVar(), Lit::Event::GetType(), Lit::Event::GetVar(), and Munits::second.

Referenced by Fill().

00148 {   
00149    if(fTree)
00150    {
00151       cerr << "LikeModule::Add() - can not add event: tree is already built" << endl;
00152       return;      
00153    }
00154 
00155    if(fDimn < 1)
00156    {
00157       fDimn = event.GetNVar();
00158    }
00159    else if(fDimn != event.GetNVar())
00160    {
00161       cerr << "LikeModule::Add() - number of dimensions does not match previous events" << endl;
00162       return;
00163    }
00164 
00165    fEvent.push_back(event);
00166 
00167    for(unsigned int ivar = 0; ivar < fDimn; ++ivar)
00168    {
00169       fVar[ivar].push_back(event.GetVar(ivar));
00170    }
00171 
00172    std::map<short, unsigned int>::iterator cit = fCount.find(event.GetType());
00173    if(cit == fCount.end())
00174    {
00175       fCount[event.GetType()] = 1;
00176    }
00177    else
00178    {
00179       ++(cit -> second);
00180    }
00181 }

Lit::Node * Lit::LikeModule::Balance ( unsigned int  bdepth  )  [private]

Definition at line 426 of file LikeModule.cxx.

References fVar, it, Munits::second, and size.

Referenced by Fill().

00427 {
00428    // Balance() balances binary tree for first bdepth levels
00429    // for each depth we split sorted depth % dimension variables
00430    // into 2^bdepth parts
00431 
00432    if(fVar.empty() || fDimn != fVar.size())
00433    {
00434       cout << "LikeModule::Balance - can not build a tree" << endl;
00435       return 0;
00436    }
00437 
00438    const unsigned int size = (fVar.begin() -> second).size();
00439    if(size < 1)
00440    {
00441       cout << "LikeModule::Balance - can not build a tree without events" << endl;
00442       return 0;
00443    }
00444 
00445    for(VarMap::const_iterator it = fVar.begin(); it != fVar.end(); ++it)
00446    {
00447       if((it -> second).size() != size)
00448       {
00449          cout << "LikeModule::Balance - # of variables doesn't match between dimensions" << endl;
00450          return 0;
00451       }
00452    }
00453 
00454    if(double(fDimn*size) < pow(2.0, double(bdepth)))
00455    {
00456       cout << "LikeModule::Balance - balance depth exceeds number of events" << endl;
00457       return 0;      
00458    }   
00459 
00460    cout << "Balancing tree for " << fDimn << " variables with " << size << " values" << endl;
00461 
00462    vector<Node *> pvec, cvec;
00463 
00464    VarMap::const_iterator it = fVar.find(0);
00465    if(it == fVar.end() || (it -> second).size() < 2)
00466    {
00467       cout << "Missing 0 variable" << endl;
00468       return 0;
00469    }
00470 
00471    const Event pevent(VarVec(fDimn, (it -> second)[size/2]), -1.0, -1);
00472    
00473    Node *tree = new Node(0, pevent, 0);
00474    
00475    pvec.push_back(tree);
00476 
00477    for(unsigned int depth = 1; depth < bdepth; ++depth)
00478    {            
00479       const unsigned int mod = depth % fDimn;
00480    
00481       VarMap::const_iterator vit = fVar.find(mod);
00482       if(vit == fVar.end())
00483       {
00484          cerr << "Missing " << mod << " variable" << endl;
00485          return 0;
00486       }
00487       const vector<double> &dvec = vit -> second;
00488 
00489       if(dvec.size() < 2)
00490       {
00491          cerr << "Missing " << mod << " variable" << endl;
00492          return 0;       
00493       }
00494 
00495       //cout << "Mod = " << mod << endl;
00496 
00497       unsigned int ichild = 1;
00498       for(vector<Node *>::iterator pit = pvec.begin(); pit != pvec.end(); ++pit)
00499       {
00500          Node *parent = *pit;
00501 
00502          const VarType lmedian = dvec[size*ichild/(2*pvec.size() + 1)];
00503          //cout << "ChildL " << setw(2) << ichild << " lmedian " << lmedian << endl;
00504          ++ichild;
00505 
00506          const VarType rmedian = dvec[size*ichild/(2*pvec.size() + 1)];
00507          //cout << "ChildR " << setw(2) << ichild << " rmedian " << rmedian << endl;
00508          ++ichild;
00509       
00510          const Event levent(VarVec(fDimn, lmedian), -1.0, -1);
00511          const Event revent(VarVec(fDimn, rmedian), -1.0, -1);
00512 
00513          Node *lchild = new Node(parent, levent, mod);
00514          Node *rchild = new Node(parent, revent, mod);
00515 
00516          parent -> SetNodeL(lchild);
00517          parent -> SetNodeR(rchild);
00518 
00519          cvec.push_back(lchild);
00520          cvec.push_back(rchild);
00521       }
00522 
00523       pvec = cvec;
00524       cvec.clear();
00525    }
00526 
00527    return tree;
00528 }

void Lit::LikeModule::ComputeMetric ( unsigned int  ifrac = 10  )  [private]

Definition at line 531 of file LikeModule.cxx.

References fEvent, fVar, fVarScale, Scale(), and Munits::second.

Referenced by Fill().

00532 {
00533    // compute scale factor for each variable (dimension) so that 
00534    // distance is computed uniformely along each dimension
00535    // compute width of interval that includes (100 - 2*ifrac)% of events
00536    // below, assume that in fVar each vector of values is sorted
00537 
00538    if(ifrac > 49)
00539    {
00540       cerr << "LikeModule::ComputeMetric() - fraction can not exceed 50%" << endl;
00541       return;
00542    }
00543    if(fEvent.empty())
00544    {
00545       cerr << "LikeModule::ComputeMetric() - event vector is empty" << endl;
00546       return;
00547    }
00548    if(!fVarScale.empty())
00549    {
00550       cerr << "LikeModule::ComputeMetric() - metric is already computed" << endl;
00551       return;
00552    }
00553 
00554    cout << "Computing scale for 1d axes: ifrac = " << ifrac << endl;
00555 
00556    fVarScale.clear();
00557    
00558    for(VarMap::const_iterator vit = fVar.begin(); vit != fVar.end(); ++vit)
00559    {
00560       const vector<double> &dvec = vit -> second;
00561       
00562       vector<double>::const_iterator beg_it = dvec.end();
00563       vector<double>::const_iterator end_it = dvec.end();
00564       for(vector<double>::const_iterator dit = dvec.begin(); dit != dvec.end(); ++dit)
00565       {
00566          const int dist = distance(dvec.begin(), dit);
00567 
00568          if((100*dist)/dvec.size() == ifrac && beg_it == dvec.end())
00569          {
00570             beg_it = dit;
00571          }
00572 
00573          if((100*dist)/dvec.size() == 100 - ifrac && end_it == dvec.end())
00574          {
00575             end_it = dit;
00576          }
00577       }
00578 
00579       if(beg_it == dvec.end() || end_it == dvec.end())
00580       {
00581          cerr << "LikeModule::ComputeMetric() - failed to determine scale " << vit -> first << endl;
00582          continue;
00583       }
00584 
00585       const double lpos = *beg_it;
00586       const double rpos = *end_it;
00587 
00588       if(!(lpos < rpos))
00589       {
00590          cerr << "LikeModule::ComputeMetric() - min value is greater than max value" << endl;
00591          continue;       
00592       }
00593       
00594       cout << "Variable " << vit -> first 
00595            << " included " << distance(beg_it, end_it) + 1
00596            << " events: width = " << setfill(' ') << setw(5) << setprecision(3) << rpos - lpos
00597            << ", (min, max) = (" << setfill(' ') << setw(5) << setprecision(3) << lpos 
00598            << ", " << setfill(' ') << setw(5) << setprecision(3) << rpos << ")" << endl;
00599 
00600       fVarScale[vit -> first] = rpos - lpos;
00601    }
00602 
00603    fVar.clear();
00604 
00605    for(unsigned int ievent = 0; ievent < fEvent.size(); ++ievent)
00606    {      
00607       fEvent[ievent] = Scale(fEvent[ievent]);
00608 
00609       for(unsigned int ivar = 0; ivar < fDimn; ++ivar)
00610       {
00611          fVar[ivar].push_back(fEvent[ievent].GetVar(ivar));
00612       }  
00613    }
00614 }

bool Lit::LikeModule::Fill ( unsigned short  optimize_depth = 7,
const std::string &  option = "" 
)

Definition at line 184 of file LikeModule.cxx.

References Add(), Balance(), ComputeMetric(), fCount, fDimn, fEvent, fTree, fVar, it, min, and Munits::second.

00185 {
00186    if(fTree)
00187    {
00188       cerr << "LikeModule::Fill - tree already have been created" << endl;
00189       return false;
00190    }
00191 
00192    // If trim option is set then find class with lowest number of events
00193    // and set that as maximum number of events for all other classes.
00194    unsigned int min = 0;
00195    if(option.find("trim") != string::npos)
00196    {  
00197       for(map<short, unsigned int>::const_iterator it = fCount.begin(); it != fCount.end(); ++it)
00198       {
00199          if(min == 0 || min > it -> second)
00200          {
00201             min = it -> second;
00202          }
00203       }
00204       
00205       cout << "LikeModule::Fill - trimming all classes to " << min << " events" << endl;
00206       for(map<short, unsigned int>::const_iterator it = fCount.begin(); it != fCount.end(); ++it)
00207       {
00208          cout << "class " << it -> first << " has " 
00209               << setfill(' ') << setw(8) << it -> second << " events" << endl;
00210       }
00211 
00212       fCount.clear();
00213       fVar.clear();
00214 
00215       EventVec evec;
00216 
00217       for(EventVec::const_iterator event = fEvent.begin(); event != fEvent.end(); ++event)
00218       {
00219          std::map<short, unsigned int>::iterator cit = fCount.find(event -> GetType());
00220          if(cit == fCount.end())
00221          {
00222             fCount[event -> GetType()] = 1;
00223          }
00224          else if(cit -> second < min)
00225          {
00226             ++(cit -> second);
00227          }
00228          else
00229          {
00230             continue;
00231          }
00232          
00233          for(unsigned int d = 0; d < fDimn; ++d)
00234          {
00235             fVar[d].push_back(event -> GetVar(d));
00236          }       
00237 
00238          evec.push_back(*event);
00239       }
00240 
00241       cout << "LikeModule::Fill - erased " << fEvent.size() - evec.size() << " events" << endl;
00242 
00243       fEvent = evec;
00244    }
00245 
00246    //clear event count
00247    fCount.clear();
00248 
00249    // sort each variable for all events - needs this before Balance()
00250    for(VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it)
00251    {
00252      std::sort((it -> second).begin(), (it -> second).end());
00253    }
00254 
00255    if(option.find("metric") != string::npos)
00256    {
00257       ComputeMetric();
00258 
00259       // sort again each variable for all events - needs this before Balance()
00260       // rescaling has changed variable values
00261       for(VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it)
00262       {
00263         std::sort((it -> second).begin(), (it -> second).end());
00264       }
00265    }
00266 
00267    // If optimize_depth > 0 then fill first optimize_depth levels
00268    // with empty nodes that split separating variable in half for
00269    // all child nodes. If optimize_depth = 0 then split variable 0
00270    // at the median (in half) and return it as root node
00271    fTree = Balance(optimize_depth);
00272 
00273    if(!fTree)
00274    {
00275       cout << "LikeModule::Fill - failed to create tree" << endl;
00276       return false;      
00277    }      
00278    
00279    for(EventVec::const_iterator event = fEvent.begin(); event != fEvent.end(); ++event)
00280    {
00281       fTree -> Add(*event, 0);
00282 
00283       std::map<short, unsigned int>::iterator cit = fCount.find(event -> GetType());
00284       if(cit == fCount.end())
00285       {
00286          fCount[event -> GetType()] = 1;
00287       }
00288       else
00289       {
00290          ++(cit -> second);
00291       }
00292    }
00293    
00294    for(map<short, unsigned int>::const_iterator it = fCount.begin(); it != fCount.end(); ++it)
00295    {
00296       cout << "class " << it -> first << " has " 
00297            << setfill(' ') << setw(8) << it -> second << " events" << endl;
00298    }
00299 
00300    if(option.find("erase") != string::npos)
00301    {
00302       cout << "Erasing event vector with size " << fEvent.size() << endl;
00303       fEvent.clear();
00304       fVar.clear();
00305    }
00306    
00307    return true;
00308 }

bool Lit::LikeModule::Find ( unsigned int  nfind,
const std::string &  option 
) const
bool Lit::LikeModule::Find ( Event  event,
unsigned int  nfind = 100 
) const

Definition at line 311 of file LikeModule.cxx.

References Lit::Result::Clear(), fDimn, Lit::Result::fEvent, Lit::Result::fList, Lit::Result::fProbNN, fResult, fTree, fVarScale, Lit::Result::GetList(), Lit::Event::GetNVar(), Scale(), and Munits::second.

00312 {  
00313    // if tree has been filled then search for nfind closest events 
00314    // if metic (fVarScale map) is computed then rescale event variables
00315    // using previsouly computed width of variable distribution
00316 
00317    if(!fTree)
00318    {
00319       cerr << "LikeModule::Find() - tree has not been filled" << endl;
00320       return false;
00321    }
00322    if(fDimn != event.GetNVar())
00323    {
00324       cerr << "LikeModule::Find() - number of dimension does not match training events" << endl;
00325       return false;
00326    }
00327    if(nfind < 1)
00328    {
00329       cerr << "LikeModule::Find() - requested 0 nearest neighbors" << endl;
00330       return false;
00331    }
00332 
00333    // if variable widths are computed then rescale variable in this event
00334    // to same widths as events in stored kd-tree
00335    if(!fVarScale.empty())
00336    {
00337       event = Scale(event);
00338    }
00339 
00340    fResult.Clear();
00341 
00342    fResult.fEvent = event;
00343 
00344    // recursive kd-tree search for nfind-nearest neighbors
00345    Lit::Find(fResult.fList, fTree, event, nfind);
00346 
00347    for(List::const_iterator lit = fResult.GetList().begin(); lit != fResult.GetList().end(); ++lit)
00348    {
00349       const Node *node = lit -> first;
00350 
00351       map<short, double>::iterator pit = fResult.fProbNN.find(node -> GetType());
00352       if(pit == fResult.fProbNN.end())
00353       {
00354          fResult.fProbNN[node -> GetType()] = node -> GetWeight()/double(nfind);
00355       }
00356       else
00357       {
00358          pit -> second += node -> GetWeight()/double(nfind);
00359       }
00360    }
00361 
00362    return true;
00363 }

const EventVec & Lit::LikeModule::GetEventVec (  )  const [inline]

Definition at line 82 of file LikeModule.h.

References fEvent.

00083    {
00084       return fEvent;
00085    }

const std::map< int, double > & Lit::LikeModule::GetMetric (  )  const [inline]

Definition at line 90 of file LikeModule.h.

References fVarScale.

00091    {
00092       return fVarScale;
00093    }

const Result & Lit::LikeModule::GetResult (  )  const [inline]

Definition at line 78 of file LikeModule.h.

References fResult.

00079    {
00080       return fResult;
00081    }

const LikeModule::VarMap & Lit::LikeModule::GetVarMap (  )  const [inline]

Definition at line 86 of file LikeModule.h.

References fVar.

00087    {
00088       return fVar;
00089    }

void Lit::LikeModule::Print ( std::ostream &  os  )  const

Definition at line 664 of file LikeModule.cxx.

00665 {
00666    os << "---------------------------------------------------------------------------------"<< endl;
00667    os << "Dumping stringstream content of LikeModule" << endl;
00668    os << "---------------------------------------------------------------------------------"<< endl;
00669 }

void Lit::LikeModule::Print ( void   )  const

Definition at line 658 of file LikeModule.cxx.

Referenced by Anp::FillkNN::Find().

00659 {
00660    Print(std::cout);
00661 }

bool Lit::LikeModule::Read ( TDirectory *  dir,
int  nevent = 0,
const std::string &  treename = "events" 
)
bool Lit::LikeModule::Read ( const std::string &  filepath,
int  nevent = 0,
const std::string &  treename = "events" 
)
const Lit::Event Lit::LikeModule::Scale ( const Event event  )  const [private]

Definition at line 617 of file LikeModule.cxx.

References fVarScale, Lit::Event::GetNVar(), Lit::Event::GetWeight(), and Munits::second.

Referenced by ComputeMetric(), and Find().

00618 {
00619    // scale each event variable so that rms of variables is approximately 1.0
00620    // this allows comparisons of variables with distinct scales and units
00621    
00622    if(fVarScale.empty())
00623    {
00624       return event;
00625    }
00626 
00627    if(event.GetNVar() != fVarScale.size())
00628    {
00629       cerr << "LikeModule::Scale() - mismatched metric and event size" << endl;
00630       return event;
00631    }
00632 
00633    VarVec vvec(event.GetNVar(), 0.0);
00634 
00635    for(unsigned int ivar = 0; ivar < event.GetNVar(); ++ivar)
00636    {
00637       map<int, double>::const_iterator fit = fVarScale.find(ivar);
00638       if(fit == fVarScale.end())
00639       {
00640          cerr << "LikeModule::Scale() - failed to find scale for " << ivar << endl;
00641          continue;
00642       }
00643       
00644       if(fit -> second > 0.0)
00645       {
00646          vvec[ivar] = event.GetVar(ivar)/fit -> second;
00647       }
00648       else
00649       {
00650          cerr << "Variable " << ivar << " has zero width" << endl;
00651       }
00652    }
00653 
00654    return Event(vvec, event.GetWeight(), event.GetType(), event.GetTgtVec());
00655 }

void Lit::LikeModule::Write ( TDirectory *  dir,
const std::string &  treename = "events" 
)
bool Lit::LikeModule::Write ( const std::string &  filepath,
const std::string &  treename = "events" 
)

Member Data Documentation

std::map<short, unsigned int> Lit::LikeModule::fCount [private]

Definition at line 72 of file LikeModule.h.

Referenced by Add(), and Fill().

unsigned int Lit::LikeModule::fDimn [private]

Definition at line 64 of file LikeModule.h.

Referenced by Add(), Fill(), and Find().

Definition at line 74 of file LikeModule.h.

Referenced by Add(), ComputeMetric(), Fill(), and GetEventVec().

Result Lit::LikeModule::fResult [mutable, private]

Definition at line 70 of file LikeModule.h.

Referenced by Find(), and GetResult().

Definition at line 66 of file LikeModule.h.

Referenced by Add(), Fill(), Find(), and ~LikeModule().

Definition at line 75 of file LikeModule.h.

Referenced by Add(), Balance(), ComputeMetric(), Fill(), and GetVarMap().

std::map<int, double> Lit::LikeModule::fVarScale [private]

Definition at line 68 of file LikeModule.h.

Referenced by ComputeMetric(), Find(), GetMetric(), and Scale().


The documentation for this class was generated from the following files:

Generated on 16 Apr 2018 for loon by  doxygen 1.6.1