StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSsdFastSimMaker.cxx
1 /*
2  *
3  * Author: J. Bouchet, Subatech; Y. Fisyak, BNL; J. Vanfossen, Kent State
4  *
5  *
6  **********************************************************
7  *
8  */
9 
10 #include "Stiostream.h"
11 #include "StSsdFastSimMaker.h"
12 #include "StHit.h"
13 #include "StEventTypes.h"
14 #include "StChain.h"
15 
16 #include "Sti/StiVMCToolKit.h"
17 #include "StarClassLibrary/StRandom.hh"
18 #include "tables/St_HitError_Table.h"
19 
20 #include "TH1.h"
21 #include "TH2.h"
22 #include <math.h>
23 
24 #include "tables/St_g2t_ssd_hit_Table.h"
25 #include "StSsdDbMaker/StSsdDbMaker.h"
26 #include "StSsdDbMaker/StSstDbMaker.h"
27 #include "StSsdUtil/StSsdBarrel.hh"
28 #include "StSsdHitCollection.h"
29 #include "StEvent.h"
30 #include "StMcEvent.hh"
31 ClassImp(StSsdFastSimMaker)
32 
33  StSsdFastSimMaker::StSsdFastSimMaker(const char *name):StMaker(name){
34  mHit = new StSsdHit();
35  WaferNumb = 0;
36  HitsMap = 0;
37  dX = 0;
38  dY = 0;
39  dZ = 0;
40  Local = 0;
41  Ratio = 0;
42  };
43 
44  StSsdFastSimMaker::~StSsdFastSimMaker(){
45  delete mHit;
46  delete WaferNumb ;
47  delete HitsMap ;
48  delete dX ;
49  delete dY ;
50  delete dZ ;
51  delete Local ;
52  delete Ratio ;
53 }
54 
55 int StSsdFastSimMaker::Init()
56 {
57  int seed=time(NULL);
58  myRandom=new StRandom();
59  myRandom->setSeed(seed);
60 
61  // Define various SSD specific geom. variables.
62  mResXSsd = 0.0000;
63  mResZSsd = 0.0000;
64  mSmear=1; //used to turn on or off smearing
65 
66  LOG_DEBUG << "Init() : Defining the histograms" << endm;
67  if(IAttr(".histos")){
68  WaferNumb = new TH1S("WaferNumb","Hits from geant per Wafer Id",1620,7000,8620);
69  WaferNumb->SetXTitle("from 7000 to 7000*(iW*100)+16");
70  WaferNumb->SetLabelSize(0.03,"X");
71  WaferNumb->SetLabelSize(0.03,"Y");
72  HitsMap = new TH2S("HitsMap","Map of hits in ssd wafers",20,1,21,16,1,17);
73  HitsMap->SetXTitle("Ladder");
74  HitsMap->SetYTitle("Wafer");
75  HitsMap->SetLabelSize(0.03,"X");
76  HitsMap->SetLabelSize(0.03,"Y");
77  dX = new TH1F("dX","difference in X after smearing the hit",200,-0.02,0.02);
78  dX->SetXTitle("x_{G} - x_{G}^{smear} [cm]");
79  dX->SetLabelSize(0.03,"X");
80  dX->SetLabelSize(0.03,"Y");
81  dY = new TH1F("dY","difference in Y after smearing the hit",200,-0.02,0.02);
82  dY->SetXTitle("y_{G} - y_{G}^{smear} [cm]");
83  dY->SetLabelSize(0.03,"X");
84  dY->SetLabelSize(0.03,"Y");
85  dZ = new TH1F("dZ","difference in Z after smearing the hit",800,-2,2);
86  dZ->SetXTitle("z_{G} - z_{G}^{smear} [cm]");
87  dZ->SetLabelSize(0.03,"X");
88  dZ->SetLabelSize(0.03,"Y");
89  Local = new TH2F("Local","Local x vs local y : hits ",800,-4,4,250,-2.25,2.25);
90  Local->SetXTitle("x_{l}[cm]");
91  Local->SetYTitle("y_{l}[cm]");
92  Local->SetLabelSize(0.03,"X");
93  Local->SetLabelSize(0.03,"Y");
94  Ratio = new TH1F("Ratio","Ratio between initial geant hits and after removal of the dead areas",120,0,1.2);
95  Ratio->SetLabelSize(0.03,"X");
96  Ratio->SetLabelSize(0.03,"Y");
97  }
98  return kStOk;
99 }
100 //____________________________________________________________
101 int StSsdFastSimMaker::InitRun(int RunNo)
102 {
103  // Define various SSD hit errors from database
104  TDataSet *set = GetDataBase("Calibrations/tracker");
105  St_HitError *tableSet = (St_HitError *)set->Find("ssdHitError");
106  HitError_st* hitError = tableSet->GetTable();
107  mResXSsd = sqrt(hitError->coeff[0]);
108  mResZSsd = sqrt(hitError->coeff[3]);
109  LOG_DEBUG << "Smearing SSD hits by " << mResXSsd << " " << mResZSsd << " cm in the SSD " << endm;
110 
111  // geometry parameters
112  TDataSet *ssdparams = GetInputDB("Geometry/ssd");
113  if (! ssdparams) {
114  LOG_ERROR << "No access to Geometry/ssd parameters" << endm;
115  return kStFatal;
116  }
117  TDataSetIter local(ssdparams);
118  m_ctrl = (St_slsCtrl *)local("slsCtrl");
119  m_dimensions = (St_ssdDimensions *)local("ssdDimensions");
120  m_positions = (St_ssdWafersPosition *)local("ssdWafersPosition");
121 
122  if (!m_ctrl) {
123  LOG_ERROR << "No access to control parameters" << endm;
124  return kStFatal;
125  }
126  if ((!m_dimensions)||(!m_positions)) {
127  LOG_ERROR << "No access to geometry parameters" << endm;
128  return kStFatal;
129  }
130  St_ssdConfiguration* configTable = (St_ssdConfiguration*) local("ssdConfiguration");
131  if (!configTable) {
132  LOG_ERROR << "InitRun : No access to ssdConfiguration database" << endm;
133  return kStFatal;
134  }
135  m_config = (ssdConfiguration_st*) configTable->GetTable() ;
136 
137  return kStOk;
138 }
139 //____________________________________________________________
141 //____________________________________________________________
143 {
144  // Get the input data structures from StEvent
145  mEvent = (StEvent*) GetInputDS("StEvent");
146  if(mEvent)
147  {
148  mCol = mEvent->ssdHitCollection();
149  if (!mCol)
150  {
151  LOG_WARN <<"StSsdFastSimulatorMaker -E- no SsdHitCollection!" << endm;
152  mCol = new StSsdHitCollection;
153  mEvent->setSsdHitCollection(mCol);
154  LOG_WARN <<"Make() has added a non existing StSsdHitCollection" <<endm;
155  }
156  }
157  if (! mEvent) {
158  LOG_WARN << "No StEvent on input, bye bye" << endm; return kStWarn;
159  mCol =0;
160  }
161  TDataSetIter geant(GetInputDS("geant"));
162  if (! gGeoManager) GetDataBase("VmcGeometry");
163  LOG_DEBUG << "Geometry Loaded" << endm;
164  St_g2t_ssd_hit *g2t_ssd_hit = (St_g2t_ssd_hit *) geant("g2t_ssd_hit");
165  if (! g2t_ssd_hit) {
166  LOG_WARN << "No g2t_ssd_hit on input, bye bye" << endm; return kStWarn;
167  return kStWarn;
168  }
169  g2t_ssd_hit_st *g2t = g2t_ssd_hit->GetTable();
170 
171  LOG_INFO<<"#### START OF SSD FAST SIM MAKER ####"<<endm;
172  mySsd = StSsdBarrel::Instance();
173  ssdDimensions_st *dimensions = m_dimensions->GetTable();
174  setSsdParameters(dimensions);
175  if(Debug()>1) printSsdParameters();
176  Int_t inContainer = 0;
177  Int_t goodHits = 0;
178  if(g2t_ssd_hit){
179  LOG_INFO<<Form("NumberOfRows = %d Size of g2t_ssd table=%d",(int)g2t_ssd_hit->GetNRows(),(int)g2t_ssd_hit->GetTableSize())<<endm;
180  Int_t minWaf = mSsdLayer*1000;
181  Int_t currWafId = 0;
182  Int_t currWafNumb = 0;
183  Int_t currLadder = 0;
184  Int_t i = 0;
185  unsigned char c = 0;
186  Int_t iok = 0;
187  Int_t in = 0;
188  for (i = 0; i < g2t_ssd_hit->GetNRows() ; i++) {
189  currWafId=g2t[i].volume_id%10000;
190  if (currWafId > minWaf) {
191  currLadder = StSsdBarrel::Instance()->idWaferToLadderNumb(currWafId);
192  currWafNumb = StSsdBarrel::Instance()->idWaferToWafer(currWafId);
193  StThreeVectorF error(0.,0.,0.);
194  mHit = new StSsdHit(g2t[i].x,error,currWafId,g2t[i].de,c);
195  LOG_DEBUG<<Form("currWafId=%d currWafNum=%d currLadder=%d",currWafId,currWafNumb,currLadder)<<endm;
196  // now we get the position of this wafer
197  // GEANT hits are saved in local coordinate
198  Double_t xl[3] = {mHit->position().x(),mHit->position().y(),mHit->position().z()};
199  LOG_DEBUG <<Form("local position x=%f y=%f z=%f",xl[0],xl[1],xl[2])<<endm;
200  LOG_DEBUG <<"will smear X"<<endm;
201  Double_t xlSmear[3]={0.,0.,0.};
202  xlSmear[0] = (distortHit(xl[0], mResXSsd));
203  LOG_DEBUG <<"will smear Z"<<endm;
204  xlSmear[2] = (distortHit(xl[2], mResZSsd));
205  xlSmear[1] = xl[1];
206  LOG_DEBUG <<Form("smeared local position x=%f y=%f z=%f",xlSmear[0],xlSmear[1],xlSmear[2])<<endm;
207  Double_t xgSmear[3]={0.,0.,0.};
208  mySsd->mLadders[currLadder]->mWafers[currWafNumb]->LocalToMaster(xlSmear,xgSmear);
209  LOG_DEBUG<< "After : global position are :"<<endm;
210  LOG_DEBUG <<Form("x=%f y=%f z=%f",xgSmear[0],xgSmear[1],xgSmear[2])<<endm;
211  LOG_DEBUG <<Form("Smearing done...")<< endm;
212  in = IsOnWafer(xlSmear[0],xlSmear[2]);
213  if(in==0)continue;
214  LOG_DEBUG << "good hit in wafer active area " << endm;
215  iok = RemoveTriangle(xlSmear[0],xlSmear[2]);
216  if(iok==0)continue;
217  goodHits++;
218  LOG_DEBUG << "good hit after triangle rejection" << endm;
219  Local->Fill(xlSmear[0],xlSmear[2]);
220  StSsdHit *mSmearedhit = new StSsdHit(xgSmear,error,currWafId,g2t[i].de,c);
221  //fill histograms
222  if(IAttr(".histos")){
223  HitsMap->Fill(currLadder+1,currWafNumb+1,1);
224  WaferNumb->Fill(currWafId);
225  dX->Fill(mHit->position().x()-xgSmear[0]);
226  dY->Fill(mHit->position().y()-xgSmear[1]);
227  dZ->Fill(mHit->position().z()-xgSmear[2]);
228  }
229  // encode the hardware position
230  // 2^3 detector ID number (8)
231  // 2^4 4-12 num_wafer (0-319)
232  Int_t hw =
233  8
234  + 16 * idWaferToWaferNumb(currWafId);
235  mSmearedhit->setHardwarePosition(hw);
236  mSmearedhit->setLocalPosition(xlSmear[0],xlSmear[2]);
237  mSmearedhit->setIdTruth(g2t[i].track_p,100);
238  inContainer+=mCol->addHit(mSmearedhit);
239  }
240  }
241  }
242  if(inContainer){
243  LOG_INFO<<"#### -> "<<inContainer<<" HITS WRITTEN INTO CONTAINER ####"<<endm;
244  Ratio->Fill(inContainer/(float)g2t_ssd_hit->GetNRows());
245  }
246  else {
247  LOG_INFO<<"######### NO SSD HITS WRITTEN INTO CONTAINER ####"<<endm;
248  }
249  LOG_DEBUG << "ssd col= "<<mCol->numberOfHits()<< " inContainer=" << inContainer <<" good hits = " << goodHits <<" initial # hits="<<g2t_ssd_hit->GetNRows()<<endm;
250  mySsd->Reset();
251  return kStOK;
252 }
253 //________________________________________________________________________________
254 Bool_t StSsdFastSimMaker::accept(StEvent* event){
255  return event ? true : false;
256 }
257 //_______________________________________________________________________________
258 Float_t StSsdFastSimMaker::distortHit(Float_t x, double res){
259  double test;
260  LOG_DEBUG << "In distortHit " << endm;
261  LOG_DEBUG << Form("smear me by %f",res)<<endm;
262  if(mSmear){
263  test = x + myRandom->gauss(0,res);
264 
265  LOG_DEBUG<< " x was " <<x<< " and is now " << test << endm;
266  return test;
267  }
268  else return x;
269 }
270 //_______________________________________________________________
271 Int_t StSsdFastSimMaker::idWaferToWaferNumb(Int_t idWafer)
272 {
273  // idwafer = layer*1000+waf*100+ladder
274  Int_t iW = (int)((idWafer - mSsdLayer*1000)/100);
275  Int_t iL = idWafer - mSsdLayer*1000 - iW*100;
276  return ((iL-1)*mNWaferPerLadder + iW -1);
277 }
278 //_______________________________________________________________
279 Int_t StSsdFastSimMaker::idWaferToLadderNumb(Int_t idWafer)
280 {
281  // idwafer = layer*1000+waf*100+ladder
282  Int_t iW = (int)((idWafer - mSsdLayer*1000)/100);
283  Int_t iL = idWafer - mSsdLayer*1000 - iW*100;
284  return iL-1;
285 }
286 //_______________________________________________________________
287 Int_t StSsdFastSimMaker::waferNumbToIdWafer(Int_t waferNumb)
288 {
289  Int_t iL = 1+(int)((waferNumb)/mNLadder);
290  Int_t iW = waferNumb-((iL-1)*mNLadder)+1;
291  return mSsdLayer*1000 + iW*100 + iL;
292 }
293 //________________________________________________________________________________
294 void StSsdFastSimMaker::setSsdParameters(ssdDimensions_st *geom_par){
295  mDimensions = geom_par;
296  mSsdLayer = 7; // all layers : 1->7
297  mDetectorLargeEdge = 2.*geom_par[0].waferHalfActLength;
298  mDetectorSmallEdge = 2.*geom_par[0].waferHalfActWidth;
299  mNLadder = 20;
300  mNWaferPerLadder = geom_par[0].wafersPerLadder;
301  mNStripPerSide = geom_par[0].stripPerSide;
302  mStripPitch = geom_par[0].stripPitch;
303  mTheta = geom_par[0].stereoAngle;
304 }
305 //________________________________________________________________________________
306 void StSsdFastSimMaker::printSsdParameters(){
307  LOG_INFO << "###Ladders = " <<mNLadder<<"###"<< endm;
308  LOG_INFO << "###Wafers per Ladder = " <<mNWaferPerLadder<<"###"<< endm;
309 }
310 void StSsdFastSimMaker::Clear(Option_t *option)
311 {
312  /*noop*/
313 }
314 //________________________________________________________________________________
315 Int_t StSsdFastSimMaker::RemoveTriangle(Float_t x, Float_t y)
316 {
317  Int_t iok = 0;
318  Float_t beta = (TMath::Pi()/2.) - (mTheta/2.);
319  Float_t X = (mDetectorSmallEdge/2.)*TMath::Tan(mTheta/2.);
320  Float_t l = (mDetectorLargeEdge/2.) -X ;
321  LOG_DEBUG <<Form("beta=%f arctan(beta)=%f X=%f l=%f mdetectorsmallEdge=%f mDetectorLargeEdge=%f\n",beta,TMath::ATan(beta),X,l,mDetectorSmallEdge,mDetectorLargeEdge)<<endm;
322  if(x>(mDetectorLargeEdge/2.) -X){
323  if(y < TMath::ATan(beta)*(x-l)){
324  LOG_DEBUG<<"upper right corner , hit rejected at x=" << x <<" y=" <<y << endm;
325  iok =0;
326  }
327  else
328  if(y>-1*TMath::ATan(beta)*(x-l)){
329  LOG_DEBUG<<"bottom right corner , hit rejected at x=" << x <<" y=" <<y << endm;
330  iok = 0; //bottom right corner
331  }
332  }
333  if(x<((-1*mDetectorLargeEdge/2.)+X)){
334  if(y<-1*TMath::ATan(beta)*(x+l)){
335  LOG_DEBUG<<"upper left corner , hit rejected at x=" << x <<" y=" <<y << endm;
336  iok =0; //upper left corner
337  }
338  else {
339  if(y>1*TMath::ATan(beta)*(x+l)){
340  LOG_DEBUG<<"bottom left corner , hit rejected at x=" << x <<" y=" <<y << endm;
341  iok = 0; //bottom left corner
342  }
343  }
344  }
345  else{ iok= 1;}
346 return iok; // good hit
347 }
348 //_______________________________________________________________________________
349 int StSsdFastSimMaker::IsOnWafer(Float_t x , Float_t y){
350  //Find out for a given z coord and Hardware pos is it on the wafer
351  if((x <(mDetectorLargeEdge/2.)) && (x > (-mDetectorLargeEdge/2.)) &&
352  (y <(mDetectorSmallEdge/2.)) && (y > (-mDetectorSmallEdge/2.)))
353  return 1;
354  return 0;
355 }
virtual Int_t Make()
virtual void Clear(Option_t *option="")
User defined functions.
virtual Int_t Finish()
Definition: Stypes.h:42
Definition: Stypes.h:40
Definition: Stypes.h:41
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362