StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SvtMatchedTree.cxx
1 #include "SvtMatchedTree.h"
2 #include <stdlib.h>
3 #include "Riostream.h"
4 #include "TROOT.h"
5 #include "TSystem.h"
6 #include "TFile.h"
7 #include "TKey.h"
8 #include "TRandom.h"
9 #include "TTree.h"
10 #include "TBranch.h"
11 #include "TStopwatch.h"
12 #include "StThreeVectorF.hh"
13 #include "StMatrixF.hh"
14 #include "TH1.h"
15 #include "TH2.h"
16 #include "TProfile.h"
17 #include "TMath.h"
18 #include "TVector3.h"
19 #include "TProcessID.h"
20 #include "StEvent.h"
21 #include "StPrimaryVertex.h"
22 #include "StBFChain.h"
23 #include "TGeoMatrix.h"
24 #include "St_db_Maker/St_db_Maker.h"
25 #include "tables/St_svtWafersPosition_Table.h"
26 #include "tables/St_ssdWafersPosition_Table.h"
27 #include "tables/St_Survey_Table.h"
28 #include "StSvtPool/EventT/EventT.h"
29 #include "StSsdDbMaker/StSsdDbMaker.h"
30 #include "StSvtDbMaker/StSvtDbMaker.h"
31 //________________________________________________________________________________
32 SvtMatchedTree::SvtMatchedTree(const Char_t *name) : StMaker(name),fFile(0), fTree(0), fEvent(0) {
33  SetMinNoHits();
34  SetpCut();
35  SetOut();
36 }
37 //________________________________________________________________________________
38 Int_t SvtMatchedTree::Init() {
39  SetTree();
40  return kStOK;
41 }
42 //________________________________________________________________________________
44  fFile = fTree->GetCurrentFile(); //just in case we switched to a new file
45  fFile->Write();
46  fTree->Print();
47  return kStOK;
48 }
49 
50 //________________________________________________________________________________
51 void SvtMatchedTree::SetTree() {
52  StBFChain *chain = (StBFChain *) StMaker::GetChain();
53  if (! chain) return;
54  // root.exe
55  Int_t split = 99; // by default, split Event in sub branches
56  Int_t comp = 1; // by default file is compressed
57  Int_t branchStyle = 1; //new style by default
58  if (split < 0) {branchStyle = 0; split = -1-split;}
59  Int_t bufsize;
60  //Authorize Trees up to 2 Terabytes (if the system can do it)
61  TTree::SetMaxTreeSize(1000*Long64_t(2000000000));
62  TString FName(fOut);
63  if (fMinNoHits > 0) FName += Form("_%i_%f2.1_",fMinNoHits,fpCut);
64  FName += gSystem->BaseName(chain->GetFileIn().Data());
65  FName.ReplaceAll("st_physics","");
66  FName.ReplaceAll(".event","");
67  FName.ReplaceAll(".daq",".root");
68  fFile = new TFile(FName,"RECREATE","TTree with SVT + SSD hits and tracks");
69  fFile->SetCompressionLevel(comp);
70  // Create a ROOT Tree and one superbranch
71  fTree = new TTree("T","TTree with SVT + SSD hits and tracks");
72  fTree->SetAutoSave(1000000000); // autosave when 1 Gbyte written
73  bufsize = 64000;
74  if (split) bufsize /= 4;
75  fEvent = new EventT();
76  TTree::SetBranchStyle(branchStyle);
77  TBranch *branch = fTree->Branch("EventT", &fEvent, bufsize,split);
78  branch->SetAutoDelete(kFALSE);
79 }
80 //________________________________________________________________________________
82  if (! EventT::RotMatrices()) MakeListOfRotations();
83  StEvent* pEvent = (StEvent*) GetInputDS("StEvent");
84  if (pEvent && pEvent->primaryVertex() && !fEvent->Build(pEvent,fMinNoHits,fpCut)) fTree->Fill(); //fill the tree
85  return kStOK;
86 }
87 //________________________________________________________________________________
88 void SvtMatchedTree::Print(Option_t *opt) const {
89  if (! EventT::RotMatrices()) return;
90  TIter next(EventT::RotMatrices());
91  TGeoHMatrix *comb = 0;
92  while ((comb = (TGeoHMatrix *) next())) {
93  Int_t Id;
94  sscanf(comb->GetName()+1,"%04i",&Id);
95  Int_t Ladder = Id%100;
96  Int_t Layer = Id/1000; if (Layer > 7) Layer = 7;
97  Int_t Wafer = (Id - 1000*Layer)/100;
98  cout << comb->GetName() << "\tLayer/Ladder/Wafer = " << Layer << "/" << Ladder << "/" << Wafer << endl;
99  comb->Print();
100  cout << "=================================" << endl;
101  }
102 }
103 //________________________________________________________________________________
104 void SvtMatchedTree::MakeListOfRotations() {
105  if (EventT::RotMatrices()) return;
106  THashList *rotMHash = new THashList(100,0);
107  rotMHash->SetOwner(kFALSE);
108  EventT::SetRotMatrices(rotMHash);
109  THashList *hash = 0;
110  for (Int_t i = 0; i < 2; i++) {
111  if (i == 0 && gStSvtDbMaker) hash = gStSvtDbMaker->GetRotations();
112  if (i == 1 && gStSsdDbMaker) hash = gStSsdDbMaker->GetRotations();
113  if (! hash) continue;
114  TIter next(hash);
115  TGeoHMatrix *comb;
116  while ((comb = (TGeoHMatrix *) next())) rotMHash->Add(comb);
117  }
118 #if 0
119  TDataSet *set = GetDataBase("Geometry/ssd");
120  St_Survey *SsdOnGlobal = (St_Survey *) set->Find("SsdOnGlobal");
121  if (! SsdOnGlobal) {cout << "SsdOnGlobal has not been found" << endl; return;}
122  TGeoHMatrix GL, LS,SG,LA,WG;
123  Survey_st *OnGlobal = SsdOnGlobal->GetTable(); // SSD and SVT as whole
124  GL.SetRotation(&OnGlobal->r00);
125  GL.SetTranslation(&OnGlobal->t0);
126 
127  // SSD
128  St_Survey *SsdSectorsOnGlobal = (St_Survey *) set->Find("SsdSectorsOnGlobal");
129  if (! SsdSectorsOnGlobal) {cout << "SsdSectorsOnGlobal has not been found" << endl; return;}
130  St_Survey *SsdLaddersOnSectors = (St_Survey *) set->Find("SsdLaddersOnSectors");// ladders in the SSD sector coordinate systems
131  if (! SsdLaddersOnSectors) {cout << "SsdLaddersOnSectors has not been found" << endl; return;}
132  St_Survey *SsdWafersOnLadders = (St_Survey *) set->Find("SsdWafersOnLadders"); // wafers in the SSD ladder coordinate systems
133  if (! SsdWafersOnLadders) {cout << "SsdWafersOnLadders has not been found" << endl; return;}
134  Survey_st *SectorsOnGlobal = SsdSectorsOnGlobal->GetTable(); // sectors in the SSD barrel coordinate system
135  Survey_st *LaddersOnSectors = SsdLaddersOnSectors->GetTable();// ladders in the SSD sector coordinate systems
136  Survey_st *WafersOnLadders = SsdWafersOnLadders->GetTable(); // wafers in the SSD ladder coordinate systems
137  Int_t NoSectors = SsdSectorsOnGlobal->GetNRows();
138  Int_t NoLadders = SsdLaddersOnSectors->GetNRows();
139  Int_t NoWafers = SsdWafersOnLadders->GetNRows();
140  Int_t num = 0;
141  for (Int_t i = 0; i < NoWafers; i++,WafersOnLadders++) {
142  Int_t Id = WafersOnLadders->Id;
143  TGeoHMatrix *comb = (TGeoHMatrix *) rotMHash->FindObject(Form("R%04i",Id));
144  if (comb) continue;
145  comb = new TGeoHMatrix(Form("R%04i",Id));
146  Int_t layer = Id/1000;
147  if (layer > 7) layer = 7;
148  Int_t ladder = Id%100;
149  TGeoHMatrix *WL = (TGeoHMatrix *) rotMHash->FindObject(Form("WL%04i",Id));
150  if (! WL) {
151  WL = new TGeoHMatrix(Form("WL%04i",Id));
152  WL->SetRotation(&WafersOnLadders->r00);
153  WL->SetTranslation(&WafersOnLadders->t0); //cout << "WL\t"; WL.Print();
154  rotMHash->Add(WL);
155  }
156  LaddersOnSectors = SsdLaddersOnSectors->GetTable();
157  Int_t Ladder = 0;
158  Int_t Sector = 0;
159  for (Int_t l = 0; l < NoLadders; l++, LaddersOnSectors++) {
160  //cout << "LaddersOnSectors Id\t" << LaddersOnSectors->Id << endl;
161  Ladder = LaddersOnSectors->Id%100;
162  if (Ladder == ladder) {
163  Sector = LaddersOnSectors->Id/100;
164  LS.SetRotation(&LaddersOnSectors->r00);
165  LS.SetTranslation(&LaddersOnSectors->t0);
166  //cout << "LS\t"; LS.Print();
167  break;
168  }
169  }
170  if (Sector <= 0 || Sector > 4) {cout << "Sector has not been defined" << endl; continue;}
171  SectorsOnGlobal = SsdSectorsOnGlobal->GetTable();
172  Int_t sector = 0;
173  for (Int_t s = 0; s <NoSectors; s++, SectorsOnGlobal++) {
174  //cout << "SectorsOnGlobal Id\t" << SectorsOnGlobal->Id << endl;
175  if (SectorsOnGlobal->Id != Sector) continue;
176  sector = Sector;
177  SG.SetRotation(&SectorsOnGlobal->r00);
178  SG.SetTranslation(&SectorsOnGlobal->t0); //cout << "Sector\t" << Sector << "\tSG\t"; SG.Print();
179  break;
180  }
181  if (! sector) {cout << "Sector\t" << Sector << " has not been found" << endl; continue;}
182  // WG = SG * LS * WL * LA; //cout << "WG\t"; WG.Print();
183  // WG = SG * LS * WL * LA = SG * ( LS * WL * LA * WL**-1 ) *WL
184  WG = GL * SG * LS * (*WL); //cout << "WG\t"; WG.Print();
185  ssdWafersPosition_st row;
186  row.id = Id;
187  row.id_shape = 2;
188  row.ladder = ladder;
189  row.layer = layer;
190  num++;
191  row.num_chip = (num-1)%16 + 1;
192  // TGeoHMatrix WGInv = WG.Inverse();
193  // Double_t *wgrot = WGInv.GetRotationMatrix();
194  Double_t *r = WG.GetRotationMatrix();
195  row.driftDirection[0] = r[0]; row.normalDirection[0] = r[1]; row.transverseDirection[0] = r[2];
196  row.driftDirection[1] = r[3]; row.normalDirection[1] = r[4]; row.transverseDirection[1] = r[5];
197  row.driftDirection[2] = r[6]; row.normalDirection[2] = r[7]; row.transverseDirection[2] = r[8];
198  Double_t norm;
199  TVector3 d(row.driftDirection); norm = 1/d.Mag(); d *= norm;
200  TVector3 t(row.transverseDirection); norm = 1/t.Mag(); t *= norm;
201  TVector3 n(row.normalDirection);
202  TVector3 c = d.Cross(t);
203  if (c.Dot(n) < 0) c *= -1;
204  d.GetXYZ(row.driftDirection);
205  t.GetXYZ(row.transverseDirection);
206  c.GetXYZ(row.normalDirection);
207 
208  Double_t *wgtr = WG.GetTranslation();
209  // memcpy(row.driftDirection,wgrot, 9*sizeof(Double_t));
210  memcpy(row.centerPosition,wgtr, 3*sizeof(Double_t));
211  Double_t rot[9] = {
212  row.driftDirection[0], row.transverseDirection[0], row.normalDirection[0],
213  row.driftDirection[1], row.transverseDirection[1], row.normalDirection[1],
214  row.driftDirection[2], row.transverseDirection[2], row.normalDirection[2]};
215  Double_t tr[3] = {row.centerPosition[0],
216  row.centerPosition[1],
217  row.centerPosition[2]};
218  comb->SetRotation(rot);
219  comb->SetTranslation(tr);
220  rotMHash->Add(comb);
221  }
222  // SVT
223  set = GetDataBase("Geometry/svt");
224  St_Survey *WaferOnLadder = (St_Survey *) set->Find("WaferOnLadder");
225  St_Survey *LadderOnSurvey = (St_Survey *) set->Find("LadderOnSurvey");
226  St_Survey *LadderOnShell = (St_Survey *) set->Find("LadderOnShell");
227  St_Survey *ShellOnGlobal = (St_Survey *) set->Find("ShellOnGlobal");
228  Int_t NW = WaferOnLadder->GetNRows();
229  Int_t NL = LadderOnSurvey->GetNRows();
230  Survey_st *waferOnLadder = WaferOnLadder->GetTable();
231  Survey_st *shellOnGlobal0 = ShellOnGlobal->GetTable(0);
232  Survey_st *shellOnGlobal1 = ShellOnGlobal->GetTable(1);
233  TGeoHMatrix LSU, LSH, SHG[2];
234  SHG[0].SetRotation(&shellOnGlobal0->r00);
235  SHG[0].SetTranslation(&shellOnGlobal0->t0);
236  SHG[1].SetRotation(&shellOnGlobal1->r00);
237  SHG[1].SetTranslation(&shellOnGlobal1->t0);
238 
239  for (Int_t i = 0; i < NW; i++, waferOnLadder++) {
240  Int_t id = waferOnLadder->Id;
241  Int_t wbarrel = id/1000;
242  Int_t wwafer = (id - 1000*wbarrel)/100;
243  Int_t wladder = id%100;
244  Int_t wlayer = 2*wbarrel + wladder%2 - 1;
245  // Id = 1000* layer + 100* wafer + ladder;
246  Int_t Id = 1000*wlayer + 100*wwafer + wladder;
247  TGeoHMatrix *comb = (TGeoHMatrix *) rotMHash->FindObject(Form("R%04i",Id));
248  if (comb) continue;
249  comb = new TGeoHMatrix(Form("R%04i",Id));
250  Int_t Found = 0;
251  Survey_st *ladderOnSurvey = LadderOnSurvey->GetTable();
252  Survey_st *ladderOnShell = LadderOnShell->GetTable();
253  for ( Int_t j = 0; j < NL; j++, ladderOnSurvey++, ladderOnShell++) {
254  Int_t Idl = ladderOnSurvey->Id;
255  Int_t lladder = Idl%100;
256  if( wladder != lladder ) continue;
257  LSH.SetRotation(&ladderOnShell->r00);
258  LSH.SetTranslation(&ladderOnShell->t0);
259  Int_t Shell = 1;
260  if( (wbarrel == 1 && wladder <= 4) || (wbarrel == 2 && wladder <= 6) || (wbarrel == 3 && wladder <= 8) ) Shell = 0;
261  // shellOnGlobal * ladderOnShell * ladderOnSurvey * waferOnLadder
262  TGeoHMatrix *WL = (TGeoHMatrix *) rotMHash->FindObject(Form("WL%04i",Id));
263  if (! WL) {
264  TGeoHMatrix wL;
265  wL.SetRotation(&waferOnLadder->r00);
266  wL.SetTranslation(&waferOnLadder->t0);
267  WL = new TGeoHMatrix();
268  LSU.SetRotation(&ladderOnSurvey->r00);
269  LSU.SetTranslation(&ladderOnSurvey->t0);
270  *WL = LSU * wL;
271  WL->SetName(Form("WL%04i",Id));
272  rotMHash->Add(WL);
273  }
274  // WG = GL * SHG * LSH * LSU * (*WL); // WG.Print();
275  WG = GL * SHG[Shell] * LSH * (*WL); // WG.Print();
276  Double_t *r = WG.GetRotationMatrix();
277  Int_t fail = 0;
278  for (int l = 0; l < 9; l++) {
279  if (TMath::Abs(r[l]) >= 1.000001) fail++;
280  }
281  if (fail) {
282  cout << "===============" << waferOnLadder->Id << " "<< id << " " << Id <<endl;
283  cout << "WG\t"; WG.Print();
284  }
285  svtWafersPosition_st row;
286  row.driftDirection[0] = r[0]; row.normalDirection[0] = r[1]; row.transverseDirection[0] = r[2];
287  row.driftDirection[1] = r[3]; row.normalDirection[1] = r[4]; row.transverseDirection[1] = r[5];
288  row.driftDirection[2] = r[6]; row.normalDirection[2] = r[7]; row.transverseDirection[2] = r[8];
289  Double_t norm;
290  TVector3 d(row.driftDirection); norm = 1/d.Mag(); d *= norm;
291  TVector3 t(row.transverseDirection); norm = 1/t.Mag(); t *= norm;
292  TVector3 n(row.normalDirection);
293  TVector3 c = d.Cross(t);
294  if (c.Dot(n) < 0) c *= -1;
295  d.GetXYZ(row.driftDirection);
296  t.GetXYZ(row.transverseDirection);
297  c.GetXYZ(row.normalDirection);
298 
299  row.ID = 100*wwafer + wladder + 1000*wlayer;
300  Double_t *wgtr = WG.GetTranslation();
301  memcpy(row.centerPosition,wgtr, 3*sizeof(Double_t));
302  Double_t rot[9] = {
303  row.driftDirection[0], row.transverseDirection[0], row.normalDirection[0],
304  row.driftDirection[1], row.transverseDirection[1], row.normalDirection[1],
305  row.driftDirection[2], row.transverseDirection[2], row.normalDirection[2]};
306  Double_t tr[3] = {row.centerPosition[0],
307  row.centerPosition[1],
308  row.centerPosition[2]};
309  comb->SetRotation(rot);
310  comb->SetTranslation(tr);
311  rotMHash->Add(comb);
312  Found++;
313  break;
314  }
315  assert(Found);
316  }
317 #endif
318  TIter next(rotMHash);
319  TGeoHMatrix *comb;
320  Int_t fail = 0;
321  while ((comb = (TGeoHMatrix *) next())) {
322  TString Name(comb->GetName());
323  if (Name.BeginsWith("R")) {
324  TGeoHMatrix *WL = (TGeoHMatrix *) rotMHash->FindObject(Form("WL%s",Name.Data()+1));
325  if (! WL) {
326  cout << Form("WL%s",Name.Data()+1) << " has not been found" << endl;
327  fail++;
328  }
329  }
330  }
331  assert(! fail);
332 #if 0
333  if (fFile) {
334  TDirectory *g = 0;
335  if (gDirectory != fFile) {
336  g = gDirectory;
337  fFile->cd();
338  }
339  rotMHash->Write();
340  if (g) g->cd();
341  }
342 #endif
343 }
virtual Int_t Make()
Definition: EventT.h:32
virtual Int_t Finish()
Definition: Stypes.h:40
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362