]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/macro/hits.C
commented parameters setting commented
[u/mrichter/AliRoot.git] / AliGeant4 / macro / hits.C
1 // $Id$
2
3 #include <iostream.h>
4
5 void hits()
6 {
7   hits("FMD","1");
8   hits("PHOS","1");
9   hits("START","1");
10   hits("TOF","2");
11   hits("TPC","2");
12
13   //hits("ITS","5");
14   //hits("MUON","0");
15   //hits("PMD","1");
16   //hits("RICH","1");
17 }  
18
19 void hits(const TString& detName, const TString& detVersion)
20 {
21   cout << detName << endl;
22
23   // labels
24   TString g3 = "G3: ";
25   TString g4 = "G4: ";
26
27   // construct file names
28   TString top = getenv("ALICE_ROOT");
29   TString f1NameEnd = "test10.root";
30   TString f2NameEnd = "test20.root";
31
32   TString g3File1Name = top + "/test/" + f1NameEnd;
33   TString g3File2Name = top + "/test/" + f2NameEnd;
34   TString g4File1Name = top + "/AliGeant4/test/" + f1NameEnd;
35   TString g4File2Name = top + "/AliGeant4/test/" + f2NameEnd;
36   
37   cout << "Files: " << endl;
38   cout << g3File1Name << endl;
39   cout << g3File2Name << endl;
40   cout << g4File1Name << endl;
41   cout << g4File2Name << endl;
42
43   // link shared libs
44   if (gClassTable->GetID("AliRun") < 0) {
45     gROOT->LoadMacro("loadlibs.C");
46     loadlibs();
47   }
48   cout << "AliRoot libraries were loaded." << endl;
49       
50   // set histogram ranges
51   Int_t nbin;
52   Int_t g3xmin; Int_t g4xmin; Int_t g3xmax; Int_t g4xmax;
53   Int_t g3ymin; Int_t g4ymin; Int_t g3ymax; Int_t g4ymax;
54   Int_t g3zmin; Int_t g4zmin; Int_t g3zmax; Int_t g4zmax;
55   SetHistogramRanges(detName, nbin, 
56                      g3xmin, g3xmax, g4xmin, g4xmax,
57                      g3ymin, g3ymax, g4ymin, g4ymax,
58                      g3zmin, g3zmax, g4zmin, g4zmax);
59
60
61   // create histograms
62   TH1F* g3x  = new TH1F("g3x",  g3 + detName + " hits per x", nbin, g3xmin, g3xmax);
63   TH1F* g3xh = new TH1F("g3xh", g3 + detName + " hits per x", nbin, g3xmin, g3xmax);
64   TH1F* g4x  = new TH1F("g4x",  g4 + detName + " hits per x", nbin, g4xmin, g4xmax);
65   TH1F* g4xh = new TH1F("g4xh", g4 + detName + " hits per x", nbin, g4xmin, g4xmax);
66
67   TH1F* g3z  = new TH1F("g3z",  g3 + detName + " hits per z", nbin, g3zmin, g3zmax);
68   TH1F* g3zh = new TH1F("g3zh", g3 + detName + " hits per z", nbin, g3zmin, g3zmax);
69   TH1F* g4z  = new TH1F("g4z",  g4 + detName + " hits per z", nbin, g4zmin, g4zmax);
70   TH1F* g4zh = new TH1F("g4zh", g4 + detName + " hits per z", nbin, g4zmin, g4zmax);
71
72   TH1F* g3y  = 0;
73   TH1F* g3yh = 0;
74   TH1F* g4y  = 0;
75   TH1F* g4yh = 0;
76   if (detName == "PHOS") {
77     TH1F* g3y  = new TH1F("g3y",  g3 + detName + " hits per y", nbin, g3ymin, g3ymax);
78     TH1F* g3yh = new TH1F("g3yh", g3 + detName + " hits per y", nbin, g3ymin, g3ymax);
79     TH1F* g4y  = new TH1F("g4y",  g4 + detName + " hits per y", nbin, g4ymin, g4ymax);
80     TH1F* g4yh = new TH1F("g4yh", g4 + detName + " hits per y", nbin, g4ymin, g4ymax);
81   }
82
83   cout << "Histograms were created." << endl;
84
85   // fill histograms
86   AliDetector* detector;
87
88   //detector = LoadDetector(g3File1Name, detName, g3);
89   //FillHistogram(detector, g3, g3x, g3y, g3z); 
90
91   detector = LoadDetector(g3File2Name, detName,  g3);
92   FillHistogram(detector, g3, g3xh, g3yh, g3zh); 
93   //TH1F* g3xh = LoadHistograms("g3xh"); 
94   //TH1F* g3zh = LoadHistograms("g3zh"); 
95
96   detector = LoadDetector(g4File1Name, detName, g4);
97   FillHistogram(detector, g4, g4x, g4y, g4z); 
98
99   detector = LoadDetector(g4File2Name, detName, g4);
100   FillHistogram(detector, g4, g4xh, g4yh, g4zh); 
101
102   // compose picture name
103   TString gifNameBase =  "hits" + detName + "v" + detVersion;
104   TString title = detName + " hits";
105
106   // draw histohrams
107   DrawHistograms(title, gifNameBase + ".gif", g3xh, g4xh, g3zh, g4zh);
108   if (detName == "PHOS") 
109     DrawHistograms(title, gifNameBase + "2.gif", g3yh, g4yh, g3zh, g4zh);
110
111   //DrawHistograms(title, gifNameBase + "_x.gif", g3x, g4x, g3xh, g4xh);
112   //DrawHistograms(title, gifNameBase + "_y.gif", g3y, g4y, g3yh, g4yh);
113   //DrawHistograms(title, gifNameBase + "_z.gif", g3z, g4z, g3zh, g4zh);
114   
115 }
116
117 void SetHistogramRanges(TString& detName, Int_t& nbin, 
118                         Int_t& g3xmin, Int_t& g3xmax,
119                         Int_t& g4xmin, Int_t& g4xmax,
120                         Int_t& g3ymin, Int_t& g3ymax, 
121                         Int_t& g4ymin, Int_t& g4ymax, 
122                         Int_t& g3zmin, Int_t& g3zmax, 
123                         Int_t& g4zmin, Int_t& g4zmax)
124 {                        
125   nbin = 200;
126
127   g3xmin = 0; g4xmin = g3xmin;
128   g3xmax = 1; g4xmax = g3xmax;
129   g3ymin = 0; g4ymin = g3xmin;
130   g3ymax = 1; g4ymax = g3xmax;
131   g3zmin = 0; g4zmin = g3zmin;
132   g3zmax = 1; g4zmax = g3zmax;
133
134   if (detName == "FMD") {
135     g3xmin = -50; g4xmin = g3xmin;
136     g3xmax =  50; g4xmax = g3xmax;
137     g3zmin = -700; g4zmin = g3zmin;
138     g3zmax =  150; g4zmax = g3zmax;
139   }
140   else if (detName == "ITS") {
141     g3xmin = -60; g4xmin = g3xmin;
142     g3xmax =  60; g4xmax = g3xmax;
143     g3zmin = -60; g4zmin = g3zmin;
144     g3zmax =  60; g4zmax = g3zmax;
145   }  
146   else if (detName == "MUON") {
147     g3xmin = -450; g4xmin = g3xmin;
148     g3xmax =  450; g4xmax = g3xmax;
149     g3zmin =  400; g4zmin = g3zmin;
150     g3zmax = 1800; g4zmax = g3zmax;
151   }
152   else if (detName == "PHOS") {
153     g3xmin = -400; g4xmin = g3xmin;
154     g3xmax =  400; g4xmax = g3xmax;
155     g3ymin =  250; g4ymin = g3ymin;
156     g3ymax =  500; g4ymax = g3ymax;
157     g3zmin =  -80; g4zmin = g3zmin;
158     g3zmax =   80; g4zmax = g3zmax;
159   }
160   else if (detName == "PMD") {
161     g3xmin = -200; g4xmin = g3xmin*10;
162     g3xmax =  200; g4xmax = g3xmax*10;
163     g3zmin = -582; g4zmin = g3zmin*10;
164     g3zmax = -578; g4zmax = g3zmax*10;
165   }
166   else if (detName == "RICH") {
167     g3xmin = -250; g4xmin = g3xmin;
168     g3xmax =  250; g4xmax = g3xmax;
169     g3zmin = -250; g4zmin = g3zmin;
170     g3zmax =  250; g4zmax = g3zmax;
171   }
172   else if (detName == "START") {
173     g3xmin = -10; g4xmin = g3xmin;
174     g3xmax =  10; g4xmax = g3xmax;
175     g3zmin = -80; g4zmin = g3zmin;
176     g3zmax =  80; g4zmax = g3zmax;
177   }
178   else if (detName == "TOF") {
179     g3xmin = -400; g4xmin = g3xmin;
180     g3xmax =  400; g4xmax = g3xmax;
181     g3zmin = -400; g4zmin = g3zmin;
182     g3zmax =  400; g4zmax = g3zmax;
183   }
184   else if (detName == "TPC") {
185     g3xmin = -300; g4xmin = g3xmin;
186     g3xmax =  300; g4xmax = g3xmax;
187     g3zmin = -300; g4zmin = g3zmin;
188     g3zmax =  300; g4zmax = g3zmax;
189   }
190 }  
191
192 AliDetector* LoadDetector(TString& fileName, TString& detName, TString& label)
193
194   // connect the Root files
195   TFile* file = new TFile(fileName);
196   if (!file) {
197     cerr << "Root file was not found." << endl;
198     return 0;
199   }
200
201   // get AliRun object from file 
202   if (gAlice) delete gAlice;
203   gAlice = 0;
204   gAlice = (AliRun*)file->Get("gAlice");
205   if (!gAlice) {
206     cerr << label << "AliRun object not found in a file." << endl;
207     return 0;
208   }
209       
210   // import the hits trees 
211   Int_t nparticles = gAlice->GetEvent(0);
212   if (nparticles <= 0) {
213     cerr << label << "No particles in a file." << endl;
214     return 0;
215   }  
216   cout << label << "got nparticles = " << nparticles << endl;
217
218   // get detector
219   AliDetector* detector = gAlice->GetDetector(detName);
220   if (!detector) {
221     cerr << label << "No detector " << detName << " in a file." << endl;
222     return 0;
223   }  
224   
225   return detector;
226 }  
227
228 void FillHistogram(AliDetector* detector, TString& label, 
229                    TH1F* hx, TH1F* hy, TH1F* hz) 
230
231   Int_t nofHits = 0;
232   // get number of primary tracks
233   Int_t ntracks = gAlice->TreeH()->GetEntries();
234   cout << label << "got ntracks = " << ntracks << endl;
235
236   // loop on tracks in the hits container
237   for (Int_t i=0; i<ntracks; i++) {
238     // loop on hits  
239     for(AliHit* hit= detector->FirstHit(i); hit; hit=detector->NextHit()) {
240
241       TString detName = detector->GetName();
242       if (detName == "PHOS") { 
243         // PHOS special
244         FillPHOSHit(detector, hit, hx, hy, hz);
245       } 
246       else {
247         FillHit(hit, hx, hy, hz);
248       } 
249         
250       nofHits++;
251     }
252   } 
253 /*  
254   TFile* file = new TFile("tmp.root","recreate");
255   hx->Write();
256   hz->Write();
257   file->Close();
258 */
259   cout << label << "filled " << nofHits << " hits" << endl;
260 }  
261
262 TH1F* LoadHistograms(TString name)
263 {
264   TFile* file = new TFile("tmp.root");
265   return (TH1F*)file->Get(name);
266 }  
267
268 void FillHit(AliHit* hit, TH1F* hx, TH1F* hy, TH1F* hz) 
269
270   if (hx) hx->Fill(hit->X());
271   if (hy) hy->Fill(hit->Y());
272   if (hz) hz->Fill(hit->Z());
273 }  
274
275 void FillPHOSHit(AliDetector* detector, AliHit* hit, 
276                  TH1F* hx, TH1F* hy, TH1F* hz) 
277
278   // PHOS special
279   Float_t id = ((AliPHOSHit*)hit)->GetId();
280       
281   TVector3 pos;
282   ((AliPHOS*)detector)->GetGeometry()->RelPosInAlice(id, pos);
283     
284   if (hx) hx->Fill(pos.X());
285   if (hy) hy->Fill(pos.Y());
286   if (hz) hz->Fill(pos.Z());
287 }  
288
289 void DrawHistograms(TString& title, TString& gifName,
290                     TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4)
291 {
292 // Create canvas, set the view range, show histograms.
293 // ---
294
295   if (h1 && h2 && h3 && h4) {
296     // create canvas
297     TCanvas* canvas = new TCanvas("c1", title, 400, 10, 800, 600);
298     canvas->Divide(2,2);
299     
300     // draw histograms
301     canvas->cd(1); h1->Draw();
302     canvas->cd(2); h2->Draw(); 
303     canvas->cd(3); h3->Draw(); 
304     canvas->cd(4); h4->Draw();
305     
306     // save gif
307     //canvas->SaveAs(gifNameBase + "_x.gif"); 
308     canvas->SaveAs(gifName); 
309   }  
310 }