Fork me on GitHub

source: git/modules/SimpleCalorimeter.cc@ e7cccc2

3.4.3pre05
Last change on this file since e7cccc2 was 341014c, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

apply .clang-format to all .h, .cc and .cpp files

  • Property mode set to 100644
File size: 15.5 KB
RevLine 
[01f457a]1/*
2 * Delphes: a framework for fast simulation of a generic collider experiment
3 * Copyright (C) 2012-2014 Universite catholique de Louvain (UCL), Belgium
[1fa50c2]4 *
[01f457a]5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
[1fa50c2]9 *
[01f457a]10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
[1fa50c2]14 *
[01f457a]15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
[82575a3]19/** \class SimpleCalorimeter
20 *
21 * Fills SimpleCalorimeter towers, performs SimpleCalorimeter resolution smearing,
22 * and creates energy flow objects (tracks, photons, and neutral hadrons).
23 *
24 * \author P. Demin - UCL, Louvain-la-Neuve
25 *
26 */
27
28#include "modules/SimpleCalorimeter.h"
29
30#include "classes/DelphesClasses.h"
31#include "classes/DelphesFactory.h"
32#include "classes/DelphesFormula.h"
33
34#include "ExRootAnalysis/ExRootClassifier.h"
[341014c]35#include "ExRootAnalysis/ExRootFilter.h"
36#include "ExRootAnalysis/ExRootResult.h"
[82575a3]37
38#include "TDatabasePDG.h"
[341014c]39#include "TFormula.h"
[82575a3]40#include "TLorentzVector.h"
[341014c]41#include "TMath.h"
42#include "TObjArray.h"
43#include "TRandom3.h"
44#include "TString.h"
[82575a3]45
46#include <algorithm>
47#include <iostream>
48#include <sstream>
[341014c]49#include <stdexcept>
[82575a3]50
51using namespace std;
52
53//------------------------------------------------------------------------------
54
55SimpleCalorimeter::SimpleCalorimeter() :
56 fResolutionFormula(0),
[df636c74]57 fItParticleInputArray(0), fItTrackInputArray(0)
[82575a3]58{
[341014c]59
[82575a3]60 fResolutionFormula = new DelphesFormula;
[efac6f9]61 fTowerTrackArray = new TObjArray;
62 fItTowerTrackArray = fTowerTrackArray->MakeIterator();
[82575a3]63}
64
65//------------------------------------------------------------------------------
66
67SimpleCalorimeter::~SimpleCalorimeter()
68{
[341014c]69
[82575a3]70 if(fResolutionFormula) delete fResolutionFormula;
[efac6f9]71 if(fTowerTrackArray) delete fTowerTrackArray;
72 if(fItTowerTrackArray) delete fItTowerTrackArray;
[82575a3]73}
74
75//------------------------------------------------------------------------------
76
77void SimpleCalorimeter::Init()
78{
79 ExRootConfParam param, paramEtaBins, paramPhiBins, paramFractions;
80 Long_t i, j, k, size, sizeEtaBins, sizePhiBins;
81 Double_t fraction;
82 TBinMap::iterator itEtaBin;
[341014c]83 set<Double_t>::iterator itPhiBin;
84 vector<Double_t> *phiBins;
[82575a3]85
86 // read eta and phi bins
87 param = GetParam("EtaPhiBins");
88 size = param.GetSize();
89 fBinMap.clear();
90 fEtaBins.clear();
91 fPhiBins.clear();
[341014c]92 for(i = 0; i < size / 2; ++i)
[82575a3]93 {
[341014c]94 paramEtaBins = param[i * 2];
[82575a3]95 sizeEtaBins = paramEtaBins.GetSize();
[341014c]96 paramPhiBins = param[i * 2 + 1];
[82575a3]97 sizePhiBins = paramPhiBins.GetSize();
98
99 for(j = 0; j < sizeEtaBins; ++j)
100 {
101 for(k = 0; k < sizePhiBins; ++k)
102 {
103 fBinMap[paramEtaBins[j].GetDouble()].insert(paramPhiBins[k].GetDouble());
104 }
105 }
106 }
107
108 // for better performance we transform map of sets to parallel vectors:
109 // vector< double > and vector< vector< double >* >
110 for(itEtaBin = fBinMap.begin(); itEtaBin != fBinMap.end(); ++itEtaBin)
111 {
112 fEtaBins.push_back(itEtaBin->first);
[341014c]113 phiBins = new vector<double>(itEtaBin->second.size());
[82575a3]114 fPhiBins.push_back(phiBins);
115 phiBins->clear();
116 for(itPhiBin = itEtaBin->second.begin(); itPhiBin != itEtaBin->second.end(); ++itPhiBin)
117 {
118 phiBins->push_back(*itPhiBin);
119 }
120 }
121
122 // read energy fractions for different particles
123 param = GetParam("EnergyFraction");
124 size = param.GetSize();
125
126 // set default energy fractions values
127 fFractionMap.clear();
128 fFractionMap[0] = 1.0;
129
[341014c]130 for(i = 0; i < size / 2; ++i)
[82575a3]131 {
[341014c]132 paramFractions = param[i * 2 + 1];
[82575a3]133 fraction = paramFractions[0].GetDouble();
[341014c]134 fFractionMap[param[i * 2].GetInt()] = fraction;
[82575a3]135 }
[e5767b57]136
[4b9a2dc]137 // read min E value for towers to be saved
[e5767b57]138 fEnergyMin = GetDouble("EnergyMin", 0.0);
139
140 fEnergySignificanceMin = GetDouble("EnergySignificanceMin", 0.0);
141
[fc6300d]142 // flag that says if current calo is Ecal of Hcal (will then fill correct values of Eem and Ehad)
143 fIsEcal = GetBool("IsEcal", false);
144
[e5767b57]145 // switch on or off the dithering of the center of calorimeter towers
[4e09c3a]146 fSmearTowerCenter = GetBool("SmearTowerCenter", true);
[e5767b57]147
[82575a3]148 // read resolution formulas
149 fResolutionFormula->Compile(GetString("ResolutionFormula", "0"));
[e5767b57]150
[82575a3]151 // import array with output from other modules
152 fParticleInputArray = ImportArray(GetString("ParticleInputArray", "ParticlePropagator/particles"));
153 fItParticleInputArray = fParticleInputArray->MakeIterator();
154
155 fTrackInputArray = ImportArray(GetString("TrackInputArray", "ParticlePropagator/tracks"));
156 fItTrackInputArray = fTrackInputArray->MakeIterator();
157
158 // create output arrays
159 fTowerOutputArray = ExportArray(GetString("TowerOutputArray", "towers"));
[df636c74]160
[a98c7ef]161 fEFlowTrackOutputArray = ExportArray(GetString("EFlowTrackOutputArray", "eflowTracks"));
[82575a3]162 fEFlowTowerOutputArray = ExportArray(GetString("EFlowTowerOutputArray", "eflowTowers"));
163}
164
165//------------------------------------------------------------------------------
166
167void SimpleCalorimeter::Finish()
168{
[341014c]169 vector<vector<Double_t> *>::iterator itPhiBin;
[82575a3]170 if(fItParticleInputArray) delete fItParticleInputArray;
171 if(fItTrackInputArray) delete fItTrackInputArray;
172 for(itPhiBin = fPhiBins.begin(); itPhiBin != fPhiBins.end(); ++itPhiBin)
173 {
174 delete *itPhiBin;
175 }
176}
177
178//------------------------------------------------------------------------------
179
180void SimpleCalorimeter::Process()
181{
182 Candidate *particle, *track;
183 TLorentzVector position, momentum;
184 Short_t etaBin, phiBin, flags;
185 Int_t number;
186 Long64_t towerHit, towerEtaPhi, hitEtaPhi;
187 Double_t fraction;
188 Double_t energy;
[7da1826]189 Double_t sigma;
190 Double_t energyGuess;
191
[82575a3]192 Int_t pdgCode;
193
194 TFractionMap::iterator itFractionMap;
195
[341014c]196 vector<Double_t>::iterator itEtaBin;
197 vector<Double_t>::iterator itPhiBin;
198 vector<Double_t> *phiBins;
[82575a3]199
[341014c]200 vector<Long64_t>::iterator itTowerHits;
[82575a3]201
202 DelphesFactory *factory = GetFactory();
203 fTowerHits.clear();
204 fTowerFractions.clear();
205 fTrackFractions.clear();
[e5767b57]206
[82575a3]207 // loop over all particles
208 fItParticleInputArray->Reset();
209 number = -1;
[341014c]210 while((particle = static_cast<Candidate *>(fItParticleInputArray->Next())))
[82575a3]211 {
212 const TLorentzVector &particlePosition = particle->Position;
213 ++number;
214
215 pdgCode = TMath::Abs(particle->PID);
216
217 itFractionMap = fFractionMap.find(pdgCode);
218 if(itFractionMap == fFractionMap.end())
219 {
220 itFractionMap = fFractionMap.find(0);
221 }
222
223 fraction = itFractionMap->second;
224 fTowerFractions.push_back(fraction);
[e5767b57]225
[82575a3]226 if(fraction < 1.0E-9) continue;
227
228 // find eta bin [1, fEtaBins.size - 1]
229 itEtaBin = lower_bound(fEtaBins.begin(), fEtaBins.end(), particlePosition.Eta());
230 if(itEtaBin == fEtaBins.begin() || itEtaBin == fEtaBins.end()) continue;
231 etaBin = distance(fEtaBins.begin(), itEtaBin);
232
233 // phi bins for given eta bin
234 phiBins = fPhiBins[etaBin];
235
236 // find phi bin [1, phiBins.size - 1]
237 itPhiBin = lower_bound(phiBins->begin(), phiBins->end(), particlePosition.Phi());
238 if(itPhiBin == phiBins->begin() || itPhiBin == phiBins->end()) continue;
239 phiBin = distance(phiBins->begin(), itPhiBin);
240
241 flags = 0;
242 flags |= (pdgCode == 11 || pdgCode == 22) << 1;
243
244 // make tower hit {16-bits for eta bin number, 16-bits for phi bin number, 8-bits for flags, 24-bits for particle number}
245 towerHit = (Long64_t(etaBin) << 48) | (Long64_t(phiBin) << 32) | (Long64_t(flags) << 24) | Long64_t(number);
246
247 fTowerHits.push_back(towerHit);
248 }
249
250 // loop over all tracks
251 fItTrackInputArray->Reset();
252 number = -1;
[341014c]253 while((track = static_cast<Candidate *>(fItTrackInputArray->Next())))
[82575a3]254 {
255 const TLorentzVector &trackPosition = track->Position;
256 ++number;
257
258 pdgCode = TMath::Abs(track->PID);
259
260 itFractionMap = fFractionMap.find(pdgCode);
261 if(itFractionMap == fFractionMap.end())
262 {
263 itFractionMap = fFractionMap.find(0);
264 }
265
266 fraction = itFractionMap->second;
[e5767b57]267
[82575a3]268 fTrackFractions.push_back(fraction);
[e5767b57]269
[82575a3]270 // find eta bin [1, fEtaBins.size - 1]
271 itEtaBin = lower_bound(fEtaBins.begin(), fEtaBins.end(), trackPosition.Eta());
272 if(itEtaBin == fEtaBins.begin() || itEtaBin == fEtaBins.end()) continue;
273 etaBin = distance(fEtaBins.begin(), itEtaBin);
274
275 // phi bins for given eta bin
276 phiBins = fPhiBins[etaBin];
277
278 // find phi bin [1, phiBins.size - 1]
279 itPhiBin = lower_bound(phiBins->begin(), phiBins->end(), trackPosition.Phi());
280 if(itPhiBin == phiBins->begin() || itPhiBin == phiBins->end()) continue;
281 phiBin = distance(phiBins->begin(), itPhiBin);
282
283 flags = 1;
284
285 // make tower hit {16-bits for eta bin number, 16-bits for phi bin number, 8-bits for flags, 24-bits for track number}
286 towerHit = (Long64_t(etaBin) << 48) | (Long64_t(phiBin) << 32) | (Long64_t(flags) << 24) | Long64_t(number);
287
288 fTowerHits.push_back(towerHit);
289 }
290
291 // all hits are sorted first by eta bin number, then by phi bin number,
292 // then by flags and then by particle or track number
293 sort(fTowerHits.begin(), fTowerHits.end());
294
295 // loop over all hits
296 towerEtaPhi = 0;
297 fTower = 0;
298 for(itTowerHits = fTowerHits.begin(); itTowerHits != fTowerHits.end(); ++itTowerHits)
299 {
300 towerHit = (*itTowerHits);
301 flags = (towerHit >> 24) & 0x00000000000000FFLL;
[341014c]302 number = (towerHit)&0x0000000000FFFFFFLL;
[82575a3]303 hitEtaPhi = towerHit >> 32;
304
305 if(towerEtaPhi != hitEtaPhi)
306 {
307 // switch to next tower
308 towerEtaPhi = hitEtaPhi;
309
310 // finalize previous tower
311 FinalizeTower();
312
313 // create new tower
314 fTower = factory->NewCandidate();
315
316 phiBin = (towerHit >> 32) & 0x000000000000FFFFLL;
317 etaBin = (towerHit >> 48) & 0x000000000000FFFFLL;
318
319 // phi bins for given eta bin
320 phiBins = fPhiBins[etaBin];
321
322 // calculate eta and phi of the tower's center
[341014c]323 fTowerEta = 0.5 * (fEtaBins[etaBin - 1] + fEtaBins[etaBin]);
324 fTowerPhi = 0.5 * ((*phiBins)[phiBin - 1] + (*phiBins)[phiBin]);
[82575a3]325
326 fTowerEdges[0] = fEtaBins[etaBin - 1];
327 fTowerEdges[1] = fEtaBins[etaBin];
328 fTowerEdges[2] = (*phiBins)[phiBin - 1];
329 fTowerEdges[3] = (*phiBins)[phiBin];
330
331 fTowerEnergy = 0.0;
[df636c74]332
[efac6f9]333 fTrackEnergy = 0.0;
334 fTrackSigma = 0.0;
[e5767b57]335
[82575a3]336 fTowerTime = 0.0;
337 fTrackTime = 0.0;
[e5767b57]338
339 fTowerTimeWeight = 0.0;
340
[82575a3]341 fTowerTrackHits = 0;
342 fTowerPhotonHits = 0;
[e5767b57]343
[efac6f9]344 fTowerTrackArray->Clear();
[341014c]345 }
[82575a3]346
347 // check for track hits
348 if(flags & 1)
349 {
350 ++fTowerTrackHits;
351
[341014c]352 track = static_cast<Candidate *>(fTrackInputArray->At(number));
[82575a3]353 momentum = track->Momentum;
354 position = track->Position;
[e5767b57]355
[82575a3]356 energy = momentum.E() * fTrackFractions[number];
[e5767b57]357
[341014c]358 fTrackTime += TMath::Sqrt(energy) * position.T();
[efac6f9]359 fTrackTimeWeight += TMath::Sqrt(energy);
[e5767b57]360
[df636c74]361 if(fTrackFractions[number] > 1.0E-9)
362 {
[341014c]363
364 // compute total charged energy
365 fTrackEnergy += energy;
366 sigma = fResolutionFormula->Eval(0.0, fTowerEta, 0.0, momentum.E());
367 if(sigma / momentum.E() < track->TrackResolution)
368 energyGuess = energy;
369 else
370 energyGuess = momentum.E();
371
372 fTrackSigma += ((track->TrackResolution) * energyGuess) * ((track->TrackResolution) * energyGuess);
373 fTowerTrackArray->Add(track);
[df636c74]374 }
[341014c]375
[d67b86b]376 else
[df636c74]377 {
378 fEFlowTrackOutputArray->Add(track);
379 }
[82575a3]380
381 continue;
382 }
[e5767b57]383
[82575a3]384 // check for photon and electron hits in current tower
385 if(flags & 2) ++fTowerPhotonHits;
[e5767b57]386
[341014c]387 particle = static_cast<Candidate *>(fParticleInputArray->At(number));
[82575a3]388 momentum = particle->Momentum;
389 position = particle->Position;
390
391 // fill current tower
392 energy = momentum.E() * fTowerFractions[number];
[e5767b57]393
[82575a3]394 fTowerEnergy += energy;
[e5767b57]395
[341014c]396 fTowerTime += energy * position.T();
[e0f8f99]397 fTowerTimeWeight += energy;
[e5767b57]398
[82575a3]399 fTower->AddCandidate(particle);
400 }
401
402 // finalize last tower
403 FinalizeTower();
404}
405
406//------------------------------------------------------------------------------
407
408void SimpleCalorimeter::FinalizeTower()
409{
[df636c74]410 Candidate *tower, *track, *mother;
[341014c]411 Double_t energy, neutralEnergy, pt, eta, phi;
[efac6f9]412 Double_t sigma, neutralSigma;
[82575a3]413 Double_t time;
[341014c]414
[efac6f9]415 Double_t weightTrack, weightCalo, bestEnergyEstimate, rescaleFactor;
[82575a3]416
[a98c7ef]417 TLorentzVector momentum;
418 TFractionMap::iterator itFractionMap;
[df636c74]419
[82575a3]420 if(!fTower) return;
421
422 sigma = fResolutionFormula->Eval(0.0, fTowerEta, 0.0, fTowerEnergy);
423
424 energy = LogNormal(fTowerEnergy, sigma);
[e5767b57]425
[341014c]426 time = (fTowerTimeWeight < 1.0E-09) ? 0.0 : fTowerTime / fTowerTimeWeight;
[82575a3]427
[4b9a2dc]428 sigma = fResolutionFormula->Eval(0.0, fTowerEta, 0.0, energy);
[e5767b57]429
[341014c]430 if(energy < fEnergyMin || energy < fEnergySignificanceMin * sigma) energy = 0.0;
[efac6f9]431
[4e09c3a]432 if(fSmearTowerCenter)
[e5767b57]433 {
434 eta = gRandom->Uniform(fTowerEdges[0], fTowerEdges[1]);
435 phi = gRandom->Uniform(fTowerEdges[2], fTowerEdges[3]);
436 }
437 else
438 {
439 eta = fTowerEta;
440 phi = fTowerPhi;
441 }
[82575a3]442
443 pt = energy / TMath::CosH(eta);
444
445 fTower->Position.SetPtEtaPhiE(1.0, eta, phi, time);
446 fTower->Momentum.SetPtEtaPhiE(pt, eta, phi, energy);
[e5767b57]447
[fc6300d]448 fTower->Eem = (!fIsEcal) ? 0 : energy;
[df636c74]449 fTower->Ehad = (fIsEcal) ? 0 : energy;
[fc6300d]450
[82575a3]451 fTower->Edges[0] = fTowerEdges[0];
452 fTower->Edges[1] = fTowerEdges[1];
453 fTower->Edges[2] = fTowerEdges[2];
454 fTower->Edges[3] = fTowerEdges[3];
455
456 // fill SimpleCalorimeter towers
457 if(energy > 0.0) fTowerOutputArray->Add(fTower);
458
[efac6f9]459 // e-flow candidates
460
461 //compute neutral excess
[341014c]462
[efac6f9]463 fTrackSigma = TMath::Sqrt(fTrackSigma);
[341014c]464 neutralEnergy = max((energy - fTrackEnergy), 0.0);
465
[efac6f9]466 //compute sigma_trk total
[341014c]467 neutralSigma = neutralEnergy / TMath::Sqrt(fTrackSigma * fTrackSigma + sigma * sigma);
468
[efac6f9]469 // if neutral excess is significant, simply create neutral Eflow tower and clone each track into eflowtrack
470 if(neutralEnergy > fEnergyMin && neutralSigma > fEnergySignificanceMin)
[a98c7ef]471 {
[efac6f9]472 // create new photon tower
[341014c]473 tower = static_cast<Candidate *>(fTower->Clone());
[efac6f9]474 pt = neutralEnergy / TMath::CosH(eta);
[df636c74]475
[efac6f9]476 tower->Eem = (!fIsEcal) ? 0 : neutralEnergy;
477 tower->Ehad = (fIsEcal) ? 0 : neutralEnergy;
478 tower->PID = (fIsEcal) ? 22 : 0;
[341014c]479
[efac6f9]480 tower->Momentum.SetPtEtaPhiE(pt, eta, phi, neutralEnergy);
481 fEFlowTowerOutputArray->Add(tower);
[341014c]482
[efac6f9]483 fItTowerTrackArray->Reset();
[341014c]484 while((track = static_cast<Candidate *>(fItTowerTrackArray->Next())))
[efac6f9]485 {
486 mother = track;
[341014c]487 track = static_cast<Candidate *>(track->Clone());
[efac6f9]488 track->AddCandidate(mother);
[df636c74]489
[efac6f9]490 fEFlowTrackOutputArray->Add(track);
491 }
[df636c74]492 }
[341014c]493
[efac6f9]494 // if neutral excess is not significant, rescale eflow tracks, such that the total charged equals the best measurement given by the calorimeter and tracking
[341014c]495 else if(fTrackEnergy > 0.0)
[df636c74]496 {
[341014c]497 weightTrack = (fTrackSigma > 0.0) ? 1 / (fTrackSigma * fTrackSigma) : 0.0;
498 weightCalo = (sigma > 0.0) ? 1 / (sigma * sigma) : 0.0;
499
500 bestEnergyEstimate = (weightTrack * fTrackEnergy + weightCalo * energy) / (weightTrack + weightCalo);
501 rescaleFactor = bestEnergyEstimate / fTrackEnergy;
502
[efac6f9]503 fItTowerTrackArray->Reset();
[341014c]504 while((track = static_cast<Candidate *>(fItTowerTrackArray->Next())))
[efac6f9]505 {
506 mother = track;
[341014c]507 track = static_cast<Candidate *>(track->Clone());
[efac6f9]508 track->AddCandidate(mother);
[a98c7ef]509
[efac6f9]510 track->Momentum *= rescaleFactor;
[e5767b57]511
[efac6f9]512 fEFlowTrackOutputArray->Add(track);
513 }
[82575a3]514 }
[341014c]515}
[82575a3]516
517//------------------------------------------------------------------------------
518
519Double_t SimpleCalorimeter::LogNormal(Double_t mean, Double_t sigma)
520{
521 Double_t a, b;
522
523 if(mean > 0.0)
524 {
[341014c]525 b = TMath::Sqrt(TMath::Log((1.0 + (sigma * sigma) / (mean * mean))));
526 a = TMath::Log(mean) - 0.5 * b * b;
[82575a3]527
[341014c]528 return TMath::Exp(a + b * gRandom->Gaus(0.0, 1.0));
[82575a3]529 }
530 else
531 {
532 return 0.0;
533 }
534}
Note: See TracBrowser for help on using the repository browser.