FairRoot/PandaRoot
runTripletFinderMini.C
Go to the documentation of this file.
1 #include "drawhelpers.hxx"
2 
3 void SaveAndUpdateHisto(TH1* currenthisto, TFile& storagefile)
4 {
5  if (storagefile.Get(currenthisto->GetName()) != 0) {
6  currenthisto->Add((TH1*)storagefile.Get(currenthisto->GetName()));
7  }
8  cout << currenthisto->GetName() << ": " << currenthisto->GetEntries() << endl;
9  currenthisto->Write(currenthisto->GetName(), TObject::kWriteDelete);
10 }
11 
13  gROOT->Reset();
14  // Verbosity level (0=quiet, 1=event level, 2=track level, 3=debug)
15  Int_t iVerbose = 0;
16 
17  enum DetectorId {
19 
20  TStopwatch timer;
21  timer.Start();
22  gROOT->LoadMacro("$VMCWORKDIR/gconfig/rootlogon.C");
23  rootlogon();
24 
25  TDatabasePDG *pdg = new TDatabasePDG();
26 
27  // Reco Parameters
28  Double_t stthitlifetime = 245;
29 
30  TString simFileName = "Sim_Dpm_500.root";
31  TString parFileName = "Sim_Dpm_500_params.root";
32  TString digiFileName = "Sim_Dpm_500_digi.root";
33  TString recoFileName = "Sim_Dpm_500_reco.root";
34  TString outFileName = "Sim_Dpm_500_streamdisplay.root";
35  TString histofilename = "onlinetrkmccheck.root";
36 
37  TFile filedigi(digiFileName.Data());
38  TFile filereco(recoFileName.Data());
39  TFile filerecopixel(recoFileName.Data());
40 
41  TTree *treedigi = (TTree*) filedigi.Get("pndsim");
42  TTree *recotree = (TTree*) filereco.Get("pndsim");
43 
44  TFile simfile(simFileName.Data());
45  TTree* mctree = (TTree*) simfile.Get("pndsim");
46  TClonesArray* mcarray = new TClonesArray("PndMCTrack");
47  mctree->SetBranchAddress("MCTrack", &mcarray);
48 
49  FairRunAna *fRun = new FairRunAna();
50  fRun->SetInputFile(simFileName.Data());
51  fRun->AddFriend(recoFileName.Data());
52  fRun->AddFriend(digiFileName.Data());
53  fRun->SetOutputFile(outFileName.Data());
54  fRun->Init();
55  FairRuntimeDb* rtdb = fRun->GetRuntimeDb();
56 
57  PndOnlineGeometryManager *online_geometry = new PndOnlineGeometryManager(rtdb, parFileName.Data());
58 
59  PndOnlineManager *online = new PndOnlineManager();
60  online->AddGeometryManager(online_geometry);
61 
62  online->LoadStream(PndOnline::kSTT,"STTSortedHits",stthitlifetime);
63  online->LoadStream(PndOnline::kMVDPixel,"MVDHitsPixel",stthitlifetime);
64  online->LoadStream(PndOnline::kMVDStrip,"MVDHitsStrip",stthitlifetime);
65  //online->SetHESRRevolutionDuration(3000);
66 
67  TClonesArray* tubearray = online_geometry->GetDetectorGeometry(PndOnline::kSTT);
68 
69  // load task
70  PndOnlineSttTripletFinderMini *triplet_finder = new PndOnlineSttTripletFinderMini(online, tubearray);
71  online->AddTask((FairTask*)triplet_finder);
72 
73  // initialize display
74  TCanvas* c1 = new TCanvas("c1");
75  c1->Range(-42,-42,42,42);
76  c1->SetCanvasSize(1200, 1200);
77  TText* mytext = new TText();
78 
79  // event loop!
80  int current_time = 0;
81  int drawtime = 0;
82  int delta_t = 2050;
83  int drawdeltat = 5;
84  int final_time = 10050;
85 
86  online->SetHESRRevolutionDuration(delta_t);
87 
88  online->Init();
89 
90  TObjArray* online_fairhits_stt = 0;
91  TObjArray* online_tracks = 0;
92  TObjArray* online_tracks2 = 0;
93  TObjArray* online_fairhits_mvd = 0;
94 
95  std::vector<double> mctracktime;
96  std::vector<double> mctrackpt;
97  std::vector<double> mctrackphi;
98  std::vector<int> mctrackstatus;
99  std::vector<double> recotracktime;
100  std::vector<double> recotrackpt;
101  std::vector<double> recotrackphi;
102  std::vector<int> recotrackstatus;
103  std::vector<int> recotrackmcid;
104  int totalmctracks = 0;
105  int goodmctracks = 0;
106  int totalrecotracks = 0;
107  int goodrecotracks = 0;
108  int norecotracks = 0;
109  int deltaphifailed = 0;
110  int deltaptfailed = 0;
111  int deltabothfailed = 0;
112  int secondaries = 0;
113  int globalmaxmcid = 0;
114 
115  //std::map<int, std::map<int, int> > foundmap90;
116  //std::map<int, std::map<int, int> > foundmap80;
117 
118  int mcidgood90 = 0;
119  int mcidgood80 = 0;
120  int mcidbad = 0;
121 
122  //output histos
123  TH2D* hmcall = new TH2D("hmcall", "hmcall", 1500, -15, 15, 75, 0, 1.5);
124  TH2D* hmcprime = new TH2D("hmcprime", "hmcprime", 1500, -15, 15, 75, 0, 1.5);
125  TH2D* hmcrecoable = new TH2D("hmcrecoable", "hmcrecoable", 1500, -15, 15, 75, 0, 1.5);
126  TH2D* hdafuq = new TH2D("hdafuq", "hdafuq", 1500, -15, 15, 75, 0, 1.5);
127  TH2D* hmcrecoed = new TH2D("hmcrecoed", "hmcrecoed", 1500, -15, 15, 75, 0, 1.5);
128  TH2D* hmcnotrecoed = new TH2D("hmcnotrecoed", "hmcnotrecoed", 1500, -15, 15, 75, 0, 1.5);
129 
130 
131  for (current_time = 0; current_time < final_time; current_time += delta_t) {
132  online->Clear();
133  //online->LoadHits( PndOnlineManager::kHESRRevolution );
134  online->ClearTracks();
135  online->LoadHits( delta_t );
136  //online->LoadHits( current_time );
137  online->Process();
138  //online->Clear();
139 
140  online_tracks = online->GetTrackObjectList();
141  int maxmcid = 0;
142  int minmcid = 0;
143  int firsti = 0;
144  if (online_tracks->GetEntriesFast() > 0) {
145  PndOnlineTrack* firstTrack = (PndOnlineTrack*) online_tracks->At(firsti);
146  while (firstTrack->DrawMode() != PndOnlineTrack::kVertexMomentumAsCircleHack) {
147  firsti++;
148  if (firsti > online_tracks->GetEntriesFast()) {
149  cout << "No drawable online Track in Array. We have a severe problem." << endl;
150  break;
151  }
152  firstTrack = (PndOnlineTrack*) online_tracks->At(firsti);
153  }
154  maxmcid = firstTrack->MCEntryNumber();
155  minmcid = firstTrack->MCEntryNumber();
156  }
157  for (int i = 0; i < online_tracks->GetEntriesFast(); i++) {
158  PndOnlineTrack* myTrack = (PndOnlineTrack*) online_tracks->At(i);
159  if (myTrack->DrawMode() == PndOnlineTrack::kVertexMomentumAsCircleHack) {
160  TVector2 innerhit;
161  innerhit.Set(myTrack->Vertex().x(), myTrack->Vertex().y());
162  TVector2 outerhit;
163  outerhit.Set(myTrack->Momentum().x(), myTrack->Momentum().y());
164  TVector2 circleorigin;
165  Double_t circleradius;
166  Int_t clockwise;
167  CalculateCircle(&innerhit, &outerhit, &circleorigin, &circleradius, &clockwise);
168  Double_t trackpt = circleradius * 2.0 * 3.0;
169  Double_t orthophi = circleorigin.Phi();
170  Double_t trackphi = orthophi + 0.5*TMath::Pi()*clockwise;
171  if (trackphi < 0) {
172  trackphi += 2*TMath::Pi();
173  }
174  if (trackphi > 2*TMath::Pi()) {
175  trackphi -= 2*TMath::Pi();
176  }
177 
178  cout << "Reco Track MCEntryNumber: " << myTrack->MCEntryNumber() << endl;
179  cout << "Reco Track MCEntryMajority: " << myTrack->MCEntryMajority() << endl;
180  cout << "Reco Track MCTrackNumber: " << myTrack->MCTrackNumber() << endl;
181  cout << "Reco Track MCTrackMajority: " << myTrack->MCTrackMajority() << endl;
182  cout << "Reco Track HitCount: " << myTrack->HitCount() << endl;
183 
184  if ( (myTrack->MCEntryMajority() > 0.9) && (myTrack->MCTrackMajority() > 0.9) ) {
185  //(foundmap90[myTrack->MCEntryNumber()][myTrack->MCTrackNumber()])++
186  mcidgood90++;
187  }
188  if ( (myTrack->MCEntryMajority() > 0.8) && (myTrack->MCTrackMajority() > 0.8) ) {
189  //(foundmap80[myTrack->MCEntryNumber()][myTrack->MCTrackNumber()])++
190  mcidgood80++;
191  } else {
192  mcidbad++;
193  }
194 
195  int mcentrynumber = myTrack->MCEntryNumber();
196  if (mcentrynumber > maxmcid) {
197  maxmcid = mcentrynumber;
198  }
199  if (mcentrynumber < minmcid) {
200  minmcid = mcentrynumber;
201  }
202 
203  //std::cout << "RecoTrack: " << totalrecotracks << ", MCEntryNumber: " << mcentrynumber << " DigiTime: " << myTrack->T0Min() << ", T0time: " << myTrack->T0() << ", Pt " << trackpt << ", Phi: " << trackphi << " TriPhi: " << innerhit.Phi() << ", Radius: " << circleradius << ", DeltaT0: " << myTrack->T0Max()-myTrack->T0Min() << endl;
204 
205  //std::cout << "RecoTrack: " << totalrecotracks << ", MCEntryNumber: " << mcentrynumber << ", Pt " << trackpt << ", Phi: " << trackphi << " TriPhi: " << innerhit.Phi() << ", Radius: " << circleradius << endl;
206 
207  //recotracktime.push_back(myTrack->T0Min());
208  recotrackpt.push_back(trackpt);
209  recotrackphi.push_back(trackphi);
210  recotrackstatus.push_back(0);
211  recotrackmcid.push_back(mcentrynumber);
212  totalrecotracks++;
213  }
214  }
215  if (maxmcid > globalmaxmcid) {
216  globalmaxmcid = maxmcid;
217  }
218  //cout << "MC ID Range: " << minmcid << ", " << globalmaxmcid << endl;
219  for (int j = minmcid; j < mctree->GetEntriesFast(); j++) {
220  cout << "MC outer loop: " << j << endl;
221  if (j > globalmaxmcid) {
222  break;
223  }
224  mctree->GetEntry(j);
225  cout << "MC Entry count: " << mcarray->GetEntriesFast() << endl;
226  for (int k = 0; k < mcarray->GetEntriesFast(); k++) {
227  PndMCTrack* mctrack = (PndMCTrack*)mcarray->At(k);
228  if (mctrack == 0) {
229  cout << "MC Track has NULL pointer. Oooops." << endl;
230  continue;
231  }
232  hmcall->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
233  bool isrecoable = false;
234  if (mctrack->GetMotherID() == -1) {
235  hmcprime->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
236  isrecoable = true;
237  if (mctrack->GetMomentum().Pz() < -0.4) {
238  cout << "Critical Track at Event: " << j << " Track " << k << " Particle: " << mctrack->GetPdgCode() << endl;
239  //cout << "Particle Mass: " << pdg->GetParticle(mctrack->GetPdgCode())->Mass() << " Charge: " << pdg->GetParticle(mctrack->GetPdgCode())->Charge() << endl;
240  //cout << "MCTrack STT Hits: " << mctrack->GetNPoints(kSTT) << ", Pz: " << mctrack->GetMomentum().Pz() << ", Pt: " << mctrack->GetMomentum().Pt() << ", Phi: " << mctrack->GetMomentum().Phi() << ", Radius: " << mctrack->GetMomentum().Pt() / (3.0*2.0) << endl;
241  cout << "MCVertex: " << mctrack->GetStartVertex().Mag() << " XYZ: " << mctrack->GetStartVertex().x() << ", " << mctrack->GetStartVertex().y() << ", " << mctrack->GetStartVertex().z() << endl;
242  }
243  }
244  //if (mctrack->GetMotherID() == -1 && mctrack->GetNPoints(kSTT) > 0) {
245  //cout << TRho::Instance()->GetPDG()->GetParticle(mctrack->GetPdgCode())->Mass() << endl;
246  //}
247 
248  //bool isrecoable = true;
249  if (mctrack->GetMomentum().Pt() < 0.052) isrecoable = false;
250  if ( (mctrack->GetMomentum().Pt() < 0.052) && (mctrack->GetNPoints(kSTT) > 0) ) {
251  cout << "Low pt hit" << endl;
252  }
253  if ( (mctrack->GetMomentum().Pt()/mctrack->GetMomentum().Pz() < 28.0/110.0) && (mctrack->GetMomentum().Pz() > 0) ) isrecoable = false;
254  if ( (-(mctrack->GetMomentum().Pt())/mctrack->GetMomentum().Pz() < 28.0/55.0) && (mctrack->GetMomentum().Pz() < 0) ) isrecoable = false;
255  if ( (mctrack->GetMotherID() == -1) && isrecoable && (mctrack->GetNPoints(kSTT) < 3) ) {
256  if (TMath::Abs(pdg->GetParticle(mctrack->GetPdgCode())->Charge()) < 1) isrecoable = false;
257  if (pdg->GetParticle(mctrack->GetPdgCode())->Mass() > 1) isrecoable = false;
258  }
259  if ( (mctrack->GetMotherID() == -1) && isrecoable && (mctrack->GetMomentum().Mag()/pdg->GetParticle(mctrack->GetPdgCode())->Mass() < 0.4) ) {
260  cout << "Low betagamma particle" << endl;
261  isrecoable = false;
262  }
263  if ( (mctrack->GetMotherID() == -1) && isrecoable && (mctrack->GetNPoints(kSTT) < 3) ) {
264  cout << "Meh, reco possibility mismatch." << endl;
265  hdafuq->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
266  cout << "Dafuq at Event: " << j << " Track " << k << " Particle: " << mctrack->GetPdgCode() << " Charge: " << pdg->GetParticle(mctrack->GetPdgCode())->Charge() << endl;
267  mctrack->GetMomentum().Print();
268  isrecoable = false;
269  }
270 
271  //if (mctrack->GetMotherID() == -1 && mctrack->GetNPoints(kSTT) > 0) {
272  if (mctrack->GetMotherID() == -1 && isrecoable) {
273  hmcrecoable->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
274  Double_t mcphi = mctrack->GetMomentum().Phi();
275  Double_t mcpt = mctrack->GetMomentum().Pt()*1000;
276  if (mcphi < 0) {
277  mcphi += 2*TMath::Pi();
278  }
279  //cout << "MCTrack: " << j << " " << k << ", Pt: " << mcpt << ", Phi: " << mcphi << ", Radius: " << mcpt / (3.0*2.0) << endl;
280  //cout << "MCVertex: " << mctrack->GetStartVertex().Mag() << " XYZ: " << mctrack->GetStartVertex().x() << ", " << mctrack->GetStartVertex().y() << ", " << mctrack->GetStartVertex().z() << endl;
281  int trackstatus = 0;
282  if (mctrack->GetStartVertex().Mag() < 2) {
283  for (int r = 0; r < recotrackmcid.size(); r++) {
284  if (recotrackmcid.at(r) == j) {
285  Double_t deltaphi = recotrackphi.at(r) - mcphi;
286  if (deltaphi < 0) {
287  deltaphi *= -1;
288  }
289  Double_t deltapt = recotrackpt.at(r) - mcpt;
290  if (deltapt < 0) {
291  deltapt *= -1;
292  }
293  bool phiok = (deltaphi < 0.1);
294  bool ptok = ( (deltapt < mcpt * 0.1) || ((mcpt > 450)&&(recotrackpt.at(r) > 450)) );
295  if (phiok && ptok) {
296  trackstatus++;
297  recotrackstatus.at(r) = 1;
298  //hmcrecoed->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
299  } else {
300  //hmcnotrecoed->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
301  }
302  if ((!phiok) && ptok) {
303  deltaphifailed++;
304  }
305  if (phiok && (!ptok)) {
306  deltaptfailed++;
307  }
308  if ((!phiok) && (!ptok)) {
309  deltabothfailed++;
310  }
311  }
312  }
313  //cout << "Found " << trackstatus << " matching reco tracks." << endl;
314  if (trackstatus == 0) {
315  hmcnotrecoed->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
316  norecotracks++;
317  } else {
318  hmcrecoed->Fill(mctrack->GetMomentum().Pz(), mctrack->GetMomentum().Pt());
319  }
320  mctrackstatus.push_back(trackstatus);
321  goodmctracks++;
322  } else {
323  secondaries++;
324  }
325  }
326  }
327  }
328  }
329 
330  for (int s = 0; s < mctrackstatus.size(); s++) {
331  if (mctrackstatus.at(s) > 0) {
332  goodrecotracks++;
333  }
334  }
335 
336  int recotrackstatusgoodcount = 0;
337  int recotrackstatusbadcount = 0;
338 
339  for (int s = 0; s < recotrackstatus.size(); s++) {
340  if (recotrackmcid.at(s) <= globalmaxmcid) {
341  if (recotrackstatus.at(s) > 0) {
342  recotrackstatusgoodcount++;
343  } else {
344  recotrackstatusbadcount++;
345  }
346  }
347  }
348 
349  //for (std::map<int, std::map<int, int> >::iterator iterevent = foundmap90.begin(); iterevent != foundmap90.end(); iterevent++) {
350  //for (itertrack = *iterevent.begin(); itertrack != *iterevent.end(); itertrack++
351  //}
352 
353  cout << "Total reconstructed track count: " << totalrecotracks << endl;
354  cout << "Reached MC ID: " << globalmaxmcid << endl;
355  cout << "Reconstructable MC track count: " << goodmctracks << endl;
356  cout << "MC Tracks reconstructed: " << goodrecotracks << endl;
357  cout << "MC Tracks NOT reconstructed: " << norecotracks << endl;
358  cout << "Reco Status Good: " << recotrackstatusgoodcount << endl;
359  cout << "Reco Status Bad: " << recotrackstatusbadcount << endl;
360  cout << "Delta phi failed: " << deltaphifailed << endl;
361  cout << "Delta pt failed: " << deltaptfailed << endl;
362  cout << "Delta both failed: " << deltabothfailed << endl;
363  cout << "Secondary Tracks: " << secondaries << endl;
364 
365  cout << "MC Match Good 90: " << mcidgood90 << endl;
366  cout << "MC Match Good 80: " << mcidgood80 << endl;
367  cout << "MC Match Bad: " << mcidbad << endl;
368 
369  //c1->Print("hitdisplay.gif++");
370 
371  TFile outputstorage(histofilename, "UPDATE");
372  outputstorage.cd();
373  SaveAndUpdateHisto(hmcall, outputstorage);
374  SaveAndUpdateHisto(hmcprime, outputstorage);
375  SaveAndUpdateHisto(hmcrecoable, outputstorage);
376  SaveAndUpdateHisto(hdafuq, outputstorage);
377  SaveAndUpdateHisto(hmcrecoed, outputstorage);
378  SaveAndUpdateHisto(hmcnotrecoed, outputstorage);
379 
380  // ----- Finish -------------------------------------------------------
381  timer.Stop();
382  Double_t rtime = timer.RealTime();
383  Double_t ctime = timer.CpuTime();
384  cout << endl << endl;
385  cout << "Macro finished succesfully." << endl;
386  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
387  cout << endl;
388  // ------------------------------------------------------------------------
389  return 0;
390 }
PndMCTrack * mctrack
TFile filereco("MvdStt_Test_reco.root")
void SaveAndUpdateHisto(TH1 *currenthisto, TFile &storagefile)
double r
Definition: RiemannTest.C:14
Int_t i
Definition: run_full.C:25
Int_t GetNPoints(DetectorId detId) const
Definition: PndMCTrack.cxx:120
Int_t GetPdgCode() const
Definition: PndMCTrack.h:73
TLorentzVector s
Definition: Pnd2DStar.C:50
TVector3 GetMomentum() const
Definition: PndMCTrack.h:78
FairRunAna * fRun
Definition: hit_dirc.C:58
static T Abs(const T &x)
Definition: PndCAMath.h:39
Double_t
TTree * treedigi
TStopwatch timer
Definition: hit_dirc.C:51
TFile filedigi("testdigi.root")
int runTripletFinderMini()
FairRuntimeDb * rtdb
Definition: hit_dirc.C:66
c1
Definition: plot_dirc.C:35
Double_t ctime
Definition: hit_dirc.C:114
Int_t iVerbose
TVector3 GetStartVertex() const
Definition: PndMCTrack.h:76
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
Double_t rtime
Definition: hit_dirc.C:113
Double_t Pi