Public Member Functions | Private Attributes | List of all members
Infill::InfillChannels Class Reference
Inheritance diagram for Infill::InfillChannels:
art::EDProducer art::detail::Producer art::detail::LegacyModule art::Modifier art::ModuleBase art::ProductRegistryHelper

Public Member Functions

 InfillChannels (fhicl::ParameterSet const &p)
 
 InfillChannels (InfillChannels const &)=delete
 
 InfillChannels (InfillChannels &&)=delete
 
InfillChannelsoperator= (InfillChannels const &)=delete
 
InfillChannelsoperator= (InfillChannels &&)=delete
 
void produce (art::Event &e) override
 
void beginJob () override
 
void endJob () override
 
- Public Member Functions inherited from art::EDProducer
 EDProducer (fhicl::ParameterSet const &pset)
 
template<typename Config >
 EDProducer (Table< Config > const &config)
 
std::string workerType () const
 
- Public Member Functions inherited from art::detail::Producer
virtual ~Producer () noexcept
 
 Producer (fhicl::ParameterSet const &)
 
 Producer (Producer const &)=delete
 
 Producer (Producer &&)=delete
 
Produceroperator= (Producer const &)=delete
 
Produceroperator= (Producer &&)=delete
 
void doBeginJob (SharedResources const &resources)
 
void doEndJob ()
 
void doRespondToOpenInputFile (FileBlock const &fb)
 
void doRespondToCloseInputFile (FileBlock const &fb)
 
void doRespondToOpenOutputFiles (FileBlock const &fb)
 
void doRespondToCloseOutputFiles (FileBlock const &fb)
 
bool doBeginRun (RunPrincipal &rp, ModuleContext const &mc)
 
bool doEndRun (RunPrincipal &rp, ModuleContext const &mc)
 
bool doBeginSubRun (SubRunPrincipal &srp, ModuleContext const &mc)
 
bool doEndSubRun (SubRunPrincipal &srp, ModuleContext const &mc)
 
bool doEvent (EventPrincipal &ep, ModuleContext const &mc, std::atomic< std::size_t > &counts_run, std::atomic< std::size_t > &counts_passed, std::atomic< std::size_t > &counts_failed)
 
- Public Member Functions inherited from art::Modifier
 ~Modifier () noexcept
 
 Modifier ()
 
 Modifier (Modifier const &)=delete
 
 Modifier (Modifier &&)=delete
 
Modifieroperator= (Modifier const &)=delete
 
Modifieroperator= (Modifier &&)=delete
 
- Public Member Functions inherited from art::ModuleBase
virtual ~ModuleBase () noexcept
 
 ModuleBase ()
 
ModuleDescription const & moduleDescription () const
 
void setModuleDescription (ModuleDescription const &)
 
std::array< std::vector< ProductInfo >, NumBranchTypes > const & getConsumables () const
 
void sortConsumables (std::string const &current_process_name)
 
template<typename T , BranchType BT>
ViewToken< T > consumesView (InputTag const &tag)
 
template<typename T , BranchType BT>
ViewToken< T > mayConsumeView (InputTag const &tag)
 

Private Attributes

const geo::GeometryCorefGeom
 
std::set< raw::ChannelID_tfBadChannels
 
std::set< raw::ChannelID_tfNoisyChannels
 
std::set< raw::ChannelID_tfDeadChannels
 
std::set< readout::ROPIDfActiveRops
 
const std::string fNetworkPath
 
const std::string fNetworkNameInduction
 
const std::string fNetworkNameCollection
 
torch::jit::script::Module fInductionModule
 
torch::jit::script::Module fCollectionModule
 
const std::string fInputLabel
 

Additional Inherited Members

- Public Types inherited from art::EDProducer
using ModuleType = EDProducer
 
using WorkerType = WorkerT< EDProducer >
 
- Public Types inherited from art::detail::Producer
template<typename UserConfig , typename KeysToIgnore = void>
using Table = Modifier::Table< UserConfig, KeysToIgnore >
 
- Public Types inherited from art::Modifier
template<typename UserConfig , typename UserKeysToIgnore = void>
using Table = ProducerTable< UserConfig, detail::ModuleConfig, UserKeysToIgnore >
 
- Static Public Member Functions inherited from art::EDProducer
static void commitEvent (EventPrincipal &ep, Event &e)
 
- Protected Member Functions inherited from art::ModuleBase
ConsumesCollectorconsumesCollector ()
 
template<typename T , BranchType = InEvent>
ProductToken< T > consumes (InputTag const &)
 
template<typename Element , BranchType = InEvent>
ViewToken< Element > consumesView (InputTag const &)
 
template<typename T , BranchType = InEvent>
void consumesMany ()
 
template<typename T , BranchType = InEvent>
ProductToken< T > mayConsume (InputTag const &)
 
