CRTSimRefac_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTSimRefac
3 // Plugin Type: producer (art v2_10_03)
4 // File: CRTSimRefac_module.cc
5 //
6 // Generated at Wed Jun 27 04:09:39 2018 by Andrew Olivier using cetskelgen
7 // from cetlib version v3_02_00.
8 ////////////////////////////////////////////////////////////////////////
9 
10 // An ART module to simulate how the ProtoDUNE-SP Cosmic Ray Tagger (CRT) system
11 //responds to energy deposits.
12 //Framework includes
20 #include "fhiclcpp/ParameterSet.h"
27 #include "canvas/Persistency/Common/FindManyP.h"
28 //LArSoft includes
29 
34 #include "nug4/ParticleNavigation/ParticleList.h"
35 
36 //local includes
37 //#include "CRTTrigger.h"
39 
40 //c++ includes
41 #include <memory>
42 #include <algorithm>
43 #include <string>
44 #include <map>
45 #include <unordered_map>
46 
47 namespace CRT {
48  class CRTSimRefac;
49 }
50 
52 public:
53  explicit CRTSimRefac(fhicl::ParameterSet const & p);
54  // The compiler-generated destructor is fine for non-base
55  // classes without bare pointers or other resource use.
56 
57  // Plugins should not be copied or assigned.
58  CRTSimRefac(CRTSimRefac const &) = delete;
59  CRTSimRefac(CRTSimRefac &&) = delete;
60  CRTSimRefac & operator = (CRTSimRefac const &) = delete;
61  CRTSimRefac & operator = (CRTSimRefac &&) = delete;
62 
63  // Required functions.
64  void produce(art::Event & e) override;
65 
66 
67 private:
68 
69  // -- message logger
71 
72  //The formats for time and ADC value I will use throughout this module. If I want to change it later, I only have to change it in one place.
73  typedef long long int time;
74  typedef unsigned short adc_t;
75 
76  // Member data
78 
79 
80  double fGeVToADC; //Conversion from GeV in detector to ADC counts output by MAROC2.
81 
82  //Parameterization for algorithm that takes voltage entering MAROC2 and converts it to ADC hits. If I ultimately decide I want to
83  //study impact of more detailed simulation, factor this out into an algorithm for simulating MAROC2 that probably takes continuous
84  //voltage signal as input anyway.
85  time fIntegrationTime; //The time in ns over which charge is integrated in the CRT electronics before being sent to the DAC comparator
86  size_t fReadoutWindowSize; //The time in fIntegrationTimes during which activity in a CRT channel is sent to an ADC if the board has
87  //already decided to trigger
88  size_t fDeadtime; //The dead time in fIntegrationTimes after readout during which no energy deposits are processed by CRT boards.
89  adc_t fDACThreshold; //DAC threshold for triggering readout for any CRT strip.
90  //In GeV for now, but needs to become ADC counts one day.
91  //Should be replaced by either a lookup in a hardware database or
92  //some constant value one day.
93 };
94 
95 
97  logInfo_("CRTSimRefactor"),
98  fSimLabel(p.get<art::InputTag>("SimLabel")),
99  /*fScintillationYield(p.get<double>("ScintillationYield")),
100  fQuantumEff(p.get<double>("QuantumEff")),
101  fDummyGain(p.get<double>("DummyGain")),*/
102  fGeVToADC(p.get<double>("GeVToADC")),
103  fIntegrationTime(p.get<time>("IntegrationTime")),
104  fReadoutWindowSize(p.get<size_t>("ReadoutWindowSize")),
105  fDeadtime(p.get<size_t>("Deadtime")),
106  fDACThreshold(p.get<adc_t>("DACThreshold"))
107 {
108  produces<std::vector<CRT::Trigger>>();
109  produces<art::Assns<simb::MCParticle,CRT::Trigger>>();
110 
111 }
112 
113 
115 {
116 
117 
118  //const auto & crtHits = e.getValidHandle<std::vector<sim::AuxDetHit>>("largeant");
119  auto const allSims = e.getMany<sim::AuxDetHitCollection>();
120 
121  // -- Get all MCParticles to do assns later
122  const auto & mcp_handle = e.getValidHandle<std::vector<simb::MCParticle>>("largeant"); // -- TODO: make this an input tag
123  art::PtrMaker<simb::MCParticle> makeMCParticlePtr{e,mcp_handle.id()};
125  auto const & mcparticles = *(mcp_handle); //dereference the handle
126 
127  // -- Construct map of trackId to MCParticle handle index to do assns later
128  std::unordered_map<int, int> map_trackID_to_handle_index;
129  for (size_t idx = 0; idx < mcparticles.size(); ++idx){
130  int tid = mcparticles[idx].TrackId();
131  map_trackID_to_handle_index.insert(std::make_pair(tid,idx));
132  }
133 
134  auto trigCol = std::make_unique<std::vector<CRT::Trigger>>();
135 
136  std::unique_ptr< art::Assns<simb::MCParticle, CRT::Trigger>> partToTrigger( new art::Assns<simb::MCParticle, CRT::Trigger>);
137 
138  art::PtrMaker<CRT::Trigger> makeTrigPtr(e);
139 
140 
142  std::map<int, std::map<time, std::vector<std::pair<CRT::Hit, int>>>> crtHitsModuleMap;
143  for(auto const& auxHits : allSims){
144  for(const auto & eDep: * auxHits)
145  {
146  const size_t tAvg = eDep.GetEntryT();
147  crtHitsModuleMap[(eDep.GetID())/64][tAvg/fIntegrationTime].emplace_back(CRT::Hit((eDep.GetID())%64, eDep.GetEnergyDeposited()*0.001f*fGeVToADC),eDep.GetTrackID());
148  mf::LogDebug("TrueTimes") << "Assigned true hit at time " << tAvg << " to bin " << tAvg/fIntegrationTime << ".\n";
149  }
150  }
151 
152 
153  // -- For each CRT module
154  for(const auto & crtHitsMappedByModule : crtHitsModuleMap)
155  {
156  int crtChannel = -1;
157  int module = crtHitsMappedByModule.first;
158 
159  std::string modStrng="U"+std::to_string(module+1);
160  if (module>15) modStrng="D"+std::to_string(module+1-16);
161  if ((module+1)==17) crtChannel=22;
162  if ((module+1)==1) crtChannel=24;
163  for (int i=0; i<32; ++i){
164  if (crtChannel==22 || crtChannel==24) break;
165  const auto& det = geom->AuxDet(i);
166  if(det.Name().find(modStrng) != std::string::npos){
167  crtChannel=i; break;
168  }
169  }
170  const auto crtHitsMappedByTime = crtHitsMappedByModule.second;
171 
172  mf::LogDebug("channels") << "Processing channel " << module << "\n";
173 
174  std::stringstream ss;
175 
176  mf::LogDebug("timeToHitTrackIds") << "Constructed readout windows for module " << crtChannel << ":\n"
177  << ss.str() << "\n";
178 
179 
180 
181  auto lastTimeStamp=time(0);
182  int i=0;
183  for(auto window : crtHitsMappedByTime)
184  {
185  if (i!=0 && (time(fDeadtime)+lastTimeStamp)>window.first && lastTimeStamp<window.first) continue;
186  i++;
187  const auto& hitsInWindow = window.second;
188  const auto aboveThresh = std::find_if(hitsInWindow.begin(), hitsInWindow.end(),
189  [this](const auto& hitPair) { return hitPair.first.ADC() > fDACThreshold; });
190 
191 
192 if(aboveThresh != hitsInWindow.end()){
193 
194  std::vector<CRT::Hit> hits;
195  std::set<int> trkIDCheck;
196  const time timestamp = window.first; //Set timestamp before window is changed.
197  const time end = (timestamp+fReadoutWindowSize);
198  std::set<uint32_t> channelBusy; //A std::set contains only unique elements. This set stores channels that have already been read out in
199  //this readout window and so are "busy" and cannot contribute any more hits.
200  for(auto busyCheckWindow : crtHitsMappedByTime ){
201  if (time(busyCheckWindow.first)<timestamp || time(busyCheckWindow.first)>end) continue;
202  for(const auto& hitPair: window.second){
203  const auto channel = hitPair.first.Channel(); //TODO: Get channel number back without needing art::Ptr here.
204  // Maybe store crt::Hits.
205  if(channelBusy.insert(channel).second){
206  hits.push_back(hitPair.first);
207 
208 
209  if (hitPair.first.ADC()>fDACThreshold){
210 
211  int tid = hitPair.second;
212 
213 
214 
215  trkIDCheck.insert(tid);
216 
217 
218  }
219  }
220  }
221  }
222 
223  for (int tid : trkIDCheck){
224  // -- safe index retrieval
225  int index = 0;
226  auto search = map_trackID_to_handle_index.find(tid);
227  if (search != map_trackID_to_handle_index.end()){
228  index = search->second;
229  mf::LogDebug("GetAssns") << "Found index : " << index;
230  } else {
231  mf::LogDebug("GetAssns") << "No matching index... strange";
232  continue;
233  }
234 
235  // -- Sanity check, not needed
236  simb::MCParticle particle = mcparticles[index];
237  mf::LogDebug("GetAssns") << "TrackId from particle obtained with index " << index
238  << " is : " << particle.TrackId() << " , expected: " << tid;
239  mf::LogDebug("GetMCParticle") << particle;
240 
241  auto const mcptr = makeMCParticlePtr(index);
242  partToTrigger->addSingle(mcptr, makeTrigPtr(trigCol->size()-1));
243 
244  }
245  //std::cout<<"Hits Generated:"<<hits.size()<<std::endl;
246  lastTimeStamp=window.first;
247 
248  MF_LOG_DEBUG("CreateTrigger") << "Creating CRT::Trigger...\n";
249  trigCol->emplace_back(crtChannel, timestamp*fIntegrationTime, std::move(hits));
250  } // For each readout with a triggerable hit
251  } // For each time window
252 
253  } //For each CRT module
254 
255  // -- Put Triggers and Assns into the event
256  mf::LogDebug("CreateTrigger") << "Putting " << trigCol->size() << " CRT::Triggers into the event at the end of analyze().\n";
257  e.put(std::move(trigCol));
258  e.put(std::move(partToTrigger));
259 
260 }
261 
262 
263 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void produce(art::Event &e) override
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
uint8_t channel
Definition: CRTFragment.hh:201
Particle class.
CRTSimRefac & operator=(CRTSimRefac const &)=delete
art framework interface to geometry description
int TrackId() const
Definition: MCParticle.h:210
unsigned short adc_t
art::InputTag fSimLabel
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
CRTSimRefac(fhicl::ParameterSet const &p)
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
Definition: DataViewImpl.h:479
def move(depos, offset)
Definition: depos.py:107
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
Definition: search.py:1
AuxDetGeo const & AuxDet(unsigned int const ad=0) const
Returns the specified auxiliary detector.
std::vector< AuxDetHit > AuxDetHitCollection
Definition: AuxDetHit.h:183
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
#define MF_LOG_DEBUG(id)
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34