GenFromLib_source.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file GenFromLib_source.cc
3 // \brief Reconstitute ART events from a LEM library
4 // \author Christopher Backhouse - bckhouse@caltech.edu
5 ////////////////////////////////////////////////////////////////////////
6 
7 // Framework includes
10 #include "fhiclcpp/ParameterSet.h"
12 
23 #include "fhiclcpp/ParameterSet.h"
25 
26 
27 #include "TFile.h"
28 #include "TTree.h"
29 
30 #include "Calibrator/Calibrator.h"
32 #include "Geometry/Geometry.h"
33 #include "RawData/RawDigit.h"
34 #include "RawData/RawTrigger.h"
35 #include "SummaryData/RunData.h"
36 #include "RecoBase/CellHit.h"
37 #include "LEM/func/EventSummary.h"
38 
39 namespace lem
40 {
41  /// Reconstitute ART events from a %LEM library
42  class GenFromLib
43  {
44  public:
45  GenFromLib(const fhicl::ParameterSet& pset,
47  const art::SourceHelper& pm);
48 
49  ~GenFromLib();
50 
51  void readFile(const std::string& fname,
52  art::FileBlock*& fb);
53 
54  bool readNext(const art::RunPrincipal* inR,
55  const art::SubRunPrincipal* inSR,
56  art::RunPrincipal*& outR,
57  art::SubRunPrincipal*& outSR,
58  art::EventPrincipal*& outE);
59 
60  void closeCurrentFile();
61 
62  void reconfigure(const fhicl::ParameterSet& pset);
63 
64  protected:
65  art::SourceHelper const& fSourceHelper; ///< Class to help with {Run, SubRun, Event}Principal construction.
66 
67  bool fOnce; ///< Flag that this is the first call to \ref readNext
68 
69  int fTreeIdx;
70  int fEvtNum;
71 
72  TFile* fFile;
73  TTree* fTree;
74  };
75 
76  //.......................................................................
79  const art::SourceHelper& pm)
80  : fSourceHelper(pm), fOnce(true), fEvtNum(1), fTree(0)
81  {
82  reconfigure(pset);
83 
84  help.reconstitutes<std::vector<rawdata::RawTrigger>, art::InEvent>("daq");
85  help.reconstitutes<std::vector<rawdata::RawDigit>, art::InEvent>("daq");
87 
88  // help.reconstitutes<lem::EventSummary, art::InEvent>("daq");
89  }
90 
91  //......................................................................
93  {
94  }
95 
96  //......................................................................
98  {
99  // We want to store exactly the values of the hits, only rescale them later
100  // when actually doing matching.
101  //
102  // NB: static nature of these means it's impossible to regen an event and
103  // then match it using different values here.
106  }
107 
108  //......................................................................
110  {
111  fFile = new TFile(fname.c_str());
112  if(fFile->IsZombie()) throw art::Exception(art::errors::FileOpenError);
113  fTree = (TTree*)fFile->Get("makelib/tree");
115  fTreeIdx = 0;
116 
118 
119  fb = new art::FileBlock(art::FileFormatVersion(1, "GenFromLib 12-08-10"),
120  fname);
121  }
122 
123  //......................................................................
125  const art::SubRunPrincipal* /*inSR*/,
126  art::RunPrincipal*& outR,
127  art::SubRunPrincipal*& outSR,
128  art::EventPrincipal*& outE)
129  {
130  const int tstamp = 0;
131 
132  if(fOnce){
133  fOnce = false;
134 
135  outR = fSourceHelper.makeRunPrincipal(1, tstamp);
136  outSR = fSourceHelper.makeSubRunPrincipal(1, 1, tstamp);
137 
138  // Need to write the detector into the file, because we're the first
140 
141  std::unique_ptr<sumdata::RunData> runcol(new sumdata::RunData(geom->DetId(),
142  geom->FileBaseName()));
143  art::put_product_in_principal(std::move(runcol), *outR, "daq");
144  }
145 
146  // Ran off the end of this tree. Get another one, or quit
147  if(fTreeIdx >= fTree->GetEntries()) return false;
148 
149  outE = fSourceHelper.makeEventPrincipal(1, 1, fEvtNum, tstamp);
150  ++fEvtNum;
151 
155 
156  std::unique_ptr<std::vector<rawdata::RawDigit> > digcol(new std::vector<rawdata::RawDigit>);
157  std::unique_ptr<std::vector<rawdata::RawTrigger> > trigcol(new std::vector<rawdata::RawTrigger>);
158 
159  // Doesn't matter if we say it's enriched or not. Not serialized.
161  ++fTreeIdx;
162 
163  // Stick the original event summary in the record as well
164  // TODO: at least in the TBrowser this looks corrupted
165  // std::unique_ptr<lem::EventSummary> sumcol(new lem::EventSummary(sum));
166  // art::put_product_in_principal(std::move(sumcol), *outE, "daq");
167 
168  // Note: AddVtxToLib depends on these being the offsets. If they're changed
169  // here they need to change there as well.
170  const int plane0 = geom->NPlanes()/2-128;
171  const int cell0 = geom->Plane(0)->Ncells()/2-128;
172  assert(plane0 >= 0);
173  assert(cell0 >= 0);
174 
175  for(int hitIdx = 0; hitIdx < sum.nhits; ++hitIdx){
176  const LiteHit& hit = sum.hits[hitIdx];
177 
178  const daqchannelmap::lchan chan = cmap->Map()->encodeLChan(geom->DetId(), hit.Plane()+plane0, hit.Cell()+cell0);
179 
180  // Put in 100 as a reference value
181  const short adc = 100;
182  const int tdc = 10000;
183 
184  rawdata::RawDigit dig(chan, 0, {adc}, tdc);
185  dig.SetMC();
186 
187  // Push the hit through the procedure CalHit will use, and find out
188  // what PECorr it'll end up with. Use that to scale to the PECorr value
189  // we actually want it to obtain. It's not so vital this is right, I'm
190  // not sure anyone downstream cares about the overall scaling.
191  const rb::CellHit chit(cal->MakeCellHit(&dig));
192  const double control = cal->GetPECorr(chit, 0);
193  assert(control > 0);
194  const short newadc = 100*hit.pecorr/control;
195  dig.SetADC(0, newadc);
196  digcol->push_back(dig);
197  } // end for hitIdx
198 
199  // Event display for one insists on a RawTrigger, give it junk
200  trigcol->push_back(rawdata::RawTrigger());
201 
202  art::put_product_in_principal(std::move(digcol), *outE, "daq");
203  art::put_product_in_principal(std::move(trigcol), *outE, "daq");
204 
205  return true;
206  }
207 
208  //......................................................................
210  {
211  fFile->Close();
212  fTree = 0;
213  fTreeIdx = 0;
214  }
215 
217 
218  DEFINE_ART_INPUT_SOURCE(GenFromLibSource)
219 
220 } // end namespace lem
221 ////////////////////////////////////////////////////////////////////////
unsigned short nhits
Definition: EventSummary.h:53
EventPrincipal * makeEventPrincipal(EventAuxiliary const &eventAux, std::shared_ptr< History > &&history) const
Simple representation of event for LEM use.
Definition: EventSummary.h:26
static double fgChargePower
Definition: EventSummary.h:76
std::enable_if_t< P::branch_type==InEvent||P::branch_type==InResults > put_product_in_principal(std::unique_ptr< T > &&product, P &principal, std::string const &module_label, std::string const &instance_name={})
art::Source< GenFromLib > GenFromLibSource
void reconfigure(const fhicl::ParameterSet &pset)
unsigned int Ncells() const
Number of cells in this plane.
Definition: PlaneGeo.h:43
art::SourceHelper const & fSourceHelper
Class to help with {Run, SubRun, Event}Principal construction.
const daqchannelmap::DAQChannelMap * Map() const
Definition: CMap.h:57
float pecorr
Definition: LiteHit.h:24
int Cell() const
Definition: LiteHit.h:39
const PlaneGeo * Plane(unsigned int i) const
lchan encodeLChan(int detId, plane_t plane, cell_t cell) const
rb::CellHit MakeCellHit(const rawdata::RawDigit *rawdigit)
static void InitFromTree(TTree *tr)
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
PID
Definition: FillPIDs.h:14
bool fOnce
Flag that this is the first call to readNext.
GenFromLib(const fhicl::ParameterSet &pset, art::ProductRegistryHelper &help, const art::SourceHelper &pm)
int Plane() const
Definition: LiteHit.h:38
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
bool readNext(const art::RunPrincipal *inR, const art::SubRunPrincipal *inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
RunPrincipal * makeRunPrincipal(RunAuxiliary const &runAux) const
static EventSummary FromTree(TTree *tr, int evtIdx, bool enrich)
Reconstitute ART events from a LEM library.
void SetMC(bool isMC=true)
Definition: RawDigit.h:106
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
A rawdata::RawDigit with channel information decoded.
Definition: CellHit.h:27
Definition: structs.h:12
void geom(int which=0)
Definition: geom.C:163
static double fgDecayRate
Definition: EventSummary.h:75
assert(nhit_max >=nhit_nbins)
double GetPECorr(rb::CellHit const &cellhit, double w)
SubRunPrincipal * makeSubRunPrincipal(SubRunAuxiliary const &subRunAux) const
unsigned int NPlanes() const
Simple representation of event for LEM use.
Compressed hit info, basic component of LEM events.
Definition: LiteHit.h:18
Double_t sum
Definition: plot.C:31
#define DEFINE_ART_INPUT_SOURCE(klass)
Encapsulate the geometry of one entire detector (near, far, ndos)
void readFile(const std::string &fname, art::FileBlock *&fb)
std::string FileBaseName() const
enum BeamMode string