LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PPFXFluxReader.cxx
Go to the documentation of this file.
1 
7 //LArSoft
12 
13 //ART, ...
20 #include "art_root_io/TFileService.h"
23 
24 //root
25 #include "TFile.h"
26 #include "TH1D.h"
27 #include "TTree.h"
28 
31 
32 #include "dk2nu/tree/NuChoice.h"
33 #include "dk2nu/tree/dk2nu.h"
34 
35 #include "GENIE/Framework/EventGen/EventRecord.h"
36 
37 namespace fluxr {
38 
41  art::SourceHelper const& pm)
42  : fSourceHelper(pm)
43  , fSubRunID()
44  , fInputType(ps.get<std::string>("inputType"))
45  , fTLmctruth{helper.reconstitutes<std::vector<simb::MCTruth>, art::InEvent>("flux")}
46  , fTLmcflux{helper.reconstitutes<std::vector<simb::MCFlux>, art::InEvent>("flux")}
47  , fTLdk2nu{fInputType == "dk2nu" ?
48  helper.reconstitutes<std::vector<bsim::Dk2Nu>, art::InEvent>("flux") :
49  fTLmctruth}
50  , fTLnuchoice{fInputType == "dk2nu" ?
51  helper.reconstitutes<std::vector<bsim::NuChoice>, art::InEvent>("flux") :
52  fTLmctruth}
53  {
54  helper.reconstitutes<sumdata::POTSummary, art::InSubRun>("flux");
55 
56  if (fInputType == "dk2nu") {
57  helper.reconstitutes<art::Assns<simb::MCTruth, bsim::Dk2Nu>, art::InEvent>("flux");
58  helper.reconstitutes<art::Assns<simb::MCTruth, bsim::NuChoice>, art::InEvent>("flux");
59  helper.reconstitutes<art::Assns<simb::MCTruth, simb::MCFlux>, art::InEvent>("flux");
60 
61  fConfigPS = ps.get<fhicl::ParameterSet>(ps.get<std::string>("dk2nuConfig"));
62  }
63 
65  //initialize beam histograms specified in fhicl file
66  art::TFileDirectory tffluxdir = tfs->mkdir("Flux");
67  string nutype[] = {"nue", "nuebar", "numu", "numubar"};
68  string nultx[] = {"#nu_{e}", "#bar{#nu}_{e}", "#nu_{#mu}", "#bar{#nu}_{#mu}"};
69  string pltx[] = {"#mu^{#pm}", "#pi^{#pm}", "K^{0}_{L}", "K^{#pm}"};
70  string secltx[] = {"pBe->#pi^{#pm}->...->#mu^{#pm}",
71  "pBe->#pi^{#pm}->..(not #mu^{#pm})..",
72  "pBe->K^{0}_{L}->...",
73  "pBe->K^{#pm}->...",
74  "pBe->(p or n)->..."};
75 
76  int nbins = ps.get<int>("nBins", 0);
77  int Elow = ps.get<float>("Elow", 0);
78  int Ehigh = ps.get<float>("Ehigh", 0);
79 
80  for (int i = 0; i < 4; i++) {
81  fHFlux[i] = tffluxdir.make<TH1D>(Form("h50%i", i + 1),
82  Form("%s (all);Energy %s (GeV);#phi(%s)/50MeV/POT",
83  nultx[i].c_str(),
84  nultx[i].c_str(),
85  nultx[i].c_str()),
86  nbins,
87  Elow,
88  Ehigh);
89  fHFlux[i]->Sumw2();
90  }
91 
92  for (int inu = 0; inu < 4; inu++) {
93  for (int ipar = 0; ipar < 4; ipar++) {
94  fHFluxParent[inu][ipar] =
95  tffluxdir.make<TH1D>(Form("h5%i%i", ipar + 1, inu + 1),
96  Form("...->%s->%s;Energy %s (GeV);#phi(%s)/50MeV/POT",
97  pltx[ipar].c_str(),
98  nultx[inu].c_str(),
99  nultx[inu].c_str(),
100  nultx[inu].c_str()),
101  nbins,
102  Elow,
103  Ehigh);
104 
105  fHFluxParent[inu][ipar]->Sumw2();
106  }
107  for (int isec = 0; isec < 5; isec++) {
108  fHFluxSec[inu][isec] =
109  tffluxdir.make<TH1D>(Form("h7%i%i", isec + 1, inu + 1),
110  Form("%s->%s;Energy %s (GeV);#phi(%s)/50MeV/POT",
111  secltx[isec].c_str(),
112  nultx[inu].c_str(),
113  nultx[inu].c_str(),
114  nultx[inu].c_str()),
115  nbins,
116  Elow,
117  Ehigh);
118  fHFluxSec[inu][isec]->Sumw2();
119  }
120  }
121 
122  fEventCounter = 0;
123  fEntry = ps.get<uint32_t>("skipEvents", 0);
124  fMaxEvents = ps.get<int>("maxEvents", -1);
125  }
126 
128  {
130  fEventCounter = 0;
131  fFluxInputFile->Close();
132  delete fFluxInputFile;
133  fSkipEvents = 0; //if crossing file boundary don't skip events in next file
134  fEntry = 0;
135  }
136 
137  void PPFXFluxReader::readFile(std::string const& name, art::FileBlock*& fb)
138  {
139  // Fill and return a new Fileblock.
140  fb = new art::FileBlock(art::FileFormatVersion(1, "PPFXFluxReader"), name);
141 
142  fFluxInputFile = new TFile(name.c_str());
143  if (fFluxInputFile->IsZombie()) {
144  //throw cet::exception(__PRETTY_FUNCTION__) << "Failed to open "<<fFluxInputFile<<std::endl;
145  }
146 
147  if (fInputType == "gsimple") {
149  ((GSimpleInterface*)fFluxDriver)->SetRootFile(fFluxInputFile);
150  }
151  else if (fInputType == "dk2nu") {
152  fFluxDriver = new DK2NuInterface();
153  ((DK2NuInterface*)fFluxDriver)->SetRootFile(fFluxInputFile);
155  }
156  else {
157  throw cet::exception(__PRETTY_FUNCTION__)
158  << "Ntuple format " << fInputType << " not supported" << std::endl;
159  }
160 
161  std::cout << "File has " << fFluxDriver->GetEntries() << " entries" << std::endl;
162  std::cout << "POT = " << fFluxDriver->GetPOT() << std::endl;
163  std::cout << "Run = " << fFluxDriver->GetRun() << std::endl;
164  fPOT += fFluxDriver->GetPOT();
165  }
166 
168  art::SubRunPrincipal* const& /*inSR*/,
169  art::RunPrincipal*& outR,
170  art::SubRunPrincipal*& outSR,
171  art::EventPrincipal*& outE)
172  {
173  if (fMaxEvents > 0 && fEventCounter == unsigned(fMaxEvents)) return false;
174 
175  // Create empty result, then fill it from current file:
176  std::unique_ptr<std::vector<simb::MCFlux>> mcfluxvec(new std::vector<simb::MCFlux>);
177  std::unique_ptr<std::vector<simb::MCTruth>> mctruthvec(new std::vector<simb::MCTruth>);
178 
179  std::unique_ptr<std::vector<bsim::Dk2Nu>> dk2nuvec(new std::vector<bsim::Dk2Nu>);
180  std::unique_ptr<std::vector<bsim::NuChoice>> nuchoicevec(new std::vector<bsim::NuChoice>);
181  std::unique_ptr<art::Assns<simb::MCTruth, bsim::Dk2Nu>> dk2nuassn(
183  std::unique_ptr<art::Assns<simb::MCTruth, bsim::NuChoice>> nuchoiceassn(
185  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCFlux>> mcfluxassn(
187 
188  simb::MCFlux flux;
189  if (!fFluxDriver->FillMCFlux(fEntry, flux)) return false;
190 
191  //fake mctruth product to cheat eventweight that gets neutrino energy from it
192  simb::MCTruth mctruth;
193  simb::MCParticle mcpnu(0, flux.fntype, "Flux");
195  mctruth.Add(mcpnu);
196  mctruth.SetNeutrino(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
197  mctruthvec->push_back(mctruth);
198 
199  if (fInputType == "dk2nu") {
200  dk2nuvec->push_back(*((DK2NuInterface*)fFluxDriver)->GetDk2Nu());
201  nuchoicevec->push_back(*((DK2NuInterface*)fFluxDriver)->GetNuChoice());
202  }
203 
204  int ipdg = 0;
205  //select index matching order in nutype defined in constructor
206  if (flux.fntype == 12)
207  ipdg = 0;
208  else if (flux.fntype == -12)
209  ipdg = 1;
210  else if (flux.fntype == 14)
211  ipdg = 2;
212  else if (flux.fntype == -14)
213  ipdg = 3;
214 
215  double enu = flux.fnenergyn;
216  double totwgh = flux.fnwtnear * flux.fnimpwt / fPOT;
217  if (totwgh < 0 || totwgh > 100) {
218  std::cout << " Bad weight " << totwgh << std::endl;
219  totwgh = 0;
220  }
221  if (fInputType == "gsimple") totwgh = 1. / fPOT; //for gsimple files weight is actually 1
222 
223  fHFlux[ipdg]->Fill(enu, totwgh);
224 
225  if (flux.fptype == 13 || flux.fptype == -13) //mu+-
226  fHFluxParent[ipdg][0]->Fill(enu, totwgh);
227  else if (flux.fptype == 211 || flux.fptype == -211) //pi+-
228  fHFluxParent[ipdg][1]->Fill(enu, totwgh);
229  else if (flux.fptype == 130) //K0L
230  fHFluxParent[ipdg][2]->Fill(enu, totwgh);
231  else if (flux.fptype == 321 || flux.fptype == -321) //K+-
232  fHFluxParent[ipdg][3]->Fill(enu, totwgh);
233 
234  if (fabs(flux.ftptype == 211) && fabs(flux.fptype) == 13)
235  fHFluxSec[ipdg][0]->Fill(enu, totwgh);
236  else if (fabs(flux.ftptype) == 211)
237  fHFluxSec[ipdg][1]->Fill(enu, totwgh);
238  else if (fabs(flux.ftptype) == 130)
239  fHFluxSec[ipdg][2]->Fill(enu, totwgh);
240  else if (fabs(flux.ftptype) == 321)
241  fHFluxSec[ipdg][3]->Fill(enu, totwgh);
242  else if (flux.ftptype == 2212 || flux.ftptype == 2112)
243  fHFluxSec[ipdg][4]->Fill(enu, totwgh);
244 
245  mcfluxvec->push_back(flux);
246  fEventCounter++;
247  fEntry++;
248 
249  art::RunNumber_t rn;
250  if (fFluxDriver->GetRun() > 0) { rn = fFluxDriver->GetRun(); }
251  else {
252  rn = 999999;
253  }
254  art::Timestamp tstamp(time(0));
255 
256  art::SubRunID newID(rn, 0); //subrun not used in flux files, so set to 0
257  if (fSubRunID.runID() != newID.runID()) { // New Run
258  outR = fSourceHelper.makeRunPrincipal(rn, tstamp);
259  mf::LogInfo(__FUNCTION__) << "Cached run number (" << fSubRunID.run()
260  << ") does not match current run number (" << newID.run()
261  << ")! Reassigning variable...." << std::endl;
262  }
263  if (fSubRunID != newID) { // New SubRun
264  mf::LogInfo(__FUNCTION__) << "Cached subrun number (" << fSubRunID.subRun()
265  << ") does not match current subrun number (" << newID.subRun()
266  << ")! Reassigning variable...." << std::endl;
267  outSR = fSourceHelper.makeSubRunPrincipal(rn, 0, tstamp);
268  std::unique_ptr<sumdata::POTSummary> pot(new sumdata::POTSummary);
269  pot->totpot = fPOT;
270  pot->totgoodpot = fPOT;
271 
272  art::put_product_in_principal(std::move(pot), *outSR, "flux");
273 
274  fSubRunID = newID;
275  }
276 
277  outE =
279 
280  // Put products in the event.
281  art::put_product_in_principal(std::move(mcfluxvec), *outE, "flux");
282  art::put_product_in_principal(std::move(mctruthvec), *outE, "flux");
283  if (fInputType == "dk2nu") {
284  art::put_product_in_principal(std::move(dk2nuvec), *outE, "flux");
285  art::put_product_in_principal(std::move(nuchoicevec), *outE, "flux");
286 
287  auto aptr = fSourceHelper.makePtr<simb::MCTruth>(fTLmctruth, *outE, 0);
288  auto bptr = fSourceHelper.makePtr<bsim::Dk2Nu>(fTLdk2nu, *outE, 0);
289  auto cptr = fSourceHelper.makePtr<bsim::NuChoice>(fTLnuchoice, *outE, 0);
290  auto dptr = fSourceHelper.makePtr<simb::MCFlux>(fTLmcflux, *outE, 0);
291 
292  dk2nuassn->addSingle(aptr, bptr);
293  nuchoiceassn->addSingle(aptr, cptr);
294  mcfluxassn->addSingle(aptr, dptr);
295 
296  art::put_product_in_principal(std::move(dk2nuassn), *outE, "flux");
297  art::put_product_in_principal(std::move(nuchoiceassn), *outE, "flux");
298  art::put_product_in_principal(std::move(mcfluxassn), *outE, "flux");
299  }
300 
301  return true;
302  }
303 
304 }
fhicl::ParameterSet fConfigPS
Ptr< T > makePtr(TypeLabel const &t, Principal const &p, typename Ptr< T >::key_type key) const
virtual Long64_t GetEntries() const =0
art::TypeLabel fTLdk2nu
virtual float GetPOT() const =0
virtual TLorentzVector GetNuPosition() const =0
void AddTrajectoryPoint(TLorentzVector const &position, TLorentzVector const &momentum)
Definition: MCParticle.h:258
virtual bool FillMCFlux(Long64_t ientry, simb::MCFlux &mclux)=0
T * get() const
Definition: ServiceHandle.h:69
SubRunPrincipal * makeSubRunPrincipal(SubRunAuxiliary subRunAux) const
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
art::TypeLabel fTLmcflux
int ftptype
Definition: MCFlux.h:82
TH1D * fHFluxParent[4][4]
EventPrincipal * makeEventPrincipal(EventAuxiliary eventAux) const
STL namespace.
void SetNeutrino(int CCNC, int mode, int interactionType, int target, int nucleon, int quark, double w, double x, double y, double qsqr)
Definition: MCTruth.cxx:30
virtual int GetRun() const =0
bool readNext(art::RunPrincipal *const &inR, art::SubRunPrincipal *const &inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
object containing MC flux information
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
RunID const & runID() const
Definition: SubRunID.h:79
double fnwtnear
Definition: MCFlux.h:44
FluxInterface * fFluxDriver
int fptype
Definition: MCFlux.h:63
RunNumber_t run() const
Definition: SubRunID.h:85
T get(std::string const &key) const
Definition: ParameterSet.h:314
PPFXFluxReader(fhicl::ParameterSet const &pset, art::ProductRegistryHelper &helper, art::SourceHelper const &pm)
TFile fb("Li6.root")
art::SourceHelper const & fSourceHelper
art::TypeLabel fTLnuchoice
static constexpr SubRunID flushSubRun() noexcept
Definition: SubRunID.h:171
void Add(simb::MCParticle const &part)
Definition: MCTruth.h:80
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
RunPrincipal * makeRunPrincipal(RunAuxiliary runAux) const
Definition: SourceHelper.cc:89
std::enable_if_t<!detail::range_sets_supported(P::branch_type)> put_product_in_principal(std::unique_ptr< T > &&product, P &principal, std::string const &module_label, std::string const &instance_name={})
int fntype
Definition: MCFlux.h:51
TH1D * fHFluxSec[4][5]
virtual TLorentzVector GetNuMomentum() const =0
art::SubRunID fSubRunID
art::TypeLabel fTLmctruth
SubRunNumber_t subRun() const
Definition: SubRunID.h:91
Event generator information.
Definition: MCTruth.h:32
void readFile(std::string const &name, art::FileBlock *&fb)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
double fnenergyn
Definition: MCFlux.h:43
double fnimpwt
Definition: MCFlux.h:72
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:120