CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HcalForwardLibWriter.cc
Go to the documentation of this file.
2 
3 #include "TFile.h"
4 #include "TTree.h"
5 
7  edm::ParameterSet theParms = iConfig.getParameter<edm::ParameterSet>("HcalForwardLibWriterParameters");
8  edm::FileInPath fp = theParms.getParameter<edm::FileInPath>("FileName");
9  nbins = theParms.getParameter<int>("Nbins");
10  nshowers = theParms.getParameter<int>("Nshowers");
11  bsize = theParms.getParameter<int>("BufSize");
12  splitlevel = theParms.getParameter<int>("SplitLevel");
13  compressionAlgo = theParms.getParameter<int>("CompressionAlgo");
14  compressionLevel = theParms.getParameter<int>("CompressionLevel");
15 
16  std::string pName = fp.fullPath();
17  if (pName.find('.') == 0)
18  pName.erase(0, 2);
19  theDataFile = pName;
20  readUserData();
21 
22  fs->file().cd();
23  fs->file().SetCompressionAlgorithm(compressionAlgo);
24  fs->file().SetCompressionLevel(compressionLevel);
25 
26  LibTree = new TTree("HFSimHits", "HFSimHits");
27 
28  //https://root.cern/root/html534/TTree.html
29  // TBranch*Branch(const char* name, const char* classname, void** obj, Int_t bufsize = 32000, Int_t splitlevel = 99)
30  emBranch = LibTree->Branch("emParticles", "HFShowerPhotons-emParticles", &emColl, bsize, splitlevel);
31  hadBranch = LibTree->Branch("hadParticles", "HFShowerPhotons-hadParticles", &hadColl, bsize, splitlevel);
32 }
33 
35 
37  // Event info
38  std::vector<double> en;
39  double momBin[16] = {2, 3, 5, 7, 10, 15, 20, 30, 50, 75, 100, 150, 250, 350, 500, 1000};
40  en.reserve(nbins);
41  for (int i = 0; i < nbins; ++i)
42  en.push_back(momBin[i]);
43 
44  int nem = 0;
45  int nhad = 0;
46 
47  //shower photons
48  int n = theFileHandle.size();
49  // cycle over files ++++++++++++++++++++++++++++++++++++++++++++++++++++
50  for (int i = 0; i < n; ++i) {
51  std::string fn = theFileHandle[i].name;
52  std::string particle = theFileHandle[i].id;
53 
54  // std::cout << "*** Input file " << i << " " << fn << std::endl;
55 
56  TFile* theFile = new TFile(fn.c_str(), "READ");
57  TTree* theTree = (TTree*)gDirectory->Get("g4SimHits/CherenkovPhotons");
58  int nphot = 0;
59 
60  const int size = 10000;
61  if (nshowers > size) {
62  edm::LogError("HcalForwardLibWriter") << "Too big Nshowers number";
63  return;
64  }
65 
66  float x[size] = {0.};
67  float y[size] = {0.};
68  float z[size] = {0.};
69  float t[size] = {0.};
70  float lambda[size] = {0.};
71  int fiberId[size] = {0};
72  float primZ; // added
73 
74  theTree->SetBranchAddress("nphot", &nphot);
75  theTree->SetBranchAddress("x", &x);
76  theTree->SetBranchAddress("y", &y);
77  theTree->SetBranchAddress("z", &z);
78  theTree->SetBranchAddress("t", &t);
79  theTree->SetBranchAddress("lambda", &lambda);
80  theTree->SetBranchAddress("fiberId", &fiberId);
81  theTree->SetBranchAddress("primZ", &primZ); // added
82  int nentries = int(theTree->GetEntries());
83  int ngood = 0;
84  int nbytes = 0;
85  // cycle over showers ====================================================
86  for (int iev = 0; iev < nentries; iev++) {
87  nbytes += theTree->GetEntry(iev);
88  if (primZ < 990.)
89  continue; // exclude showers with interactions in front of HF (1m of air)
90  ngood++;
91  if (ngood > nshowers)
92  continue;
93  if (particle == "electron") {
94  emColl.clear();
95  } else {
96  hadColl.clear();
97  }
98  float nphot_long = 0;
99  float nphot_short = 0;
100  // cycle over photons in shower -------------------------------------------
101  for (int iph = 0; iph < nphot; ++iph) {
102  if (fiberId[iph] == 1) {
103  nphot_long++;
104  } else {
105  nphot_short++;
106  z[iph] = -z[iph];
107  }
108 
109  HFShowerPhoton::Point pos(x[iph], y[iph], z[iph]);
110  HFShowerPhoton aPhoton(pos, t[iph], lambda[iph]);
111  if (particle == "electron") {
112  emColl.push_back(aPhoton);
113  } else {
114  hadColl.push_back(aPhoton);
115  }
116  }
117  // end of cycle over photons in shower -------------------------------------------
118 
119  if (particle == "electron") {
120  LibTree->SetEntries(nem + 1);
121  emBranch->Fill();
122  nem++;
123  emColl.clear();
124  } else {
125  LibTree->SetEntries(nhad + 1);
126  nhad++;
127  hadBranch->Fill();
128  hadColl.clear();
129  }
130  }
131  // end of cycle over showers ====================================================
132  theFile->Close();
133  }
134  // end of cycle over files ++++++++++++++++++++++++++++++++++++++++++++++++++++
135 }
136 
138 
140  fs->file().cd();
141  LibTree->Write();
142  LibTree->Print();
143 }
144 
146  std::ifstream input(theDataFile.c_str());
147  if (input.fail()) {
148  return 0;
149  }
150  std::string theFileName, thePID;
151  int mom;
152  int k = 0;
153  while (!input.eof()) {
154  input >> theFileName >> thePID >> mom;
155  if (!input.fail()) {
156  FileHandle aFile;
157  aFile.name = theFileName;
158  aFile.id = thePID;
159  aFile.momentum = mom;
160  theFileHandle.push_back(aFile);
161  ++k;
162  } else {
163  input.clear();
164  }
165  input.ignore(999, '\n');
166  }
167  return k;
168 }
169 //define this as a plug-in
math::XYZPointF Point
point in the space
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Error, false > LogError
edm::Service< TFileService > fs
static std::string const input
Definition: EdmProvDump.cc:47
int iEvent
Definition: GenABIO.cc:224
TFile & file() const
return opened TFile
Definition: TFileService.h:37
HcalForwardLibWriter(const edm::ParameterSet &)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< FileHandle > theFileHandle
HitContainer const *__restrict__ TkSoA const *__restrict__ Quality const *__restrict__ CAHitNtupletGeneratorKernelsGPU::HitToTuple const *__restrict__ int32_t int iev
HFShowerPhotonCollection hadColl
tuple size
Write out results.
HFShowerPhotonCollection emColl