00001
00002 #include "DsPullEvent.h"
00003
00004 #include "G4DataHelpers/G4DhHit.h"
00005
00006 #include "Event/GenHeader.h"
00007
00008 #include "GiGa/IGiGaSvc.h"
00009 #include "G4DataHelpers/IHistoryKeeper.h"
00010 #include "G4DataHelpers/INeutronCaptureInfo.h"
00011 #include "G4DataHelpers/G4DhNeutronCapture.h"
00012
00013 #include "G4Event.hh"
00014 #include "G4PrimaryVertex.hh"
00015
00016 DsPullEvent::DsPullEvent(const std::string& name, ISvcLocator* pSvcLocator)
00017 : DybAlgorithm<DayaBay::SimHeader>(name,pSvcLocator)
00018 , m_giga(0)
00019 , m_historyKeeper(0)
00020
00021 {
00022 declareProperty("GenLocation",m_genLocation=DayaBay::GenHeaderLocation::Default,
00023 "Location in the TES where the input GenHeader is to be found.");
00024 }
00025
00026 DsPullEvent::~DsPullEvent()
00027 {
00028 }
00029
00030 StatusCode DsPullEvent::initialize()
00031 {
00032 m_giga = svc<IGiGaSvc>("GiGa",true);
00033 m_historyKeeper = svc<IHistoryKeeper>("HistoryKeeper",true);
00034
00035 m_capinfo = tool<INeutronCaptureInfo>("G4DhNeutronCaptureInfoTool");
00036
00037 return StatusCode::SUCCESS;
00038 }
00039
00040 StatusCode DsPullEvent::execute()
00041 {
00042 DayaBay::SimHeader* header = MakeHeaderObject();
00043
00044
00045
00046
00047
00048 DayaBay::GenHeader* gen_header = getTES<DayaBay::GenHeader>(m_genLocation);
00049 header->setTimeStamp(gen_header->timeStamp());
00050
00052
00053 const G4Event* g4event = 0;
00054 m_giga->retrieveEvent(g4event);
00055 if (!g4event) {
00056 error() << "No G4Event!" << endreq;
00057 return StatusCode::FAILURE;
00058 }
00059
00060
00061 G4DhNeutronCapture capture;
00062 m_capinfo->addCapture(capture);
00063
00064 int nverts = g4event->GetNumberOfPrimaryVertex();
00065 if( nverts == 0 ) {
00066 warning() << "The g4event has zero primary vertices!" << endreq;
00067 return StatusCode::SUCCESS;
00068 }
00069
00070
00071 debug() << "Pulled event with " << nverts
00072 << " primary vertices, event id:" << g4event->GetEventID() << endreq;
00073 G4PrimaryVertex* g4vtx = g4event->GetPrimaryVertex(0);
00074 while (g4vtx) {
00075 debug() << "\n\tat (" << g4vtx->GetX0() << "," << g4vtx->GetY0() << "," << g4vtx->GetZ0() << ")";
00076 g4vtx = g4vtx->GetNext();
00077 break;
00078 }
00079 debug() << endreq;
00080
00082
00083
00084 DayaBay::SimParticleHistory* history =0;
00085 m_historyKeeper->ClaimCurrentHistory(history);
00086 header->setParticleHistory(history);
00087
00089
00090 DayaBay::SimUnobservableStatisticsHeader* unobs =0;
00091 m_historyKeeper->ClaimCurrentUnobservable(unobs);
00092 header->setUnobservableStatistics(unobs);
00093
00095
00096 G4HCofThisEvent* hcs = g4event->GetHCofThisEvent();
00097 if (!hcs) {
00098 warning() << "No HitCollections in this event" << endreq;
00099 return StatusCode::SUCCESS;
00100 }
00101 int nhc = hcs->GetNumberOfCollections();
00102 if (!nhc) {
00103 warning() << "Number of HitCollections is zero" << endreq;
00104 return StatusCode::SUCCESS;
00105 }
00106 debug () << "# HitCollections = " << nhc << endreq;
00107
00108
00109 DayaBay::SimHitHeader* hit_header = new DayaBay::SimHitHeader(header);
00110 header->setHits(hit_header);
00111
00112 double earliestTime = 0;
00113 double latestTime = 0;
00114 Context context;
00115 context.SetSimFlag(SimFlag::kMC);
00116 bool firstDetector = true;
00117 int hitcount=0;
00118
00119 for (int ihc=0; ihc<nhc; ++ihc) {
00120 G4DhHitCollection* g4hc = dynamic_cast<G4DhHitCollection*>(hcs->GetHC(ihc));
00121 if (!g4hc) {
00122 error() << "Failed to get hit collection #" << ihc << endreq;
00123 return StatusCode::FAILURE;
00124 }
00125
00126
00127 size_t nhits = g4hc->GetSize();
00128 hitcount+=nhits;
00129 if (!nhits) continue;
00130
00131 bool firstHit = true;
00132 DayaBay::SimHitCollection::hit_container hits;
00133 DayaBay::Detector detector;
00134 DayaBay::SimHitCollection* shc =
00135 new DayaBay::SimHitCollection(hit_header,detector,hits);
00136 for (size_t ihit=0; ihit<nhits; ++ihit) {
00137 DayaBay::SimHit* simhit = (*g4hc)[ihit]->get();
00138 if(history) {
00139 int trackid = (*g4hc)[ihit]->trackId();
00140 simhit->setAncestor(history->track(trackid));
00141 }
00142
00143 if(firstHit){
00144 detector = DayaBay::Detector(simhit->sensDetId()).siteDetPackedData();
00145 if(firstDetector){
00146
00147 context.SetSite(detector.site());
00148 context.SetDetId(detector.detectorId());
00149 }
00150 if(context.GetSite() != detector.site()){
00151
00152 context.SetSite(Site::kUnknown);
00153 }
00154 if(context.GetDetId() != detector.detectorId()){
00155
00156 context.SetDetId(DetectorId::kUnknown);
00157 }
00158 }
00159
00160 debug() << "Hit Time: " << simhit->hitTime() << endreq;
00161 if((firstHit && firstDetector) || simhit->hitTime() < earliestTime)
00162 earliestTime = simhit->hitTime();
00163 if((firstHit && firstDetector) || simhit->hitTime() > latestTime)
00164 latestTime = simhit->hitTime();
00165 firstHit = false;
00166
00167 simhit->setHc(shc);
00168 hits.push_back(simhit);
00169 }
00170 firstDetector = false;
00171
00172 shc->setDetector(detector);
00173 shc->setCollection(hits);
00174 debug() << "Adding " << detector.detName() << " hits ("
00175 << shc->collection().size() << ") to header." << endreq;
00176 hit_header->addHitCollection(shc);
00177 }
00178
00179
00180 debug() << "Header Time sec, nanosec: " << header->timeStamp().GetSec()
00181 << " " << header->timeStamp().GetNanoSec() << endreq;
00182 debug() << "Earliest Hit: " << earliestTime << endreq;
00183 debug() << "Latest Hit: " << latestTime << endreq;
00184 TimeStamp earliestTS(header->timeStamp());
00185 TimeStamp latestTS(header->timeStamp());
00186 earliestTS.Add(earliestTime * 1e-9);
00187 latestTS.Add(latestTime * 1e-9);
00188 debug() << "Earliest Time sec, nanosec: " << earliestTS.GetSec()
00189 << " " << earliestTS.GetNanoSec() << endreq;
00190 debug() << "Latest Time sec, nanosec: " << latestTS.GetSec()
00191 << " " << latestTS.GetNanoSec() << endreq;
00192 header->setEarliest(earliestTS);
00193 header->setLatest(latestTS);
00194 debug() << "Earliest Time sec, nanosec: " << header->earliest().GetSec()
00195 << " " << header->earliest().GetNanoSec() << endreq;
00196 debug() << "Latest Time sec, nanosec: " << header->latest().GetSec()
00197 << " " << header->latest().GetNanoSec() << endreq;
00198 context.SetTimeStamp(gen_header->timeStamp());
00199 header->setContext(context);
00200
00201 if(hitcount==0) {
00202 header->setTimeStamp(gen_header->earliest());
00203 header->setEarliest(gen_header->earliest());
00204 header->setLatest(gen_header->earliest());
00205 }
00206
00207 return StatusCode::SUCCESS;
00208 }
00209
00210 StatusCode DsPullEvent::finalize()
00211 {
00212 return StatusCode::SUCCESS;
00213 }
00214