template<typename Element , BranchType = InEvent>
ViewToken< Element > mayConsumeView (InputTag const &)
 
template<typename T , BranchType = InEvent>
void mayConsumeMany ()
 

Detailed Description

Definition at line 43 of file InfillChannels_module.cc.

Constructor & Destructor Documentation

Infill::InfillChannels::InfillChannels ( fhicl::ParameterSet const &  p)
explicit

Definition at line 79 of file InfillChannels_module.cc.

80  : EDProducer{p},
81  fNetworkPath (p.get<std::string> ("NetworkPath")),
82  fNetworkNameInduction (p.get<std::string> ("NetworkNameInduction")),
83  fNetworkNameCollection (p.get<std::string> ("NetworkNameCollection")),
84  fInputLabel (p.get<std::string> ("InputLabel"))
85 {
86  consumes<std::vector<raw::RawDigit>>(fInputLabel);
87 
88  produces<std::vector<raw::RawDigit>>();
89 }
const std::string fNetworkNameCollection
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
p
Definition: test.py:223
const std::string fNetworkPath
const std::string fNetworkNameInduction
Infill::InfillChannels::InfillChannels ( InfillChannels const &  )
delete
Infill::InfillChannels::InfillChannels ( InfillChannels &&  )
delete

Member Function Documentation

void Infill::InfillChannels::beginJob ( )
overridevirtual

Reimplemented from art::EDProducer.

Definition at line 178 of file InfillChannels_module.cc.

179 {
181 
182  // Dead channels = bad channels + noisy channels
185 
186  std::merge(
187  fBadChannels.begin(), fBadChannels.end(), fNoisyChannels.begin(), fNoisyChannels.end(),
188  std::inserter(fDeadChannels, fDeadChannels.begin())
189  );
190 
191  // Get active ROPs (not facing a wall and has dead channels)
194  for (iRop = rBegin; iRop != rEnd; ++iRop) { // Iterate over ROPs in the detector
195  bool hasDeadCh = false;
196  for (raw::ChannelID_t ch : fDeadChannels) {
197  if (fGeom->ChannelToROP(ch) == *iRop) {
198  hasDeadCh = true;
199  break;
200  }
201  }
202  if (!hasDeadCh) continue; // Don't need to infill ROPs without dead channels
203 
204  for (const geo::TPCID tpcId : fGeom->ROPtoTPCs(*iRop)) {
205  const geo::TPCGeo tpc = fGeom->TPC(tpcId);
206  const TGeoVolume* tpcVol = tpc.ActiveVolume();
207 
208  if (tpcVol->Capacity() > 1000000) { // At least one of the ROP's TPCIDs needs to be active
209  // Networks expect a fixed image size
210  if(fGeom->SignalType(*iRop) == geo::kInduction && fGeom->Nchannels(*iRop) > 800) {
211  std::cerr << "InfillChannels_module.cc: Induction view network cannot handle more then 800 channels\n";
212  std::abort();
213  }
214  if(fGeom->SignalType(*iRop) == geo::kCollection && fGeom->Nchannels(*iRop) > 480) {
215  std::cerr << "InfillChannels_module.cc: Collection view network cannot handle more then 400 channels\n";
216  std::abort();
217  }
218 
219  fActiveRops.insert(*iRop);
220  break;
221  }
222  }
223  }
224 
225  // Check dead channels resemble the dead channels used for training
226  raw::ChannelID_t chGap = 1;
227  for (const raw::ChannelID_t ch : fDeadChannels) {
228  if (fDeadChannels.count(ch + 1)) {
229  ++chGap;
230  continue;
231  }
232  if (fGeom->ChannelToROP(ch - chGap) == fGeom->ChannelToROP(ch + 1)) {
233  if (fGeom->SignalType(ch) == geo::kCollection && chGap > 3) {
234  std::cerr << "There are dead channel gap larger than what was seen in training --- ";
235  std::cerr << "**Consider retraining collection plane infill network**" << std::endl;
236  }
237  else if (fGeom->SignalType(ch) == geo::kInduction && chGap > 2) {
238  std::cerr << "There are dead channel gap larger than what was seen in training --- ";
239  std::cerr << "**Consider retraining induction plane infill network**" << std::endl;
240  }
241  }
242  chGap = 1;
243  }
244 
245  // Load torchscripts
246  std::cout << "Loading modules..." << std::endl;
247  const char* networkPath = std::getenv(fNetworkPath.c_str());
248  if (networkPath == nullptr) {
249  std::cerr << "InfillChannels_module.cc: Environment variable " << fNetworkPath << " was not found";
250  std::abort();
251  }
252  const std::string networkLocInduction = std::string(networkPath) + "/" + fNetworkNameInduction;
253  const std::string networkLocCollection = std::string(networkPath) + "/" + fNetworkNameCollection;
254 
255  try {
256  fInductionModule = torch::jit::load(networkLocInduction);
257  std::cout << "Induction module loaded from " << networkLocInduction <<std::endl;
258  }
259  catch (const c10::Error& err) {
260  std::cerr << "error loading the model\n";
261  std::cerr << err.what();
262  }
263  try {
264  fCollectionModule = torch::jit::load(networkLocCollection);
265  std::cout << "Collection module loaded from " << networkLocCollection << std::endl;
266  }
267  catch (const c10::Error& err) {
268  std::cerr << "error loading the model\n";
269  std::cerr << err.what();
270  }
271 }
std::vector< geo::TPCID > ROPtoTPCs(readout::ROPID const &ropid) const
Returns a list of ID of TPCs the specified ROP spans.
std::set< raw::ChannelID_t > fDeadChannels
const std::string fNetworkNameCollection
std::set< raw::ChannelID_t > fBadChannels
static constexpr BeginPos_t begin_pos
Definition: GeometryCore.h:107
std::string string
Definition: nybbler.cc:12
Base forward iterator browsing all readout plane IDs in the detector.
Geometry information for a single TPC.
Definition: TPCGeo.h:38
static constexpr EndPos_t end_pos
Definition: GeometryCore.h:108
virtual const provider_type * provider() const override
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
std::set< raw::ChannelID_t > fNoisyChannels
const geo::GeometryCore * fGeom
Signal from induction planes.
Definition: geo_types.h:145
nvidia::inferenceserver::client::Error Error
Definition: triton_utils.h:15
std::string getenv(std::string const &name)
Definition: getenv.cc:15
const TGeoVolume * ActiveVolume() const
Definition: TPCGeo.h:119
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
void err(const char *fmt,...)
Definition: message.cpp:226
readout::ROPID ChannelToROP(raw::ChannelID_t channel) const
const std::string fNetworkPath
const std::string fNetworkNameInduction
torch::jit::script::Module fInductionModule
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
torch::jit::script::Module fCollectionModule
QTextStream & endl(QTextStream &s)
Signal from collection planes.
Definition: geo_types.h:146
def load(filename, jpath="depos")
Definition: depos.py:34
std::set< readout::ROPID > fActiveRops
void Infill::InfillChannels::endJob ( )
overridevirtual

