All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/sbndcode/gallery/galleryAnalysis/galleryAnalysis.cpp
Go to the documentation of this file.
1 /**
2  * @file galleryAnalysis.cpp
3  * @brief Template analysis program based on gallery.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date October 21, 2017
6  *
7  * The script is an adaptation of the official gallery demo script demo.cc at
8  * https://github.com/marcpaterno/gallery-demo .
9  *
10  * To jump into the action, look for `SERVICE PROVIDER SETUP` and
11  * `SINGLE EVENT PROCESSING` tags in the source code.
12  *
13  * The approach for loading services is the lowest level LArSoft provides.
14  * An higher level one is to use `testing::TesterEnvironment` as in some service
15  * provider unit tests (e.g., `geo::GeometryCore` and `detinfo::LArProperties`).
16  *
17  */
18 
19 // our additional code
20 #include "TrackAnalysis.h"
21 #include "HitAnalysisAlg.h"
22 #include "MCAssociations.h"
23 
24 // SBND code
26 
27 // LArSoft
28 // - data products
30 // - DetectorProperties
33 // - DetectorClocks
36 // - LArProperties
39 // - Geometry
43 // - configuration
45 
46 // gallery/canvas
47 #include "gallery/Event.h"
48 #include "canvas/Utilities/InputTag.h"
49 #include "messagefacility/MessageLogger/MessageLogger.h"
50 #include "fhiclcpp/ParameterSet.h"
51 
52 // ROOT
53 #include "TFile.h"
54 
55 // C/C++ standard libraries
56 #include <string>
57 #include <vector>
58 #include <memory> // std::make_unique()
59 #include <iostream> // std::cerr
60 
61 
62 #if !defined(__CLING__)
63 // these are needed in the main() function
64 #include <algorithm> // std::copy()
65 #include <iterator> // std::back_inserter()
66 #include <iostream> // std::cerr
67 #endif // !__CLING__
68 
69 /**
70  * @brief Runs the analysis macro.
71  * @param configFile path to the FHiCL configuration to be used for the services
72  * @param inputFiles vector of path of file names
73  * @return an integer as exit code (0 means success)
74  */
75 int galleryAnalysis(std::string const& configFile, std::vector<std::string> const& inputFiles)
76 {
77  /*
78  * the "test" environment configuration
79  */
80  // read FHiCL configuration from a configuration file:
81  fhicl::ParameterSet config = lar::standalone::ParseConfiguration(configFile);
82 
83  // set up message facility (always picked from "services.message")
84  lar::standalone::SetupMessageFacility(config, "galleryAnalysis");
85 
86  // configuration from the "analysis" table of the FHiCL configuration file:
87  auto const& analysisConfig = config.get<fhicl::ParameterSet>("analysis");
88 
89  // ***************************************************************************
90  // *** SERVICE PROVIDER SETUP BEGIN ****************************************
91  // ***************************************************************************
92  //
93  // Uncomment the things you need
94  // (and make sure the corresponding headers are also uncommented)
95  //
96 
97  // geometry setup (it's special)
98  auto geom = lar::standalone::SetupGeometry<geo::ChannelMapSBNDAlg>(config.get<fhicl::ParameterSet>("services.Geometry"));
99 
100  // LArProperties setup
101  auto larp = testing::setupProvider<detinfo::LArPropertiesStandard>(config.get<fhicl::ParameterSet>("services.LArPropertiesService"));
102 
103  // DetectorClocks setup
104  auto detclk = testing::setupProvider<detinfo::DetectorClocksStandard>(config.get<fhicl::ParameterSet>("services.DetectorClocksService"));
105 
106  // DetectorProperties setup
107  auto detp = testing::setupProvider<detinfo::DetectorPropertiesStandard>(config.get<fhicl::ParameterSet>("services.DetectorPropertiesService"),
108  detinfo::DetectorPropertiesStandard::providers_type{geom.get(),static_cast<detinfo::LArProperties const*>(larp.get())}); // TODO type cast is required until issue #18001 is solved
109 
110  // ***************************************************************************
111  // *** SERVICE PROVIDER SETUP END ****************************************
112  // ***************************************************************************
113 
114  /*
115  * the preparation of input file list
116  */
117  std::vector<std::string> const allInputFiles = expandInputFiles(inputFiles);
118 
119  /*
120  * other parameters
121  */
122  art::InputTag trackTag = analysisConfig.get<art::InputTag>("tracks");
123  art::InputTag hitsTag = analysisConfig.get<art::InputTag>("hits");
124 
125  /*
126  * preparation of histogram output file
127  */
128  std::unique_ptr<TFile> pHistFile;
129  if (analysisConfig.has_key("histogramFile"))
130  {
131  std::string fileName = analysisConfig.get<std::string>("histogramFile");
132  std::cout << "Creating output file: '" << fileName << "'" << std::endl;
133  pHistFile = std::make_unique<TFile>(fileName.c_str(), "RECREATE");
134  }
135 
136  /*
137  * preparation of the algorithm class
138  */
139  TrackAnalysis trackAnalysis(analysisConfig.get<fhicl::ParameterSet>("trackAnalysis"));
140 
141  trackAnalysis.setup(*geom, pHistFile.get());
142  trackAnalysis.prepare();
143  auto const clock_data = detclk->DataForJob();
144  auto const det_prop_data = detp->DataFor(clock_data);
145  HitAnalysis::HitAnalysisAlg hitAnalysisAlg(analysisConfig.get<fhicl::ParameterSet>("hitAnalysisAlg"));
146 
147  hitAnalysisAlg.setup(*geom, pHistFile.get());
148 
149  MCAssociations mcAssociations(analysisConfig.get<fhicl::ParameterSet>("mcAssociations"));
150 
151  mcAssociations.setup(*geom, det_prop_data, pHistFile.get());
152  mcAssociations.prepare();
153 
154  int numEvents(0);
155 
156  /*
157  * the event loop
158  */
159  for (gallery::Event event(allInputFiles); !event.atEnd(); event.next())
160  {
161  // *************************************************************************
162  // *** SINGLE EVENT PROCESSING BEGIN *************************************
163  // *************************************************************************
164 
165  mf::LogVerbatim("galleryAnalysis") << "This is event " << event.fileEntry() << "-" << event.eventEntry();
166 
167  trackAnalysis.processTracks(*(event.getValidHandle<std::vector<recob::Track>>(trackTag)));
168 
169  hitAnalysisAlg.fillHistograms(*(event.getValidHandle<std::vector<recob::Hit>>(hitsTag)));
170 
171  mcAssociations.doTrackHitMCAssociations(event);
172 
173  numEvents++;
174 
175  // *************************************************************************
176  // *** SINGLE EVENT PROCESSING END *************************************
177  // *************************************************************************
178 
179  } // for
180 
181  trackAnalysis.finish();
182  mcAssociations.finish();
183 
184  hitAnalysisAlg.endJob(numEvents);
185 
186  return 0;
187 } // galleryAnalysis()
188 
189 
190 /// Version with a single input file.
191 int galleryAnalysis(std::string const& configFile, std::string filename)
192  { return galleryAnalysis(configFile, std::vector<std::string>{ filename }); }
193 
194 #if !defined(__CLING__)
195 int main(int argc, char** argv) {
196 
197  char **pParam = argv + 1, **pend = argv + argc;
198  if (pParam == pend) {
199  std::cerr << "Usage: " << argv[0] << " configFile [inputFile ...]"
200  << std::endl;
201  return 1;
202  }
203  std::string const configFile = *(pParam++);
204  std::vector<std::string> fileNames;
205  std::copy(pParam, pend, std::back_inserter(fileNames));
206 
207  return galleryAnalysis(configFile, fileNames);
208 } // main()
209 
210 #endif // !__CLING__
Utilities for one-line geometry initialization.
Service provider with utility LAr functions.
BEGIN_PROLOG could also be cerr
Helper functions for support of LArPropertiesService in LArSoft tests.
BEGIN_PROLOG could also be dds filename
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:193
Helper functions for support of DetectorClocksService in LArSoft tests.
Access the description of detector geometry.
void SetupMessageFacility(fhicl::ParameterSet const &pset, std::string applName="standalone")
Sets up the message facility service.
Collection of functions for quick setup of basic facilities.
void processTracks(std::vector< recob::Track > const &tracks)
std::vector< std::string > expandInputFiles(std::vector< std::string > const &filePaths)
Expands all input files into a vector of file paths.
Helpers for support of DetectorPropertiesService in LArSoft tests.
Provides recob::Track data product.
int galleryAnalysis(std::string const &configFile, std::vector< std::string > const &inputFiles)
Runs the analysis macro.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
T copy(T const &v)
fhicl::ParameterSet ParseConfiguration(std::string configPath, cet::filepath_maker &lookupPolicy)
Parses a FHiCL configuration file.
int main(int argc, char **argv)
void setup(const geo::GeometryCore &, TDirectory *)
Begin job method.
void setup(geo::GeometryCore const &geom, TDirectory *outDir)
BEGIN_PROLOG could also be cout
void setup(const geo::GeometryCore &, const detinfo::DetectorPropertiesData &, TDirectory *)
Channel mapping for SBND.