StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StPxlFastSim.cxx
1 /*
2  *
3  * Author: A. Rose, LBL, Y. Fisyak, BNL, M. Miller, MIT, M. Mustafa
4  *
5  *
6  **********************************************************
7  * $Log: StPxlFastSim.cxx,v $
8  * Revision 1.13 2016/04/14 23:10:19 smirnovd
9  * Cosmetic changes
10  *
11  * * Make sure we call functions from std library as it was probably intended
12  *
13  * * Removed unnecessary #included dependency
14  * - Nothing in the code seems to come from the stdio.h header
15  *
16  * * End log messages with endm to flush the internal buffer
17  *
18  * Revision 1.12 2015/05/14 18:57:52 smirnovd
19  * Squashed commit of the following:
20  *
21  * StPxlFastSim: Streamlined creation of PXL hits by making use of StPxlUtil/StPxlDigiHit
22  *
23  * StPxlHitMaker: Updated comments
24  *
25  * StPxlHitMaker: Streamlined creation of PXL hits by making use of StPxlUtil/StPxlDigiHit
26  *
27  * StPxlDigiHit: A helper to manipulate local hit position in StPxlHit
28  *
29  * StPxlConsts: Define constants in namespace
30  *
31  * For safety reasons, the intentions is to move the constants into the namespace
32  * and get rid of those defined in the global space.
33  *
34  * Revision 1.11 2015/05/07 21:24:31 smirnovd
35  * StPxlSimMaker: Switched to using consts from StPxlUtil/
36  *
37  * Revision 1.10 2015/03/13 18:45:01 perev
38  * Roll back
39  *
40  * Revision 1.8 2015/01/27 19:11:49 mstftsm
41  * Set idTruth of StPxlHit to -999 if parentTrack of mcHit does not exist (for protection).
42  *
43  * Revision 1.7 2015/01/27 01:31:09 smirnovd
44  * Minor refactoring of StPxlFastSim::distortHit() to include a new warning for unphysical hit position
45  *
46  * Revision 1.6 2014/07/03 19:46:37 mstftsm
47  * Revereted the changes made for the pileup adder. That does not belong to the master branch.
48  *
49  * Revision 1.4 2014/03/13 17:00:19 mstftsm
50  * StPxlSimMaker has a method to switch on random seed for StRandom generatos in simulators. Default is not a random seed.
51  *
52  * Revision 1.2 2013/11/14 19:10:27 mstftsm
53  * StMcPxlHit has been changed to be on local coordinates. We no longer transfor from global to local before smearing
54  *
55  * Revision 1.1 2013/05/12 21:43:32 jeromel
56  * Initial revision, code peer review closed 2013/05/06
57  *
58  * Revision 1.5 2013/05/09 02:58:36 mstftsm
59  * Fixed a bug which called for sensor local Z value.
60  *
61  *
62  */
63 
64 #include <cmath>
65 
66 #include "StMessMgr.h"
67 #include "Stypes.h"
68 #include "Stiostream.h"
69 #include "StPxlFastSim.h"
70 #include "StEvent/StPxlHit.h"
71 #include "StEvent/StPxlHitCollection.h"
72 #include "StMcEvent/StMcPxlHitCollection.hh"
73 #include "StMcEvent/StMcPxlHit.hh"
74 #include "tables/St_HitError_Table.h"
75 #include "StarClassLibrary/StRandom.hh"
76 #include "StThreeVectorF.hh"
77 #include "StPxlDbMaker/StPxlDb.h"
78 #include "StPxlUtil/StPxlConstants.h"
79 #include "StPxlUtil/StPxlDigiHit.h"
80 
81 #include "TGeoManager.h"
82 #include "TGeoMatrix.h"
83 
84 #include "TDataSet.h"
85 #include "TObjectSet.h"
86 
88 {
89  if (mRandom) delete mRandom;
90  if (mPxlDb) delete mPxlDb;
91 }
92 //____________________________________________________________
93 Int_t StPxlFastSim::initRun(const TDataSet& calib_db, const TObjectSet* pxlDbDataSet, const Int_t run)
94 {
95  // run is not used in the current implementation, but might be necessary in the future.
96  LOG_INFO << "StPxlFastSim::init()" << endm;
97 
98  if(pxlDbDataSet != 0)
99  {
100  mPxlDb = (StPxlDb *)pxlDbDataSet->GetObject();
101  if (!mPxlDb)
102  {
103  LOG_ERROR << "StPxlFastSim - E - mPxlDb is not available" << endm;
104  return kStErr;
105  }
106  else
107  {
108  LOG_INFO << "StPxlFastSim - Using geometry from pxlDB" <<endm;
109  }
110  }
111  else
112  {
113  LOG_INFO << "StPxlFastSim - Using ideal geometry" <<endm;
114  }
115 
116  if (!mRandom) mRandom = new StRandom();
117  if(mUseRandomSeed)
118  {
119  Int_t seed = time(NULL);
120  mRandom->setSeed(seed);
121  LOG_INFO << "StPxlFastSim - smearing random generator is using random seed = " << seed <<endm;
122  }
123  else
124  {
125  LOG_INFO << "StPxlFastSim - smearing random generator is using default seed" <<endm;
126  }
127 
128  St_HitError *pxlTableSet = (St_HitError*)calib_db.Find("PixelHitError");
129 
130  if (!pxlTableSet)
131  {
132  LOG_ERROR << "StPxlFastSim - E - PixelHitError is not available" << endm;
133  return kStErr;
134  }
135 
136  HitError_st* pxlHitError = pxlTableSet->GetTable();
137 
138  if (!pxlHitError)
139  {
140  LOG_ERROR << "StPxlFastSim - E - pxl hit table is not available in PixelHitError" << endm;
141  return kStErr;
142  }
143 
144  // please note that what is called local Y in the PXL sensor design
145  // is actually called Z in STAR coordinates convention
146  if (pxlHitError->coeff[0] <= 0 || pxlHitError->coeff[3] <= 0)
147  {
148  LOG_ERROR << "StPxlFastSim - E - negative or corrupted PXL hits errors in DB" << endm;
149  return kStErr;
150  }
151 
152  mResXPix = std::sqrt(pxlHitError->coeff[0]); // local x
153  mResZPix = std::sqrt(pxlHitError->coeff[3]); // local Y
154  mResYPix = 0;//sqrt(pxlHitError->coeff[2]); // needs to be updated in the DB later
155 
156  return kStOk;
157 }
158 //____________________________________________________________
160  StPxlHitCollection& pxlHitCol)
161 {
162  Float_t smearedX = 0, smearedY = 0, smearedZ = 0;
163 
164 
165  // Loop over sectors
166  for (UInt_t iSec = 0; iSec < mcPxlHitCol.numberOfSectors(); iSec++)
167  {
168  const StMcPxlSectorHitCollection* mcPxlSectorHitCol = mcPxlHitCol.sector(iSec);
169  if (!mcPxlSectorHitCol) continue;
170 
171  for (UInt_t iLad = 0; iLad < mcPxlSectorHitCol->numberOfLadders(); iLad++)
172  {
173  const StMcPxlLadderHitCollection* mcPxlLadderHitCol = mcPxlSectorHitCol->ladder(iLad);
174  if (!mcPxlLadderHitCol) continue;
175 
176  for (UInt_t iSen = 0; iSen < mcPxlLadderHitCol->numberOfSensors(); iSen++)
177  {
178  const StMcPxlSensorHitCollection* mcPxlSensorHitCol = mcPxlLadderHitCol->sensor(iSen);
179  if (!mcPxlSensorHitCol) continue;
180 
181  UInt_t nSenHits = mcPxlSensorHitCol->hits().size();
182  LOG_DEBUG << "Sector/Ladder/Sensor = " << iSec + 1 << "/" << iLad + 1 << "/" << iSen + 1 << ". Number of sensor hits = " << nSenHits << endm;
183 
184  // Loop over hits in the sensor
185  for (UInt_t iHit = 0; iHit < nSenHits; iHit++)
186  {
187  StMcPxlHit* mcPix = mcPxlSensorHitCol->hits()[iHit];
188 
189  //Long_t volId = mcPix->volumeId();
190  Int_t sector = mcPix->sector();
191  Int_t ladder = mcPix->ladder();
192  //Int_t sensor = mcPix->sensor();
193 
194  Double_t localPixHitPos[3] = {mcPix->position().x(), mcPix->position().y(), mcPix->position().z()};
195 
196  LOG_DEBUG << "localPixHitPos = " << localPixHitPos[0] << " " << localPixHitPos[1] << " " << localPixHitPos[2] << endm;
197  // please note that what is called local Y in the PXL sensor design
198  // is actually called Z in STAR coordinates convention and vice-versa
199  smearedX = distortHit(localPixHitPos[0], mResXPix, StPxlConsts::kPxlActiveLengthX / 2.0);
200  smearedZ = distortHit(localPixHitPos[2], mResZPix, StPxlConsts::kPxlActiveLengthY / 2.0);
201  if (mResYPix) smearedY = distortHit(localPixHitPos[1], mResYPix, 0.0020); // Not properly constrained yet
202  else smearedY = localPixHitPos[1];
203  localPixHitPos[0] = smearedX;
204  localPixHitPos[2] = smearedZ;
205  localPixHitPos[1] = smearedY;
206  LOG_DEBUG << "smearedlocal = " << localPixHitPos[0] << " " << localPixHitPos[1] << " " << localPixHitPos[2] << endm;
207  Double_t smearedGlobalPixHitPos[3] = {0, 0, 0};
208  localToMatser(localPixHitPos,smearedGlobalPixHitPos,iSec+1,iLad+1,iSen+1);
209 
210  StThreeVectorF gpixpos(smearedGlobalPixHitPos);
211  StThreeVectorF mRndHitError(0., 0., 0.);
212 
213  UInt_t hw = sector * 10 + ladder; // needs to be updated later after clustering alogrithms are finalized
214 
215  unsigned short idTruth = mcPix->parentTrack() ? mcPix->parentTrack()->key() : -999;
216  unsigned short quality = mcPix->parentTrack() ? 100 : 0;
217 
218  StPxlDigiHit* tempHit = new StPxlDigiHit(localPixHitPos, iSec+1, mcPix->ladder(), mcPix->sensor(),
219  gpixpos, mRndHitError, hw, mcPix->dE(), 0, idTruth, quality, mcPix->key());
220 
221  LOG_DEBUG << "key() : " << mcPix->key() - 1 << " idTruth: " << mcPix->parentTrack()->key() << endm;
222  LOG_DEBUG << "from StMcPxlHit : x= " << mcPix->position().x() << "; y= " << mcPix->position().y() << "; z= " << mcPix->position().z() << endm;
223  LOG_DEBUG << "pxlHit location x= " << tempHit->position().x() << "; y= " << tempHit->position().y() << "; z= " << tempHit->position().z() << endm;
224 
225  pxlHitCol.addHit(tempHit);
226  }
227  }
228  }
229  }
230 
231  return kStOK;
232 }
233 
234 
241 double StPxlFastSim::distortHit(const double x, const double res, const double detLength) const
242 {
243  // Do not smear x when it is outside the physical limits. Issue a warning instead
244  if (std::fabs(x) > detLength) {
245  LOG_WARN << "distortHit() - Generated hit is outside detector sensor plane" << endm;
246  return x;
247  }
248 
249  double smeared_x;
250 
251  do {
252  smeared_x = mRandom->gauss(x, res);
253  } while ( std::fabs(smeared_x) > detLength);
254 
255  return smeared_x;
256 }
257 
258 
259 //____________________________________________________________
260 void StPxlFastSim::localToMatser(Double_t* local,Double_t* master,Int_t sector,Int_t ladder,Int_t sensor)
261 {
262  if(mPxlDb)
263  {
264  TGeoHMatrix *combP = (TGeoHMatrix *)mPxlDb->geoHMatrixSensorOnGlobal(sector, ladder,sensor);
265  combP->LocalToMaster(local, master);
266  }
267  else
268  {
269  TString Path("");
270  LOG_DEBUG << endm;
271  Path = Form("/HALL_1/CAVE_1/TpcRefSys_1/IDSM_1/PXMO_1/PXLA_%i/LADR_%i/PXSI_%i/PLAC_1", sector, ladder, sensor);
272 
273  gGeoManager->RestoreMasterVolume();
274  gGeoManager->CdTop();
275  gGeoManager->cd(Path);
276 
277  gGeoManager->GetCurrentMatrix()->LocalToMaster(local, master);
278  }
279 }
280 /*
281  *
282  * Author: M. Mustafa
283  *
284  *
285  **********************************************************
286  * $Log: StPxlFastSim.cxx,v $
287  * Revision 1.13 2016/04/14 23:10:19 smirnovd
288  * Cosmetic changes
289  *
290  * * Make sure we call functions from std library as it was probably intended
291  *
292  * * Removed unnecessary #included dependency
293  * - Nothing in the code seems to come from the stdio.h header
294  *
295  * * End log messages with endm to flush the internal buffer
296  *
297  * Revision 1.12 2015/05/14 18:57:52 smirnovd
298  * Squashed commit of the following:
299  *
300  * StPxlFastSim: Streamlined creation of PXL hits by making use of StPxlUtil/StPxlDigiHit
301  *
302  * StPxlHitMaker: Updated comments
303  *
304  * StPxlHitMaker: Streamlined creation of PXL hits by making use of StPxlUtil/StPxlDigiHit
305  *
306  * StPxlDigiHit: A helper to manipulate local hit position in StPxlHit
307  *
308  * StPxlConsts: Define constants in namespace
309  *
310  * For safety reasons, the intentions is to move the constants into the namespace
311  * and get rid of those defined in the global space.
312  *
313  * Revision 1.11 2015/05/07 21:24:31 smirnovd
314  * StPxlSimMaker: Switched to using consts from StPxlUtil/
315  *
316  * Revision 1.10 2015/03/13 18:45:01 perev
317  * Roll back
318  *
319  * Revision 1.8 2015/01/27 19:11:49 mstftsm
320  * Set idTruth of StPxlHit to -999 if parentTrack of mcHit does not exist (for protection).
321  *
322  * Revision 1.7 2015/01/27 01:31:09 smirnovd
323  * Minor refactoring of StPxlFastSim::distortHit() to include a new warning for unphysical hit position
324  *
325  * Revision 1.6 2014/07/03 19:46:37 mstftsm
326  * Revereted the changes made for the pileup adder. That does not belong to the master branch.
327  *
328  * Revision 1.4 2014/03/13 17:00:19 mstftsm
329  * StPxlSimMaker has a method to switch on random seed for StRandom generatos in simulators. Default is not a random seed.
330  *
331  * Revision 1.2 2013/11/14 19:10:27 mstftsm
332  * StMcPxlHit has been changed to be on local coordinates. We no longer transfor from global to local before smearing
333  *
334  * Revision 1.1 2013/05/12 21:43:32 jeromel
335  * Initial revision, code peer review closed 2013/05/06
336  *
337  * Revision 1.5 2013/05/09 02:58:36 mstftsm
338  * Fixed a bug which called for sensor local Z value.
339  *
340  */
341 
int sector() const
Definition: StMcPxlHit.hh:33
Int_t addPxlHits(const StMcPxlHitCollection &, StPxlHitCollection &)
creates an StPxlHit object for every StMcPxlHit, and fills the hit StPxlHitCollection container...
Definition: Stypes.h:40
Int_t initRun(const TDataSet &calib_db, const TObjectSet *pxlDbDataSet, const Int_t run)
A random seed is passed to mRandom PXL smearing resolutions (PixelHitError) are fetched from calib_db...
~StPxlFastSim()
This class does not own any hit containers. mRandom is deleted here.
int ladder() const
Definition: StMcPxlHit.hh:37
virtual TObject * GetObject() const
The depricated method (left here for the sake of the backward compatibility)
Definition: TObjectSet.h:56
Definition: Stypes.h:44
int sensor() const
Definition: StMcPxlHit.hh:41
Definition: Stypes.h:41
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362