Reimplemented from art::EDProducer.

Definition at line 273 of file InfillChannels_module.cc.

274 {
275  // Implementation of optional member function here.
276 }
InfillChannels& Infill::InfillChannels::operator= ( InfillChannels const &  )
delete
InfillChannels& Infill::InfillChannels::operator= ( InfillChannels &&  )
delete
void Infill::InfillChannels::produce ( art::Event e)
overridevirtual

Implements art::EDProducer.

Definition at line 91 of file InfillChannels_module.cc.

92 {
93  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService>()->DataFor(e);
94  // Networks expect a fixed image size
95  if (detProp.NumberTimeSamples() > 6000) {
96  std::cerr << "InfillChannels_module.cc: Networks cannot handle more than 6000 time ticks\n";
97  std::abort();
98  }
99 
100  typedef std::array<short, 6000> vecAdc;
101  std::map<raw::ChannelID_t, vecAdc> infilledAdcs;
102  torch::Tensor maskedRopTensor;
103  torch::Tensor infilledRopTensor;
104 
105  auto digs = e.getHandle<std::vector<raw::RawDigit> >(fInputLabel);
106 
107  // Get infilled adc ROP by ROP
108  for (const readout::ROPID& currentRop : fActiveRops) {
109  maskedRopTensor = torch::zeros(
110  {1, 1 ,6000, fGeom->Nchannels(currentRop)}, torch::dtype(torch::kFloat32).device(torch::kCPU).requires_grad(false)
111  );
112  auto maskedRopTensorAccess = maskedRopTensor.accessor<float, 4>();
113 
114  const raw::ChannelID_t firstCh = fGeom->FirstChannelInROP(currentRop);
115 
116  // Fill ROP image
117  for (const raw::RawDigit& dig : *digs) {
118  if (fDeadChannels.count(dig.Channel())) continue;
119 
120  readout::ROPID rop = fGeom->ChannelToROP(dig.Channel());
121  if (rop != currentRop) continue;
122 
123  raw::RawDigit::ADCvector_t adcs(dig.Samples());
124  raw::Uncompress(dig.ADCs(), adcs, dig.Compression());
125 
126  for (unsigned int tick = 0; tick < adcs.size(); ++tick) {
127  const int adc = adcs[tick] ? int(adcs[tick]) - dig.GetPedestal() : 0;
128 
129  maskedRopTensorAccess[0][0][tick][dig.Channel() - firstCh] = adc;
130  }
131  }
132 
133  // Do the Infill
134  std::vector<torch::jit::IValue> inputs;
135  inputs.push_back(maskedRopTensor);
136  if (fGeom->SignalType(currentRop) == geo::kInduction) {
137  torch::NoGradGuard no_grad_guard;
138  infilledRopTensor = fInductionModule.forward(inputs).toTensor().detach();
139  }
140  else if (fGeom->SignalType(currentRop) == geo::kCollection) {
141  torch::NoGradGuard no_grad_guard;
142  infilledRopTensor = fCollectionModule.forward(inputs).toTensor().detach();
143  }
144 
145  // Store infilled ADC of dead channels
146  auto infilledRopTensorAccess = infilledRopTensor.accessor<float, 4>();
147  for (const raw::ChannelID_t ch : fDeadChannels) {
148  if (fGeom->ChannelToROP(ch) == currentRop) {
149  for (unsigned int tick = 0; tick < detProp.NumberTimeSamples(); ++tick) {
150  infilledAdcs[ch][tick] = (short)std::round(infilledRopTensorAccess[0][0][tick][ch - firstCh]);
151  }
152  }
153  } // Could break early if ch > last ch in currentRop to save a bit of looping?
154  }
155 
156  // Encode infilled ADC into RawDigit and put back onto event
157  auto infilledDigs = std::make_unique<std::vector<raw::RawDigit>>();
158  *infilledDigs = *digs;
159  for (raw::RawDigit& dig : *infilledDigs) {
160  if (infilledAdcs.count(dig.Channel())) {
161  raw::RawDigit::ADCvector_t infilledAdc(
162  infilledAdcs[dig.Channel()].begin(), (infilledAdcs[dig.Channel()].begin() + detProp.NumberTimeSamples())
163  );
164 
165  // Get new pedestal
166  auto infilledAdcMin = std::min_element(infilledAdc.begin(), infilledAdc.end());
167  short ped = *infilledAdcMin < 0 ? std::abs(*infilledAdcMin) + 1 : 0;
168  for (short& adc : infilledAdc) adc += ped;
169 
170  raw::Compress(infilledAdc, dig.Compression()); // need to consider compression parameters
171  dig = raw::RawDigit(dig.Channel(), dig.Samples(), infilledAdc, dig.Compression());
172  dig.SetPedestal(ped);
173  }
174  }
175  e.put(std::move(infilledDigs));
176 }
std::set< raw::ChannelID_t > fDeadChannels
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:73
int16_t adc
Definition: CRTFragment.hh:202
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
T abs(T value)
const geo::GeometryCore * fGeom
Signal from induction planes.
Definition: geo_types.h:145
raw::ChannelID_t FirstChannelInROP(readout::ROPID const &ropid) const
Returns the ID of the first channel in the specified readout plane.
def move(depos, offset)
Definition: depos.py:107
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
Class identifying a set of planes sharing readout channels.
readout::ROPID ChannelToROP(raw::ChannelID_t channel) const
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
Definition: RawDigit.cxx:68
void Compress(std::vector< short > &adc, raw::Compress_t compress)
Compresses a raw data buffer.
Definition: raw.cxx:19
torch::jit::script::Module fInductionModule
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
torch::jit::script::Module fCollectionModule
Signal from collection planes.
Definition: geo_types.h:146
std::set< readout::ROPID > fActiveRops

