MakeInfillTrainingData_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: MakeInfillTrainingData
3 // Plugin Type: analyzer (art v3_05_01)
4 // File: MakeInfillTrainingData
5 //
6 // Generated at Wed Dec 9 11:34:56 2020 by Alexander Wilkinson using cetskelgen
7 // from cetlib version v3_10_00.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 
20 #include "art_root_io/TFileService.h"
21 #include "art_root_io/TFileDirectory.h"
25 #include "lardataobj/RawData/raw.h"
28 
29 #include <TH2F.h>
30 #include <TTree.h>
31 #include <TGeoVolume.h>
32 #include <TFile.h>
33 
34 #include <vector>
35 #include <string>
36 #include <map>
37 #include <algorithm>
38 #include <iterator>
39 #include <utility>
40 
41 namespace Infill {
42  class MakeInfillTrainingData;
43 }
44 
45 
47 public:
49  // The compiler-generated destructor is fine for non-base
50  // classes without bare pointers or other resource use.
51 
52  // Plugins should not be copied or assigned.
57 
58  // Required functions.
59  void analyze(art::Event const& e) override;
60 
61  // Selected optional functions.
62  void beginJob() override;
63  void endJob() override;
64 
65 private:
66  // Declare member data here.
68 
70 
71  std::set<raw::ChannelID_t> fBadChannels;
72  std::set<raw::ChannelID_t> fNoisyChannels;
73  std::set<raw::ChannelID_t> fDeadChannels;
74 
75  std::set<readout::ROPID> fActiveRops;
76 
78 };
79 
81  : EDAnalyzer{p},
82  fInputLabel (p.get<std::string> ("inputLabel"))
83 {
84  consumes<std::vector<raw::RawDigit>>(fInputLabel);
85 }
86 
88 {
89  std::string sEvent = (
90  "Ev" + std::to_string(e.id().event()) + "Run" + std::to_string(e.id().run()) +
91  "SRun" + std::to_string(e.id().subRun())
92  );
93  std::map<readout::ROPID, TH2F*> ropImages;
94 
95  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService>()->DataFor(e);
96  // Networks expect a fixed image size
97  if (detProp.NumberTimeSamples() != 6000) {
98  std::cerr << "MakeInfillTrainingData_module.cc: Training data should have 6000 time ticks\n";
99  std::abort();
100  }
101 
102  // Prepare TH2s
103  for (const readout::ROPID& rop : fActiveRops) {
104  std::string sTitle = (
105  sEvent + "_TPCset" + std::to_string(rop.TPCset) + "_ROP" + std::to_string(rop.ROP)
106  );
107  ropImages[rop] = tfs->make<TH2F>(
108  sTitle.c_str(), sTitle.c_str(), fGeom->Nchannels(rop), 0, fGeom->Nchannels(rop), 6000, 0, 6000
109  );
110  }
111 
112  auto digs = e.getHandle<std::vector<raw::RawDigit>>(fInputLabel);
113 
114  // Fill TH2s
115  for(const raw::RawDigit& dig : *digs){
116  readout::ROPID rop = fGeom->ChannelToROP(dig.Channel());
117  if(ropImages.count(rop)){ // Ignores ROPs associated with only inactive TPCIDs
118  raw::RawDigit::ADCvector_t adcs(dig.Samples());
119  // raw::Uncompress(dig.ADCs(), adcs, dig.GetPedestal(), dig.Compression());
120  raw::Uncompress(dig.ADCs(), adcs, dig.Compression());
121 
122  const raw::ChannelID_t firstCh = fGeom->FirstChannelInROP(rop);
123  for(unsigned int tick = 0; tick < adcs.size(); ++tick){
124  const int adc = adcs[tick] ? int(adcs[tick]) - dig.GetPedestal() : 0;
125 
126  ropImages[rop]->Fill(dig.Channel() - firstCh, tick, adc);
127  }
128  }
129  }
130 
131  tfs->file().Write();
132  tfs->file().Flush();
133  for(auto it: ropImages) delete it.second;
134 }
135 
137 {
139 
140  // Get active ROPs (not facing a wall)
143  for (iRop = rBegin; iRop != rEnd; ++iRop) { // Iterate over ROPs in detector
144  for (const geo::TPCID tpcId : fGeom->ROPtoTPCs(*iRop)) {
145  const geo::TPCGeo tpc = fGeom->TPC(tpcId);
146  const TGeoVolume* tpcVol = tpc.ActiveVolume();
147 
148  if (tpcVol->Capacity() > 1000000) { // At least one of the ROP's TPCIDs needs to be active
149  // Networks expect a fixed image size
150  if(fGeom->SignalType(*iRop) == geo::kInduction && fGeom->Nchannels(*iRop) != 800) {
151  std::cerr << "MakeInfillTrainingData_module.cc: Induction view training data should have 800 channels\n";
152  std::abort();
153  }
154  if(fGeom->SignalType(*iRop) == geo::kCollection && fGeom->Nchannels(*iRop) != 480) {
155  std::cerr << "MakeInfillTrainingData_module.cc: Collection view training data should have 480 channels\n";
156  std::abort();
157  }
158 
159  fActiveRops.insert(*iRop);
160  break;
161  }
162  }
163  }
164 }
165 
167 {
168  std::cout << "Getting dead channels..." << std::endl;
169 
172 
173  std::merge(
174  fBadChannels.begin(), fBadChannels.end(), fNoisyChannels.begin(), fNoisyChannels.end(),
175  std::inserter(fDeadChannels, fDeadChannels.begin())
176  );
177 
178  // Print dead channels to terminal
179  for(const readout::ROPID& rop : fActiveRops) {
180  std::cout << "(TPCset " << rop.TPCset << ", ROP " << rop.ROP << "): ";
181  const raw::ChannelID_t firstCh = fGeom->FirstChannelInROP(rop);
182  for (const raw::ChannelID_t ch : fDeadChannels) {
183  if (fGeom->ChannelToROP(ch) == rop) {
184  std::cout << ch - firstCh << " ";
185  }
186  } // Could break early if ch > last ch in currentRop to save a bit of looping?
187  std::cout << std::endl;
188  }
189 }
190 
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 > fNoisyChannels
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
static constexpr BeginPos_t begin_pos
Definition: GeometryCore.h:107
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
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
art::ServiceHandle< art::TFileService > tfs
static constexpr EndPos_t end_pos
Definition: GeometryCore.h:108
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:73
virtual const provider_type * provider() const override
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.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
RunNumber_t run() const
Definition: EventID.h:98
art framework interface to geometry description
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
MakeInfillTrainingData(fhicl::ParameterSet const &p)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
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.
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
p
Definition: test.py:223
const TGeoVolume * ActiveVolume() const
Definition: TPCGeo.h:119
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
Description of geometry of one entire detector.
Class identifying a set of planes sharing readout channels.
std::set< raw::ChannelID_t > fBadChannels
std::set< raw::ChannelID_t > fDeadChannels
readout::ROPID ChannelToROP(raw::ChannelID_t channel) const
void analyze(art::Event const &e) override
EventNumber_t event() const
Definition: EventID.h:116
Access the description of detector geometry.
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
Interface for experiment-specific service for channel quality info.
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
MakeInfillTrainingData & operator=(MakeInfillTrainingData const &)=delete
SubRunNumber_t subRun() const
Definition: EventID.h:110
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)
Signal from collection planes.
Definition: geo_types.h:146