#include using namespace ROOT; #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace RAT; using namespace RAT::Classifiers; using namespace RAT::PDFs; using namespace RAT::Optimisers; using namespace RAT::PMTSelectors; using namespace RAT::DS; #include using namespace std; ClassifierProcessor::ClassifierProcessor() : Processor("classifier"), fClassifier(NULL), fPDF(NULL), fOptimiser(NULL), fSelector(NULL) { } ClassifierProcessor::~ClassifierProcessor() { delete fClassifier; delete fPDF; delete fOptimiser; delete fSelector; } void ClassifierProcessor::SetI( const std::string& param, const int value ) { vector parts = split( param, "." ); if( parts.size() != 2 ) throw Processor::ParamUnknown( param ); if( parts[0] == string( "classifier" ) && fClassifier !=NULL ) fClassifier->SetI( parts[1], value ); else if( parts[0] == string( "pdf" ) && fPDF !=NULL ) fPDF->SetI( parts[1], value ); else if( parts[0] == string( "optimiser" ) && fOptimiser !=NULL ) fOptimiser->SetI( parts[1], value ); else if( parts[0] == string( "selector" ) && fSelector !=NULL ) fSelector->SetI( parts[1], value ); else if( parts[0] == string( "seed" ) && parts[1] == string( "pass" ) && !fSeedPass.empty() ) fSeedPass.back() = value; else throw Processor::ParamUnknown( param ); } void ClassifierProcessor::SetD( const std::string& param, const double value ) { vector parts = split( param, "." ); if( parts.size() != 2 ) throw Processor::ParamUnknown( param ); if( parts[0] == string( "classifier" ) && fClassifier !=NULL ) fClassifier->SetD( parts[1], value ); else if( parts[0] == string( "pdf" ) && fPDF !=NULL ) fPDF->SetD( parts[1], value ); else if( parts[0] == string( "optimiser" ) && fOptimiser !=NULL ) fOptimiser->SetD( parts[1], value ); else if( parts[0] == string( "selector" ) && fSelector !=NULL ) fSelector->SetD( parts[1], value ); else throw Processor::ParamUnknown( param ); } void ClassifierProcessor::SetS( const std::string& param, const std::string& value ) { if( param == string( "classifier" ) ) { try { fClassifier = ClassifierFactory::Get()->GetClassifier( value ); } catch( FactoryUnknownID& e ) { Log::Die( "ClassifierProcessor::SetS: Error classifier " + e.id + " is unknown." ); } } else if( param == string( "pdf" ) ) { try { fPDF = PDFFactory::Get()->GetPDF( value ); } catch( FactoryUnknownID& e ) { Log::Die( "ClassifierProcessor::SetS: Error pdf " + e.id + " is unknown." ); } dynamic_cast< PDFClassifier* >( fClassifier )->SetPDF( fPDF ); } else if( param == string( "optimiser" ) ) { try { fOptimiser = OptimiserFactory::Get()->GetOptimiser( value ); } catch( FactoryUnknownID& e ) { Log::Die( "ClassifierProcessor::SetS: Error optimiser " + e.id + " is unknown." ); } dynamic_cast< OptimisedClassifier* >( fClassifier )->SetOptimiser( fOptimiser ); } else if( param == string( "selector" ) ) { try { fSelector = PMTSelectorFactory::Get()->GetPMTSelector( value ); } catch( FactoryUnknownID& e ) { Log::Die( "ClassifierProcessor::SetS: Error selector " + e.id + " is unknown." ); } dynamic_cast< SelectorClassifier* >( fClassifier )->AddPMTSelector( fSelector ); } else if( param == string( "seed" ) ) { fSeeds.push_back( value ); fSeedPass.push_back( -1 ); } else if( param == string( "name" ) ) fNickName = value; else { vector parts = split( param, "." ); if( parts.size() != 2 ) throw Processor::ParamUnknown( param ); if( parts[0] == string( "classifier" ) && fClassifier !=NULL ) fClassifier->SetS( parts[1], value ); else if( parts[0] == string( "pdf" ) && fPDF !=NULL ) fPDF->SetS( parts[1], value ); else if( parts[0] == string( "optimiser" ) && fOptimiser !=NULL ) fOptimiser->SetS( parts[1], value ); else if( parts[0] == string( "selector" ) && fSelector !=NULL ) fSelector->SetS( parts[1], value ); else throw Processor::ParamUnknown( param ); } } void ClassifierProcessor::BeginOfRun( DS::Run& run ) { if( fClassifier ) fClassifier->BeginOfRun( run ); if( fPDF ) fPDF->BeginOfRun( run ); if( fOptimiser ) fOptimiser->BeginOfRun( run ); if( fSelector ) fSelector->BeginOfRun( run ); // Now construct the result name stringstream resultName; if( fNickName.empty() ) { resultName << fClassifier->GetName(); if( fOptimiser != NULL ) resultName << ":" << fOptimiser->GetName(); if( fPDF != NULL ) resultName << ":" << fPDF->GetName(); if( fSelector != NULL ) resultName << ":" << fSelector->GetName(); if( fSeeds.empty() == false ) for( vector< string >::const_iterator iSeed = fSeeds.begin(); iSeed != fSeeds.end(); ++iSeed ) resultName << ":" << *iSeed; fNickName = resultName.str(); } } Processor::Result ClassifierProcessor::DSEvent( DS::Run& run, DS::Entry& ds ) { const size_t pass = MetaInformation::Get()->GetCurrentPass(); for( size_t iEV = 0; iEV < ds.GetEVCount(); iEV++ ) { DS::EV& ev = ds.GetEV( iEV ); ev.AddClassifierPass( pass ); vector pmtData; for( size_t iPMTCal =0; iPMTCal < ev.GetCalPMTs().GetCount(); iPMTCal++ ) pmtData.push_back( FitterPMT( ev.GetCalPMTs().GetPMT( iPMTCal ) ) ); fClassifier->SetEventData( pmtData, &ev, &run ); SeededClassifier* seedCast = dynamic_cast< SeededClassifier* >( fClassifier ); if( seedCast != NULL ) { seedCast->DefaultSeed(); if( fSeeds.empty() == false ) { for( unsigned int iSeed = 0; iSeed < fSeeds.size(); iSeed++ ) { try { if( fSeedPass[ iSeed ] == -1 ) seedCast->SetSeed( ev.GetFitResult( fSeeds[ iSeed ] ) ); else seedCast->SetSeed( ev.GetFitResults( fSeedPass[ iSeed ] ).GetResult( fSeeds[ iSeed ] ) ); } catch( DataNotFound& ) { warn << "ClassifierProcessor::Event: Cannot seed from " << fSeeds[ iSeed ] << newline; } } } } // Try to fit try { TStopwatch timer; timer.Start( true ); ClassifierResult result = fClassifier->GetClassification(); timer.Stop(); result.SetExecutionTime( timer.RealTime() ); ev.SetClassifierResult( pass, fNickName, result ); } catch( Classifier::ClassifierFailError& error ) { warn << "ClassifierProcessor::Event " << error.what() << newline; } } return Processor::OK; } void ClassifierProcessor::EndOfRun( DS::Run& run ) { if( fClassifier ) fClassifier->EndOfRun( run ); if( fPDF ) fPDF->EndOfRun( run ); if( fOptimiser ) fOptimiser->EndOfRun( run ); if( fSelector ) fSelector->EndOfRun( run ); }