Member Data Documentation

std::set<readout::ROPID> Infill::InfillChannels::fActiveRops
private

Definition at line 69 of file InfillChannels_module.cc.

std::set<raw::ChannelID_t> Infill::InfillChannels::fBadChannels
private

Definition at line 65 of file InfillChannels_module.cc.

torch::jit::script::Module Infill::InfillChannels::fCollectionModule
private

Definition at line 75 of file InfillChannels_module.cc.

std::set<raw::ChannelID_t> Infill::InfillChannels::fDeadChannels
private

Definition at line 67 of file InfillChannels_module.cc.

const geo::GeometryCore* Infill::InfillChannels::fGeom
private

Definition at line 63 of file InfillChannels_module.cc.

torch::jit::script::Module Infill::InfillChannels::fInductionModule
private

Definition at line 74 of file InfillChannels_module.cc.

const std::string Infill::InfillChannels::fInputLabel
private

Definition at line 76 of file InfillChannels_module.cc.

const std::string Infill::InfillChannels::fNetworkNameCollection
private

Definition at line 73 of file InfillChannels_module.cc.

const std::string Infill::InfillChannels::fNetworkNameInduction
private

Definition at line 72 of file InfillChannels_module.cc.

const std::string Infill::InfillChannels::fNetworkPath
private

Definition at line 71 of file InfillChannels_module.cc.

std::set<raw::ChannelID_t> Infill::InfillChannels::fNoisyChannels
private

Definition at line 66 of file InfillChannels_module.cc.


The documentation for this class was generated from the following file: