Skip to content
Snippets Groups Projects
GenAlgo.cpp 2.92 KiB
Newer Older
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
#include "GenAlgo.h"

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


#include "edm4hep/MCParticleCollection.h"//plico
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed

#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"
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed

using namespace std;

DECLARE_COMPONENT(GenAlgo)

GenAlgo::GenAlgo(const std::string& name, ISvcLocator* pSvcLocator): GaudiAlgorithm(name, pSvcLocator) {
    declareProperty("MCParticle", m_hdl, "MCParticle collection (output)");
    declareProperty("GenTools", m_genToolNames, "List of GenTools");
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
    m_evtid = 0;

}

StatusCode
GenAlgo::initialize() {
    if (m_genToolNames.size()==0) {
        error() << "Please specify the gentools to be used in GenAlgo." << endmsg;
        return StatusCode::FAILURE;
    }

    for (auto gtname: m_genToolNames) {
        m_genTools.push_back(gtname);
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
    }
    
    // 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);
    // }
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
    //IGenTool* gen_writer  = new GenWriter (outputName);    
    //m_genTools.push_back(gen_writer);

    // cout << "initialize done" << endl; 
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
    return StatusCode::SUCCESS;

}

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

    for(auto gentool: m_genTools) {
        if (gentool->mutate(m_event)) {} 
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
        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(auto gentool: m_genTools) {
    //     if (gentool->finish()) {} 
    //     else {cout << "finish Failed" << endl; return StatusCode::FAILURE; }
    // }
fangwx@ihep.ac.cn's avatar
V3
fangwx@ihep.ac.cn committed
    return StatusCode::SUCCESS;
}