/* *************************************** * Author: M.Babai@rug.nl * * *************************************** */ /* * Example program. This code shows how to use the classify * procedure. This classifier is implemented based on the KNN * algorithm. An implementation of kd-tree is used to improve the * recognition performance. */ // C++ headers #include // Local headers #include "PndKnnClassify.h" // Root and PandaRoot. #include "TStopwatch.h" #include "TNtuple.h" void printResult( std::map& res, unsigned int evtId){ std::cout << "\t==================================" << std::endl; std::cout << " Evt Num = " << evtId << std::endl; for( std::map::iterator ii=res.begin(); ii != res.end(); ++ii) { std::cout <<"\t" << (*ii).first << "\t=> " << (*ii).second << std::endl; } std::cout << "\t==================================" << std::endl; } /* ********************************************* * Testing routine, can be deleted afterwards. * * ********************************************* */ int main(int argc, char** argv) { if(argc < 5){ std::cerr << "\t" << "./classify " << " " << std::endl; return 1; } // Init input variables. std::string InPutFileName = argv[1]; std::string NumNeistr = argv[2]; std::string InputEvents = argv[3]; std::string EvtTreeName = argv[4]; // Convert to int. std::istringstream buff(NumNeistr); unsigned int NumNei = 0; buff >> NumNei; // Containers to hold labels and variable names. std::vector clasNames; std::vector vars; // 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"); vars.push_back("emc"); vars.push_back("z20"); vars.push_back("z53"); vars.push_back("lat"); //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 PndKnnClassify cls (InPutFileName, clasNames, vars); // Set classifier parameters and init. cls.SetEvtParam(0.8,1.0); cls.SetKnn(NumNei); cls.InitKNN(); std::cout << ".......... Init is done." << std::endl; 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()); // TObjArray* Namen = events->GetListOfBranches(); 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(); // Perform classification of the available events. unsigned int misCl = 0; for(int ev = 0; ev < events->GetEntriesFast(); ev++){ events->GetEntry(ev); cls.GetMvaValues(curEvt, res); const std::string* resStr = cls.Classify(curEvt); //printResult(res); if( *resStr != EvtTreeName){ misCl++; printResult(res, ev); } delete resStr; } timer.Stop(); rtime = timer.RealTime(); ctime = timer.CpuTime(); std::cout << " Classifier timing results:"<< std::endl; std::cout<< "RealTime = " << rtime << " seconds, CpuTime = " << ctime <<" Seconds.\n" << std::endl; // Classifier evaluation info. std::cout << "+++++++++++++++++++++++++++++++++++++++" << std::endl << " Total number of classified events: " << events->GetEntriesFast() << std::endl << " Number of missclassified: " << misCl << " = " << ( static_cast(misCl) * 100.00)/ static_cast(events->GetEntriesFast()) <<" %" << std::endl << " Correct cassified = " << (events->GetEntriesFast() - misCl) << std::endl << " (time / event) = " << rtime/(events->GetEntriesFast()) << std::endl << " With #neighb = " << NumNei << std::endl << "+++++++++++++++++++++++++++++++++++++++" << std::endl; // Close open file inFile.Close(); return 0; }