CbmBftc.cxx 7.32 KB
Newer Older
Eoin Clerkin's avatar
Eoin Clerkin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             * 
 *              GNU Lesser General Public Licence (LGPL) version 3,             *  
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
#include "CbmBftc.h"

#include "CbmBftcPoint.h"
#include "CbmBftcGeo.h"
#include "CbmBftcGeoPar.h"

#include "FairVolume.h"
#include "FairGeoVolume.h"
#include "FairGeoNode.h"
#include "FairRootManager.h"
#include "FairGeoLoader.h"
#include "FairGeoInterface.h"
#include "FairRun.h"
#include "FairRuntimeDb.h"
#include "MyProjDetectorList.h"
#include "MyProjStack.h"

#include "TClonesArray.h"
#include "TVirtualMC.h"
#include "TGeoManager.h"
#include "TGeoBBox.h"
#include "TGeoCompositeShape.h"
#include "TGeoTube.h"
#include "TGeoMaterial.h"
#include "TGeoMedium.h"

#include <iostream>
using std::cout;
using std::endl;

CbmBftc::CbmBftc()
  : FairDetector("CbmBftc", kTRUE, kCbmBftc),
    fTrackID(-1),
    fVolumeID(-1),
    fPos(),
    fMom(),
    fTime(-1.),
    fLength(-1.),
    fELoss(-1),
    fCbmBftcPointCollection(new TClonesArray("CbmBftcPoint"))
{
}

CbmBftc::CbmBftc(const char* name, Bool_t active)
  : FairDetector(name, active, kCbmBftc),
    fTrackID(-1),
    fVolumeID(-1),
    fPos(),
    fMom(),
    fTime(-1.),
    fLength(-1.),
    fELoss(-1),
    fCbmBftcPointCollection(new TClonesArray("CbmBftcPoint"))
{
}

CbmBftc::CbmBftc(const CbmBftc& right)
  : FairDetector(right),
    fTrackID(-1),
    fVolumeID(-1),
    fPos(),
    fMom(),
    fTime(-1.),
    fLength(-1.),
    fELoss(-1),
    fCbmBftcPointCollection(new TClonesArray("CbmBftcPoint"))
{
}

CbmBftc::~CbmBftc()
{
  if (fCbmBftcPointCollection) {
    fCbmBftcPointCollection->Delete();
    delete fCbmBftcPointCollection;
  }
}

void CbmBftc::Initialize()
{
  /**
   * WORKAROUND needed for Geant4 in MT mode
   * Call AddSensitiveVolume for sensitive volumes in order to fill
   * thread-local FairModule::svList.
   */
  DefineSensitiveVolumes();

  FairDetector::Initialize();
  FairRuntimeDb* rtdb= FairRun::Instance()->GetRuntimeDb();
  CbmBftcGeoPar* par=(CbmBftcGeoPar*)(rtdb->getContainer("CbmBftcGeoPar"));
}

Bool_t  CbmBftc::ProcessHits(FairVolume* vol)
{
  /** This method is called from the MC stepping */

  //Set parameters at entrance of volume. Reset ELoss.
  if ( TVirtualMC::GetMC()->IsTrackEntering() ) {
    fELoss  = 0.;
    fTime   = TVirtualMC::GetMC()->TrackTime() * 1.0e09;
    fLength = TVirtualMC::GetMC()->TrackLength();
    TVirtualMC::GetMC()->TrackPosition(fPos);
    TVirtualMC::GetMC()->TrackMomentum(fMom);
  }

  // Sum energy loss for all steps in the active volume
  fELoss += TVirtualMC::GetMC()->Edep();

  // Create CbmBftcPoint at exit of active volume
  if ( TVirtualMC::GetMC()->IsTrackExiting()    ||
       TVirtualMC::GetMC()->IsTrackStop()       ||
       TVirtualMC::GetMC()->IsTrackDisappeared()   ) {
    fTrackID  = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber();
    fVolumeID = vol->getMCid();
    if (fELoss == 0. ) { return kFALSE; }
    AddHit(fTrackID, fVolumeID, TVector3(fPos.X(),  fPos.Y(),  fPos.Z()),
           TVector3(fMom.Px(), fMom.Py(), fMom.Pz()), fTime, fLength,
           fELoss);

    // Increment number of CbmBftc det points in TParticle
    MyProjStack* stack = (MyProjStack*) TVirtualMC::GetMC()->GetStack();
    stack->AddPoint(kCbmBftc);
  }

  return kTRUE;
}

void CbmBftc::EndOfEvent()
{

  LOG(info) << "CbmBftc: " << fCbmBftcPointCollection->GetEntriesFast()
            << " points registered in this event";

  fCbmBftcPointCollection->Clear();

}



