FairRoot/PandaRoot
materialana.C
Go to the documentation of this file.
1 // root macro to analyze the simulation output
2 #include <map>
3 #include <string>
4 #include <iostream>
5 #include <vector>
6 
7 // #include <TStopwatch.h>
8 #include <TFile.h>
9 #include <TTree.h>
10 #include <TGeoManager.h>
11 #include <TClonesArray.h>
12 #include <TH1D.h>
13 #include <TROOT.h> // for global root variables
14 #include <TH2D.h>
15 #include <TVector3.h>
16 #include <TPad.h>
17 #include <TCanvas.h>
18 #include <TStopwatch.h>
19 #include <THStack.h>
20 #include <TProfile.h>
21 #include <TProfile2D.h>
22 #include <TColor.h>
23 
24 // called from pandaroot/macro/mvd/Ralf
25 //how to do this better?
26 #include "$VMCWORKDIR/base/FairRadLenPoint.h"
27 #include "$VMCWORKDIR/pnddata/PndMCTrack.h"
28 // #include "$VMCWORKDIR/mvd/MvdTools/PndGeoHandling.h"
29 #include "$VMCWORKDIR/macro/mvd/Tools.C"
30 
31 int materialana(int nEvents = 10, bool verbose = false)
32 {
33 // int nEvents = 100;
34 // bool verbose = false;
35  bool draw1 = true;
36  bool draw2 = true;
37  bool draw3 = true;
38 
39  // ----- Load libraries ------------------------------------------------
40 // gROOT->Macro("../Libs.C");
41 // gROOT->LoadMacro("../Tools.C");
43 
44  // ----- Timer --------------------------------------------------------
45  TStopwatch timer;
46  timer.Start();
47  // ------------------------------------------------------------------------
48 // std::string inFile = "../data/mvdmaterial.root";
49  std::string inFile = "../data/mvdTestGeo.root";
50 
51  TFile* f = new TFile(inFile.c_str()); // the sim file you want to analyse
52  TTree *t=(TTree *) f->Get("pndsim") ;
53 
54  TClonesArray* mc_array=new TClonesArray("PndMCTrack");
55  t->SetBranchAddress("MCTrack",&mc_array);//Branch names
56 
57  TClonesArray* rad_array=new TClonesArray("FairRadLenPoint");
58  t->SetBranchAddress("RadLen",&rad_array);
59 
60  TGeoManager *geoMan = (TGeoManager*) gDirectory->Get("FAIRGeom");
61 // PndGeoHandling* fGeoH = new PndGeoHandling();
62 
63  // histos
64  int res = 200; int angres = 180;
65  TH1D* hRadLenDistMat = new TH1D("radldm","Material thicknesses",100,0,15);
66  TH1D* hRadLenDistEff = new TH1D("radlde","Effective Radiation length values per volume",100,0,0.1);
67  TH2D* hisxy = new TH2D("hisxy","MC Points, xy view",res,-15.,15.,res,-15.,15.);
68  TH2D* hisrz = new TH2D("hisrz","MC Points, rz view",res,-20.,20.,res,-15.,25.);
69 
70  TClonesArray* arrhisxy = new TClonesArray("TH2D");
71  TClonesArray* arrhisrz = new TClonesArray("TH2D");
72  TClonesArray* arrprothe = new TClonesArray("TProfile");
73  TClonesArray* arrprophi = new TClonesArray("TProfile");
74 
75  std::vector<std::string> namesList;
76 // namesList.push_back("StripSensor");
77 // namesList.push_back("SensorActive");
78  namesList.push_back("pipe");
79  namesList.push_back("cave");//Reste, ausser "/cave"
80  namesList.push_back("Sensor");
81 // namesList.push_back("Support");
82 // namesList.push_back("MVD");
83 
84  std::map<int,double> radlList;
85  int maxnames=namesList.size();
86  for(int i=0;i<maxnames;i++){
87  TString name = "hisxy-";
88  TString name2 = "hisrz-";
89  TString name3 = "hthe-";
90  TString name4 = "hphi-";
91  name += i; name2 += i; name3 += i; name4 += i;
92  new ((*arrhisxy)[i]) TH2D(name.Data(),(namesList[i]).c_str(),2*res,-15.,15.,2*res,-15.,15.);
93  new ((*arrhisrz)[i]) TH2D(name2.Data(),(namesList[i]).c_str(),2*res,-15.,15.,2*res,-15.,15.);
94  new ((*arrprothe)[i]) TProfile(name3.Data(),(namesList[i]).c_str(),angres,0.,TMath::Pi());
95  new ((*arrprophi)[i]) TProfile(name4.Data(),(namesList[i]).c_str(),angres,-1.*TMath::Pi(),TMath::Pi());
96  radlList[i]=0.;
97  }
98  new ((*arrhisxy)[maxnames]) TH2D("hisxy-rest","Support",2*res,-15.,15.,2*res,-15.,15.);
99  new ((*arrhisrz)[maxnames]) TH2D("hisrz-rest","Support",2*res,-15.,15.,2*res,-15.,15.);
100  new ((*arrprothe)[maxnames]) TProfile("hthe-rest","Support",angres,0.,TMath::Pi());
101  new ((*arrprophi)[maxnames]) TProfile("hphi-rest","Support",angres,-1.*TMath::Pi(),TMath::Pi());
102  radlList[maxnames]=0.;
103 
104  TProfile* thetaprofile = new TProfile("thetaprof","Radiaion length (#theta)",angres,0.,TMath::Pi());
105  TProfile* phiprofile = new TProfile("phiprof","Radiaion length (#phi)",angres,-1.*TMath::Pi(),TMath::Pi());
106 
107 // TH2D* histhephi = new TH2D("hThePhi","",100,0.2,TMath::Pi()-0.3,100,-1.*TMath::Pi(),TMath::Pi());
108  TProfile2D* histhephi = new TProfile2D("hThePhi","",90,0.2,2.8,90,-1.*TMath::Pi(),TMath::Pi());
109  histhephi->SetTitle("Radiation length map #theta & #phi;#theta;#phi");
110 
111  TH1D* hisx = new TH1D("hX","X",100,-15.,15.);
112  TH1D* hisy = new TH1D("hY","Y",100,-15.,15.);
113  TH1D* hisz = new TH1D("hZ","Z",100,-15.,15.);
114 
115  TString detname,volname;
116  double radlen=0.,effradl=0.,effradlsum=0.,theta=0.,phi=0.;
117  TVector3 in, out, dist, point;
118  TVector3 vtx, mom;
119 
120  for (Int_t event=0; event<nEvents && event<t->GetEntriesFast(); event++)
121  {
122  t->GetEntry(event);
123  if(verbose) cout<<"Event No "<<event<<endl;
124  else if (!(event%1000)) cout <<"Event No "<<event<<endl;
125 
126  for (Int_t trackno=0; trackno<mc_array->GetEntriesFast();trackno++){
127  PndMCTrack* aTrack = (PndMCTrack*)mc_array->At(trackno);
128  mom = aTrack->GetMomentum();
129  theta = mom.Theta(); phi = mom.Phi();
130 // if(theta < 0.05 || theta > (TMath::Pi()-0.05)) continue; // cut strange angles
131 // vtx = aTrack->GetStartVertex();
132  effradl=0.; effradlsum=0.;
133  for(uint i=0;i<radlList.size();i++) radlList[i]=0.;
134  for (Int_t k=0; k<rad_array->GetEntriesFast(); k++){
135  FairRadLenPoint* radpoint = (FairRadLenPoint*)rad_array->At(k);
136  if (radpoint->GetTrackID() != trackno) continue;
137  radlen = radpoint->GetRadLength();
138  in = radpoint->GetPosition();
139  out = radpoint->GetPositionOut();
140  dist = in - out;
141  point.SetXYZ(0.5*(in.x()+out.x()),0.5*(in.y()+out.y()),0.5*(in.z()+out.z()));
142  if(in.Mag() < 0.02) continue; // cut target
143  effradl = dist.Mag()/radlen;
144  effradlsum += effradl;
145  TGeoNode* node = geoMan->FindNode(point.x(),point.y(),point.z());
146  if( 0==node)
147  {
148  std::cout<<"Warning: There is a node not defined properly!"<<std::endl;
149  cout<<"\tEvent No "<<event<<" \t RadLenPoint No."<< k<<endl;
150  continue;
151  }
152  node->cd();
153 // detname = geoMan->GetPath();
154  volname = node->GetVolume()->GetName();
155 // if(verbose)cout<<detname.Data()<<endl;
156  if("cave"==volname && point.Mag() > 20.) continue; // cut mainly the outer cave
157  if(verbose){
158  cout<<"--> "<<volname.Data()<<endl;
159  }
160 
161  hRadLenDistMat->Fill(dist.Mag());
162  hRadLenDistEff->Fill(effradl);
163 
164  hisx->Fill(in.X(),effradl);
165  hisy->Fill(in.Y(),effradl);
166  hisz->Fill(in.Z(),effradl);
167 
168  hisxy->Fill(in.X(),in.Y());
169  if(in.Y()>0.) hisrz->Fill(in.Z(),in.Perp());
170  else hisrz->Fill(in.Z(),-1.*in.Perp());
171 
172  int selected = maxnames;
173  for(uint i=0;i<namesList.size();i++){
174  if (volname.Contains( (namesList[i]).c_str())){
175  selected=i;
176  break;
177  }
178  }
179  radlList[selected]+=effradl;
180  ((TH2D*)arrhisxy->At(selected))->Fill(point.X(),point.Y(),effradl);
181  if(point.Y()>0.) ((TH2D*)arrhisrz->At(selected))->Fill(point.Z(),point.Perp(),effradl);
182  else ((TH2D*)(arrhisrz->At(selected)))->Fill(point.Z(),-1.*point.Perp(),effradl);
183  }//radpoints
184  thetaprofile->Fill(theta,effradlsum);
185  phiprofile->Fill(phi,effradlsum);
186  histhephi->Fill(theta,phi,effradlsum);
187 
188  for(int i=0;i<=maxnames;i++){
189  effradl=radlList[i];
190  ((TProfile*)arrprothe->At(i))->Fill(theta,effradl);
191  ((TProfile*)arrprophi->At(i))->Fill(phi,effradl);
192  }
193 
194  }//tracks
195 
196  }// end for event
197 
198 
199 
200 Int_t a=3,b=3;
201 int resol = 250;
202 if(draw1){
203 TCanvas* can1 = new TCanvas("can1","MCHit view in MVD",0,0,a*resol,b*resol);
204 can1->Divide(a,b);
205 
206 can1->cd(1);
207 hisxy->SetStats(false);
208 hisxy->DrawCopy("colz");
209 
210 can1->cd(2);
211 hisrz->SetStats(false);
212 hisrz->DrawCopy("colz");
213 
214 can1->cd(3);
215 TPad* mypad = (TPad*) gPad;
216 mypad->Divide(2,2);
217 mypad->cd(1);
218 hisx->SetFillColor(38);
219 hisx->DrawCopy();
220 mypad->cd(2);
221 hisy->SetFillColor(45);
222 hisy->DrawCopy();
223 mypad->cd(3);
224 hisz->SetFillColor(30);
225 hisz->DrawCopy();
226 mypad->cd(4);
227 // THStack* hs = new THStack("hs","test stacked histograms");
228 // //makes no sens - just test technics
229 // hs->Add(hisx);
230 // hs->Add(hisy);
231 // hs->Add(hisz);
232 // hs->Draw();
233 // // TLegend* legend = BuildLegend_THStack( hs, 0.8, 0.55, 0.98, 0.98);
234 // TLegend* legend = new TLegend(0.8, 0.55, 0.98, 0.98);
235 // legend->AddEntry(hisx,"","F");
236 // legend->AddEntry(hisy,"","F");
237 // legend->AddEntry(hisz,"","F");
238 // legend->Draw();
239 
240 can1->cd(4);
241 gPad->SetLogy();
242 hRadLenDistMat->SetFillColor(38);
243 hRadLenDistMat->DrawCopy();
244 
245 can1->cd(5);
246 gPad->SetLogy();
247 hRadLenDistEff->SetFillColor(38);
248 hRadLenDistEff->DrawCopy();
249 
250 can1->cd(6);
251 
252 can1->cd(7);
253 gPad->SetLogy();
254 thetaprofile->SetFillColor(30);
255 thetaprofile->SetLineColor(30);
256 thetaprofile->Draw("hist");
257 
258 can1->cd(8);
259 gPad->SetLogy();
260 phiprofile->SetFillColor(45);
261 phiprofile->SetLineColor(45);
262 phiprofile->Draw("hist");
263 
264 can1->cd(9);
265 histhephi->SetStats(false);
266 histhephi->DrawCopy("colz");
267 
268 }
269 
270 
271 if(draw2){
272 resol = 250;
273 a=2,b=1;
274 TCanvas* can2 = new TCanvas("can2","MCHit view in MVD",50,50,a*2*resol,b*2*resol);
275 can2->Divide(a,b);
276 
277 resol = 300;
278 a=2,b=2;
279 TCanvas* can4 = new TCanvas("can4","MCHit view in MVD",100,100,a*resol,b*resol);
280 can4->Divide(a,b);
281 
282 /*EColor colors[12] =
283  {kOrange,kAzure,kTeal ,kRed,kBlue,kGreen, kMagenta,kCyan,kYellow ,kPink,kViolet,kSpring };*/
284 int colors[4] = {38,17,45,30};
285 int coloff = -0;
286 THStack* thetastack = new THStack("thetastack","");
287 thetastack->SetTitle("Stacked Radiation lengths #theta;#theta;X/X_{0} ");
288 THStack* phistack = new THStack("phistack","");
289 phistack->SetTitle("Stacked Radiation lengths #phi;#phi;X/X_{0} ");
290 TProfile* aprof=0;
291 TH1D* ahist=0;
292 TLegend* legMat=new TLegend(0.6, 0.75, 0.8, 0.98);
293 
294 for(int i=maxnames;i>=0;i--){
295  aprof=(TProfile*)arrprothe->At(i);
296  ahist=aprof->ProjectionX();
297  ahist->SetFillColor(colors[i] + coloff);
298  ahist->SetLineColor(colors[i] + coloff);
299  legMat->AddEntry(ahist,"","F");
300  can4->cd(i+1);
301  gPad->SetLogy();
302  ahist->DrawCopy("hist");
303  thetastack->Add(ahist);
304 
305  aprof=(TProfile*)arrprophi->At(i);
306  ahist=aprof->ProjectionX();
307  ahist->SetFillColor(colors[i] + coloff);
308  ahist->SetLineColor(colors[i] + coloff);
309  phistack->Add(ahist);
310 }
311 can2->cd(1);
312 thetastack->SetMaximum(0.1);
313 thetastack->Draw("hist");
314 legMat->Draw();
315 
316 can2->cd(2);
317 phistack->SetMaximum(0.1);
318 phistack->Draw("hist");
319 legMat->Draw();
320 
321 
322 
323 }
324 
325 if(draw3){
326 
327 a=maxnames+1;
328 b=2;
329 if(a>4) resol = int(1200/a);
330 TCanvas* can3 = new TCanvas("can3","MCHit view in MVD",150,150,a*resol,b*resol);
331 can3->Divide(a,b);
332 
333 for(int i=0;i<a;i++){
334  can3->cd(i+1);
335  ((TH2D*)arrhisxy->At(i))->DrawCopy("colz");
336  can3->cd(i+a+1);
337  ((TH2D*)arrhisrz->At(i))->DrawCopy("colz");
338 }
339 }
340 
341 // can1->Print("outAnaMvdSim.ps");
342  // ----- Finish -------------------------------------------------------
343  timer.Stop();
344  Double_t rtime = timer.RealTime();
345  Double_t ctime = timer.CpuTime();
346  cout << endl << endl;
347  cout << "Macro finished succesfully." << endl;
348  //cout << "Output file is " << outFile << endl;
349  //cout << "Parameter file is " << parFile << endl;
350  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
351  cout << endl;
352  // ------------------------------------------------------------------------
353 
354  return 0;
355 }
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
TTree * b
#define verbose
TGeoManager * geoMan
TVector3 GetMomentum() const
Definition: PndMCTrack.h:78
TClonesArray * mc_array
Definition: anaLmdCluster.C:25
Double_t mom
Definition: plot_dirc.C:14
TCanvas * can4
Definition: anaLmdReco.C:203
TString inFile
Definition: hit_dirc.C:8
int materialana(int nEvents=10, bool verbose=false)
Definition: materialana.C:31
Int_t a
Definition: anaLmdDigi.C:126
Double_t
Int_t nEvents
Definition: hit_dirc.C:11
TStopwatch timer
Definition: hit_dirc.C:51
TClonesArray * point
Definition: anaLmdDigi.C:29
TFile * f
Definition: bump_analys.C:12
TString detname
Definition: anasim.C:61
TPad * mypad
TFile * out
Definition: reco_muo.C:20
TH2D * hisxy
Definition: anaLmdDigi.C:38
TString name
Double_t ctime
Definition: hit_dirc.C:114
TH2D * hisrz
Definition: anaLmdDigi.C:41
HISThit_ene Fill(sum_hit_ene)
TCanvas * can2
TTree * t
Definition: bump_analys.C:13
TCanvas * can1
Double_t rtime
Definition: hit_dirc.C:113
TCanvas * can3
Double_t Pi
LoadPandaStyle()
mcTheta DrawCopy()