star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ApplyClassifier.C
Go to the documentation of this file.
1 #include "TROOT.h"
2 #include "Riostream.h"
3 #include "TFile.h"
4 #include "TMath.h"
5 #include "TTree.h"
6 #include "TDatime.h"
7 #include "TBranch.h"
8 #include "TMVA/Tools.h"
9 #include "TMVA/Reader.h"
10 #include "TMVA/Factory.h"
11 #include "weights/TMVAClassification_Fisher.class.C"
12 
13 void SetTreeVariables(TTree* oldtree){
14 
15  struct primVtxPoint_t{
16  Int_t event,index,rank,mult,refMult,maxmult;
17  Float_t primX,primY,primZ,zVpd;
18  Float_t McX, McY, McZ;
19  Float_t chi2;
20  Int_t beam,postx,prompt,cross,tof,notof,EEMC,noEEMC,BEMC,noBEMC;
21  };
22  primVtxPoint_t primVtx;
23 
24  TTree *primaryvtx = new TTree("primaryvtx","The Primary Vertices");
25  primaryvtx->SetAutoSave(1000000);
26  primaryvtx->Branch("event",&primVtx.event,"event/I");
27  primaryvtx->Branch("index",&primVtx.index,"index/I");
28  primaryvtx->Branch("rank",&primVtx.rank,"rank/I");
29  primaryvtx->Branch("mult",&primVtx.mult,"mult/I");
30  primaryvtx->Branch("refMult",&primVtx.refMult,"refMult/I");
31  primaryvtx->Branch("maxmult",&primVtx.maxmult,"maxmult/I");
32  primaryvtx->Branch("primX",&primVtx.primX,"primX/F");
33  primaryvtx->Branch("primY",&primVtx.primY,"primY/F");
34  primaryvtx->Branch("primZ",&primVtx.primZ,"primZ/F");
35  primaryvtx->Branch("zVpd",&primVtx.zVpd,"zVpd/F");
36  primaryvtx->Branch("McX",&primVtx.McX,"McX/F");
37  primaryvtx->Branch("McY",&primVtx.McY,"McY/F");
38  primaryvtx->Branch("McZ",&primVtx.McZ,"McZ/F");
39  primaryvtx->Branch("chi2",&primVtx.chi2,"chi2/F");
40  primaryvtx->Branch("beam",&primVtx.beam,"beam/I");
41  primaryvtx->Branch("postx",&primVtx.postx,"postx/I");
42  primaryvtx->Branch("prompt",&primVtx.prompt,"prompt/I");
43  primaryvtx->Branch("cross",&primVtx.cross,"cross/I");
44  primaryvtx->Branch("tof",&primVtx.tof,"tof/I");
45  primaryvtx->Branch("notof",&primVtx.notof,"notof/I");
46  primaryvtx->Branch("EEMC",&primVtx.EEMC,"EEMC/I");
47  primaryvtx->Branch("noEEMC",&primVtx.noEEMC,"noEEMC/I");
48  primaryvtx->Branch("BEMC",&primVtx.BEMC,"BEMC/I");
49  primaryvtx->Branch("noBEMC",&primVtx.noBEMC,"noBEMC/I");
50 }
51 
53 
54  TDatime now; //Set time in Root
55  now.Print();
56 
57  gROOT->cd(); //Need to erase the memory so no problem filling the tree
58 
60  TString separator(":");
61  TString Vars ("postx:prompt:cross:tof:notof:BEMC:noBEMC:EEMC:noEEMC");
62  TObjArray *array = Vars.Tokenize(separator);
63 
64  vector<string> inputVars;
65  TIter next(array);
66  TObjString *objs;
67  while ((objs = (TObjString *) next())){
68  inputVars.push_back(objs->GetString().Data() );}
69 
70  vector<double>* inputVec = new vector<double>(inputVars.size());
71 
72  IClassifierReader *classReader = new ReadFisher(inputVars);
74 
75  TFile *input = new TFile("outvertex.root");
76  TTree *oldtree = (TTree*)input->Get("primaryvtx");
77  SetTreeVariables(oldtree);
78  oldtree->SetBranchStatus("*",1);
79 
80  TFile *newfile = new TFile("outvertex_tmva","recreate");
81  TTree *newtree = oldtree->CloneTree();
82  Float_t tmva=-2.1;
83  TBranch *Ttmva = newtree->Branch("tmva",&tmva,"tmva/F");
84 
85  Long64_t nentries = oldtree->GetEntriesFast();
86 
87  cout << "Number of events: " << nentries << endl;
88 
89  for (Long64_t i=0; i<nentries;i++) {
90  oldtree->GetEntry(i);
91  if( i % 1000000 == 0) cout << "Reading event: " << i << endl;
92 
94  Float_t dataArray1[9] = {primVtx.postx,primVtx.prompt,primVtx.cross,
95  primVtx.tof,primVtx.notof,
96  primVtx.BEMC,primVtx.noBEMC,
97  primVtx.EEMC,primVtx.noEEMC};
98  UInt_t N1 = inputVars.size();
99  for (UInt_t jjj = 0 ; jjj < N1 ; jjj++) (*inputVec)[jjj] = dataArray1[jjj];
100  tmva = classReader->GetMvaValue(*inputVec);
102 
103  Ttmva->Fill();
104  }
105 
106  newfile->Write();
107  //cout << "Number of entries: " << nentries << endl;
108  //cout << "Good eventes: " << good << endl;
109  TDatime now1; //Set time in Root
110  now1.Print();
111 }
void SetTreeVariables(TTree *oldtree)
void ApplyClassifier()