00001 #define McReconAlg_CPP
00002
00003
00004
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/AlgFactory.h"
00007 #include "GaudiKernel/IDataProviderSvc.h"
00008 #include "GaudiKernel/SmartDataPtr.h"
00009 #include "GaudiKernel/Algorithm.h"
00010
00011
00012 #include "GaudiKernel/INTupleSvc.h"
00013 #include "GaudiKernel/INTuple.h"
00014 #include "GaudiKernel/NTuple.h"
00015 #include "GaudiKernel/SmartDataPtr.h"
00016 #include "GaudiKernel/StatusCode.h"
00017
00018 #include "CLHEP/Geometry/Point3D.h"
00019 #include "CLHEP/Vector/LorentzVector.h"
00020
00021 #include "GlastEvent/MonteCarlo/McVertex.h"
00022 #include "GlastEvent/MonteCarlo/McParticle.h"
00023 #include "GlastEvent/TopLevel/Event.h"
00024 #include "GlastEvent/TopLevel/MCEvent.h"
00025 #include "GlastEvent/Recon/ISiRecObjs.h"
00026
00027 #include "ntupleWriterSvc/INTupleWriterSvc.h"
00028
00029
00030
00036 class McReconAlg : public Algorithm {
00037
00038 public:
00040 McReconAlg(const std::string& name, ISvcLocator* pSvcLocator);
00041
00042 StatusCode initialize();
00043 StatusCode execute();
00044 StatusCode finalize();
00045
00046 private:
00047 std::string m_tupleName;
00048 INTupleWriterSvc *m_ntupleWriteSvc;
00049
00050 };
00051
00052
00053 static const AlgFactory<McReconAlg> Factory;
00054 const IAlgFactory& McReconAlgFactory = Factory;
00055
00057
00058 Algorithm(name, pSvcLocator){
00059
00060 declareProperty("tupleName", m_tupleName="");
00061
00062
00063 }
00064
00065
00068 StatusCode McReconAlg::initialize() {
00069
00070 StatusCode sc = StatusCode::SUCCESS;
00071
00072 MsgStream log(msgSvc(), name());
00073
00074
00075 setProperties();
00076
00077
00078 sc = service("ntupleWriterSvc", m_ntupleWriteSvc);
00079
00080 if( sc.isFailure() ) {
00081 log << MSG::ERROR << "McReconAlg failed to get the ntupleWriterSvc" << endreq;
00082 return sc;
00083 }
00084
00085 if( m_tupleName.empty() ) {
00086 log << MSG::WARNING << "Property \"tupleName\" not defined: will not write to the tuple" << endreq;
00087 }
00088 return sc;
00089 }
00090
00091
00092
00093
00094 StatusCode McReconAlg::execute() {
00095
00096 StatusCode sc = StatusCode::SUCCESS;
00097 MsgStream log( msgSvc(), name() );
00098
00099 if( m_tupleName.empty()) return sc;
00100
00101 McVertexList * vertList = SmartDataPtr<McVertexList>(eventSvc(), "/Event/MC/McVertexCol");
00102
00103 if( vertList == 0)
00104 {
00105 log << MSG::ERROR << "McRecon Failed to get /Event/MC/McVertexCol" << endreq;
00106 sc = StatusCode::FAILURE;
00107 return sc;
00108 }
00109
00110
00111 SmartDataPtr<Event> event(eventSvc(),"/Event");
00112 if( 0==event) { log << MSG::ERROR << "could not find the event header" << endreq;
00113 return StatusCode::FAILURE;
00114 }
00115
00116 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"Event_ID",event->event());
00117 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"Run_Number",event->run());
00118 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"Elapsed_Time",event->time().time()/1e6);
00119
00120
00121 SmartDataPtr<MCEvent> mcEvent(eventSvc(),"/Event/MC");
00122 if( 0==mcEvent) { log << MSG::ERROR << "could not find the MCEvent header" << endreq;
00123 return StatusCode::FAILURE;
00124 }
00125
00126 McVertex* mcVert = vertList->front();
00127
00128 if(mcVert == 0)
00129 {
00130 log << MSG::ERROR << "McVertex list is empty" << endreq;
00131 sc = StatusCode::FAILURE;
00132 return sc;
00133 }
00134
00135 HepLorentzVector vec = mcVert->initialFourMomentum();
00136
00137 double ke = vec.e()-vec.m();
00138 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_Energy",ke);
00139 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_logE",log10(ke));
00140
00141 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_src_Id", mcEvent->sourceId());
00142
00143 Hep3Vector p(vec), dir(p.unit());
00144 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_xDir",dir.x());
00145 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_yDir",dir.y());
00146 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_zDir",dir.z());
00147
00148
00149 HepPoint3D pos = mcVert->finalPosition();
00150 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_X0",pos.x());
00151 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_Y0",pos.y());
00152 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(),"MC_Z0",pos.z());
00153
00154
00155
00156
00157 double Gamma_Xdir_Err = -9999;
00158 double Gamma_Ydir_Err = -9999;
00159 double Gamma_Err = -9999;
00160
00161 ISiRecObjs* pSiRecObjs = SmartDataPtr<ISiRecObjs>(eventSvc(),"/Event/TkrRecon/SiRecObjs");
00162
00163 if (pSiRecObjs)
00164 {
00165 if (pSiRecObjs->numGammas() > 0)
00166 {
00167
00168 Vector t0 = pSiRecObjs->getGammaDirection(0);
00169
00170
00171
00172 Gamma_Xdir_Err = t0.x() - dir.x();
00173 Gamma_Ydir_Err = t0.y() - dir.y();
00174
00175 Gamma_Err = t0 * dir;
00176
00177 if (Gamma_Err > 1.0) Gamma_Err = 1.0;
00178 else if (Gamma_Err <= -1.0) Gamma_Err = -0.99999;
00179
00180 Gamma_Err = acos(Gamma_Err);
00181 }
00182 }
00183
00184 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(), "MC_Gamma_Xdir_Err", Gamma_Xdir_Err);
00185 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(), "MC_Gamma_Ydir_Err", Gamma_Ydir_Err);
00186 sc = m_ntupleWriteSvc->addItem(m_tupleName.c_str(), "MC_Gamma_Err", Gamma_Err);
00187
00188 return sc;
00189 }
00190
00191
00192
00193 StatusCode McReconAlg::finalize() {
00194 StatusCode sc = StatusCode::SUCCESS;
00195
00196 MsgStream log(msgSvc(), name());
00197 log << MSG::INFO << "finalize finishing up McReonAlg " << endreq;
00198
00199 return StatusCode::SUCCESS;
00200 }
00201
00202
00203
00204
00205