FairRoot/PandaRoot
PndMdtPointsToWaveform.cxx
Go to the documentation of this file.
1 //----------------------------------------------------------------------
2 // File and Version Information:
3 // $Id: //
4 // Description:
5 // Class PndMdtPointsToWaveform. Module to take the Mdt Points list for the
6 // mdt and make induced current.
7 //
8 // Author List:
9 // Jifeng Hu, hu@to.infn.it, Torino University
10 //
11 //----------------------------------------------------------------------
12 
14 #include "PndMdtPointsToWaveform.h"
15 #include "PndMdtPoint.h"
16 #include "PndMdtParamDigi.h"
17 #include "FairEventHeader.h"
18 #include "PndMdtWaveform.h"
19 #include "TRandom.h"
20 #include "FairRootManager.h"
21 #include "FairRunAna.h"
22 #include "FairRuntimeDb.h"
23 #include "PndMCTrack.h"
24 #include "TStopwatch.h"
25 #include "TROOT.h"
26 #include "TClonesArray.h"
27 #include "PndMdtIGeometry.h"
28 #include <iostream>
29 #include <cassert>
30 
31 using std::cout;
32 using std::endl;
33 using std::fstream;
34 
35 
37  //fDigiPar(new PndMdtDigiPar())
38  //, fGeoPar(new PndMdtGeoPar())
39  fWaveformArray(0)
40  , fTimeOrderedWaveform(kFALSE)
41  , fDataBuffer(0)
42  , fVerbose(verbose)
43 {
44  SetPersistency(storewaves);
45 }
46 //--------------
47 // Destructor --
48 //--------------
49 
51 {
52 }
53 
54 
56 {
57  // Get RootManager
58  FairRootManager* ioman = FairRootManager::Instance();
59  if ( ! ioman )
60  {
61  cout << "-E- PndMdtPointsToWaveform::Init: "
62  << "RootManager not instantiated!" << endl;
63  return kFATAL;
64  }
65 
66  // Get input array
67  fMcTrackArray= (TClonesArray*) ioman->GetObject("MCTrack");
68  if ( ! fMcTrackArray) {
69  cout << "-W- PndMdtPointsToWaveform::Init: "
70  << "No MCTrack array!" << endl;
71  return kERROR;
72  }
73 
74  // Get input array
75  fPointArray = (TClonesArray*) ioman->GetObject("MdtPoint");
76  if ( ! fPointArray) {
77  cout << "-W- PndMdtPointsToWaveform::Init: "
78  << "No MdtHit array!" << endl;
79  return kERROR;
80  }
82  fDataBuffer = new PndMdtWaveformWriteoutBuffer("MdtWaveform", "Mdt", GetPersistency());
83  fDataBuffer->ActivateBuffering(kTRUE);
84  fDataBuffer->SetVerbose(fVerbose);
85  ioman->RegisterWriteoutBuffer("MdtWaveform", fDataBuffer);
86  }else{
87  fWaveformArray = ioman->Register("MdtWaveform", "PndMdtWaveform", "Mdt",GetPersistency());
88  }
89 
90 
91  //fDigiPar->printParams();
92 
94  HowManyPoint = 0;
95 
97  fParamDigiModel->UseNoise(kTRUE);
98  fParamDigiModel->SetNoiseWidth(0.05, 0.02);
100  fParamDigiModel->UsePlot(kFALSE);
103  //fParamDigiModel->SetVerbose(2);
105 
107  fGeoIF->SetVerbose(0);
108  fGeoIF->AddSensor("MDT");
109  fGeoIF->AddSensor("GasCell");
110  Bool_t fGoodGeo = fGeoIF->Init();
111  if(fGoodGeo) fGeoIF->Print();
112 
113 
114  fFile = new TFile("PndMdtPointsToWaveform.root", "RECREATE");
115  tTree = new TTree("pt", "time-stamp diff");
116  tTree->Branch("wt", &fWirpT, "wt/D");
117  tTree->Branch("st", &fStripT, "st/D");
118  tTree->Branch("et", &fEvtT, "et/D");
119  tTree->Branch("len", &fLength, "len/D");
120  tTree->Branch("dis", &fDis, "dis/D");
121  tTree->Branch("mod", &fMod, "mod/I");
122  tTree->Branch("pid", &fPid, "pid/I");
123  //tTree->Branch("xs", &fxs, "xs/D");
124  //tTree->Branch("ys", &fys, "ys/D");
125  //tTree->Branch("zs", &fzs, "zs/D");
126  //tTree->Branch("xe", &fxe, "xe/D");
127  //tTree->Branch("ye", &fye, "ye/D");
128  //tTree->Branch("ze", &fze, "ze/D");
129 
130  cout << "-I- PndMdtPointsToWaveform: Intialization "<<(fGoodGeo ? "successful." : "failed.") << endl;
131 
132  return fGoodGeo ? kSUCCESS : kERROR;
133 }
135 {
137  exec_t();
138  else
139  exec_e();
140 }
142 {
143  TStopwatch timer;
144  if (fVerbose>2){
145  timer.Start();
146  }
147 
148  fWaveformArray->Clear();
149 
150  Double_t fEventTime = FairRootManager::Instance()->GetEventTime();//nano seconds
151  Int_t nHits = fPointArray->GetEntriesFast();
152  Int_t evtNo = FairRunAna::Instance()->GetEventHeader()->GetMCEntryNumber();
153  if (fVerbose>1){
154  cout<<"================PndMdtPointsToWaveform==============================================="<<endl;
155  cout<<"Event No.: "<<evtNo<<", Event Time: "<<fEventTime<<", Load in "<<nHits<< " PndMdtPoint."<<endl;
156  }
157  std::map<key, Int_t> fMcTrackingInfo;
158  PndMdtPoint* thePoint(0);
159  Int_t TrackID;
160  Int_t ParticleType;
161  Int_t DetectorID;
162  TVector3 EntrancePosition;
163  TVector3 ExitingPosition;
164  TVector3 Momentum;
165  Double_t fWaveformTimeStamp;
166 
167  Int_t nPoints = fPointArray->GetEntriesFast();
168  HowManyPoint += nPoints;
169  std::map<Int_t, PndMdtWaveform* > listofWaveforms;
170 
171  Double_t fAmplitude;
172  Double_t fTimeStamp;
173 
174  TVector3 fTubeCenter;
175  for (Int_t ip=0; ip<nPoints; ip++)
176  {
177  thePoint = (PndMdtPoint*) fPointArray->At(ip);
178  if(thePoint->GetEnergyLoss() == 0) continue;
179  key _TrkandDetID(thePoint->GetTrackID(), thePoint->GetDetectorID());
180  //cout<<"TrackID = "<<thePoint->GetTrackID()<<", DetID = "<<thePoint->GetDetectorID()<<endl;
181  std::map<key, Int_t>::iterator it = fMcTrackingInfo.find(_TrkandDetID);
182  if( fMcTrackingInfo.end() == it )
183  {
184  fMcTrackingInfo.insert(std::pair<key,Int_t>(_TrkandDetID, 1));
185  TrackID = thePoint->GetTrackID();
186  DetectorID = thePoint->GetDetectorID();
187  //ParticleType = thePoint->GetParticleType();
188  EntrancePosition = thePoint->GetPosIn();
189  ExitingPosition = thePoint->GetPosOut();
190  Momentum = thePoint->GetMomIn();
191  fWaveformTimeStamp = thePoint->GetTime() + fEventTime;
192 
193  //fxs = EntrancePosition.X();
194  //fys = EntrancePosition.Y();
195  //fzs = EntrancePosition.Z();
196  fLength = thePoint->GetLength();
197  fMod = thePoint->GetModule();
198 
199  //
200  TVector3 fNewEntrance(0,0,0);
201  TVector3 fNewExit(0,0,0);
202  Bool_t fOK1 = fGeoIF->MasterToLocal(DetectorID, EntrancePosition, fNewEntrance);
203  Bool_t fOK2 = fGeoIF->MasterToLocal(DetectorID, ExitingPosition, fNewExit);
204  if( ! (fOK1 && fOK2) ) {
205  cout <<"PndMdtPointsToWaveform::exec_e, invliad detector index #"<<DetectorID<<endl;
206  continue;
207  }
208  TVector2 fexit = fNewExit.XYvector();
209  TVector2 fentr = fNewEntrance.XYvector();
210  TVector2 fvd = fexit - fentr;
211 
212  fDis = TMath::Abs(fexit.Mod()*fentr.Mod()*TMath::Sin(fexit.DeltaPhi(fentr)))/fvd.Mod();
213  if(fVerbose >1 ){
214  cout<<"================================================"<<endl;
215  cout<<"Mod = "<<thePoint->GetModule()<<endl;
216  cout<<"Oct = "<<thePoint->GetSector()<<endl
217  <<"Entrance "<<EntrancePosition<<endl
218  <<"Exiting "<<ExitingPosition<<endl
219  <<"new Entrance "<<fNewEntrance<<endl
220  <<"new Exiting "<<fNewExit<<endl;
221  }
222  PndMCTrack *mcTrk = (PndMCTrack*)fMcTrackArray->At(thePoint->GetTrackID());
223  ParticleType= PdgToIndex(mcTrk->GetPdgCode());
224  fPid = ParticleType;
225 
226  fParamDigiModel->SetParams(ParticleType
227  , Momentum
228  , fNewEntrance
229  , fNewExit);
230  //cout<<"================================================"<<endl;
232 
233  PndMdtWaveform* aWf= new PndMdtWaveform(TrackID, DetectorID, fWaveformTimeStamp, kTRUE);
234  //++nWaveformProduced;
236 
237  std::map<Int_t, PndMdtWaveform*>::iterator mit3 = listofWaveforms.find(DetectorID);
238  if( mit3 == listofWaveforms.end())
239  {//the first waveform
240  listofWaveforms.insert(std::pair<Int_t, PndMdtWaveform*>(DetectorID, aWf));
241  }else{
242  *(mit3->second) += (*aWf);
243  //delete aWf;
244  }
245 
246  Digitize(aWf, fTimeStamp, fAmplitude, kTRUE);
247  fWirpT = fTimeStamp + fEventTime;
248 
249  const std::map<Int_t, std::vector<Double_t> >& fStripSignals = fParamDigiModel->GetStripSignals();
250 
251  std::map<Int_t, std::vector<Double_t> >::const_iterator mit = fStripSignals.begin();
252  std::map<Int_t, std::vector<Double_t> >::const_iterator mend = fStripSignals.end();
253  Int_t newDetID;
254  for(; mit != mend; ++mit){
255  //re-make detector id;
256  fGeoIF->MapWireToStrip(DetectorID, fNewEntrance, newDetID);//waiting for updates
257  Int_t lid = mit->first;
258  if(lid%2 == 0) newDetID += lid/2;
259  else newDetID -= lid/2;
260  PndMdtWaveform* aSf= new PndMdtWaveform(TrackID, newDetID, fWaveformTimeStamp, kFALSE);
261  //++nWaveformProduced;
262  aSf->SetSignal(mit->second);
263 
264  Digitize(aSf, fTimeStamp, fAmplitude, kFALSE);
265  fStripT = fTimeStamp + fEventTime;
266  fEvtT = fEventTime;
267 
268  std::map<Int_t, PndMdtWaveform*>::iterator mit1 = listofWaveforms.find(newDetID);
269  if( mit1 == listofWaveforms.end())
270  {//the first waveform
271  listofWaveforms.insert(std::pair<Int_t, PndMdtWaveform*>(newDetID, aSf));
272  }else{
273  *(mit1->second) += (*aSf);
274  //delete aSf;
275  }
276  }
277  tTree->Fill();
278  }else{
279  ++ it->second;
280  }
281  }
282 
283  std::map<Int_t, PndMdtWaveform*>::iterator mit2 = listofWaveforms.begin();
284  for(; mit2 != listofWaveforms.end(); ++ mit2){
285  (*fWaveformArray)[fWaveformArray->GetEntriesFast()] = mit2->second;
287  }
288 
289  if(fVerbose > 1){
290  std::map<key, Int_t>::iterator it = fMcTrackingInfo.begin();
291  std::map<key, Int_t>::iterator end = fMcTrackingInfo.end();
292  for(; it != end; ++it){
293  cout<<"Track #"<<it->first.TrkID<<" contributes "<<it->second<<" points in tube "<<it->first.DetID<<endl;
294  }
295  }
296  if(fVerbose > 1){
297  cout<<"Number of produced waveforms: "<< fWaveformArray->GetEntriesFast()<<endl;
298  cout<<"================PndMdtPointsToWaveform==============================================="<<endl;
299  }
300 
301  if (fVerbose>2){
302  timer.Stop();
303  Double_t rtime = timer.RealTime();
304  Double_t ctime = timer.CpuTime();
305  cout << "PndMdtPointsToWaveform, Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
306  }
307 }
308 
309 
311 {
312  TStopwatch timer;
313  if (fVerbose>2){
314  timer.Start();
315  }
316 
317  Double_t fEventTime = FairRootManager::Instance()->GetEventTime();//nano seconds
318  Int_t nHits = fPointArray->GetEntriesFast();
319  Int_t evtNo = FairRunAna::Instance()->GetEventHeader()->GetMCEntryNumber();
320  if (fVerbose>1){
321  cout<<"================PndMdtPointsToWaveform==============================================="<<endl;
322  cout<<"Event No.: "<<evtNo<<", Event Time: "<<fEventTime<<", Load in "<<nHits<< " PndMdtHit."<<endl;
323  cout<<"Size of PndMdtWaveformWriteoutBuffer: "<< fDataBuffer->GetNData()<<endl;
324  cout<<"================PndMdtPointsToWaveform==============================================="<<endl;
325  }
326  std::map<key, Int_t> fMcTrackingInfo;
327  PndMdtPoint* thePoint(0);
328  Int_t TrackID;
329  Int_t ParticleType;
330  Long_t DetectorID;
331  TVector3 EntrancePosition;
332  TVector3 ExitingPosition;
333  TVector3 Momentum;
334  Double_t TimeStamp;
335 
336  Int_t nPoints = fPointArray->GetEntriesFast();
337  HowManyPoint += nPoints;
338  for (Int_t ip=0; ip<nPoints; ip++)
339  {
340  thePoint = (PndMdtPoint*) fPointArray->At(ip);
341  if(thePoint->GetEnergyLoss() == 0) continue;
342  key _TrkandDetID(thePoint->GetTrackID(), thePoint->GetDetectorID());
343  std::map<key, Int_t>::iterator it = fMcTrackingInfo.find(_TrkandDetID);
344  if( fMcTrackingInfo.end() == it )
345  {
346  fMcTrackingInfo.insert(std::pair<key,Int_t>(_TrkandDetID, 1));
347  TrackID = thePoint->GetTrackID();
348  DetectorID = thePoint->GetDetectorID();
349  //ParticleType = thePoint->GetParticleType();
350  EntrancePosition = thePoint->GetPosIn();
351  ExitingPosition = thePoint->GetPosOut();
352  Momentum = thePoint->GetMomIn();
353  TimeStamp = thePoint->GetTime() + fEventTime;
354 
355  TVector3 fNewEntrance(0,0,0);
356  TVector3 fNewExit(0,0,0);
357  Bool_t fOK1 = fGeoIF->MasterToLocal(DetectorID, EntrancePosition, fNewEntrance);
358  Bool_t fOK2 = fGeoIF->MasterToLocal(DetectorID, ExitingPosition, fNewExit);
359  if( ! (fOK1 && fOK2) ) {
360  cout <<"PndMdtPointsToWaveform::exec_t, invalid detector index #"<<DetectorID<<endl;
361  continue;
362  }
363  TVector2 fexit = fNewExit.XYvector();
364  TVector2 fentr = fNewEntrance.XYvector();
365  TVector2 fvd = fexit - fentr;
366 
367  fDis = TMath::Abs(fexit.Mod()*fentr.Mod()*TMath::Sin(fexit.DeltaPhi(fentr)))/fvd.Mod();
368  if(fVerbose > 1){
369  cout<<"================================================"<<endl;
370  cout<<"Mod = "<<thePoint->GetModule()<<endl;
371  cout<<"Oct = "<<thePoint->GetSector()<<endl
372  <<"Entrance "<<EntrancePosition<<endl
373  <<"Exiting "<<ExitingPosition<<endl
374  <<"new Entrance "<<fNewEntrance<<endl
375  <<"new Exiting "<<fNewExit<<endl;
376  }
377  PndMCTrack *mcTrk = (PndMCTrack*)fMcTrackArray->At(thePoint->GetTrackID());
378  ParticleType= PdgToIndex(mcTrk->GetPdgCode());
379  fPid = ParticleType;
380 
381  fParamDigiModel->SetParams(ParticleType
382  , Momentum
383  , fNewEntrance
384  , fNewExit);
386 
387  PndMdtWaveform* aWf = new PndMdtWaveform(TrackID, DetectorID, TimeStamp, kTRUE);//wire signal
390  fDataBuffer->FillNewData(aWf, aWf->GetTimeStamp(), aWf->GetActiveTime());
391  //aWf->Print();
392  delete aWf;
393 
394  const std::map<Int_t, std::vector<Double_t> >& fStripSignals = fParamDigiModel->GetStripSignals();
395  std::map<Int_t, std::vector<Double_t> >::const_iterator mit = fStripSignals.begin();
396  std::map<Int_t, std::vector<Double_t> >::const_iterator mend = fStripSignals.end();
397  Int_t detID;
398  for(; mit != mend; ++mit, ++nWaveformProduced){
399  //re-make detector id;
400  fGeoIF->MapWireToStrip(DetectorID, EntrancePosition, detID);//waiting for updates for new geometry versions
401  Int_t lid = mit->first;
402  if(lid%2 == 0) detID += lid/2;
403  else detID -= lid/2;
404  PndMdtWaveform* aSf = new PndMdtWaveform(TrackID, detID, TimeStamp, kFALSE);//strip signal
406  aSf->SetSignal(mit->second);
407  fDataBuffer->FillNewData(aSf, aSf->GetTimeStamp(), aSf->GetActiveTime());
408  delete aSf;
409  }
410  }else{
411  ++ it->second;
412  }
413  }
414 
415  if(fVerbose > 1){
416  std::map<key, Int_t>::iterator it = fMcTrackingInfo.begin();
417  std::map<key, Int_t>::iterator end = fMcTrackingInfo.end();
418  for(; it != end; ++it){
419  cout<<"Track #"<<it->first.TrkID<<" contributes "<<it->second<<" points in tube "<<it->first.DetID<<endl;
420  }
421  }
422 
423  if (fVerbose>2){
424  timer.Stop();
425  Double_t rtime = timer.RealTime();
426  Double_t ctime = timer.CpuTime();
427  cout << "PndMdtPointsToWaveform, Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
428  }
429 }
430 
432 
433  // Get run and runtime database
434  FairRun* run = FairRun::Instance();
435  if ( ! run ) Fatal("SetParContainers", "No analysis run");
436 
437  FairRuntimeDb* db = run->GetRuntimeDb();
438  if ( ! db ) Fatal("SetParContainers", "No runtime database");
439 
440  // Get Mdt geometry parameter container
441  // fGeoPar = (PndMdtGeoPar*) db->getContainer("PndMdtGeoPar");
442  // Get Mdt digitisation parameter container
443  // fDigiPar = (PndMdtDigiPar*) db->getContainer("PndMdtDigiPar");
444 
445 }
446 
448 {
449  SetPersistency(val);
450  return;
451 }
453 {
454  if (fVerbose>0)
455  {
456  std::cout<<"==================================================="<<std::endl;
457  std::cout<<"PndMdtPointsToWaveform::FinishTask"<<std::endl;
458  std::cout<<"***************************************************"<<std::endl;
459  std::cout<<"Read points# "<<HowManyPoint<<std::endl;
460  std::cout<<"Produce waveforms# "<<nWaveformProduced<<std::endl;
461  std::cout<<"***************************************************"<<std::endl;
462  }
463  fFile->cd();
464  tTree->Write();
465  fFile->Close();
466 }
468 {
469  const Double_t fWireNoiseSigma = 0.05;
470  const Double_t fStripNoiseSigma = 0.01;
471  const Double_t fSamplingInterval = 10.;//nano seconds
472 
473  time = -1;
474  amp = -9999;
475  Double_t fPeak = 0.;
476  Bool_t NotFound = kTRUE;
477  //Double_t fNoiseSigma; //[R.K. 01/2017] unused variable?
478  Double_t fThreshold(1e9);
479  if(isWire) fThreshold = 5.*fWireNoiseSigma;
480  else fThreshold = 5.*fStripNoiseSigma;
481 
482  const std::vector<Double_t>& fSignalData = theWf->GetSignal();
483  for(size_t is=0; is < fSignalData.size(); ++ is){
484  if(TMath::Abs(fSignalData[is]) > TMath::Abs(fPeak)) fPeak = fSignalData[is];
485  if(TMath::Abs(fSignalData[is]) > fThreshold && NotFound) {
486  time = is*fSamplingInterval;//nano seconds
487  NotFound = kFALSE;
488  }
489  }
490  amp = fPeak;
491  return time > 0;
492 }
494 {
495  Int_t abspdg = abs(pdg);
496  if(abspdg == 11) return 0;
497  else if(abspdg == 211) return 2;
498  else if(abspdg == 13) return 1;
499  else if(abspdg == 2212) return 4;
500  else if(abspdg == 321) return 3;
501  else return 1;
502 }
503 
int fVerbose
Definition: poormantracks.C:24
Bool_t Digitize(PndMdtWaveform *theWf, Double_t &time, Double_t &amp, Bool_t isWire)
Double_t GetActiveTime() const
PndMdtWaveformWriteoutBuffer * fDataBuffer
Int_t run
Definition: autocutx.C:47
void UseGaussianAmp(Bool_t swith)
Bool_t MasterToLocal(Int_t iDetId, TVector3 &master, TVector3 &local)
TVector3 GetPosOut() const
Definition: PndMdtPoint.h:49
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
void SetNoiseWidth(Double_t anode, Double_t strip)
PndMdtParamDigi * fParamDigiModel
Int_t GetPdgCode() const
Definition: PndMCTrack.h:73
#define verbose
const std::vector< Double_t > & GetSignal() const
static T Sin(const T &x)
Definition: PndCAMath.h:42
void SetPersistency(Bool_t val=kTRUE)
PndMdtPointsToWaveform(Int_t verbose=0, Bool_t storewaves=kTRUE)
Bool_t MapWireToStrip(Int_t iDetId, const TVector3 &fEntryPos, Int_t &fStripDetID)
static T Abs(const T &x)
Definition: PndCAMath.h:39
Short_t GetSector() const
Definition: PndMdtPoint.h:54
int nHits
Definition: RiemannTest.C:16
Double_t
TStopwatch timer
Definition: hit_dirc.C:51
void UsePlot(Bool_t swith=kTRUE)
void AddSensor(TString _v)
static int is
Definition: ranlxd.cxx:374
TVector3 GetPosIn() const
Definition: PndMdtPoint.h:47
Double_t ctime
Definition: hit_dirc.C:114
virtual void Exec(Option_t *opt)
void SetVerbose(Int_t _v)
void UseDetailedSim(Bool_t swith=kTRUE)
static PndMdtIGeometry * Instance()
PndMdtParamDigi & SetParams(Int_t ptlType, TVector3 iniP, TVector3 iniPos, TVector3 finalPos, Double_t stripLen=100.)
void Compute(Bool_t useConvolution=kTRUE)
ClassImp(PndAnaContFact)
void UseNoise(Bool_t swith)
Double_t rtime
Definition: hit_dirc.C:113
void SetSignal(const std::vector< Double_t > &v)
const std::vector< Double_t > & GetWireSignal() const
TVector3 GetMomIn() const
Definition: PndMdtPoint.h:48
const std::map< Int_t, std::vector< Double_t > > & GetStripSignals() const
void SetOptimization(Int_t val)
void Print() const
Short_t GetModule() const
Definition: PndMdtPoint.h:53