void CbmBftc::Register()
{

  /** This will create a branch in the output tree called
      CbmBftcPoint, setting the last parameter to kFALSE means:
      this collection will not be written to the file, it will exist
      only during the simulation.
  */

  if ( ! gMC->IsMT() ) {
    FairRootManager::Instance()->Register("CbmBftcPoint", "CbmBftc",
                                          fCbmBftcPointCollection, kTRUE);
  } else {
    FairRootManager::Instance()->RegisterAny("CbmBftcPoint",
                                             fCbmBftcPointCollection, kTRUE);
  }

}


TClonesArray* CbmBftc::GetCollection(Int_t iColl) const
{
  if (iColl == 0) { return fCbmBftcPointCollection; }
  else { return NULL; }
}

void CbmBftc::Reset()
{
  fCbmBftcPointCollection->Clear();
}

void CbmBftc::ConstructGeometry()
{
    TGeoVolume *top=gGeoManager->GetTopVolume();
    TGeoMedium *Si =gGeoManager->GetMedium("Si");
    TGeoMedium *Carbon = gGeoManager->GetMedium("C");

    if(Si==0){
        TGeoMaterial *matSi     = new TGeoMaterial("Si", 28.0855, 14, 2.33);
        Si     = new TGeoMedium("Si", 2, matSi);
    }
    if(Carbon==0){
        TGeoMaterial *matCarbon    = new TGeoMaterial("C", 12.011, 6.0, 2.265);
        Carbon     = new TGeoMedium("C", 3, matCarbon);
    }


    TGeoVolume *det1= gGeoManager->MakeTubs("Det1",Si,5,80,0.1,0,360);
    TGeoRotation r1;
    r1.SetAngles(0,0,0);
    TGeoTranslation t1(0, 0, 0);
    TGeoCombiTrans c1(t1, r1);
    TGeoHMatrix *h1 = new TGeoHMatrix(c1);
    top->AddNode(det1,1,h1);
    det1->SetLineColor(kGreen);
    AddSensitiveVolume(det1);

    TGeoVolume *passive1= gGeoManager->MakeTubs("Pass1",Si,5,120,10,0,360);
    TGeoRotation rp1;
    rp1.SetAngles(0,0,0);
    TGeoTranslation tp1(0, 0, 20);
    TGeoCombiTrans cp1(tp1, rp1);
    TGeoHMatrix *hp1 = new TGeoHMatrix(cp1);
    top->AddNode(passive1,1,hp1);
    passive1->SetLineColor(kRed);



    TGeoVolume *det2= gGeoManager->MakeTubs("Det2",Si,5,150,0.1,0,360);
    TGeoRotation r2;
    r2.SetAngles(0,0,0);
    TGeoTranslation t2(0, 0, 70);
    TGeoCombiTrans c2(t2, r2);
    TGeoHMatrix *h2 = new TGeoHMatrix(c2);
    top->AddNode(det2,1,h2);
    det2->SetLineColor(kGreen);
    AddSensitiveVolume(det2);

    TGeoVolume *det3= gGeoManager->MakeTubs("Det3",Si,5,150,0.1,0,360);
    TGeoRotation r3;
    r3.SetAngles(0,0,0);
    TGeoTranslation t3(0, 0, 150);
    TGeoCombiTrans c3(t3, r3);
    TGeoHMatrix *h3 = new TGeoHMatrix(c3);
    top->AddNode(det3,1,h3);
    det3->SetLineColor(kGreen);
    AddSensitiveVolume(det3);
}

CbmBftcPoint* CbmBftc::AddHit(Int_t trackID, Int_t detID,
                                      TVector3 pos, TVector3 mom,
                                      Double_t time, Double_t length,
                                      Double_t eLoss)
{
  TClonesArray& clref = *fCbmBftcPointCollection;
  Int_t size = clref.GetEntriesFast();
  return new(clref[size]) CbmBftcPoint(trackID, detID, pos, mom,
         time, length, eLoss);
}

FairModule* CbmBftc::CloneModule() const
{
  return new CbmBftc(*this);
}

void CbmBftc::DefineSensitiveVolumes()
{
  TObjArray* volumes = gGeoManager->GetListOfVolumes();
  TIter next(volumes);
  TGeoVolume* volume;
  while ( ( volume = static_cast<TGeoVolume*>(next()) ) ) {
    if ( CheckIfSensitive(volume->GetName()) ) {
      LOG(debug2)<<"Sensitive Volume "<< volume->GetName();
      AddSensitiveVolume(volume);
    }
  }
}

Bool_t CbmBftc::CheckIfSensitive(std::string name)
{
  if(TString(name).Contains("Det"))
  {
    return kTRUE;
  }
  return kFALSE;
}

ClassImp(CbmBftc)