3 //--------------------------------------------------------------------
4 template<typename Evt> //DO NOT USE THIS FUNCTION FROM WITHIN ART! The ParticleInventoryService is designed to impliment these methods as cleanly as possible within the art framework.
5 void ParticleInventory::PrepEvent (const Evt& evt ){
6 if(!(this->CanRun(evt))){
7 throw cet::exception("ParticleInventory")
8 << "Particle Inventory cannot function. "
9 << "Is this file real data?";
11 fParticleList.clear();
12 fMCTObj.fMCTruthList.clear();
13 fMCTObj.fTrackIdToMCTruthIndex.clear();
14 this->PrepParticleList(evt);
15 this->PrepMCTruthList(evt);
16 this->PrepTrackIdToMCTruthIndex(evt);
20 //--------------------------------------------------------------------
21 template<typename Evt>
22 void ParticleInventory::PrepParticleList(const Evt& evt ) const{
24 if(this->ParticleListReady( )){ //The particle list already exists. Do nothing.
27 //The particle list needs to be built
28 //We use auto so that we(the compiler) can determine which type we need for either art or gallery.
29 const auto& pHandle = evt.template getValidHandle<std::vector<simb::MCParticle>>(fG4ModuleLabel);
30 // const auto& partVecIn = evt.template getValidHandle<std::vector<simb::MCParticle>>(fG4ModuleLabel);
31 if(pHandle.failedToGet()){
32 /*mf::LogWarning("BackTracker") << "failed to get handle to simb::MCParticle from "
34 << ", return";*/ //Do this silently so we don't polute the logs. It is expected to fail for all gen and g4 jobs.
38 const auto& partVecIn = *pHandle;
39 for(const auto& partIn : partVecIn){
40 fParticleList.Add(new simb::MCParticle(partIn)); //Is this still doing a copy? If so, another method should be used.
42 fParticleList.AdoptEveIdCalculator(new sim::EmEveIdCalculator);
45 //--------------------------------------------------------------------
46 template<typename Evt> //I may want to make this function private.
47 void ParticleInventory::PrepMCTruthListAndTrackIdToMCTruthIndex(const Evt& evt ) const{
48 if( this->TrackIdToMCTruthReady() && this->MCTruthListReady( ) ){ return;}
49 this->PrepParticleList( evt); //Make sure we have built the particle list for this event
50 //const auto& mcpmctAssnsIn = *( evt.template getValidHandle<art::Assns<simb::MCParticle,simb::MCTruth>>(fG4ModuleLabel));
51 const auto& mcpmctAssnsHandle = evt.template getValidHandle<art::Assns<simb::MCParticle,simb::MCTruth>>(fG4ModuleLabel);
52 const auto& mcpmctAssnsIn = *mcpmctAssnsHandle;
53 for( const auto& mcpmctAssnIn : mcpmctAssnsIn){ //Assns are themselves a container. Loop over entries.
54 const art::Ptr<simb::MCParticle>& part=mcpmctAssnIn.first;
55 const art::Ptr<simb::MCTruth>& mct =mcpmctAssnIn.second;
56 unsigned short mctruth_idx = USHRT_MAX;
57 for (size_t i = 0; i<fMCTObj.fMCTruthList.size(); ++i){
58 if (fMCTObj.fMCTruthList[i] == mct){
63 if (mctruth_idx == USHRT_MAX){
64 fMCTObj.fMCTruthList.push_back(mct);
65 fMCTObj.fTrackIdToMCTruthIndex.emplace(part->TrackId(), fMCTObj.fMCTruthList.size() - 1);
68 fMCTObj.fTrackIdToMCTruthIndex.emplace(part->TrackId(), mctruth_idx );
73 //--------------------------------------------------------------------
74 template<typename Evt>
75 void ParticleInventory::PrepMCTruthList (const Evt& evt ) const{
76 if(this->MCTruthListReady( ) ){ return;} //If the event is data or if the truth list is already built there is nothing for us to do.
77 PrepMCTruthListAndTrackIdToMCTruthIndex( evt); //TrackIdToMCTruthIndex and MCTruthList are prepared at the same time. The access of information makes this the most convenient way to do so. It is only somewhat more expensive for the memory, but significantly less expensive for time.
80 //--------------------------------------------------------------------
81 template<typename Evt>
82 void ParticleInventory::PrepTrackIdToMCTruthIndex(const Evt& evt ) const{
83 if(this->TrackIdToMCTruthReady()){ return;} //The list already exists. Do nothing.
84 PrepMCTruthListAndTrackIdToMCTruthIndex( evt); //TrackIdToMCTruthIndex and MCTruthList are prepared at the same time. The access of information makes this the most convenient way to do so. It is only somewhat more expensive for the memory, but significantly less expensive for time.
87 template<typename Evt>
88 bool ParticleInventory::CanRun(const Evt& evt) const{
89 return !(evt.isRealData());