Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
train.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file train.C
1 #include <TFile.h>
2 #include <TTree.h>
3 #include <TString.h>
4 #include <TChain.h>
5 
6 
7 #if not defined(__CINT__) || defined(__MAKECINT__)
8 // needs to be included when makecint runs (ACLIC)
9 #include "TMVA/Factory.h"
10 #include "TMVA/Tools.h"
11 #endif
12 
13 
14 
15 TChain* handleFile(string name, string extension, string treename, int filecount){
16  TChain *all = new TChain(treename.c_str());
17  string temp;
18  for (int i = 0; i < filecount; ++i)
19  {
20 
21  ostringstream s;
22  s<<i;
23  temp = name+string(s.str())+extension;
24  all->Add(temp.c_str());
25  }
26  return all;
27 }
28 
29 
30 void makeFactory(TTree* signalTree, TTree* backTree,std::string outfile,std::string factoryname, TTree* bgTree2=NULL)
31 {
32  using namespace TMVA;
33  TString jobname(factoryname.c_str());
34  TFile *targetFile = new TFile(outfile.c_str(),"RECREATE");
35  Factory *factory = new Factory(jobname,targetFile,"Correlations=True");
36  DataLoader *loader = new DataLoader();
37  loader->AddSignalTree(signalTree,1.0);
38  loader->AddBackgroundTree(backTree,1.0);
39  if(bgTree2){
40  loader->AddBackgroundTree(bgTree2,1.0);
41  }
42  loader->AddVariable("track_deta",'F');
43  /*loader->AddVariable("track_dlayer",'I');
44  loader->AddVariable("track_layer",'I');*/
45  loader->AddVariable("track_pT",'F');
46  //loader->AddVariable("track_dca",'F');
47  loader->AddVariable("approach_dist",'F'); //idk why this is off
48  loader->AddVariable("vtx_radius",'F');
49  loader->AddVariable("vtx_chi2",'F');
50  //loader->AddVariable("vtxTrackRZ_dist",'F');
51  //loader->AddVariable("abs(vtxTrackRPhi_dist-vtxTrackRZ_dist)",'F',"space diff");
52  //loader->AddVariable("photon_m",'F');
53  //loader->AddVariable("photon_pT",'F');
54  loader->AddVariable("cluster_prob",'F');
55 
56  string track_pT_cut = "track_pT>0";
57  string vtx_radius_cut = "vtx_radius>0";
58  string em_prob_cut = "cluster_prob>=0";
59  //string layer_cuts = "track_dlayer>=0&&track_layer>=0";
60  string dist_cuts = "vtxTrackRZ_dist>=0&&vtxTrackRPhi_dist>=0&&approach_dist>=0";
61  //do I need photon cuts?
62  string tCutInitializer = em_prob_cut+"&&"+ vtx_radius_cut+"&&"+track_pT_cut+"&&"+dist_cuts;
63  TCut preTraingCuts(tCutInitializer.c_str());
64 
65 
66  loader->PrepareTrainingAndTestTree(preTraingCuts,"nTrain_Signal=0:nTrain_Background=0:nTest_Signal=0:nTest_Background=0");
67  factory->BookMethod( loader,TMVA::Types::kLikelihood, "LikelihoodD",
68  "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" );
69  factory->BookMethod(loader,Types::kCuts,"Cuts","");
70  /*factory->BookMethod( Types::kKNN, "kNN", "" ); //>100k events
71  factory->BookMethod( Types::kPDERS, "PDERS", "" );//>100k events*/
72  /*factory->BookMethod( Types::kPDEFoam, "PDEFoam", "VolFrac=.0588i:SigBgSeparate=True" );//>10k events
73  factory->BookMethod( Types::kFisher, "Fisher", "" );
74  factory->BookMethod( Types::kLD, "LD" );*/
75  /*would need to have the options tuned
76  * factory->BookMethod( Types::kFDA, "FDA", "Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:\
77  ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):\
78  FitMethod=MINUIT:\
79  ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" );*/
80 /* nerual network that would need to be worked on
81  * factory->BookMethod( Types::kMLP, "MLP_ANN", "<options>" );*/
82  factory->TrainAllMethods();
83  factory->TestAllMethods();
84  factory->EvaluateAllMethods();
85  targetFile->Write();
86  targetFile->Close();
87 }
88 
89 
90 int train(){
91  using namespace std;
92  string treePath = "/sphenix/user/vassalli/gammasample/conversiononlineanalysis";
93  string treeExtension = ".root";
94  string outname = "cutTrainA.root";
95  unsigned int nFiles=200;
96 
97  TChain *signalTree = handleFile(treePath,treeExtension,"cutTreeSignal",nFiles);
98  TChain *backHTree = handleFile(treePath,treeExtension,"cutTreeBackh",nFiles);
99  TChain *backETree = handleFile(treePath,treeExtension,"cutTreeBacke",nFiles);
100  makeFactory(signalTree,backHTree,outname,"aback",backETree);
101 /* outname="cutTrainE.root";
102  makeFactory(signalTree,backETree,outname,"eback");*/
103  return 0;
104 }