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 
14 
16 
17 // Framework includes
23 #include "art_root_io/TFileService.h"
25 #include "fhiclcpp/types/Atom.h"
26 #include "fhiclcpp/types/Table.h"
28 
29 // C++ Includes
30 #include <string>
31 #include <cmath>
32 #include <fstream>
33 
34 #include "TH2I.h" // PDG+vertex info map
35 #include "TH2F.h" // ADC and deposit maps
36 
37 namespace nnet {
38 
40  {
41  public:
42 
43  struct Config {
44  using Name = fhicl::Name;
46 
48  Name("TrainingDataAlg")
49  };
50 
52  Name("OutTextFilePath"),
53  Comment("Text files with all needed data dumped.")
54  };
55 
57  Name("DumpToRoot"),
58  Comment("Dump to ROOT histogram file (replaces the text files)")
59  };
60 
62  Name("SelectedTPC"),
63  Comment("use selected views only, or all views if empty list")
64  };
65 
67  Name("SelectedView"),
68  Comment("use selected tpc's only, or all tpc's if empty list")
69  };
70 
72  Name("Crop"),
73  Comment("Crop the projection to the event region plus margin")
74  };
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  //-----------------------------------------------------------------------
102  fTrainingDataAlg(config().TrainingDataAlg()),
103  fOutTextFilePath(config().OutTextFilePath()),
104  fDumpToRoot(config().DumpToRoot()),
105  fSelectedTPC(config().SelectedTPC()),
106  fSelectedPlane(config().SelectedView()),
107  fCrop(config().Crop())
108  {
110 
111  const size_t TPC_CNT = (size_t)fGeometry->NTPC(0);
112  if (fSelectedTPC.empty())
113  {
114  for (size_t tpc = 0; tpc < TPC_CNT; ++tpc)
115  fSelectedTPC.push_back(tpc);
116  }
117 
118  if (fSelectedPlane.empty())
119  {
120  for (size_t p = 0; p < fGeometry->MaxPlanes(); ++p )
121  fSelectedPlane.push_back(p);
122  }
123  }
124 
125  //-----------------------------------------------------------------------
127  {
128  fEvent = event.id().event();
129  fRun = event.run();
130  fSubRun = event.subRun();
131 
132  bool saveSim = fTrainingDataAlg.saveSimInfo() && !event.isRealData();
133 
134  std::ostringstream os;
135  os << "event_" << fEvent << "_run_" << fRun << "_subrun_" << fSubRun;
136 
137  std::cout << "analyze " << os.str() << std::endl;
138 
139  for (size_t i = 0; i < fSelectedTPC.size(); ++i)
140  for (size_t v = 0; v < fSelectedPlane.size(); ++v)
141  {
143 
144  unsigned int w0, w1, d0, d1;
145  if (fCrop && saveSim)
146  {
147  if (fTrainingDataAlg.findCrop(0.004F, w0, w1, d0, d1))
148  {
149  std::cout << " crop: " << w0 << " " << w1 << " " << d0 << " " << d1 << std::endl;
150  }
151  else
152  {
153  std::cout << " skip empty tpc:" << fSelectedTPC[i] << " / view:" << fSelectedPlane[v] << std::endl;
154  continue;
155  }
156  }
157  else
158  {
159  w0 = 0; w1 = fTrainingDataAlg.NWires();
160  d0 = 0; d1 = fTrainingDataAlg.NScaledDrifts();
161  }
162 
163  if (fDumpToRoot)
164  {
165  std::ostringstream ss1;
166  ss1 << "raw_" << os.str() << "_tpc_" << fSelectedTPC[i] << "_view_" << fSelectedPlane[v]; // TH2's name
167 
169  TH2F* rawHist = tfs->make<TH2F>((ss1.str() + "_raw").c_str(), "ADC", w1 - w0, w0, w1, d1 - d0, d0, d1);
170  TH2F* depHist = 0;
171  TH2I* pdgHist = 0;
172  if (saveSim)
173  {
174  depHist = tfs->make<TH2F>((ss1.str() + "_deposit").c_str(), "Deposit", w1 - w0, w0, w1, d1 - d0, d0, d1);
175  pdgHist = tfs->make<TH2I>((ss1.str() + "_pdg").c_str(), "PDG", w1 - w0, w0, w1, d1 - d0, d0, d1);
176  }
177 
178  for (size_t w = w0; w < w1; ++w)
179  {
180  auto const & raw = fTrainingDataAlg.wireData(w);
181  for (size_t d = d0; d < d1; ++d) { rawHist->Fill(w, d, raw[d]); }
182 
183  if (saveSim)
184  {
185  auto const & edep = fTrainingDataAlg.wireEdep(w);
186  for (size_t d = d0; d < d1; ++d) { depHist->Fill(w, d, edep[d]); }
187 
188  auto const & pdg = fTrainingDataAlg.wirePdg(w);
189  for (size_t d = d0; d < d1; ++d) { pdgHist->Fill(w, d, pdg[d]); }
190  }
191  }
192  }
193  else
194  {
195  std::ostringstream ss1;
196  ss1 << fOutTextFilePath << "/raw_" << os.str()
197  << "_tpc_" << fSelectedTPC[i] << "_view_" << fSelectedPlane[v];
198 
199  std::ofstream fout_raw, fout_deposit, fout_pdg;
200 
201  fout_raw.open(ss1.str() + ".raw");
202  if (saveSim)
203  {
204  fout_deposit.open(ss1.str() + ".deposit");
205  fout_pdg.open(ss1.str() + ".pdg");
206  }
207 
208  for (size_t w = w0; w < w1; ++w)
209  {
210  auto const & raw = fTrainingDataAlg.wireData(w);
211  for (size_t d = d0; d < d1; ++d) { fout_raw << raw[d] << " "; }
212  fout_raw << std::endl;
213 
214  if (saveSim)
215  {
216  auto const & edep = fTrainingDataAlg.wireEdep(w);
217  for (size_t d = d0; d < d1; ++d) { fout_deposit << edep[d] << " "; }
218  fout_deposit << std::endl;
219 
220  auto const & pdg = fTrainingDataAlg.wirePdg(w);
221  for (size_t d = d0; d < d1; ++d) { fout_pdg << pdg[d] << " "; }
222  fout_pdg << std::endl;
223  }
224  }
225 
226  fout_raw.close();
227  if (saveSim)
228  {
229  fout_deposit.close();
230  fout_pdg.close();
231  }
232  }
233  }
234 
235  } // PointIdTrainingData::analyze()
236 
238 
239 }
std::vector< float > const & wireEdep(size_t widx) const
Definition: PointIdAlg.h:277
bool saveSimInfo() const
Definition: PointIdAlg.h:265
std::string string
Definition: nybbler.cc:12
unsigned int NScaledDrifts(void) const
bool setEventData(const art::Event &event, unsigned int plane, unsigned int tpc, unsigned int cryo)
Definition: PointIdAlg.cxx:918
Raw data description.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:27
unsigned int NWires(void) const
int fRun
number of the event being processed
Access the description of detector geometry.
unsigned int MaxPlanes() const
Returns the largest number of planes among all TPCs in this detector.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:69
static Config * config
Definition: config.cpp:1054
nnet::TrainingDataAlg fTrainingDataAlg
geo::GeometryCore const * fGeometry
crop data to event (set to false when dumping noise!)
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.
p
Definition: test.py:223
AdcCodeMitigator::Name Name
int fSubRun
number of the run being processed
void analyze(const art::Event &event) override
#define Comment
PointIdTrainingData(Parameters const &config)
std::vector< int > const & wirePdg(size_t widx) const
Definition: PointIdAlg.h:278
QTextStream & endl(QTextStream &s)
Event finding and building.
std::vector< float > const & wireData(size_t widx) const