/******************** * Author: M. Babai * * M.Babai@rug.nl * ******************** */ /* * Example program. This code shows how to use the classify * procedure. This classifier is implemented based on the Parzen * Window algorithm. */ // C++ #include #include // Local #include "PndPrzWindowClassify.h" // ROOT & PandaRoot #include "TFile.h" #include "TNtuple.h" #include "TStopwatch.h" void printResultMap(std::map& res) { std::cout << "\n\t================================== \n"; for( std::map::iterator ii=res.begin(); ii != res.end(); ++ii){ std::cout <<"\t" << (*ii).first << "\t=> " << (*ii).second << std::endl; } std::cout << "\n\t================================== \n"; } /* ********************************************* * Testing routine, can be deleted afterwards. * * ********************************************* */ int main(int argc, char** argv) { if(argc < 5){ std::cerr << "\t" << "./classifyPrzWin " << " " << std::endl; return 1; } // Weights. std::string InPutFile = argv[1]; // Events to classify. std::string InputEvents = argv[2]; // Events tree name. std::string EvtTreeName = argv[3]; // File to write output. std::string OutPutFile = argv[4]; // Containers to hold labels and variable names. std::vector clasNames; std::vector vars; std::map wsize; // Classes (container to hold the class names) clasNames.push_back("electron"); clasNames.push_back("pion"); //clasNames.push_back("kaon"); //clasNames.push_back("muon"); //clasNames.push_back("proton"); //clasNames.push_back("gamma"); // Variables (names) //vars.push_back("p"); //wsize["p"] = 1.6; vars.push_back("emc"); wsize["emc"] = 2.4; vars.push_back("lat"); wsize["lat"] = 5.0; vars.push_back("z20"); wsize["z20"] = 5.0; vars.push_back("z53"); wsize["z53"] = 5.0; //vars.push_back("thetaC"); //vars.push_back("mvd"); //vars.push_back("tof"); //vars.push_back("stt"); TStopwatch timer; timer.Start(); //Create the classifier object and specify the weight file PndPrzWindowClassify cls (InPutFile, clasNames, vars); // Set classifier parameters and init. cls.setWindowSize(wsize); timer.Stop(); double rtime = timer.RealTime(); double ctime = timer.CpuTime(); std::cout << " Initialization time:" << std::endl; std::cout<< "RealTime = " << rtime << " seconds, CpuTime = " << ctime <<" Seconds" << std::endl; // Open input events file. TFile inFile(InputEvents.c_str(), "READ"); // Prepare events to be classified. TNtuple* events = (TNtuple*) inFile.Get(EvtTreeName.c_str()); std::vector curEvt(vars.size(), 0.0); // Bind tree branches to the container. for(size_t i = 0; i < vars.size(); i++){ events->SetBranchAddress( (vars[i]).c_str(), &(curEvt[i])); } // Map to store the results std::map res; // Reste and start the timer. timer.Reset(); timer.Start(); std::cout << " Classification." << std::endl; // ___________ Classification ________ unsigned int misCnt = 0; int totNumEvt = 10;//events->GetEntriesFast(); // Open OutputFile. std::ofstream Outfile; Outfile.open(OutPutFile.c_str(), std::ios::out| std::ios::trunc); Outfile << "# =========================================================" << std::endl << "# Classification output of a parzenwindow based classifier." << std::endl << "# Total number of test events = " << totNumEvt << std::endl << "# treename = " << EvtTreeName << std::endl << "# Init Window edges" << std::endl; for(size_t i = 0; i < vars.size(); i++){ Outfile << "# " << vars[i] << " = " << wsize[vars[i]] << std::endl; } Outfile << "\n# =========================================================\n"; Outfile << "# Using vaiables: "; for(size_t i = 0; i < vars.size(); i++){ Outfile << vars[i] << " "; } Outfile << "\n# =========================================================" << std::endl; // ___________ Classify input events ________ for(int ev = 0; ev < totNumEvt; ev++){ events->GetEntry(ev); //cls.GetMvaValues(curEvt, res); std::string* Winner = cls.Classify(curEvt); if( *Winner != EvtTreeName ){ misCnt++; } delete Winner; //printResultMap(res); } Outfile << std::setprecision(5) << "# Number of Missclassified events = " << misCnt << " "<< ( static_cast(misCnt * 100)/static_cast(totNumEvt) ) << " %" << std::endl; // Close open file inFile.Close(); Outfile.close(); timer.Stop(); rtime = timer.RealTime(); ctime = timer.CpuTime(); std::cout << "==============================================" << std::endl << " Classifier timing results:" << std::endl << "RealTime = " << rtime << " seconds, CpuTime = " << ctime <<" Seconds.\n" << std::endl; return 0; }