Fork me on GitHub

source: svn/trunk/modules/Calorimeter.cc@ 1188

Last change on this file since 1188 was 1145, checked in by Pavel Demin, 11 years ago

optimize LogNormal

  • Property svn:keywords set to Id Revision Date
File size: 14.3 KB
RevLine 
[608]1
[814]2/** \class Calorimeter
3 *
[894]4 * Fills calorimeter towers, performs calorimeter resolution smearing,
5 * preselects towers hit by photons and creates energy flow objects.
[814]6 *
7 * $Date: 2013-06-25 14:24:34 +0000 (Tue, 25 Jun 2013) $
8 * $Revision: 1145 $
9 *
10 *
11 * \author P. Demin - UCL, Louvain-la-Neuve
12 *
13 */
14
[608]15#include "modules/Calorimeter.h"
16
[687]17#include "classes/DelphesClasses.h"
18#include "classes/DelphesFactory.h"
[766]19#include "classes/DelphesFormula.h"
[608]20
[703]21#include "ExRootAnalysis/ExRootResult.h"
22#include "ExRootAnalysis/ExRootFilter.h"
23#include "ExRootAnalysis/ExRootClassifier.h"
24
[608]25#include "TMath.h"
26#include "TString.h"
[629]27#include "TFormula.h"
[703]28#include "TRandom3.h"
29#include "TObjArray.h"
30#include "TDatabasePDG.h"
31#include "TLorentzVector.h"
[608]32
[935]33#include <algorithm>
[703]34#include <stdexcept>
[608]35#include <iostream>
[703]36#include <sstream>
[608]37
38using namespace std;
39
40//------------------------------------------------------------------------------
41
42Calorimeter::Calorimeter() :
[886]43 fECalResolutionFormula(0), fHCalResolutionFormula(0),
44 fItParticleInputArray(0), fItTrackInputArray(0),
[931]45 fTowerTrackArray(0), fItTowerTrackArray(0),
46 fTowerPhotonArray(0), fItTowerPhotonArray(0)
[608]47{
[766]48 fECalResolutionFormula = new DelphesFormula;
49 fHCalResolutionFormula = new DelphesFormula;
[886]50 fTowerTrackArray = new TObjArray;
51 fItTowerTrackArray = fTowerTrackArray->MakeIterator();
[931]52 fTowerPhotonArray = new TObjArray;
53 fItTowerPhotonArray = fTowerPhotonArray->MakeIterator();
[608]54}
55
56//------------------------------------------------------------------------------
57
58Calorimeter::~Calorimeter()
59{
[766]60 if(fECalResolutionFormula) delete fECalResolutionFormula;
61 if(fHCalResolutionFormula) delete fHCalResolutionFormula;
[886]62 if(fTowerTrackArray) delete fTowerTrackArray;
63 if(fItTowerTrackArray) delete fItTowerTrackArray;
[931]64 if(fTowerPhotonArray) delete fTowerPhotonArray;
65 if(fItTowerPhotonArray) delete fItTowerPhotonArray;
[608]66}
67
68//------------------------------------------------------------------------------
69
70void Calorimeter::Init()
71{
[704]72 ExRootConfParam param, paramEtaBins, paramPhiBins, paramFractions;
73 Long_t i, j, k, size, sizeEtaBins, sizePhiBins, sizeFractions;
[608]74 Double_t ecalFraction, hcalFraction;
[622]75 TBinMap::iterator itEtaBin;
[659]76 set< Double_t >::iterator itPhiBin;
77 vector< Double_t > *phiBins;
[608]78
[613]79 // read eta and phi bins
[646]80 param = GetParam("EtaPhiBins");
[608]81 size = param.GetSize();
[622]82 fBinMap.clear();
[663]83 fEtaBins.clear();
84 fPhiBins.clear();
[613]85 for(i = 0; i < size/2; ++i)
[608]86 {
[646]87 paramEtaBins = param[i*2];
88 sizeEtaBins = paramEtaBins.GetSize();
[613]89 paramPhiBins = param[i*2 + 1];
90 sizePhiBins = paramPhiBins.GetSize();
[646]91
92 for(j = 0; j < sizeEtaBins; ++j)
[613]93 {
[646]94 for(k = 0; k < sizePhiBins; ++k)
95 {
[659]96 fBinMap[paramEtaBins[j].GetDouble()].insert(paramPhiBins[k].GetDouble());
[646]97 }
[613]98 }
[608]99 }
100
[661]101 // for better performance we transform map of sets to parallel vectors:
[659]102 // vector< double > and vector< vector< double >* >
[622]103 for(itEtaBin = fBinMap.begin(); itEtaBin != fBinMap.end(); ++itEtaBin)
[813]104 {
[659]105 fEtaBins.push_back(itEtaBin->first);
106 phiBins = new vector< double >(itEtaBin->second.size());
107 fPhiBins.push_back(phiBins);
[666]108 phiBins->clear();
[659]109 for(itPhiBin = itEtaBin->second.begin(); itPhiBin != itEtaBin->second.end(); ++itPhiBin)
[622]110 {
[659]111 phiBins->push_back(*itPhiBin);
[622]112 }
113 }
[935]114
[608]115 // read energy fractions for different particles
116 param = GetParam("EnergyFraction");
117 size = param.GetSize();
[935]118
[608]119 // set default energy fractions values
120 fFractionMap.clear();
121 fFractionMap[0] = make_pair(0.0, 1.0);
[935]122
[608]123 for(i = 0; i < size/2; ++i)
124 {
[703]125 paramFractions = param[i*2 + 1];
[608]126 sizeFractions = paramFractions.GetSize();
[935]127
[608]128 ecalFraction = paramFractions[0].GetDouble();
129 hcalFraction = paramFractions[1].GetDouble();
130
[703]131 fFractionMap[param[i*2].GetInt()] = make_pair(ecalFraction, hcalFraction);
[608]132 }
[629]133/*
[626]134 TFractionMap::iterator itFractionMap;
135 for(itFractionMap = fFractionMap.begin(); itFractionMap != fFractionMap.end(); ++itFractionMap)
136 {
137 cout << itFractionMap->first << " " << itFractionMap->second.first << " " << itFractionMap->second.second << endl;
138 }
[629]139*/
[935]140 // read resolution formulas
[766]141 fECalResolutionFormula->Compile(GetString("ECalResolutionFormula", "0"));
142 fHCalResolutionFormula->Compile(GetString("HCalResolutionFormula", "0"));
143
[608]144 // import array with output from other modules
[886]145 fParticleInputArray = ImportArray(GetString("ParticleInputArray", "ParticlePropagator/particles"));
146 fItParticleInputArray = fParticleInputArray->MakeIterator();
[608]147
[886]148 fTrackInputArray = ImportArray(GetString("TrackInputArray", "ParticlePropagator/tracks"));
149 fItTrackInputArray = fTrackInputArray->MakeIterator();
150
[766]151 // create output arrays
[629]152 fTowerOutputArray = ExportArray(GetString("TowerOutputArray", "towers"));
153 fPhotonOutputArray = ExportArray(GetString("PhotonOutputArray", "photons"));
[938]154
155 fEFlowTrackOutputArray = ExportArray(GetString("EFlowTrackOutputArray", "eflowTracks"));
156 fEFlowTowerOutputArray = ExportArray(GetString("EFlowTowerOutputArray", "eflowTowers"));
[608]157}
158
159//------------------------------------------------------------------------------
160
161void Calorimeter::Finish()
162{
[660]163 vector< vector< Double_t>* >::iterator itPhiBin;
[886]164 if(fItParticleInputArray) delete fItParticleInputArray;
[938]165 if(fItTrackInputArray) delete fItTrackInputArray;
[660]166 for(itPhiBin = fPhiBins.begin(); itPhiBin != fPhiBins.end(); ++itPhiBin)
167 {
168 delete *itPhiBin;
169 }
[608]170}
171
172//------------------------------------------------------------------------------
173
174void Calorimeter::Process()
175{
[886]176 Candidate *particle, *track;
[680]177 TLorentzVector position, momentum;
[1002]178 Short_t etaBin, phiBin, flags;
179 Int_t number;
[745]180 Long64_t towerHit, towerEtaPhi, hitEtaPhi;
[730]181 Double_t ecalFraction, hcalFraction;
[735]182 Double_t ecalEnergy, hcalEnergy;
[608]183 Int_t pdgCode;
184
185 TFractionMap::iterator itFractionMap;
186
[659]187 vector< Double_t >::iterator itEtaBin;
188 vector< Double_t >::iterator itPhiBin;
189 vector< Double_t > *phiBins;
[622]190
[745]191 vector< Long64_t >::iterator itTowerHits;
[935]192
[687]193 DelphesFactory *factory = GetFactory();
[661]194 fTowerHits.clear();
[663]195 fECalFractions.clear();
196 fHCalFractions.clear();
[730]197
[608]198 // loop over all particles
[886]199 fItParticleInputArray->Reset();
[666]200 number = -1;
[886]201 while((particle = static_cast<Candidate*>(fItParticleInputArray->Next())))
[608]202 {
[886]203 const TLorentzVector &particlePosition = particle->Position;
[666]204 ++number;
[608]205
[886]206 pdgCode = TMath::Abs(particle->PID);
[626]207
208 itFractionMap = fFractionMap.find(pdgCode);
209 if(itFractionMap == fFractionMap.end())
210 {
211 itFractionMap = fFractionMap.find(0);
212 }
213
214 ecalFraction = itFractionMap->second.first;
215 hcalFraction = itFractionMap->second.second;
[666]216
217 fECalFractions.push_back(ecalFraction);
218 fHCalFractions.push_back(hcalFraction);
[935]219
[626]220 if(ecalFraction < 1.0E-9 && hcalFraction < 1.0E-9) continue;
[935]221
[730]222 // find eta bin [1, fEtaBins.size - 1]
[886]223 itEtaBin = lower_bound(fEtaBins.begin(), fEtaBins.end(), particlePosition.Eta());
[659]224 if(itEtaBin == fEtaBins.begin() || itEtaBin == fEtaBins.end()) continue;
[730]225 etaBin = distance(fEtaBins.begin(), itEtaBin);
[935]226
[730]227 // phi bins for given eta bin
[659]228 phiBins = fPhiBins[etaBin];
[730]229
230 // find phi bin [1, phiBins.size - 1]
[886]231 itPhiBin = lower_bound(phiBins->begin(), phiBins->end(), particlePosition.Phi());
[659]232 if(itPhiBin == phiBins->begin() || itPhiBin == phiBins->end()) continue;
[666]233 phiBin = distance(phiBins->begin(), itPhiBin);
[608]234
[886]235 flags = (particle->Charge == 0);
[745]236 flags |= (pdgCode == 22) << 1;
[925]237 flags |= (pdgCode == 11) << 2;
[745]238
[1004]239 // make tower hit {16-bits for eta bin number, 16-bits for phi bin number, 8-bits for flags, 24-bits for particle number}
240 towerHit = (Long64_t(etaBin) << 48) | (Long64_t(phiBin) << 32) | (Long64_t(flags) << 24) | Long64_t(number);
[935]241
[661]242 fTowerHits.push_back(towerHit);
243 }
244
[886]245 // loop over all tracks
246 fItTrackInputArray->Reset();
247 number = -1;
248 while((track = static_cast<Candidate*>(fItTrackInputArray->Next())))
249 {
250 const TLorentzVector &trackPosition = track->Position;
251 ++number;
252
253 // find eta bin [1, fEtaBins.size - 1]
254 itEtaBin = lower_bound(fEtaBins.begin(), fEtaBins.end(), trackPosition.Eta());
255 if(itEtaBin == fEtaBins.begin() || itEtaBin == fEtaBins.end()) continue;
256 etaBin = distance(fEtaBins.begin(), itEtaBin);
[935]257
[886]258 // phi bins for given eta bin
259 phiBins = fPhiBins[etaBin];
260
261 // find phi bin [1, phiBins.size - 1]
262 itPhiBin = lower_bound(phiBins->begin(), phiBins->end(), trackPosition.Phi());
263 if(itPhiBin == phiBins->begin() || itPhiBin == phiBins->end()) continue;
264 phiBin = distance(phiBins->begin(), itPhiBin);
265
[1004]266 // make tower hit {16-bits for eta bin number, 16-bits for phi bin number, 8-bits for flags, 24-bits for track number}
267 towerHit = (Long64_t(etaBin) << 48) | (Long64_t(phiBin) << 32) | (Long64_t(1) << 27) | Long64_t(number);
[935]268
[886]269 fTowerHits.push_back(towerHit);
270 }
271
[894]272 // all hits are sorted first by eta bin number, then by phi bin number,
273 // then by flags and then by particle or track number
[661]274 sort(fTowerHits.begin(), fTowerHits.end());
275
[730]276 // loop over all hits
[661]277 towerEtaPhi = 0;
[732]278 fTower = 0;
[661]279 for(itTowerHits = fTowerHits.begin(); itTowerHits != fTowerHits.end(); ++itTowerHits)
280 {
281 towerHit = (*itTowerHits);
[1004]282 flags = (towerHit >> 24) & 0x00000000000000FFLL;
283 number = (towerHit) & 0x0000000000FFFFFFLL;
284 hitEtaPhi = towerHit >> 32;
[730]285
[661]286 if(towerEtaPhi != hitEtaPhi)
[608]287 {
[730]288 // switch to next tower
[661]289 towerEtaPhi = hitEtaPhi;
[730]290
[661]291 // finalize previous tower
[730]292 FinalizeTower();
[661]293
294 // create new tower
[730]295 fTower = factory->NewCandidate();
[661]296
[1004]297 phiBin = (towerHit >> 32) & 0x000000000000FFFFLL;
298 etaBin = (towerHit >> 48) & 0x000000000000FFFFLL;
[730]299
300 // phi bins for given eta bin
[661]301 phiBins = fPhiBins[etaBin];
[608]302
[730]303 // calculate eta and phi of the tower's center
[731]304 fTowerEta = 0.5*(fEtaBins[etaBin - 1] + fEtaBins[etaBin]);
305 fTowerPhi = 0.5*((*phiBins)[phiBin - 1] + (*phiBins)[phiBin]);
[626]306
[898]307 fTowerEdges[0] = fEtaBins[etaBin - 1];
308 fTowerEdges[1] = fEtaBins[etaBin];
309 fTowerEdges[2] = (*phiBins)[phiBin - 1];
310 fTowerEdges[3] = (*phiBins)[phiBin];
311
[735]312 fTowerECalEnergy = 0.0;
313 fTowerHCalEnergy = 0.0;
314
315 fTowerECalNeutralEnergy = 0.0;
316 fTowerHCalNeutralEnergy = 0.0;
[935]317
[886]318 fTowerNeutralHits = 0;
319 fTowerPhotonHits = 0;
[925]320 fTowerElectronHits = 0;
[886]321 fTowerTrackHits = 0;
322 fTowerAllHits = 0;
[935]323
[886]324 fTowerTrackArray->Clear();
[931]325 fTowerPhotonArray->Clear();
[661]326 }
327
[886]328 // check for track hits
[925]329 if(flags & 8)
[886]330 {
331 ++fTowerTrackHits;
332 track = static_cast<Candidate*>(fTrackInputArray->At(number));
333 fTowerTrackArray->Add(track);
334 continue;
335 }
336
337 particle = static_cast<Candidate*>(fParticleInputArray->At(number));
[888]338 momentum = particle->Momentum;
[886]339
[735]340 // fill current tower
341 ecalEnergy = momentum.E() * fECalFractions[number];
342 hcalEnergy = momentum.E() * fHCalFractions[number];
[931]343
[735]344 fTowerECalEnergy += ecalEnergy;
345 fTowerHCalEnergy += hcalEnergy;
[695]346
[931]347 ++fTowerAllHits;
[935]348 fTower->AddCandidate(particle);
[735]349
[730]350 // check for neutral hits in current tower
[886]351 if(flags & 1) ++fTowerNeutralHits;
[931]352
[730]353 // check for photon hits in current tower
[931]354 if(flags & 2)
355 {
356 ++fTowerPhotonHits;
357 fTowerPhotonArray->Add(particle);
358 }
[886]359
[925]360 // check for electron hits in current tower
361 if(flags & 4) ++fTowerElectronHits;
[608]362 }
363
[661]364 // finalize last tower
[730]365 FinalizeTower();
366}
367
368//------------------------------------------------------------------------------
[931]369
[730]370void Calorimeter::FinalizeTower()
371{
[934]372 Candidate *particle, *track, *tower;
[1078]373 Double_t energy, pt, eta, phi;
[735]374 Double_t ecalEnergy, hcalEnergy;
[1145]375
[734]376 if(!fTower) return;
[733]377
[1142]378// ecalEnergy = gRandom->Gaus(fTowerECalEnergy, fECalResolutionFormula->Eval(0.0, fTowerEta, 0.0, fTowerECalEnergy));
[1145]379// if(ecalEnergy < 0.0) ecalEnergy = 0.0;
[733]380
[1145]381 ecalEnergy = LogNormal(fTowerECalEnergy, fECalResolutionFormula->Eval(0.0, fTowerEta, 0.0, fTowerECalEnergy));
[733]382
[1145]383// hcalEnergy = gRandom->Gaus(fTowerHCalEnergy, fHCalResolutionFormula->Eval(0.0, fTowerEta, 0.0, fTowerHCalEnergy));
384// if(hcalEnergy < 0.0) hcalEnergy = 0.0;
385
386 hcalEnergy = LogNormal(fTowerHCalEnergy, fHCalResolutionFormula->Eval(0.0, fTowerEta, 0.0, fTowerHCalEnergy));
387
[735]388 energy = ecalEnergy + hcalEnergy;
[629]389
[1145]390// eta = fTowerEta;
391// phi = fTowerPhi;
[1078]392
393 eta = gRandom->Uniform(fTowerEdges[0], fTowerEdges[1]);
394 phi = gRandom->Uniform(fTowerEdges[2], fTowerEdges[3]);
395
[1086]396 pt = energy / TMath::CosH(eta);
397
[1078]398 fTower->Position.SetPtEtaPhiE(1.0, eta, phi, 0.0);
399 fTower->Momentum.SetPtEtaPhiE(pt, eta, phi, energy);
[735]400 fTower->Eem = ecalEnergy;
401 fTower->Ehad = hcalEnergy;
[734]402
[898]403 fTower->Edges[0] = fTowerEdges[0];
404 fTower->Edges[1] = fTowerEdges[1];
405 fTower->Edges[2] = fTowerEdges[2];
406 fTower->Edges[3] = fTowerEdges[3];
[886]407
[931]408 // fill calorimeter towers and photon candidates
409 if(energy > 0.0)
410 {
411 if((fTowerPhotonHits > 0 || fTowerElectronHits > 0) &&
412 fTowerTrackHits == 0)
[935]413 {
[931]414 fPhotonOutputArray->Add(fTower);
415 }
416
417 fTowerOutputArray->Add(fTower);
418 }
[935]419
[931]420 // fill energy flow candidates
[886]421 if(fTowerTrackHits == fTowerAllHits)
[735]422 {
[886]423 fItTowerTrackArray->Reset();
424 while((track = static_cast<Candidate*>(fItTowerTrackArray->Next())))
425 {
[938]426 fEFlowTrackOutputArray->Add(track);
[886]427 }
428 }
[931]429 else if(fTowerTrackHits > 0 &&
430 fTowerElectronHits == 0 &&
431 fTowerPhotonHits + fTowerTrackHits == fTowerAllHits)
432 {
433 fItTowerTrackArray->Reset();
434 while((track = static_cast<Candidate*>(fItTowerTrackArray->Next())))
435 {
[938]436 fEFlowTrackOutputArray->Add(track);
[931]437 }
[735]438
[931]439 if(ecalEnergy > 0.0)
440 {
441 DelphesFactory *factory = GetFactory();
[935]442
[931]443 // create new tower
444 tower = factory->NewCandidate();
[735]445
[934]446 fItTowerPhotonArray->Reset();
447 while((particle = static_cast<Candidate*>(fItTowerPhotonArray->Next())))
448 {
449 tower->AddCandidate(particle);
450 }
[935]451
[1086]452 pt = ecalEnergy / TMath::CosH(eta);
[1084]453
[1078]454 tower->Position.SetPtEtaPhiE(1.0, eta, phi, 0.0);
455 tower->Momentum.SetPtEtaPhiE(pt, eta, phi, ecalEnergy);
[931]456 tower->Eem = ecalEnergy;
457 tower->Ehad = 0.0;
458
459 tower->Edges[0] = fTowerEdges[0];
460 tower->Edges[1] = fTowerEdges[1];
461 tower->Edges[2] = fTowerEdges[2];
462 tower->Edges[3] = fTowerEdges[3];
463
[938]464 fEFlowTowerOutputArray->Add(tower);
[931]465 }
466 }
467 else if(energy > 0.0)
468 {
[938]469 fEFlowTowerOutputArray->Add(fTower);
[931]470 }
[608]471}
472
473//------------------------------------------------------------------------------
[1142]474
[1145]475Double_t Calorimeter::LogNormal(Double_t mean, Double_t sigma)
476{
477 Double_t a, b;
478
479 if(mean > 0.0)
[1142]480 {
[1145]481 b = TMath::Sqrt(TMath::Log((1.0 + (sigma*sigma)/(mean*mean))));
482 a = TMath::Log(mean) - 0.5*b*b;
483
484 return TMath::Exp(a + b*gRandom->Gaus(0, 1));
[1142]485 }
[1145]486 else
487 {
488 return 0.0;
489 }
490}
Note: See TracBrowser for help on using the repository browser.