All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
icarusalg/icarusalg/gallery/galleryAnalysis/C++/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 // ICARUS 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<icarus::ICARUSChannelMapAlg>(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"),
109  static_cast<detinfo::LArProperties const*>(larp.get())}); // TODO type cast is required until issue #18001 is solved
110 
111  // ***************************************************************************
112  // *** SERVICE PROVIDER SETUP END ****************************************
113  // ***************************************************************************
114 
115  /*
116  * the preparation of input file list
117  */
118  std::vector<std::string> const allInputFiles = expandInputFiles(inputFiles);
119 
120  /*
121  * other parameters
122  */
123  art::InputTag trackTag = analysisConfig.get<art::InputTag>("tracks");
124  art::InputTag hitsTag = analysisConfig.get<art::InputTag>("hits");
125 
126  /*
127  * preparation of histogram output file
128  */
129  std::unique_ptr<TFile> pHistFile;
130  if (analysisConfig.has_key("histogramFile"))
131  {
132  std::string fileName = analysisConfig.get<std::string>("histogramFile");
133  std::cout << "Creating output file: '" << fileName << "'" << std::endl;
134  pHistFile = std::make_unique<TFile>(fileName.c_str(), "RECREATE");
135  }
136 
137  /*
138  * preparation of the algorithm class
139  */
140  TrackAnalysis trackAnalysis(analysisConfig.get<fhicl::ParameterSet>("trackAnalysis"));
141 
142  trackAnalysis.setup(*geom, pHistFile.get());
143  trackAnalysis.prepare();
144 
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  auto const detProp = detp->DataFor(detclk->DataForJob());
151  mcAssociations.setup(*geom, detProp, 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__
Channel mapping algorithms for ICARUS detector.
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
auto const detProp
void setup(const geo::GeometryCore &, const detinfo::DetectorPropertiesData &, TDirectory *)