FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
runGenerateData.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 /*
10  * File: runGenerateData.cxx
11  * Author: winckler
12  *
13  * Created on November 27, 2014, 11:26 AM
14  */
15 
16 #include "boost/program_options.hpp"
17 
18 #include <csignal>
19 #include <iostream>
20 #include <memory>
21 #include <vector>
22 
23 // root
24 #include <TApplication.h>
25 #include <TCanvas.h>
26 #include <TH1D.h>
27 #include <TH2D.h>
28 
29 // FairRoot
30 #include "RootOutFileManager.h"
31 
32 #include <FairMQLogger.h>
33 
34 // FairRoot - Tutorial 7
35 #include "MyDigi.h"
36 #include "MyPodData.h"
37 #include "RooDataGenerator.h"
38 
39 using namespace std;
40 
41 // fill data payload and save to file
42 template<typename T, typename ManagerType>
43 void SaveDataToFile(ManagerType& outMan, RooDataSet* dataset, bool printval = false);
44 
45 template<typename T, typename ManagerType>
46 void SavePodDataToFile(ManagerType& outMan, RooDataSet* dataset, bool printval = false);
47 
48 typedef MyDigi TDigi; // non POD/complex data
49 typedef MyPodData::Digi TDigiPod; // POD/trivial data
51 
52 int main(int argc, char** argv)
53 {
54  try {
55  string filename;
56  string treename;
57  string branchname;
58  string classname;
59  string fileoption;
60  bool useTClonesArray;
61  bool plotdata;
62  unsigned int tmax;
63  double nMean;
64  double nSigma;
65 
66  namespace po = boost::program_options;
67 
68  po::options_description desc("Data generator options");
69  // clang-format off
70  desc.add_options()
71  ("output-file", po::value<string>(&filename)->required(), "Path to the output root file of generated data")
72  ("tree", po::value<string>(&treename)->default_value("T7SamplerTree"), "Name of the tree")
73  ("branch", po::value<string>(&branchname)->default_value("MyDigi"), "Name of the Branch")
74  ("class-name", po::value<string>(&classname)->default_value("MyDigi"), "Name of the Payload class")
75  ("rootfile-option", po::value<string>(&fileoption)->default_value("RECREATE"), "Root file option.")
76  ("use-TCA", po::value<bool>(&useTClonesArray)->default_value(true), "Store data bunches in TClonesArray")
77  ("plot-data", po::value<bool>(&plotdata)->default_value(false), "Plot generated data")
78  ("tmax", po::value<unsigned int>(&tmax)->default_value(100), "max time index=bunch number")
79  ("Nmean", po::value<double>(&nMean)->default_value(1595), "mean number of generated data / bunch")
80  ("Nsigma", po::value<double>(&nSigma)->default_value(25.98), "std deviation of the number of generated data / bunch");
81  // clang-format on
82 
83  po::variables_map vm;
84  try {
85  po::store(po::parse_command_line(argc, argv, desc), vm);
86 
87  if (vm.count("help")) {
88  std::cout << "Basic Command Line Parameter App" << std::endl << desc << std::endl;
89  return 0;
90  }
91 
92  po::notify(vm);
93  } catch (po::error& e) {
94  std::cerr << "error: " << e.what() << std::endl << std::endl;
95  std::cerr << desc << std::endl;
96  return 1;
97  }
98 
99  // Init output file manager
100  RootFileManager rootman;
101  rootman.SetFileProperties(filename, treename, branchname, classname, fileoption, useTClonesArray);
102  rootman.InitOutputFile();
103 
104  // Init density function for the number of digi/bunch
105  RdmVarParameters nval(nMean, nSigma);
106  RooRealVar n("N", "N", nval.fMin, nval.fMax);
107  RooGaussian gaussN("gaussN",
108  "gaussian PDF",
109  n,
110  RooFit::RooConst(nval.fMean),
111  RooFit::RooConst(nval.fSigma)); // mean/sigma same as tutorial 3
112 
113  // Init density function for the (x, y, z, t, terr) random variables
114  PDFConfig pdfConfig; // default setting (i.e. default range, mean, standard deviation)
115  MultiVariatePDF model(pdfConfig);
116 
117  // loop over t (= bunch index), generate data from pdf, fill digi and save to file
118  for (unsigned int t = 0; t < tmax; t++) {
119  unique_ptr<RooDataSet> gaussSample(gaussN.generate(n, 1));
120  unsigned int nDigi = static_cast<unsigned int>(gaussSample->get(0)->getRealValue("N"));
121  LOG(info) << "Bunch number " << t + 1 << "/" << tmax << " ("
122  << 100. * static_cast<double>(t + 1) / static_cast<double>(tmax)
123  << " %). Number of generated digis: " << nDigi
124  << ", payload = " << nDigi * (3 * sizeof(Int_t) + 2 * sizeof(Double_t)) << " bytes";
125 
126  unique_ptr<RooDataSet> simdataset(model.GetGeneratedData(nDigi, t));
127  SaveDataToFile<TDigi, RootFileManager>(rootman, simdataset.get());
128  }
129 
130  LOG(info) << "Data generation successful";
131 
132  // option : plot generated data
133  if (plotdata) {
134  RootFileManager man;
135  man.SetFileProperties(filename, treename, branchname, classname, "READ", useTClonesArray);
136  vector<vector<TDigi>> data = man.GetAllObj(filename, treename, branchname);
137 
138  TH2D histoxy("fxy",
139  "digi.fxy",
140  100,
141  pdfConfig.fX.fMin,
142  pdfConfig.fX.fMax,
143  100,
144  pdfConfig.fY.fMin,
145  pdfConfig.fY.fMax);
146  TH1D histox("fx", "digi.fx", 100, pdfConfig.fX.fMin, pdfConfig.fX.fMax);
147  TH1D histoy("fy", "digi.fy", 100, pdfConfig.fY.fMin, pdfConfig.fY.fMax);
148  TH1D histoz("fz", "digi.fz", 100, pdfConfig.fZ.fMin, pdfConfig.fZ.fMax);
149  TH1D histot("ftimestamp", "digi.ftimestamp", 10 * tmax, 0., static_cast<double>(tmax + 1));
150  TH1D histoterr("ftimestampErr", "digi.ftimestampErr", 100, pdfConfig.fTErr.fMin, pdfConfig.fTErr.fMax);
151  TH1D histoN("f_N", "Number of digi distribution", 100, nval.fMin, nval.fMax);
152 
153  for (auto& p : data) {
154  histoN.Fill(static_cast<double>(p.size()));
155  for (auto& q : p) {
156  histox.Fill(q.GetX());
157  histoy.Fill(q.GetY());
158  histoxy.Fill(q.GetX(), q.GetY());
159  histoz.Fill(q.GetZ());
160  histot.Fill(q.GetTimeStamp());
161  histoterr.Fill(q.GetTimeStampError());
162  }
163  }
164 
165  TApplication app("App", &argc, argv);
166 
167  TCanvas canvas("Tutorial7", "Tutorial7", 1000, 800);
168  canvas.Divide(4, 2);
169 
170  canvas.cd(1);
171  histox.Draw();
172 
173  canvas.cd(2);
174  histoy.Draw();
175 
176  canvas.cd(3);
177  histoxy.Draw("zcol");
178 
179  canvas.cd(4);
180  histoz.Draw();
181 
182  canvas.cd(5);
183  histot.Draw();
184 
185  canvas.cd(6);
186  histoterr.Draw();
187 
188  canvas.cd(7);
189  histoN.Draw();
190 
191  app.Run();
192  }
193  } catch (exception& e) {
194  LOG(error) << e.what();
195  return 1;
196  }
197 
198  return 0;
199 }
200 
201 template<typename T, typename ManagerType>
202 void SaveDataToFile(ManagerType& outMan, RooDataSet* dataset, bool printval)
203 {
204  vector<T> dataBunch;
205  for (int i = 0; i < dataset->numEntries(); i++) {
206  T data;
207  data.SetTimeStamp(dataset->get(i)->getRealValue("t"));
208  data.SetTimeStampError(dataset->get(i)->getRealValue("tErr"));
209 
210  data.SetX(static_cast<Int_t>(round(dataset->get(i)->getRealValue("x"))));
211  data.SetY(static_cast<Int_t>(round(dataset->get(i)->getRealValue("y"))));
212  data.SetZ(static_cast<Int_t>(round(dataset->get(i)->getRealValue("z"))));
213 
214  if (printval) {
215  LOG(info) << "x=" << data.GetX() << " y=" << data.GetY() << " z=" << data.GetZ()
216  << " t=" << data.GetTimeStamp() << " tErr=" << data.GetTimeStampError();
217  }
218  dataBunch.push_back(data);
219  }
220 
221  outMan.AddToFile(dataBunch);
222 }
223 
224 template<typename T, typename ManagerType>
225 void SavePodDataToFile(ManagerType& outMan, RooDataSet* dataset, bool printval)
226 {
227  vector<T> dataBunch;
228  for (int i = 0; i < dataset->numEntries(); i++) {
229  T data;
230  data.fTimeStamp = dataset->get(i)->getRealValue("t");
231  data.fTimeStampError = dataset->get(i)->getRealValue("tErr");
232 
233  data.fX = static_cast<Int_t>(round(dataset->get(i)->getRealValue("x")));
234  data.fY = static_cast<Int_t>(round(dataset->get(i)->getRealValue("y")));
235  data.fZ = static_cast<Int_t>(round(dataset->get(i)->getRealValue("z")));
236 
237  if (printval) {
238  LOG(info) << "x=" << data.fX << " y=" << data.fY << " z=" << data.fZ << " t=" << data.fTimeStamp
239  << " tErr=" << data.fTimeStampError;
240  }
241  dataBunch.push_back(data);
242  }
243 
244  outMan.AddToFile(dataBunch);
245 }
MyPodData::Digi TDigiPod
RdmVarParameters fY
RooDataSet * GetGeneratedData(unsigned int n, unsigned int ti)
RdmVarParameters fZ
void SavePodDataToFile(ManagerType &outMan, RooDataSet *dataset, bool printval=false)
int main(void)
Definition: MyDigi.h:32
MyDigi TDigi
void SaveDataToFile(ManagerType &outMan, RooDataSet *dataset, bool printval=false)
void SetFileProperties(const std::string &filename, const std::string &treeName, const std::string &branchName, const std::string &className="", const std::string &fileOption="RECREATE", bool useClonesArray=false, bool flowMode=true)
RdmVarParameters fTErr
RdmVarParameters fX
std::vector< std::vector< DataType > > GetAllObj(const std::string &filename, const std::string &treename, const std::string &branchname)
RootOutFileManager< TDigi > RootFileManager