FairShip
Loading...
Searching...
No Matches
strawtubes.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5// Construction of SST tracker stations
6// Last update: 15 Aug 2025
7// Contact: W.-C. Marty Lee <wei-chieh.lee@desy.de>
8
9#include "strawtubes.h"
10
11#include <array>
12#include <iostream>
13#include <sstream>
14#include <unordered_map>
15#include <utility>
16
17#include "FairGeoBuilder.h"
18#include "FairGeoInterface.h"
19#include "FairGeoLoader.h"
20#include "FairGeoMedia.h"
21#include "FairGeoNode.h"
22#include "FairGeoVolume.h"
23#include "FairLogger.h"
24#include "FairRootManager.h"
25#include "FairRun.h"
26#include "FairRuntimeDb.h"
27#include "FairVolume.h"
28#include "ShipDetectorList.h"
29#include "ShipGeoUtil.h"
30#include "ShipStack.h"
31#include "TClonesArray.h"
32#include "TGeoBBox.h"
33#include "TGeoCompositeShape.h"
34#include "TGeoManager.h"
35#include "TGeoMaterial.h"
36#include "TGeoMedium.h"
37#include "TGeoTube.h"
38#include "TMath.h"
39#include "TParticle.h"
40#include "TVector3.h"
41#include "TVirtualMC.h"
42#include "strawtubesPoint.h"
43using std::cout;
44using std::endl;
45
47 : Detector("strawtubes", kTRUE, kStraw), fMedium("air") {}
48
49strawtubes::strawtubes(std::string medium)
50 : Detector("strawtubes", kTRUE, kStraw), fMedium(std::move(medium)) {}
51
52strawtubes::strawtubes(const char* name, Bool_t active)
53 : Detector(name, active, kStraw) {}
54
55Bool_t strawtubes::ProcessHits(FairVolume* vol) {
57 // Set parameters at entrance of volume. Reset ELoss.
58 if (gMC->IsTrackEntering()) {
59 fELoss = 0.;
60 fTime = gMC->TrackTime() * 1.0e09;
61 fLength = gMC->TrackLength();
62 gMC->TrackPosition(fPos);
63 gMC->TrackMomentum(fMom);
64 // Reset the "already recorded" marker so a later traversal of the same
65 // straw (curling track, next track/event) is not silently dropped.
66 fVolumeID = -1;
67 }
68 // Sum energy loss for all steps in the active volume
69 fELoss += gMC->Edep();
70
71 // Create strawtubesPoint at exit of active volume
72 if (gMC->IsTrackExiting() || gMC->IsTrackStop() ||
73 gMC->IsTrackDisappeared()) {
74 if (fELoss == 0.) {
75 return kFALSE;
76 }
77 TParticle* p = gMC->GetStack()->GetCurrentTrack();
78 Int_t pdgCode = p->GetPdgCode();
79 fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
80 fEventID = gMC->CurrentEvent();
81 Int_t straw_uniqueId;
82 gMC->CurrentVolID(straw_uniqueId);
83 if (fVolumeID == straw_uniqueId) {
84 // std::cout << pdgCode<< " same volume again ? "<< straw_uniqueId << "
85 // exit:" << gMC->IsTrackExiting() << " stop:" << gMC->IsTrackStop() << "
86 // disappeared:" << gMC->IsTrackDisappeared()<< std::endl;
87 return kTRUE;
88 }
89 fVolumeID = straw_uniqueId;
90 // # d = |pq . u x v|/|u x v|
91 TVector3 bot, top;
92 StrawEndPoints(straw_uniqueId, bot, top);
93 TLorentzVector Pos;
94 gMC->TrackPosition(Pos);
95 Double_t xmean = (fPos.X() + Pos.X()) / 2.;
96 Double_t ymean = (fPos.Y() + Pos.Y()) / 2.;
97 Double_t zmean = (fPos.Z() + Pos.Z()) / 2.;
98 TVector3 pq = TVector3(top.x() - xmean, top.y() - ymean, top.z() - zmean);
99 TVector3 u =
100 TVector3(bot.x() - top.x(), bot.y() - top.y(), bot.z() - top.z());
101 TVector3 v =
102 TVector3(fPos.X() - Pos.X(), fPos.Y() - Pos.Y(), fPos.Z() - Pos.Z());
103 TVector3 uCrossv = u.Cross(v);
104 Double_t dist2Wire = fabs(pq.Dot(uCrossv)) / (uCrossv.Mag() + 1E-8);
105 Double_t deltaTrackLength = gMC->TrackLength() - fLength;
106 AddHit(fEventID, fTrackID, straw_uniqueId, TVector3(xmean, ymean, zmean),
107 TVector3(fMom.Px(), fMom.Py(), fMom.Pz()), fTime, deltaTrackLength,
108 fELoss, pdgCode, dist2Wire);
109 if (dist2Wire > f_inner_straw_diameter / 2) {
110 std::cout << "addhit " << dist2Wire << " straw id " << straw_uniqueId
111 << " pdgcode " << pdgCode << " dot prod " << pq.Dot(uCrossv)
112 << std::endl;
113 std::cout << " exit:" << gMC->IsTrackExiting()
114 << " stop:" << gMC->IsTrackStop()
115 << " disappeared:" << gMC->IsTrackDisappeared() << std::endl;
116 std::cout << " entry:" << fPos.X() << " " << fPos.Y() << " " << fPos.Z()
117 << std::endl;
118 std::cout << " exit:" << Pos.X() << " " << Pos.Y() << " " << Pos.Z()
119 << std::endl;
120 std::cout << " mean:" << xmean << " " << ymean << " " << zmean
121 << std::endl;
122 std::cout << " bot:" << bot.x() << " " << bot.y() << " " << bot.z()
123 << std::endl;
124 std::cout << " top:" << top.x() << " " << top.y() << " " << top.z()
125 << std::endl;
126 pq.Print();
127 u.Print();
128 v.Print();
129 uCrossv.Print();
130 }
131 // Increment number of strawtubes det points in TParticle
132 ShipStack* stack = dynamic_cast<ShipStack*>(gMC->GetStack());
133 stack->AddPoint(kStraw);
134 }
135 return kTRUE;
136}
137
138void strawtubes::SetzPositions(Double_t z1, Double_t z2, Double_t z3,
139 Double_t z4) {
140 f_T1_z = z1;
141 f_T2_z = z2;
142 f_T3_z = z3;
143 f_T4_z = z4;
144}
145
146void strawtubes::SetApertureArea(Double_t width, Double_t height) {
147 f_aperture_width = width;
148 f_aperture_height = height;
149}
150
151void strawtubes::SetStrawDiameter(Double_t outer_straw_diameter,
152 Double_t wall_thickness) {
153 f_outer_straw_diameter = outer_straw_diameter;
155 outer_straw_diameter - 2 * wall_thickness;
156}
157
158void strawtubes::SetStrawPitch(Double_t straw_pitch, Double_t layer_offset) {
159 f_straw_pitch = straw_pitch;
160 f_offset_layer = layer_offset;
161}
162
163void strawtubes::SetDeltazLayer(Double_t delta_z_layer) {
164 f_delta_z_layer = delta_z_layer;
165}
166
167void strawtubes::SetStereoAngle(Double_t stereo_angle) {
168 f_view_angle = stereo_angle;
169}
170
171void strawtubes::SetWireThickness(Double_t wire_thickness) {
172 f_wire_thickness = wire_thickness;
173}
174
175void strawtubes::SetDeltazView(Double_t delta_z_view) {
176 f_delta_z_view = delta_z_view;
177}
178
179void strawtubes::SetFrameMaterial(TString frame_material) {
180 f_frame_material = std::move(frame_material);
181}
182
183void strawtubes::SetStationEnvelope(Double_t x, Double_t y, Double_t z) {
184 f_station_width = x;
185 f_station_height = y;
186 f_station_length = z;
187}
188
193
194 TGeoVolume* top = gGeoManager->GetTopVolume();
195 ShipGeo::InitMedium("mylar");
196 TGeoMedium* mylar = gGeoManager->GetMedium("mylar");
197 ShipGeo::InitMedium("STTmix8020_1bar");
198 TGeoMedium* sttmix8020_1bar = gGeoManager->GetMedium("STTmix8020_1bar");
199 ShipGeo::InitMedium("tungsten");
200 TGeoMedium* tungsten = gGeoManager->GetMedium("tungsten");
202 TGeoMedium* FrameMatPtr = gGeoManager->GetMedium(f_frame_material);
204 TGeoMedium* med = gGeoManager->GetMedium(fMedium.c_str());
205
206 gGeoManager->SetVisLevel(4);
207 gGeoManager->SetTopVisible();
208
209 // Epsilon to avoid overlapping volumes
210 Double_t eps = 0.0001;
211 // Straw (half) length
212 Double_t straw_length = f_aperture_width + 2. * eps;
213 // Width of frame: standard HEA 500 I-beam width
214 Double_t frame_width = 49.;
215 // Offset due to floor space limitation
216 Double_t floor_offset = 14.;
217
218 Double_t rmin, rmax, T_station_z;
219
220 // Arguments of boxes are half-lengths
221 [[maybe_unused]] TGeoBBox* detbox1 = new TGeoBBox(
222 "detbox1", f_aperture_width + frame_width,
223 f_aperture_height + frame_width - floor_offset / 2., f_station_length);
224 [[maybe_unused]] TGeoBBox* detbox2 = new TGeoBBox(
225 "detbox2", straw_length + eps,
227 TMath::Tan(f_view_angle * TMath::Pi() / 180.0) * straw_length * 2 +
228 f_offset_layer / TMath::Cos(f_view_angle * TMath::Pi() / 180.0) + eps,
229 f_station_length + eps);
230 TGeoTranslation* move_up =
231 new TGeoTranslation("move_up", 0, floor_offset / 2., 0);
232 move_up->RegisterYourself();
233
234 // Composite shape to create frame
235 TGeoCompositeShape* detcomp1 =
236 new TGeoCompositeShape("detcomp1", "(detbox1:move_up)-detbox2");
237
238 // Volume: straw
239 rmin = f_inner_straw_diameter / 2.;
240 rmax = f_outer_straw_diameter / 2.;
241 // Third argument is half-length of tube
242 TGeoTube* straw_tube = new TGeoTube("straw", rmin, rmax, straw_length);
243 TGeoVolume* straw = new TGeoVolume("straw", straw_tube, mylar);
244 straw->SetLineColor(4);
245 straw->SetVisibility(kTRUE);
246
247 // Volume: gas
248 rmin = f_wire_thickness / 2. + eps;
249 rmax = f_inner_straw_diameter / 2. - eps;
250 TGeoTube* gas_tube = new TGeoTube("gas", rmin, rmax, straw_length - 2. * eps);
251 TGeoVolume* gas = new TGeoVolume("gas", gas_tube, sttmix8020_1bar);
252 gas->SetLineColor(5);
253 // Only the gas is sensitive
254 AddSensitiveVolume(gas);
255
256 // Volume: wire
257 rmin = 0.;
258 rmax = f_wire_thickness / 2.;
259 TGeoTube* wire_tube =
260 new TGeoTube("wire", rmin, rmax, straw_length - 4. * eps);
261 TGeoVolume* wire = new TGeoVolume("wire", wire_tube, tungsten);
262 wire->SetLineColor(6);
263
264 // Tracking stations
265 // statnb = station number; vnb = view number; lnb = layer number; snb = straw
266 // number
267
268 // Station box to contain all components
269 TGeoBBox* statbox =
270 new TGeoBBox("statbox", f_station_width,
271 f_station_height - floor_offset / 2., f_station_length);
272
273 f_frame_material.ToLower();
274
275 for (Int_t statnb = 1; statnb < 5; statnb++) {
276 // Tracking station loop
277 TString nmstation = "Tr";
278 std::stringstream ss;
279 ss << statnb;
280 nmstation = nmstation + ss.str();
281 switch (statnb) {
282 case 1:
283 T_station_z = f_T1_z;
284 break;
285 case 2:
286 T_station_z = f_T2_z;
287 break;
288 case 3:
289 T_station_z = f_T3_z;
290 break;
291 case 4:
292 T_station_z = f_T4_z;
293 break;
294 default:
295 T_station_z = f_T1_z;
296 }
297
298 TGeoVolume* vol = new TGeoVolume(nmstation, statbox, med);
299 // z-translate the station to its (absolute) position
300 top->AddNode(vol, statnb,
301 new TGeoTranslation(0, floor_offset / 2., T_station_z));
302
303 TGeoVolume* statframe =
304 new TGeoVolume(nmstation + "_frame", detcomp1, FrameMatPtr);
305 vol->AddNode(statframe, statnb * 1e6,
306 new TGeoTranslation(0, -floor_offset / 2., 0));
307 statframe->SetLineColor(kRed);
308
309 for (Int_t vnb = 0; vnb < 4; vnb++) {
310 // View loop
311 const Double_t angle = [&] {
312 switch (vnb) {
313 case 1:
314 return f_view_angle;
315 case 2:
316 return -f_view_angle;
317 default:
318 return 0.;
319 }
320 }();
321 const TString nmview = [&] {
322 switch (vnb) {
323 case 0:
324 return nmstation + "_y1";
325 case 1:
326 return nmstation + "_u";
327 case 2:
328 return nmstation + "_v";
329 case 3:
330 return nmstation + "_y2";
331 default:
332 return nmstation + "_y1";
333 }
334 }();
335
336 // Adjustments in the stereo views
337 // stereo_growth: extension of stereo views beyond aperture
338 // stereo_pitch: straw pitch in stereo views
339 // offset_layer: layer offset in stereo views
340 // straws_per_layer: number of straws in one layer with stereo extension
341 // If angle == 0., all numbers return the case of non-stereo views.
342 const Double_t stereo_growth =
343 TMath::Tan(TMath::Abs(angle) * TMath::Pi() / 180.0) * straw_length;
344 const Double_t stereo_pitch =
345 f_straw_pitch / TMath::Cos(TMath::Abs(angle) * TMath::Pi() / 180.0);
346 const Double_t offset_layer =
347 f_offset_layer / TMath::Cos(TMath::Abs(angle) * TMath::Pi() / 180.0);
348 const Int_t straws_per_layer =
349 std::ceil(2 * (f_aperture_height + stereo_growth) / stereo_pitch);
350
351 for (Int_t lnb = 0; lnb < 2; lnb++) {
352 // Layer loop
353 TString nmlayer = nmview + "_layer_";
354 nmlayer += lnb;
355 TGeoBBox* layer = new TGeoBBox(
356 "layer box", straw_length + eps / 4,
357 f_aperture_height + stereo_growth * 2 + offset_layer + eps / 4,
358 f_outer_straw_diameter / 2. + eps / 4);
359 TGeoVolume* layerbox = new TGeoVolume(nmlayer, layer, med);
360
361 // The layer box sits in the viewframe.
362 // Hence, z-translate the layer w.r.t. the view
363 vol->AddNode(
364 layerbox, statnb * 1e6 + vnb * 1e5 + lnb * 1e4,
365 new TGeoTranslation(0, -floor_offset / 2.,
366 (vnb - 3. / 2.) * f_delta_z_view +
367 (lnb - 1. / 2.) * f_delta_z_layer));
368
369 TGeoRotation r6s;
370 TGeoTranslation t6s;
371 for (Int_t snb = 1; snb <= straws_per_layer; snb++) {
372 // Straw loop
373 // y-translate the straw to its position
374 t6s.SetTranslation(0,
375 f_aperture_height + stereo_growth -
376 (snb - 1. / 2.) * stereo_pitch +
377 lnb * offset_layer,
378 0);
379 // Rotate the straw with stereo angle
380 r6s.SetAngles(90 + angle, 90, 0);
381 TGeoCombiTrans c6s(t6s, r6s);
382 TGeoHMatrix* h6s = new TGeoHMatrix(c6s);
383 layerbox->AddNode(
384 straw, statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 1e3 + snb, h6s);
385 layerbox->AddNode(
386 gas, statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 2e3 + snb, h6s);
387 layerbox->AddNode(
388 wire, statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 3e3 + snb, h6s);
389 // End of straw loop
390 }
391
392 if (lnb == 1) {
393 // Add one more straw at the bottom of the second layer to cover
394 // aperture entirely
395 t6s.SetTranslation(0,
396 f_aperture_height + stereo_growth -
397 (straws_per_layer - 1. / 2.) * stereo_pitch -
398 lnb * offset_layer,
399 0);
400 r6s.SetAngles(90 + angle, 90, 0);
401 TGeoCombiTrans c6s(t6s, r6s);
402 TGeoHMatrix* h6s = new TGeoHMatrix(c6s);
403 layerbox->AddNode(
404 straw,
405 statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 1e3 + straws_per_layer + 1,
406 h6s);
407 layerbox->AddNode(
408 gas,
409 statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 2e3 + straws_per_layer + 1,
410 h6s);
411 layerbox->AddNode(
412 wire,
413 statnb * 1e6 + vnb * 1e5 + lnb * 1e4 + 3e3 + straws_per_layer + 1,
414 h6s);
415 }
416 // End of layer loop
417 }
418 // End of view loop
419 }
420 // End of tracking station loop
421 }
422}
423// ----- Public method StrawDecode -------------------------------------------
424// ----- returns station, view, layer, straw number in a tuple
425// -----------------------------------
426std::array<Int_t, 4> strawtubes::StrawDecode(Int_t detID) {
427 Int_t statnb, vnb, lnb, snb;
428 statnb = detID / 1e6;
429 vnb = (detID - statnb * 1e6) / 1e5;
430 lnb = (detID - statnb * 1e6 - vnb * 1e5) / 1e4;
431 snb = detID - statnb * 1e6 - vnb * 1e5 - lnb * 1e4 - 2e3;
432
433 if (statnb < 1 || statnb > 4 || vnb < 0 || vnb > 3 || lnb < 0 || lnb > 1 ||
434 snb < 1 || snb > 317) {
435 LOG(warning) << "Invalid strawtubes detID:";
436 LOG(warning) << detID << " -> station: " << statnb << ", view: " << vnb
437 << ", layer: " << lnb << ", straw: " << snb;
438 LOG(warning) << "strawtubes detID is 7-digit!";
439 return {0, -1, -1, 0};
440 } else {
441 return {statnb, vnb, lnb, snb};
442 }
443}
444// ----- Public method StrawEndPoints
445// -------------------------------------------
446// ----- returns top (left) and bottom (right) coordinates of straw
447// -----------------------------------
448void strawtubes::StrawEndPoints(Int_t fDetectorID, TVector3& vbot,
449 TVector3& vtop)
450// method to get end points from TGeoNavigator
451{
452 // The geometry is fixed, so cache the endpoints per detID; TGeo navigation
453 // is expensive in the stepping and digitisation hot paths.
454 static std::unordered_map<Int_t, std::pair<TVector3, TVector3>> endPointCache;
455 if (auto it = endPointCache.find(fDetectorID); it != endPointCache.end()) {
456 vbot = it->second.first;
457 vtop = it->second.second;
458 return;
459 }
460 const auto [statnb, vnb, lnb, snb] = StrawDecode(fDetectorID);
461 TString stat = "Tr";
462 stat += statnb;
463 stat += "_";
464 stat += statnb;
465 const TString view = [&] {
466 switch (vnb) {
467 case 1:
468 return TString("_u");
469 case 2:
470 return TString("_v");
471 case 3:
472 return TString("_y2");
473 default:
474 return TString("_y1");
475 }
476 }();
477 TGeoNavigator* nav = gGeoManager->GetCurrentNavigator();
478 TString prefix = "Tr";
479 prefix += statnb;
480 prefix += view;
481 prefix += "_";
482 TString layer = prefix + "layer_";
483 layer += lnb;
484 layer += "_";
485 layer += statnb;
486 layer += vnb;
487 layer += lnb;
488 layer += "0000";
489 TString wire = "wire_";
490 wire += fDetectorID + 1e3;
491 TString path = "/";
492 path += stat;
493 path += "/";
494 path += layer;
495 path += "/";
496 path += wire;
497 Bool_t rc = nav->cd(path);
498 if (!rc) {
499 LOG(warning) << "strawtubes::StrawDecode, TGeoNavigator failed" << path;
500 return;
501 }
502 TGeoNode* W = nav->GetCurrentNode();
503 TGeoTube* S = dynamic_cast<TGeoTube*>(W->GetVolume()->GetShape());
504 Double_t top[3] = {0, 0, S->GetDZ()};
505 Double_t bot[3] = {0, 0, -S->GetDZ()};
506 Double_t Gtop[3], Gbot[3];
507 nav->LocalToMaster(top, Gtop);
508 nav->LocalToMaster(bot, Gbot);
509 vtop.SetXYZ(Gtop[0], Gtop[1], Gtop[2]);
510 vbot.SetXYZ(Gbot[0], Gbot[1], Gbot[2]);
511 endPointCache.emplace(fDetectorID, std::make_pair(vbot, vtop));
512}
@ kStraw
strawtubesPoint * AddHit(Args &&... args)
Definition Detector.h:38
static void StrawEndPoints(Int_t detID, TVector3 &bot, TVector3 &top)
void SetDeltazView(Double_t delta_z_view)
std::string fMedium
spatial resolution
Definition strawtubes.h:84
Double_t f_aperture_width
z-position of tracking station 4
Definition strawtubes.h:68
Double_t f_inner_straw_diameter
Aperture height (y).
Definition strawtubes.h:70
Double_t f_station_length
Station envelope height (y).
Definition strawtubes.h:81
void SetDeltazLayer(Double_t delta_z_layer)
Double_t f_wire_thickness
Stereo view angle.
Definition strawtubes.h:76
Double_t f_T2_z
z-position of tracking station 1
Definition strawtubes.h:65
TString f_frame_material
Sense wire thickness.
Definition strawtubes.h:77
Bool_t ProcessHits(FairVolume *v=nullptr) override
void SetStrawDiameter(Double_t outer_straw_diameter, Double_t wall_thickness)
void SetzPositions(Double_t z1, Double_t z2, Double_t z3, Double_t z4)
Double_t f_straw_pitch
Outer Straw diameter.
Definition strawtubes.h:72
Double_t f_T3_z
z-position of tracking station 2
Definition strawtubes.h:66
Double_t f_aperture_height
Aperture width (x).
Definition strawtubes.h:69
Double_t f_delta_z_view
Structure frame material.
Definition strawtubes.h:78
void SetStrawPitch(Double_t straw_pitch, Double_t layer_offset)
Double_t f_station_height
Station envelope width (x).
Definition strawtubes.h:80
void ConstructGeometry() override
Double_t f_T1_z
Definition strawtubes.h:64
Double_t f_view_angle
Distance (z) between layers.
Definition strawtubes.h:75
Double_t f_outer_straw_diameter
Inner Straw diameter.
Definition strawtubes.h:71
void SetFrameMaterial(TString frame_material)
Double_t f_station_width
Distance (z) between stereo views.
Definition strawtubes.h:79
void SetStereoAngle(Double_t stereo_angle)
void SetWireThickness(Double_t wire_thickness)
void SetApertureArea(Double_t width, Double_t height)
void SetStationEnvelope(Double_t x, Double_t y, Double_t z)
static std::array< Int_t, 4 > StrawDecode(Int_t detID)
Double_t f_offset_layer
Distance (y) between straws in a layer.
Definition strawtubes.h:73
Double_t f_T4_z
z-position of tracking station 3
Definition strawtubes.h:67
Double_t f_delta_z_layer
Offset (y) of straws between layers.
Definition strawtubes.h:74
Int_t InitMedium(const char *name)
Definition ShipGeoUtil.h:22