NEUTImport_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: NEUTImport
3 // Module Type: producer
4 // File: NEUTImport_module.cc
5 //
6 // author: Christoph Alt
7 // email: christoph.alt@cern.ch
8 //
9 // - Outside LArSoft: files are generated with NEUT and then converted to RooTracker-like format,
10 // using the NeutToRooTracker package.
11 // - This module: The RooTracker-like files are imported and converted to art format, event by event.
12 // Only final state particles (status 1) are added to the art events.
13 //
14 // NeutToRooTracker: https://github.com/luketpickering/NeutToRooTracker (by Luke Pickering)
15 //
16 ////////////////////////////////////////////////////////////////////////
17 
18 // ROOT includes
19 #include "TRandom3.h"
20 #include "TDatabasePDG.h"
21 #include "TString.h"
22 #include "TSystem.h" //need BaseName and DirName
23 #include "TFile.h"
24 #include "TH2D.h"
25 #include "TTree.h"
26 #include "TVector3.h"
27 #include "TLorentzVector.h"
28 
29 // Framework includes
32 #include "fhiclcpp/ParameterSet.h"
33 #include "fhiclcpp/types/Name.h"
34 #include "fhiclcpp/types/Comment.h"
35 #include "fhiclcpp/types/Atom.h"
40 #include "art_root_io/TFileService.h"
41 #include "art_root_io/TFileDirectory.h"
44 #include "cetlib_except/exception.h"
45 
46 // nutools includes
49 #include "nugen/EventGeneratorBase/evgenbase.h"
50 
51 // lar includes
54 #include "TDatabasePDG.h"
55 
56 #include <stdio.h>
57 #include <iostream>
58 #include <fstream>
59 #include <string>
60 
61 class NEUTImport;
62 
63 namespace evgendp{
64 
65  //----------------------------------------------------------------------------
66 
68  //holds RooTrackerParticle input parameters from file
69  TLorentzVector getPosition();
70  TLorentzVector getMomentum();
71 
72  int event;
73  int pdg;
74  double mass;
75  double startX;
76  double startY;
77  double startZ;
78  double momX;
79  double momY;
80  double momZ;
81  double energy;
82 
83  }; //end struct RooTrackerParticle
84 
86  return TLorentzVector(startX, startY, startZ, 0);
87  }
88 
90  return TLorentzVector( 0.001*momX, 0.001*momY, 0.001*momZ, 0.001*energy );
91  }
92 
93  //----------------------------------------------------------------------------
94 
95  class NEUTImport : public art::EDProducer {
96 
97  public:
98 
99  struct Config {
100  //holds configuration settings from the fcl
101 
102  using Name = fhicl::Name;
104 
106  Name("LogLevel"),
107  Comment("LogLevel")
108  };
109 
111  Name("StartEvent"),
112  Comment("StartEvent")
113  };
114 
115  fhicl::Atom<double> NumberOfEvents{
116  Name("NumberOfEvents"),
117  Comment("NumberOfEvents")
118  };
119 
120  fhicl::Atom<double> StartPositionX{
121  Name("StartPositionX"),
122  Comment("StartPositionX")
123  };
124 
125  fhicl::Atom<double> StartPositionY{
126  Name("StartPositionY"),
127  Comment("StartPositionY")
128  };
129 
130  fhicl::Atom<double> StartPositionZ{
131  Name("StartPositionZ"),
132  Comment("StartPositionZ")
133  };
134 
136  Name("FileName"),
137  Comment("NEUT output")
138  };
139 
140  fhicl::Atom<bool> UseRotatedStartMomentum{
141  Name("UseRotatedStartMomentum"),
142  Comment("Set to true to use rotated start momentum")
143  };
144  }; //end struct Config
145 
147 
148  explicit NEUTImport(Parameters const& config);
149 
150  NEUTImport(NEUTImport const &) = delete;
151  NEUTImport(NEUTImport &&) = delete;
152  NEUTImport & operator = (NEUTImport const &) = delete;
153  NEUTImport & operator = (NEUTImport &&) = delete;
154 
155  // Required functions.
156  void produce(art::Event & e) override;
157 
158  void beginJob() override;
159 
168 
169  std::map< int, std::vector< RooTrackerParticle*> > RooTrackerEventMap;
170 
171  };
172 
173 }//end namespace
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 
178  : EDProducer{config},
179  fLogLevel (config().LogLevel()),
180  fStartEvent (config().StartEvent()),
181  fNumberOfEvents (config().NumberOfEvents()),
182  fStartPositionX (config().StartPositionX()),
183  fStartPositionY (config().StartPositionY()),
184  fStartPositionZ (config().StartPositionZ()),
185  fFileName (config().FileName()),
186  fUseRotatedStartMomentum (config().UseRotatedStartMomentum())
187 {
188  produces< std::vector<simb::MCTruth> >();
189  produces< sumdata::RunData, art::InRun >();
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 
194 
195 
197 
198  const int NMaxParticlesPerEvent = 1000;
199 
200  int tStdHepN = 0;
201  int tStdHepStatus[NMaxParticlesPerEvent] = {0};
202  int tStdHepPdg[NMaxParticlesPerEvent] = {0};
203  double tStdHepP4[4*NMaxParticlesPerEvent] = {0.};
204 
205 
206  TFile *neutFile = new TFile(fFileName.c_str(), "READ");
207  TTree *neutTree = (TTree*)neutFile->Get("nRooTracker");
208  int NEvents = (int)neutTree->GetEntries();
209  std::cout << "Reading in " << NEvents << " NEUT events. " << std::endl;
210 
211  neutTree->SetBranchAddress("StdHepN",&tStdHepN);
212  neutTree->SetBranchAddress("StdHepStatus",&tStdHepStatus);
213  neutTree->SetBranchAddress("StdHepPdg",&tStdHepPdg);
214 
215  TTree *neutTreeRot = NULL;
216 
218  {
219  neutTreeRot = (TTree*)neutFile->Get("nRooTrackerRot");
220  neutTreeRot->SetBranchAddress("StdHepP4Rot",&tStdHepP4);
221  }
222  else
223  {
224  neutTree->SetBranchAddress("StdHepP4",&tStdHepP4);
225  }
226 
227 
228  for(int i=fStartEvent; i<fStartEvent+fNumberOfEvents; i++) //Event loop
229  {
230  neutTree->GetEntry(i);
232  {
233  neutTreeRot->GetEntry(i);
234  }
235 
236  if(fLogLevel == 1)
237  {
238  std::cout << std::endl;
239  std::cout << std::endl;
240  std::cout << "Event #" << i+1 << " in LArSoft, event #" << i << " in ROOT file." << std::endl;
241  std::cout << "NParticles:\t" << tStdHepN << " (from 0 to " << tStdHepN-1 << ")" << std::endl;
242  }
243 
244  for(int j=0; j<tStdHepN; j++)
245  {
246  double fAbsoluteParticleMomentum = sqrt( pow(tStdHepP4[4*j],2) + pow(tStdHepP4[4*j+1],2) + pow(tStdHepP4[4*j+2],2) );
247  //Only take particles with real PDG code (!=0), status 1 and momentum > 0.
248  //Also ignore first particle (j=0): this is the decaying nucleon in case of nucleon decay or incoming neutrino in case of atmospheric neutrino background.
249  if( tStdHepPdg[j] != 0 && tStdHepStatus[j] == 1 && j > 0 && fAbsoluteParticleMomentum > 0 )
250  {
251  static TDatabasePDG pdgt;
252  TParticlePDG* pdgp = pdgt.GetParticle(tStdHepPdg[j]);
253 
254  RooTrackerParticle *rootrackerparticle = new RooTrackerParticle();
255  rootrackerparticle->pdg = tStdHepPdg[j];
256  rootrackerparticle->mass = pdgp->Mass();
257  rootrackerparticle->startX = fStartPositionX;
258  rootrackerparticle->startY = fStartPositionY;
259  rootrackerparticle->startZ = fStartPositionZ;
260  rootrackerparticle->momX = tStdHepP4[4*j];
261  rootrackerparticle->momY = tStdHepP4[4*j+1];
262  rootrackerparticle->momZ = tStdHepP4[4*j+2];
263  rootrackerparticle->energy = tStdHepP4[4*j+3];
264  RooTrackerEventMap[i-fStartEvent].push_back(rootrackerparticle);
265 
266  if(fLogLevel == 1)
267  {
268  std::cout << std::endl;
269  std::cout << "Particle " << j << ":" << std::endl;
270  std::cout << "Status particle " << j << ":\t" << tStdHepStatus[j] << std::endl;
271  std::cout << "PDG particle " << j << ":\t" << rootrackerparticle->pdg << std::endl;
272  std::cout << "Mass particle " << j << ":\t" << rootrackerparticle->mass << std::endl;
273  std::cout << "StartPositionX particle " << j << ":\t" << rootrackerparticle->startX << std::endl;
274  std::cout << "StartPositionY particle " << j << ":\t" <<rootrackerparticle->startY << std::endl;
275  std::cout << "StartPositionZ particle " << j << ":\t" << rootrackerparticle->startZ << std::endl;
276  std::cout << "StartMomentumX particle " << j << ":\t" << rootrackerparticle->momX << std::endl;
277  std::cout << "StartMomentumY particle " << j << ":\t" <<rootrackerparticle->momY << std::endl;
278  std::cout << "StartMomentumZ particle " << j << ":\t" << rootrackerparticle->momZ << std::endl;
279  std::cout << "StartEnergy particle " << j << ":\t" << tStdHepP4[4*j+3] << std::endl;
280  std::cout << "Absolute momentum particle " << j << ":\t" << fAbsoluteParticleMomentum << std::endl;
281  }
282  }
283  }
284  }
285 }
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 
290  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
291 
292  simb::MCTruth truth;
293  truth.SetOrigin(simb::kUnknown);
294 
295  //read the particle map and create the MCParticle
296  int rootrackerparticlecounter=0;
297  for(RooTrackerParticle* rootrackerparticle : RooTrackerEventMap[e.id().event()-1] ){ //-1: art events start at 1, events in EventMap start at 0.
298 
299  simb::MCParticle particle( rootrackerparticlecounter, rootrackerparticle->pdg ,"primary",-200, rootrackerparticle->mass , 1 );
300  particle.AddTrajectoryPoint( rootrackerparticle->getPosition() , rootrackerparticle->getMomentum() );
301 
302  truth.Add(particle);
303 
304  rootrackerparticlecounter++;
305  }
306 
307  truthcol->push_back(truth);
308  e.put(std::move(truthcol));
309 
310 }
311 
void AddTrajectoryPoint(TLorentzVector const &position, TLorentzVector const &momentum)
Definition: MCParticle.h:257
void SetOrigin(simb::Origin_t origin)
Definition: MCTruth.h:82
std::string string
Definition: nybbler.cc:12
std::map< int, std::vector< RooTrackerParticle * > > RooTrackerEventMap
constexpr T pow(T x)
Definition: pow.h:72
NEUTImport(Parameters const &config)
ChannelGroupService::Name Name
Particle class.
void produce(art::Event &e) override
art framework interface to geometry description
void beginJob() override
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void beginJob()
Definition: Breakpoints.cc:14
static Config * config
Definition: config.cpp:1054
def move(depos, offset)
Definition: depos.py:107
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
void Add(simb::MCParticle const &part)
Definition: MCTruth.h:80
#define Comment
EventNumber_t event() const
Definition: EventID.h:116
Event generator information.
Definition: MCTruth.h:32
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)