PointIdTrainingData_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Class: PointIdTrainingData
3 // Author: P.Plonski, R.Sulej (Robert.Sulej@cern.ch), D.Stefan, May 2016
4 //
5 // Training data for PointIdAlg
6 //
7 // We use this to dump deconv. ADC for preparation of various classifiers.
8 //
9 ////////////////////////////////////////////////////////////////////////////////////////////////////
10 
16 
17 // Framework includes
21 #include "art_root_io/TFileService.h"
24 #include "fhiclcpp/types/Atom.h"
26 #include "fhiclcpp/types/Table.h"
28 
29 // C++ Includes
30 #include <cmath>
31 #include <fstream>
32 #include <string>
33 
34 #include "TH2F.h" // ADC and deposit maps
35 #include "TH2I.h" // PDG+vertex info map
36 
37 namespace {
38  template <typename Hist>
39  void writeAndDelete(Hist*& hist) {
40  if (!hist) return;
41  hist->Write();
42  delete hist;
43  hist = nullptr;
44  } // writeAndDelete()
45 } // local namespace
46 
47 
48 namespace nnet {
49 
51  public:
52  struct Config {
53  using Name = fhicl::Name;
55 
57 
59  Comment("Text files with all needed data dumped.")};
60 
62  Name("DumpToRoot"),
63  Comment("Dump to ROOT histogram file (replaces the text files)")};
64 
66  Name("SelectedTPC"),
67  Comment("use selected views only, or all views if empty list")};
68 
70  Name("SelectedView"),
71  Comment("use selected tpc's only, or all tpc's if empty list")};
72 
74  Comment("Crop the projection to the event region plus margin")};
75  };
77 
78  explicit PointIdTrainingData(Parameters const& config);
79 
80  private:
81  void analyze(const art::Event& event) override;
82 
84 
87 
88  std::vector<int> fSelectedTPC;
89  std::vector<int> fSelectedPlane;
90 
91  int fEvent; /// number of the event being processed
92  int fRun; /// number of the run being processed
93  int fSubRun; /// number of the sub-run being processed
94 
95  bool fCrop; /// crop data to event (set to false when dumping noise!)
96 
98 
99  };
100 
101  //-----------------------------------------------------------------------
103  : art::EDAnalyzer(config)
104  , fTrainingDataAlg(config().TrainingDataAlg())
105  , fOutTextFilePath(config().OutTextFilePath())
106  , fDumpToRoot(config().DumpToRoot())
107  , fSelectedTPC(config().SelectedTPC())
108  , fSelectedPlane(config().SelectedView())
109  , fCrop(config().Crop())
110  {
112 
113  const size_t TPC_CNT = (size_t)fGeometry->NTPC(0);
114  if (fSelectedTPC.empty()) {
115  for (size_t tpc = 0; tpc < TPC_CNT; ++tpc)
116  fSelectedTPC.push_back(tpc);
117  }
118 
119  if (fSelectedPlane.empty()) {
120  for (size_t p = 0; p < fGeometry->MaxPlanes(); ++p)
121  fSelectedPlane.push_back(p);
122  }
123  }
124 
125  //-----------------------------------------------------------------------
126  void
128  {
129  fEvent = event.id().event();
130  fRun = event.run();
131  fSubRun = event.subRun();
132 
133  bool saveSim = fTrainingDataAlg.saveSimInfo() && !event.isRealData();
134 
135  std::ostringstream os;
136  os << "event_" << fEvent << "_run_" << fRun << "_subrun_" << fSubRun;
137 
138  std::cout << "analyze " << os.str() << std::endl;
139 
140  auto const clockData =
142  auto const detProp =
144 
145  for (size_t i = 0; i < fSelectedTPC.size(); ++i)
146  for (size_t v = 0; v < fSelectedPlane.size(); ++v) {
148  event, clockData, detProp, fSelectedPlane[v], fSelectedTPC[i], 0);
149 
150  unsigned int w0, w1, d0, d1;
151  if (fCrop && saveSim) {
152  if (fTrainingDataAlg.findCrop(0.004F, w0, w1, d0, d1)) {
153  std::cout << " crop: " << w0 << " " << w1 << " " << d0 << " " << d1 << std::endl;
154  }
155  else {
156  std::cout << " skip empty tpc:" << fSelectedTPC[i] << " / view:" << fSelectedPlane[v]
157  << std::endl;
158  continue;
159  }
160  }
161  else {
162  w0 = 0;
163  w1 = fTrainingDataAlg.NWires();
164  d0 = 0;
166  }
167 
168  if (fDumpToRoot) {
169  std::ostringstream ss1;
170  ss1 << "raw_" << os.str() << "_tpc_" << fSelectedTPC[i] << "_view_"
171  << fSelectedPlane[v]; // TH2's name
172 
174  TH2F* rawHist =
175  tfs->make<TH2F>((ss1.str() + "_raw").c_str(), "ADC", w1 - w0, w0, w1, d1 - d0, d0, d1);
176  TH2F* depHist = 0;
177  TH2I* pdgHist = 0;
178  if (saveSim) {
179  depHist = tfs->make<TH2F>(
180  (ss1.str() + "_deposit").c_str(), "Deposit", w1 - w0, w0, w1, d1 - d0, d0, d1);
181  pdgHist = tfs->make<TH2I>(
182  (ss1.str() + "_pdg").c_str(), "PDG", w1 - w0, w0, w1, d1 - d0, d0, d1);
183  }
184 
185  for (size_t w = w0; w < w1; ++w) {
186  auto const& raw = fTrainingDataAlg.wireData(w);
187  for (size_t d = d0; d < d1; ++d) {
188  rawHist->Fill(w, d, raw[d]);
189  }
190 
191  if (saveSim) {
192  auto const& edep = fTrainingDataAlg.wireEdep(w);
193  for (size_t d = d0; d < d1; ++d) {
194  depHist->Fill(w, d, edep[d]);
195  }
196 
197  auto const& pdg = fTrainingDataAlg.wirePdg(w);
198  for (size_t d = d0; d < d1; ++d) {
199  pdgHist->Fill(w, d, pdg[d]);
200  }
201  }
202  }
203 
204  writeAndDelete(rawHist);
205  writeAndDelete(depHist);
206  writeAndDelete(pdgHist);
207 
208  }
209  else {
210  std::ostringstream ss1;
211  ss1 << fOutTextFilePath << "/raw_" << os.str() << "_tpc_" << fSelectedTPC[i] << "_view_"
212  << fSelectedPlane[v];
213 
214  std::ofstream fout_raw, fout_deposit, fout_pdg;
215 
216  fout_raw.open(ss1.str() + ".raw");
217  if (saveSim) {
218  fout_deposit.open(ss1.str() + ".deposit");
219  fout_pdg.open(ss1.str() + ".pdg");
220  }
221 
222  for (size_t w = w0; w < w1; ++w) {
223  auto const& raw = fTrainingDataAlg.wireData(w);
224  for (size_t d = d0; d < d1; ++d) {
225  fout_raw << raw[d] << " ";
226  }
227  fout_raw << std::endl;
228 
229  if (saveSim) {
230  auto const& edep = fTrainingDataAlg.wireEdep(w);
231  for (size_t d = d0; d < d1; ++d) {
232  fout_deposit << edep[d] << " ";
233  }
234  fout_deposit << std::endl;
235 
236  auto const& pdg = fTrainingDataAlg.wirePdg(w);
237  for (size_t d = d0; d < d1; ++d) {
238  fout_pdg << pdg[d] << " ";
239  }
240  fout_pdg << std::endl;
241  }
242  }
243 
244  fout_raw.close();
245  if (saveSim) {
246  fout_deposit.close();
247  fout_pdg.close();
248  }
249  }
250  }
251 
252  } // PointIdTrainingData::analyze()
253 
255 
256 }
std::vector< float > const & wireEdep(size_t widx) const
Definition: PointIdAlg.h:298
bool saveSimInfo() const
Definition: PointIdAlg.h:265
std::string string
Definition: nybbler.cc:12
bool setEventData(const art::Event &event, detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, unsigned int plane, unsigned int tpc, unsigned int cryo)
Definition: PointIdAlg.cxx:859
ChannelGroupService::Name Name
Raw data description.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
art framework interface to geometry description
int fRun
number of the event being processed
unsigned int NWires() const
geo::GeometryCore const * fGeometry
crop data to event (set to false when dumping noise!)
unsigned int MaxPlanes() const
Returns the largest number of planes among all TPCs in this detector.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
p
Definition: test.py:223
nnet::TrainingDataAlg fTrainingDataAlg
bool fCrop
number of the sub-run being processed
bool findCrop(float max_e_cut, unsigned int &w0, unsigned int &w1, unsigned int &d0, unsigned int &d1) const
Description of geometry of one entire detector.
Definition of data types for geometry description.
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
int fSubRun
number of the run being processed
void analyze(const art::Event &event) override
#define Comment
PointIdTrainingData(Parameters const &config)
unsigned int NScaledDrifts() const
Access the description of detector geometry.
std::vector< int > const & wirePdg(size_t widx) const
Definition: PointIdAlg.h:303
QTextStream & endl(QTextStream &s)
Event finding and building.
std::vector< float > const & wireData(size_t widx) const