Fork me on GitHub

source: git/display/Delphes3DGeometry.cc@ a3b2495

ImprovedOutputFile Timing dual_readout llp
Last change on this file since a3b2495 was cfc3160, checked in by Christophe Delaere <christophe.delaere@…>, 10 years ago

Migration of the code from script to library

The code still crashes, but debugging will be much easier. Also much
faster!

  • Property mode set to 100644
File size: 18.2 KB
Line 
1#include "display/Delphes3DGeometry.h"
2#include <set>
3#include <map>
4#include <utility>
5#include <vector>
6#include <algorithm>
7#include <sstream>
8#include <cassert>
9#include "TGeoManager.h"
10#include "TGeoVolume.h"
11#include "TGeoMedium.h"
12#include "TGeoNode.h"
13#include "TGeoCompositeShape.h"
14#include "TGeoMatrix.h"
15#include "TGeoTube.h"
16#include "TGeoCone.h"
17#include "TGeoArb8.h"
18#include "external/ExRootAnalysis/ExRootConfReader.h"
19#include "classes/DelphesClasses.h"
20#include "TF2.h"
21#include "TH1F.h"
22#include "TMath.h"
23
24using namespace std;
25
26Delphes3DGeometry::Delphes3DGeometry(TGeoManager *geom) {
27
28 //--- the geometry manager
29 geom_ = geom==NULL? gGeoManager : geom;
30 //gGeoManager->DefaultColors();
31
32 //--- define some materials
33 TGeoMaterial *matVacuum = new TGeoMaterial("Vacuum", 0,0,0);
34 TGeoMaterial *matAl = new TGeoMaterial("Al", 26.98,13,2.7); // placeholder
35 matVacuum->SetTransparency(85);
36 matAl->SetTransparency(85);
37
38 //--- define some media
39 TGeoMedium *Vacuum = new TGeoMedium("Vacuum",1, matVacuum);
40 TGeoMedium *Al = new TGeoMedium("Root Material",2, matAl);
41 vacuum_ = Vacuum;
42 tkmed_ = Vacuum; // placeholder
43 calomed_ = Al; // placeholder
44 mudetmed_ = Al; // placeholder
45
46 // custom parameters
47 contingency_ = 10.;
48 calo_barrel_thickness_ = 50.;
49 calo_endcap_thickness_ = 75.;
50 muonSystem_thickn_ = 10.;
51
52 // read these parameters from the Delphes Card (with default values)
53 etaAxis_ = NULL;
54 phiAxis_ = NULL;
55 tk_radius_ = 120.;
56 tk_length_ = 150.;
57 tk_etamax_ = 3.0;
58 tk_Bz_ = 1.;
59 muonSystem_radius_ = 200.;
60}
61
62void Delphes3DGeometry::readFile(const char *configFile,
63 const char* ParticlePropagator, const char* TrackingEfficiency,
64 const char* MuonEfficiency, const char* Calorimeters) {
65
66 ExRootConfReader *confReader = new ExRootConfReader;
67 confReader->ReadFile(configFile);
68
69 tk_radius_ = confReader->GetDouble(Form("%s::Radius",ParticlePropagator), 1.0)*100.; // tk_radius
70 tk_length_ = confReader->GetDouble(Form("%s::HalfLength",ParticlePropagator), 3.0)*100.; // tk_length
71 tk_Bz_ = confReader->GetDouble("ParticlePropagator::Bz", 0.0); // tk_Bz
72
73 {
74 TString tkEffFormula = confReader->GetString(Form("%s::EfficiencyFormula",TrackingEfficiency),"abs(eta)<3.0");
75 tkEffFormula.ReplaceAll("pt","x");
76 tkEffFormula.ReplaceAll("eta","y");
77 tkEffFormula.ReplaceAll("phi","0.");
78 TF2* tkEffFunction = new TF2("tkEff",tkEffFormula,0,1000,-10,10);
79 TH1F etaHisto("eta","eta",100,5.,-5.);
80 Double_t pt,eta;
81 for(int i=0;i<1000;++i) {
82 tkEffFunction->GetRandom2(pt,eta);
83 etaHisto.Fill(eta);
84 }
85 Int_t bin = -1;
86 bin = etaHisto.FindFirstBinAbove(0.5);
87 Double_t etamin = (bin>-1) ? etaHisto.GetBinLowEdge(bin) : -10.;
88 bin = etaHisto.FindLastBinAbove(0.5);
89 Double_t etamax = (bin>-1) ? etaHisto.GetBinLowEdge(bin+1) : -10.;
90 tk_etamax_ = TMath::Max(fabs(etamin),fabs(etamax)); // tk_etamax
91 delete tkEffFunction;
92 }
93
94 {
95 muondets_.push_back("muons");
96 TString muonEffFormula = confReader->GetString(Form("%s::EfficiencyFormula",MuonEfficiency),"abs(eta)<2.0");
97 muonEffFormula.ReplaceAll("pt","x");
98 muonEffFormula.ReplaceAll("eta","y");
99 muonEffFormula.ReplaceAll("phi","0.");
100 TF2* muEffFunction = new TF2("muEff",muonEffFormula,0,1000,-10,10);
101 TH1F etaHisto("eta2","eta2",100,5.,-5.);
102 Double_t pt,eta;
103 for(int i=0;i<1000;++i) {
104 muEffFunction->GetRandom2(pt,eta);
105 etaHisto.Fill(eta);
106 }
107 Int_t bin = -1;
108 bin = etaHisto.FindFirstBinAbove(0.5);
109 Double_t etamin = (bin>-1) ? etaHisto.GetBinLowEdge(bin) : -10.;
110 bin = etaHisto.FindLastBinAbove(0.5);
111 Double_t etamax = (bin>-1) ? etaHisto.GetBinLowEdge(bin+1) : -10.;
112 muonSystem_etamax_["muons"] = TMath::Max(fabs(etamin),fabs(etamax)); // muonSystem_etamax
113 delete muEffFunction;
114 }
115
116 std::string s(Calorimeters);
117 std::replace( s.begin(), s.end(), ',', ' ' );
118 std::istringstream stream( s );
119 std::string word;
120 while (stream >> word) calorimeters_.push_back(word);
121
122 caloBinning_.clear(); // calo binning
123 for(std::vector<std::string>::const_iterator calo=calorimeters_.begin();calo!=calorimeters_.end(); ++calo) {
124 set< pair<Double_t, Int_t> > caloBinning;
125 ExRootConfParam paramEtaBins, paramPhiBins;
126 ExRootConfParam param = confReader->GetParam(Form("%s::EtaPhiBins",calo->c_str()));
127 Int_t size = param.GetSize();
128 for(int i = 0; i < size/2; ++i) {
129 paramEtaBins = param[i*2];
130 paramPhiBins = param[i*2+1];
131 assert(paramEtaBins.GetSize()==1);
132 caloBinning.insert(std::make_pair(paramEtaBins[0].GetDouble(),paramPhiBins.GetSize()-1));
133 }
134 caloBinning_[*calo] = caloBinning;
135 }
136
137 set< pair<Double_t, Int_t> > caloBinning = caloBinning_[*calorimeters_.begin()];
138 Double_t *etaBins = new Double_t[caloBinning.size()]; // note that this is the eta binning of the first calo
139 unsigned int ii = 0;
140 for(set< pair<Double_t, Int_t> >::const_iterator itEtaSet = caloBinning.begin(); itEtaSet != caloBinning.end(); ++itEtaSet) {
141 etaBins[ii++] = itEtaSet->first;
142 }
143 etaAxis_ = new TAxis(caloBinning.size() - 1, etaBins);
144 phiAxis_ = new TAxis(72, -TMath::Pi(), TMath::Pi()); // note that this is fixed while #phibins could vary, also with eta, which doesn't seem possible in ROOT
145
146 muonSystem_radius_ = tk_radius_ + contingency_ + (contingency_+calo_barrel_thickness_)*calorimeters_.size() + muonSystem_thickn_;
147 muonSystem_length_ = tk_length_ + contingency_ + (contingency_+calo_endcap_thickness_)*calorimeters_.size() + muonSystem_thickn_;
148
149 delete confReader;
150
151}
152
153TGeoVolume* Delphes3DGeometry::getDetector(bool withTowers) {
154 // compute the envelope
155 Double_t system_radius = tk_radius_+calo_barrel_thickness_+3*contingency_;
156 Double_t system_length = tk_length_+contingency_+(contingency_+calo_endcap_thickness_)*calorimeters_.size()+contingency_;
157 // the detector volume
158 TGeoVolume *top = geom_->MakeBox("Delphes3DGeometry", vacuum_, system_radius, system_radius, system_length);
159 // build the detector
160 std::pair<Double_t, Double_t> limits = addTracker(top);
161 Double_t radius = limits.first;
162 Double_t length = limits.second;
163 for(std::vector<std::string>::const_iterator calo = calorimeters_.begin(); calo != calorimeters_.end(); ++calo) {
164 limits = addCalorimeter(top,calo->c_str(),radius,length,caloBinning_[*calo]);
165 if (withTowers) {
166 addCaloTowers(top,calo->c_str(),radius,length,caloBinning_[*calo]);
167 }
168 radius = limits.first;
169 length = limits.second;
170 }
171 for(std::vector<std::string>::const_iterator muon = muondets_.begin(); muon != muondets_.end(); ++muon) {
172 limits = addMuonDets(top, muon->c_str(), radius, length);
173 radius = limits.first;
174 length = limits.second;
175 }
176 // return the result
177 return top;
178}
179
180std::pair<Double_t, Double_t> Delphes3DGeometry::addTracker(TGeoVolume *top) {
181 // tracker: a cylinder with two cones substracted
182 new TGeoCone("forwardTkAcceptance",(tk_length_/2.+0.05),0.,tk_radius_,(tk_length_)*2.*exp(-tk_etamax_)/(1-exp(-2.*tk_etamax_)),tk_radius_);
183 TGeoTranslation *tr1 = new TGeoTranslation("tkacc1",0., 0., tk_length_/2.);
184 tr1->RegisterYourself();
185 TGeoRotation *negz = new TGeoRotation("tknegz",0,180,0);
186 negz->RegisterYourself();
187 TGeoCombiTrans *tr2 = new TGeoCombiTrans("tkacc2",0.,0.,-tk_length_/2.,negz);
188 tr2->RegisterYourself();
189 TGeoCompositeShape* tracker_cs = new TGeoCompositeShape("tracker_cs","forwardTkAcceptance:tkacc1+forwardTkAcceptance:tkacc2");
190 TGeoVolume *tracker = new TGeoVolume("tracker",tracker_cs,tkmed_);
191 tracker->SetLineColor(kYellow);
192 top->AddNode(tracker,1);
193 return std::make_pair(tk_radius_,tk_length_);
194}
195
196std::pair<Double_t, Double_t> Delphes3DGeometry::addCalorimeter(TGeoVolume *top, const char* name,
197 Double_t innerBarrelRadius, Double_t innerBarrelLength, set< pair<Double_t, Int_t> >& caloBinning) {
198 // parameters derived from the inputs
199 Double_t calo_endcap_etamax = TMath::Max(fabs(caloBinning.begin()->first),fabs(caloBinning.rbegin()->first));
200 Double_t calo_barrel_innerRadius = innerBarrelRadius+contingency_;
201 Double_t calo_barrel_length = innerBarrelLength + calo_barrel_thickness_;
202 Double_t calo_endcap_etamin = -log(innerBarrelRadius/(2*innerBarrelLength));
203 Double_t calo_endcap_innerRadius1 = innerBarrelLength*2.*exp(-calo_endcap_etamax)/(1-exp(-2.*calo_endcap_etamax));
204 Double_t calo_endcap_innerRadius2 = (innerBarrelLength+calo_endcap_thickness_)*2.*exp(-calo_endcap_etamax)/(1-exp(-2.*calo_endcap_etamax));
205 Double_t calo_endcap_outerRadius1 = innerBarrelRadius;
206 Double_t calo_endcap_outerRadius2 = innerBarrelRadius+calo_barrel_thickness_;
207 Double_t calo_endcap_coneThickness = TMath::Min(calo_barrel_thickness_ * (1-exp(-2.*calo_endcap_etamin)) / (2.*exp(-calo_endcap_etamin)), calo_endcap_thickness_);
208 Double_t calo_endcap_diskThickness = TMath::Max(0.,calo_endcap_thickness_-calo_endcap_coneThickness);
209
210 // calorimeters: tube truncated in eta + cones
211 new TGeoTube(Form("%s_barrel_cylinder",name),calo_barrel_innerRadius,calo_barrel_innerRadius+calo_barrel_thickness_,calo_barrel_length);
212 new TGeoCone(Form("%s_endcap_cone",name),calo_endcap_coneThickness/2.,calo_endcap_innerRadius1,calo_endcap_outerRadius1,calo_endcap_innerRadius2,calo_endcap_outerRadius2);
213 new TGeoTube(Form("%s_endcap_disk",name),calo_endcap_innerRadius2,tk_radius_+calo_barrel_thickness_,calo_endcap_diskThickness/2.);
214 TGeoTranslation *tr1 = new TGeoTranslation(Form("%s_tr1",name),0., 0., (calo_endcap_coneThickness+calo_endcap_diskThickness)/2.);
215 tr1->RegisterYourself();
216 TGeoCompositeShape *calo_endcap_cs = new TGeoCompositeShape(Form("%s_endcap_cs",name),Form("%s_endcap_cone+%s_endcap_disk:%s_tr1",name,name,name));
217 TGeoTranslation *trc1 = new TGeoTranslation(Form("%s_endcap1_position",name),0.,0., innerBarrelLength+calo_endcap_coneThickness/2.);
218 trc1->RegisterYourself();
219 TGeoRotation *negz = new TGeoRotation(Form("%s_negz",name),0,180,0);
220 TGeoCombiTrans *trc2 = new TGeoCombiTrans(Form("%s_endcap2_position",name),0.,0.,-(innerBarrelLength+calo_endcap_coneThickness/2.),negz);
221 trc2->RegisterYourself();
222 TGeoTranslation *trc1c = new TGeoTranslation(Form("%s_endcap1_position_cont",name),0.,0., innerBarrelLength+calo_endcap_coneThickness/2.+contingency_);
223 trc1c->RegisterYourself();
224 TGeoCombiTrans *trc2c = new TGeoCombiTrans(Form("%s_endcap2_position_cont",name),0.,0.,-(innerBarrelLength+calo_endcap_coneThickness/2.)-contingency_,negz);
225 trc2c->RegisterYourself();
226 TGeoVolume *calo_endcap = new TGeoVolume(Form("%s_endcap",name),calo_endcap_cs,calomed_);
227 TGeoCompositeShape *calo_barrel_cs = new TGeoCompositeShape(Form("%s_barrel_cs",name),
228 Form("%s_barrel_cylinder-%s_endcap_cs:%s_endcap1_position-%s_endcap_cs:%s_endcap2_position",name,name,name,name,name));
229 TGeoVolume *calo_barrel = new TGeoVolume(Form("%s_barrel",name),calo_barrel_cs,calomed_);
230 calo_endcap->SetLineColor(kViolet);
231 calo_endcap->SetFillColor(kViolet);
232 calo_barrel->SetLineColor(kRed);
233 top->AddNode(calo_endcap,1,trc1c);
234 top->AddNode(calo_endcap,2,trc2c);
235 top->AddNode(calo_barrel,1);
236 return std::make_pair(calo_barrel_innerRadius+calo_barrel_thickness_,innerBarrelLength+calo_endcap_thickness_+contingency_);
237}
238
239std::pair<Double_t, Double_t> Delphes3DGeometry::addMuonDets(TGeoVolume *top, const char* name, Double_t innerBarrelRadius, Double_t innerBarrelLength) {
240 // muon system: tube + disks
241 Double_t muonSystem_radius = innerBarrelRadius + contingency_;
242 Double_t muonSystem_length = innerBarrelLength + contingency_;
243 Double_t muonSystem_rmin = muonSystem_length*2.*exp(-muonSystem_etamax_[name])/(1-exp(-2.*muonSystem_etamax_[name]));
244 TGeoVolume *muon_barrel = geom_->MakeTube(Form("%s_barrel",name),mudetmed_,muonSystem_radius,muonSystem_radius+muonSystem_thickn_,muonSystem_length);
245 muon_barrel->SetLineColor(kBlue);
246 top->AddNode(muon_barrel,1);
247 TGeoVolume *muon_endcap = geom_->MakeTube(Form("%s_endcap",name),mudetmed_,muonSystem_rmin,muonSystem_radius+muonSystem_thickn_,muonSystem_thickn_/2.);
248 muon_endcap->SetLineColor(kBlue);
249 TGeoTranslation *trm1 = new TGeoTranslation(Form("%sEndcap1_position",name),0.,0.,muonSystem_length);
250 trm1->RegisterYourself();
251 TGeoTranslation *trm2 = new TGeoTranslation(Form("%sEndcap2_position",name),0.,0.,-muonSystem_length);
252 trm1->RegisterYourself();
253 top->AddNode(muon_endcap,1,trm1);
254 top->AddNode(muon_endcap,2,trm2);
255 return std::make_pair(muonSystem_radius,muonSystem_length);
256}
257
258void Delphes3DGeometry::addCaloTowers(TGeoVolume *top, const char* name,
259 Double_t innerBarrelRadius, Double_t innerBarrelLength, set< pair<Double_t, Int_t> >& caloBinning) {
260
261 TGeoVolume* calo_endcap = top->GetNode(Form("%s_endcap_1",name))->GetVolume();
262 TGeoVolume* calo_barrel = top->GetNode(Form("%s_barrel_1",name))->GetVolume();
263 Double_t calo_endcap_etamin = -log(innerBarrelRadius/(2*innerBarrelLength));
264 Double_t calo_endcap_coneThickness = TMath::Min(calo_barrel_thickness_ * (1-exp(-2.*calo_endcap_etamin)) / (2.*exp(-calo_endcap_etamin)), calo_endcap_thickness_);
265
266 // calo towers in the barrel
267 Double_t vertices[16] = {0.,0.,0.,0.,0.,0.,0.,0.}; // summit of the pyramid
268 Double_t R = tk_radius_ + contingency_+(contingency_+calo_barrel_thickness_)*calorimeters_.size(); // radius of the muons system = height of the pyramid
269 Int_t nEtaBins = caloBinning.size();
270 // this rotation is to make the tower point "up"
271 TGeoRotation* initTowerRot = new TGeoRotation(Form("%s_initTowerRot",name),0.,90.,0.);
272 TGeoCombiTrans* initTower = new TGeoCombiTrans(Form("%s_initTower",name),0.,-R/2.,0.,initTowerRot);
273 initTower->RegisterYourself();
274 // eta bins... we build one pyramid per eta slice and then translate it nphi times.
275 // phi bins represented by rotations around z
276 Double_t *y = new Double_t[nEtaBins];
277 Double_t *dx = new Double_t[nEtaBins];
278 Int_t *nphi = new Int_t[nEtaBins];
279 Int_t etaslice = 0;
280 std::map<std::pair<int,int>, TGeoRotation*> phirotations;
281 for(set< pair<Double_t, Int_t> >::const_iterator bin=caloBinning.begin(); bin!=caloBinning.end();++bin) {
282 if(abs(bin->first)>calo_endcap_etamin) continue; // only in the barrel
283 nphi[etaslice] = bin->second;
284 y[etaslice] = 0.5*R*(1-exp(-2*bin->first))/exp(-bin->first);
285 Double_t phiRotationAngle = 360./nphi[etaslice];
286 dx[etaslice] = R*tan(TMath::Pi()*phiRotationAngle/360.);
287 for(int phislice=0;phislice<nphi[etaslice];++phislice) {
288 phirotations[make_pair(etaslice,phislice)] = new TGeoRotation(Form("%s_phi%d_%d",name,etaslice,phislice),phiRotationAngle*phislice,0.,0.);
289 phirotations[make_pair(etaslice,phislice)]->RegisterYourself();
290 }
291 ++etaslice;
292 }
293 nEtaBins = etaslice;
294 for(int i=0;i<nEtaBins-1;++i) { // loop on the eta slices
295 vertices[8] = -dx[i]; vertices[9] = y[i];
296 vertices[10] = -dx[i]; vertices[11] = y[i+1];
297 vertices[12] = dx[i]; vertices[13] = y[i+1];
298 vertices[14] = dx[i]; vertices[15] = y[i];
299 new TGeoArb8(Form("%s_tower%d",name,i),R/2., vertices); // tower in the proper eta slice, at phi=0
300 // intersection between the tower and the calo_barrel
301 TGeoCompositeShape *finaltower_cs = new TGeoCompositeShape(Form("%s_ftower%d_cs",name,i),Form("%s_tower%d:%s_initTower*%s_barrel_cs",name,i,name,name));
302 TGeoVolume *finaltower = new TGeoVolume(Form("%s_ftower%d",name,i),finaltower_cs,calomed_);
303 finaltower->SetLineColor(kRed);
304 for(int j=0;j<nphi[i];++j) { // loop on the phi slices
305 calo_barrel->AddNode(finaltower,j,phirotations[make_pair(i,j)]);
306 }
307 }
308 delete[] y;
309 delete[] dx;
310 delete[] nphi;
311 //the towers in the forward region
312 R = tk_length_+contingency_+(contingency_+calo_endcap_thickness_)*calorimeters_.size(); // Z of the muons system = height of the pyramid
313 nEtaBins = caloBinning.size();
314 // translation to bring the origin of the tower to (0,0,0) (well, not really as the endcap is not yet in place)
315 TGeoTranslation* towerdz = new TGeoTranslation(Form("%s_towerdz",name),0.,0.,R/2.-(innerBarrelLength+calo_endcap_coneThickness/2.));
316 towerdz->RegisterYourself();
317 // eta bins... we build one pyramid per eta slice and then translate it nphi times.
318 Double_t *r = new Double_t[nEtaBins];
319 nphi = new Int_t[nEtaBins];
320 etaslice = 0;
321 phirotations.clear();
322 for(set< pair<Double_t, Int_t> >::const_iterator bin=caloBinning.begin(); bin!=caloBinning.end();++bin) {
323 if(bin->first<calo_endcap_etamin) continue; // only in the + endcap
324 r[etaslice] = R*2*exp(-bin->first)/(1-exp(-2*bin->first));
325 nphi[etaslice] = bin->second;
326 Double_t phiRotationAngle = 360./nphi[etaslice];
327 for(int phislice=0;phislice<nphi[etaslice];++phislice) {
328 phirotations[make_pair(etaslice,phislice)] = new TGeoRotation(Form("%s_forward_phi%d_%d",name,etaslice,phislice),phiRotationAngle*phislice,0.,0.);
329 phirotations[make_pair(etaslice,phislice)]->RegisterYourself();
330 }
331 ++etaslice;
332 }
333 nEtaBins = etaslice;
334 for(int i=0;i<nEtaBins-1;++i) { // loop on the eta slices
335 vertices[8] = -r[i+1]*sin(TMath::Pi()/nphi[i]); vertices[9] = r[i+1]*cos(TMath::Pi()/nphi[i]);
336 vertices[10] = -r[i]*sin(TMath::Pi()/nphi[i]); vertices[11] = r[i]*cos(TMath::Pi()/nphi[i]);
337 vertices[12] = r[i]*sin(TMath::Pi()/nphi[i]); vertices[13] = r[i]*cos(TMath::Pi()/nphi[i]);
338 vertices[14] = r[i+1]*sin(TMath::Pi()/nphi[i]); vertices[15] = r[i+1]*cos(TMath::Pi()/nphi[i]);
339 new TGeoArb8(Form("%sfwdtower%d",name,i),R/2., vertices); // tower in the proper eta slice, at phi=0
340 // intersection between the tower and the calo_endcap
341 TGeoCompositeShape *finalfwdtower_cs = new TGeoCompositeShape(Form("%sffwdtower%d_cs",name,i),Form("%sfwdtower%d:%s_towerdz*%s_endcap_cs",name,i,name,name));
342 TGeoVolume *finalfwdtower = new TGeoVolume(Form("%sffwdtower%d",name,i),finalfwdtower_cs,calomed_);
343 finalfwdtower->SetLineColor(kViolet);
344 for(int j=0;j<nphi[i];++j) { // loop on the phi slices
345 calo_endcap->AddNode(finalfwdtower,j,phirotations[make_pair(i,j)]);
346 }
347 }
348 delete[] r;
349 delete[] nphi;
350}
351
Note: See TracBrowser for help on using the repository browser.