Skip to content
Snippets Groups Projects
Forked from cepc / CEPCSW
1486 commits behind the upstream repository.
GenAlgo.cpp 2.65 KiB
#include "GenAlgo.h"

#include "GaudiKernel/IEventProcessor.h"
#include "GaudiKernel/IAppMgrUI.h"
#include "GaudiKernel/GaudiException.h"


#include "plcio/MCParticleCollection.h"//plico

#include <iostream>
#include <vector>
#include <fstream>

#include "IGenTool.h"
#include "GenEvent.h"
#include "StdHepRdr.h"
#include "HepevtRdr.h"// not correct still
#include "SLCIORdr.h"
#include "HepMCRdr.h"
#include "GenPrinter.h"
#include "GenWriter.h"

using namespace std;

DECLARE_COMPONENT(GenAlgo)

GenAlgo::GenAlgo(const std::string& name, ISvcLocator* pSvcLocator): GaudiAlgorithm(name, pSvcLocator) {
    declareProperty("MCParticle", m_hdl, "MCParticle collection (output)");
    m_evtid = 0;

}

StatusCode
GenAlgo::initialize() {
    
    cout << "initialize start" << endl; 
    string generatorName = m_input_file.value();
    string outputName    = m_output_file.value();
    string format        = m_input_format.value();
    IGenTool* gen_reader;
    if(format=="stdhep") gen_reader  = new StdHepRdr(generatorName);    
    else if(format=="slcio") gen_reader  = new SLCIORdr(generatorName);    
    else if(format=="hepmc") gen_reader  = new HepMCRdr(generatorName);    
    else{cout << "Error : unsupport format for generator input file" << endl; return StatusCode::FAILURE; }
    //IGenTool* gen_reader  = new HepevtRdr(generatorName);    
    m_genTools.push_back(gen_reader);
    if(m_print.value()) {
        IGenTool* gen_printer = new GenPrinter(generatorName);    
        m_genTools.push_back(gen_printer);
    }
    //IGenTool* gen_writer  = new GenWriter (outputName);    
    //m_genTools.push_back(gen_writer);

    cout << "initialize done" << endl; 
    return StatusCode::SUCCESS;

}

StatusCode
GenAlgo::execute() {
    m_evtid++;
    auto mcCol = m_hdl.createAndPut();
    MyHepMC::GenEvent m_event(*mcCol);

    for(std::vector<IGenTool*>::iterator it=m_genTools.begin(); it != m_genTools.end(); ++it) {
        if ((*it)->mutate(m_event)) {} 
        else {
            cout << "Have read all events, stop now." << endl; 
            auto ep = serviceLocator()->as<IEventProcessor>();
            if ( !ep ) {
            error() << "Cannot get IEventProcessor" << endmsg;
            return StatusCode::FAILURE;
            }
            ep->stopRun();
            return StatusCode::SUCCESS;
            
             }
    }

    return StatusCode::SUCCESS;

}

StatusCode
GenAlgo::finalize() {
    cout << "finalize" << endl; 
    for(std::vector<IGenTool*>::iterator it=m_genTools.begin(); it != m_genTools.end(); ++it) {
        if ((*it)->finish()) {} 
        else {cout << "finish Failed" << endl; return StatusCode::FAILURE; }
    }
    return StatusCode::SUCCESS;
}