CreateKNNTrainingTrees.C
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <iomanip>
4 #include <sstream>
5 #include <string>
6 #include <cstring>
7 #include "TFile.h"
8 #include "TTree.h"
9 #include "TChain.h"
10 
11 void CreateKNNTrainingTrees(std::string input_file_name="inputfilename.root", std::string output_file_name="outputfilename.root"){
12 
13  // output file with signal and background trees that can be used by TMVA for training
14  TFile* outfile = new TFile(output_file_name.c_str(),"RECREATE");
15 
16 
17  TChain* sigtree = new TChain("remidtrain/fTreeS");
18  sigtree->Add(input_file_name.c_str());
19 
20  TChain* backtree = new TChain("remidtrain/fTreeB");
21  backtree->Add(input_file_name.c_str());
22 
23  // make the signal output trees
24  float fTrackLength, fDedxSep, fScatSep, fMeasFrac, fnuE;
25  TTree* SigTree = new TTree("SigTree","Input Variables for the Signal Tree");
26  SigTree->Branch("trackLength", &fTrackLength, "trackLength/F");
27  SigTree->Branch("dedxSep", &fDedxSep, "dedxSep/F");
28  SigTree->Branch("scatSep", &fScatSep, "scatSep/F");
29  SigTree->Branch("measFrac", &fMeasFrac, "measFrac/F");
30  SigTree->Branch("nuE", &fnuE, "nuE/F");
31 
32  // setup reading in the signal tree
33  float trklensig, ESepsig, ScatSepsig, nuEsig, measfracsig;
34  sigtree->SetBranchAddress("trackLength",&trklensig);
35  sigtree->SetBranchAddress("dedxSep",&ESepsig);
36  sigtree->SetBranchAddress("scatSep",&ScatSepsig);
37  sigtree->SetBranchAddress("measFrac", &measfracsig);
38  sigtree->SetBranchAddress("visE", &nuEsig);
39 
40  int nsigevts = sigtree->GetEntries();
41  int nbackevts = backtree->GetEntries();
42 
43  int nmaxevts = nsigevts;
44  if(nsigevts > nbackevts){ nmaxevts = nbackevts; }
45 
46  std::cout<<"Number of signal events: "<<sigtree->GetEntries()<<std::endl;
47  std::cout<<"Number of background events: "<<backtree->GetEntries()<<std::endl;
48  std::cout<<"nmaxevts: " << nmaxevts << std::endl;
49 
50  int star_every = 0;
51  star_every = nmaxevts / 80;
52  for(unsigned int idx = 0; idx < nmaxevts; ++idx){
53  if(idx % star_every == 0) std::cerr << "*";
54  // get the energy of this event
55  sigtree->GetEntry(idx);
56  float energy = nuEsig;
57  fnuE = energy;
58  fTrackLength = trklensig;
59  fDedxSep = ESepsig;
60  fScatSep = ScatSepsig;
61  fMeasFrac = measfracsig;
62  SigTree->Fill();
63  } // loop over the signal tree events
64  std::cerr << "\n";
65 
66  // make the background output trees
67  TTree* BackTree = new TTree("BackTree","Input Variable for the Background Tree");
68  BackTree->Branch("trackLength", &fTrackLength, "trackLength/F");
69  BackTree->Branch("dedxSep", &fDedxSep, "dedxSep/F");
70  BackTree->Branch("scatSep", &fScatSep, "scatSep/F");
71  BackTree->Branch("measFrac", &fMeasFrac, "measFrac/F");
72  BackTree->Branch("nuE", &fnuE, "nuE/F");
73 
74  // setup reading in the background tree
75  float trklenback, ESepback, ScatSepback, nuEback, measfracback;
76  backtree->SetBranchAddress("trackLength",&trklenback);
77  backtree->SetBranchAddress("dedxSep",&ESepback);
78  backtree->SetBranchAddress("scatSep",&ScatSepback);
79  backtree->SetBranchAddress("visE", &nuEback);
80  backtree->SetBranchAddress("measFrac", &measfracback);
81 
82  for(unsigned int idx = 0; idx < nmaxevts; ++idx){
83  if(idx % star_every == 0) std::cerr << "*";
84  // get the energy of this event
85  backtree->GetEntry(idx);
86  float energy = nuEback;
87  fnuE = energy;
88  fTrackLength = trklenback;
89  fDedxSep = ESepback;
90  fScatSep = ScatSepback;
91  fMeasFrac = measfracback;
92  BackTree->Fill();
93  } // loop over the background tree events
94  std::cerr << "\n";
95 
96 
97  outfile->cd();
98  SigTree->Write();
99  BackTree->Write();
100  outfile->Write();
101  outfile->Close();
102 
103 }
OStream cerr
Definition: OStream.cxx:7
double energy
Definition: plottest35.C:25
OStream cout
Definition: OStream.cxx:6
void CreateKNNTrainingTrees(std::string input_file_name="inputfilename.root", std::string output_file_name="outputfilename.root")
FILE * outfile
Definition: dump_event.C:13
enum BeamMode string