CMS 3D CMS Logo

HcalForwardLibWriter.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <fstream>
4 #include <utility>
5 #include <vector>
6 
9 
12 
15 
20 
21 #include "TFile.h"
22 #include "TTree.h"
23 
25 public:
26  struct FileHandle {
29  int momentum;
30  };
31  explicit HcalForwardLibWriter(const edm::ParameterSet&);
32  ~HcalForwardLibWriter() override {}
33  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
34 
35 private:
36  void beginJob() override;
37  void analyze(const edm::Event&, const edm::EventSetup&) override;
38  void endJob() override;
39  int readUserData();
40  int nbins;
41  int nshowers;
42  int bsize;
46 
47  TFile* theFile;
48  TTree* theTree;
49  TFile* LibFile;
50  TTree* LibTree;
51  TBranch* emBranch;
52  TBranch* hadBranch;
53  std::vector<float>* partsEm;
54  std::vector<float>* partsHad;
55 
57  std::vector<FileHandle> theFileHandle;
58 
62 };
63 
65  edm::ParameterSet theParms = iConfig.getParameter<edm::ParameterSet>("hcalForwardLibWriterParameters");
66  edm::FileInPath fp = theParms.getParameter<edm::FileInPath>("FileName");
67  nbins = theParms.getParameter<int>("Nbins");
68  nshowers = theParms.getParameter<int>("Nshowers");
69  bsize = theParms.getParameter<int>("BufSize");
70  splitlevel = theParms.getParameter<int>("SplitLevel");
71  compressionAlgo = theParms.getParameter<int>("CompressionAlgo");
72  compressionLevel = theParms.getParameter<int>("CompressionLevel");
73 
74  std::string pName = fp.fullPath();
75  if (pName.find('.') == 0)
76  pName.erase(0, 2);
77  theDataFile = pName;
78  readUserData();
79 
81  fs->file().cd();
82  fs->file().SetCompressionAlgorithm(compressionAlgo);
83  fs->file().SetCompressionLevel(compressionLevel);
84 
85  LibTree = new TTree("HFSimHits", "HFSimHits");
86 
87  //https://root.cern/root/html534/TTree.html
88  // TBranch*Branch(const char* name, const char* classname, void** obj, Int_t bufsize = 32000, Int_t splitlevel = 99)
89  partsEm = new std::vector<float>();
90  partsHad = new std::vector<float>();
91  emBranch = LibTree->Branch("emParticles", &partsEm, bsize, splitlevel);
92  hadBranch = LibTree->Branch("hadParticles", &partsHad, bsize, splitlevel);
93 }
94 
97  desc.add<edm::FileInPath>("FileName", edm::FileInPath("SimG4CMS/ShowerLibraryProducer/data/fileList.txt"));
98  desc.add<int>("Nbins", 16);
99  desc.add<int>("Nshowers", 10000);
100  desc.add<int>("BufSize", 1);
101  desc.add<int>("SplitLevel", 2);
102  desc.add<int>("CompressionAlgo", 4);
103  desc.add<int>("CompressionLevel", 4);
104  descriptions.add("hcalForwardLibWriterParameters", desc);
105 }
106 
108  // Event info
109  std::vector<double> en;
110  double momBin[16] = {2, 3, 5, 7, 10, 15, 20, 30, 50, 75, 100, 150, 250, 350, 500, 1000};
111  en.reserve(nbins);
112  for (int i = 0; i < nbins; ++i)
113  en.push_back(momBin[i]);
114 
115  int nem = 0;
116  int nhad = 0;
117 
118  //shower photons
119  int n = theFileHandle.size();
120  // cycle over files ++++++++++++++++++++++++++++++++++++++++++++++++++++
121  for (int i = 0; i < n; ++i) {
122  std::string fn = theFileHandle[i].name;
123  std::string particle = theFileHandle[i].id;
124 
125  // edm::LogVerbatim("HcalSim") << "*** Input file " << i << " " << fn;
126 
127  TFile* theFile = new TFile(fn.c_str(), "READ");
128  TTree* theTree = (TTree*)gDirectory->Get("g4SimHits/CherenkovPhotons");
129  int nphot = 0;
130 
131  const int size = 10000;
132  if (nshowers > size) {
133  edm::LogError("HcalForwardLibWriter") << "Too big Nshowers number";
134  return;
135  }
136 
137  float x[size] = {0.};
138  float y[size] = {0.};
139  float z[size] = {0.};
140  float t[size] = {0.};
141  float lambda[size] = {0.};
142  int fiberId[size] = {0};
143  float primZ; // added
144 
145  theTree->SetBranchAddress("nphot", &nphot);
146  theTree->SetBranchAddress("x", &x);
147  theTree->SetBranchAddress("y", &y);
148  theTree->SetBranchAddress("z", &z);
149  theTree->SetBranchAddress("t", &t);
150  theTree->SetBranchAddress("lambda", &lambda);
151  theTree->SetBranchAddress("fiberId", &fiberId);
152  theTree->SetBranchAddress("primZ", &primZ); // added
153  int nentries = int(theTree->GetEntries());
154  int ngood = 0;
155  // cycle over showers ====================================================
156  for (int iev = 0; iev < nentries; iev++) {
157  if (primZ < 990.)
158  continue; // exclude showers with interactions in front of HF (1m of air)
159  ngood++;
160  if (ngood > nshowers)
161  continue;
162  unsigned int nph = nphot; //++
163  if (particle == "electron") {
164  emColl.clear();
165  partsEm->clear(); //++
166  partsEm->resize(5 * nph); //++
167  } else {
168  hadColl.clear();
169  partsHad->clear(); //++
170  partsHad->resize(5 * nph); //++
171  }
172  // cycle over photons in shower -------------------------------------------
173  for (int iph = 0; iph < nphot; ++iph) {
174  if (fiberId[iph] != 1) {
175  z[iph] = -z[iph];
176  }
177 
178  if (particle == "electron") {
179  (*partsEm)[iph] = (x[iph]);
180  (*partsEm)[iph + 1 * nph] = (y[iph]);
181  (*partsEm)[iph + 2 * nph] = (z[iph]);
182  (*partsEm)[iph + 3 * nph] = (t[iph]);
183  (*partsEm)[iph + 4 * nph] = (lambda[iph]);
184  } else {
185  (*partsHad)[iph] = (x[iph]);
186  (*partsHad)[iph + 1 * nph] = (y[iph]);
187  (*partsHad)[iph + 2 * nph] = (z[iph]);
188  (*partsHad)[iph + 3 * nph] = (t[iph]);
189  (*partsHad)[iph + 4 * nph] = (lambda[iph]);
190  }
191  }
192  // end of cycle over photons in shower -------------------------------------------
193 
194  if (particle == "electron") {
195  LibTree->SetEntries(nem);
196  emBranch->Fill();
197  nem++;
198  emColl.clear();
199  } else {
200  LibTree->SetEntries(nhad);
201  nhad++;
202  hadBranch->Fill();
203  hadColl.clear();
204  }
205  }
206  // end of cycle over showers ====================================================
207  theFile->Close();
208  }
209  // end of cycle over files ++++++++++++++++++++++++++++++++++++++++++++++++++++
210 }
211 
213 
216  fs->file().cd();
217  LibTree->Write();
218  LibTree->Print();
219 }
220 
222  std::ifstream input(theDataFile.c_str());
223  if (input.fail()) {
224  return 0;
225  }
226  std::string theFileName, thePID;
227  int mom;
228  int k = 0;
229  while (!input.eof()) {
230  input >> theFileName >> thePID >> mom;
231  if (!input.fail()) {
232  FileHandle aFile;
233  aFile.name = theFileName;
234  aFile.id = thePID;
235  aFile.momentum = mom;
236  theFileHandle.push_back(aFile);
237  ++k;
238  } else {
239  input.clear();
240  }
241  input.ignore(999, '\n');
242  }
243  return k;
244 }
245 
246 //define this as a plug-in
size
Write out results.
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
HFShowerLibraryEventInfo evtInfo
Log< level::Error, false > LogError
std::vector< float > * partsEm
static std::string const input
Definition: EdmProvDump.cc:50
int iEvent
Definition: GenABIO.cc:224
std::vector< HFShowerPhoton > HFShowerPhotonCollection
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< float > * partsHad
HcalForwardLibWriter(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< FileHandle > theFileHandle
HFShowerPhotonCollection hadColl
HFShowerPhotonCollection emColl