cd88fe7092a1f0f0d05356417249920396c5df5a
[u/mrichter/AliRoot.git] / ITS / SSDrecpointTest.C
1 void SSDrecpointTest (Int_t evNumber1=0,Int_t evNumber2=0)
2   //void SSDrecpointTest (Int_t evNumber1=0,Int_t evNumber2=999)
3 {
4 /////////////////////////////////////////////////////////////////////////
5 //   This macro is a small example of a ROOT macro
6 //   illustrating how to read the output of GALICE
7 //   and fill some histograms.
8 //   
9 //     Root > .L anal.C   //this loads the macro in memory
10 //     Root > anal();     //by default process first event   
11 //     Root > anal(2);    //process third event
12 //Begin_Html
13 /*
14 <img src="gif/anal.gif">
15 */
16 //End_Html
17 /////////////////////////////////////////////////////////////////////////
18     
19 // Dynamically link some shared libs
20
21    if (gClassTable->GetID("AliRun") < 0) {
22       gROOT->LoadMacro("loadlibs.C");
23       loadlibs();
24    }
25
26 // Connect the Root Galice file containing Geometry, Kine and Hits
27    TString *str = new TString("galice.root");
28    TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(str->Data());
29    if (!file) file = new TFile(str->Data(),"UPDATE");
30
31 // Get AliRun object from file or create it if not on file
32    //   if (!gAlice) {
33      gAlice = (AliRun*)file->Get("gAlice");
34      if (gAlice) printf("AliRun object found on file\n");
35      if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
36      //}
37
38
39      // -------------- Create ntuples --------------------
40
41      //  ntuple structures:
42
43
44           struct {
45             Int_t lay;
46             Int_t nxP;
47             Int_t nxN;
48             Int_t hitprim;
49             Int_t partcode;
50             Float_t x;
51             Float_t z;
52             Float_t dx;
53             Float_t dz;
54             Float_t pmod;
55           } ntuple_st;
56
57           struct {
58             Int_t lay;
59             Int_t lad;
60             Int_t det;
61             Int_t nxP;
62             Int_t nxN;
63             Int_t noverlaps;
64             Int_t noverprim;
65             Float_t qclP;
66             Float_t qclN;
67             Float_t qrec;
68             Float_t dx;
69             Float_t dz;
70           } ntuple1_st;
71
72           struct {
73             Int_t nxP;
74             Int_t nxN;
75             Float_t x;
76             Float_t z;
77           } ntuple2_st;
78
79           ntuple = new TTree("ntuple","Demo ntuple");
80           ntuple->Branch("lay",&ntuple_st.lay,"lay/I");
81           ntuple->Branch("nxP",&ntuple_st.nxP,"nxP/I");
82           ntuple->Branch("nxN",&ntuple_st.nxN,"nxN/I");
83           ntuple->Branch("hitprim",&ntuple_st.hitprim,"hitprim/I");
84           ntuple->Branch("partcode",&ntuple_st.partcode,"partcode/I");
85           ntuple->Branch("x",&ntuple_st.x,"x/F");
86           ntuple->Branch("z",&ntuple_st.z,"z/F");
87           ntuple->Branch("dx",&ntuple_st.dx,"dx/F");
88           ntuple->Branch("dz",&ntuple_st.dz,"dz/F");
89           ntuple->Branch("pmod",&ntuple_st.pmod,"pmod/F");
90
91           ntuple1 = new TTree("ntuple1","Demo ntuple1");
92           ntuple1->Branch("lay",&ntuple1_st.lay,"lay/I");
93           ntuple1->Branch("lad",&ntuple1_st.lad,"lad/I");
94           ntuple1->Branch("det",&ntuple1_st.det,"det/I");
95           ntuple1->Branch("nxP",&ntuple1_st.nxP,"nxP/I");
96           ntuple1->Branch("nxN",&ntuple1_st.nxN,"nxN/I");
97           ntuple1->Branch("qclP",&ntuple1_st.qclP,"qclP/F");
98           ntuple1->Branch("qclN",&ntuple1_st.qclN,"qclN/F");
99           ntuple1->Branch("qrec",&ntuple1_st.qrec,"qrec/F");
100           ntuple1->Branch("dx",&ntuple1_st.dx,"dx/F");
101           ntuple1->Branch("dz",&ntuple1_st.dz,"dz/F");
102           ntuple1->Branch("noverlaps",&ntuple1_st.noverlaps,"noverlaps/I");
103           ntuple1->Branch("noverprim",&ntuple1_st.noverprim,"noverprim/I");
104
105           ntuple2 = new TTree("ntuple2","Demo ntuple2");
106           ntuple2->Branch("nxP",&ntuple2_st.nxP,"nxP/I");
107           ntuple2->Branch("nxN",&ntuple2_st.nxN,"nxN/I");
108           ntuple2->Branch("x",&ntuple2_st.x,"x/F");
109           ntuple2->Branch("z",&ntuple2_st.z,"z/F");
110
111
112           // Create Histogramms
113
114           TH1F *NxP5 = new TH1F("NxP5","P cluster size for layer 5",20,0.,20.);
115           TH1F *NxN5 = new TH1F("NxN5","N cluster size for layer 5",20,0.,20.);
116           TH1F *NxP6 = new TH1F("NxP6","P cluster size for layer 6",20,0.,20.);
117           TH1F *NxN6 = new TH1F("NxN6","N cluster size for layer 6",20,0.,20.);
118
119           TH1F *Xres5 = new TH1F("Xres5","Xrec and Xgen difference (micr) for layers 5",100,-200.,200.);
120           TH1F *Xres6 = new TH1F("Xres6","Xrec and Xgen difference (micr) for layers 6",100,-200.,200.);
121           TH1F *Zres5 = new TH1F("Zres5","Zrec and Zgen difference (micr) for layers 5",100,-8000.,8000.);
122           TH1F *Zres6 = new TH1F("Zres6","Zrec and Zgen difference (micr) for layers 6",100,-8000.,8000.);
123           TH1F *Path5 = new TH1F("Path5","Path length in Si",100,0.,600.);
124           TH1F *Path6 = new TH1F("Path6","Path length in Si",100,0.,600.);
125           TH1F *dEdX = new TH1F("dEdX","dEdX  (KeV)",100,0.,500.);
126           TH2F *adcPadcN5all = new TH2F("adcPadcN5all","adcP/N correlation for lay5",100,0.,200.,100,0.,200.);
127           TH2F *adcPadcN6all = new TH2F("adcPadcN6all","adcP/N correlation for lay6",100,0.,200.,100,0.,200.);
128           TH2F *adcPadcN5cut = new TH2F("adcPadcN5cut","adcP/N correlation for lay5 and cut of P-N signas",100,0.,200.,100,0.,200.);
129           TH2F *adcPadcN6cut = new TH2F("adcPadcN6cut","adcP/N correlation for lay6 and cut of P-N signals",100,0.,200.,100,0.,200.);
130
131
132    AliITS *ITS  = (AliITS*) gAlice->GetModule("ITS");
133    if (!ITS) { cout << "no ITS" << endl; return; }
134    
135    //AliITSgeom *aliitsgeo = ITS->GetITSgeom();
136    AliITSgeom *geom = ITS->GetITSgeom();
137
138    //Int_t cp[8]={0,0,0,0,0,0,0,0};
139
140    cout << "SSD" << endl;
141
142    AliITSDetType *iDetType=ITS->DetType(2);
143    AliITSsegmentationSSD *seg2=(AliITSsegmentationSSD*)iDetType->GetSegmentationModel();
144    AliITSresponseSSD *res2 = (AliITSresponseSSD*)iDetType->GetResponseModel();
145    //res2->SetSigmaSpread(3.,2.);
146    AliITSsimulationSSD *sim2=new AliITSsimulationSSD(seg2,res2);
147    ITS->SetSimulationModel(2,sim2);
148
149    TClonesArray *dig2  = ITS->DigitsAddress(2);
150    TClonesArray *recp2  = ITS->ClustersAddress(2);
151    //   AliITSClusterFinderSSD *rec2=new AliITSClusterFinderSSD(seg2,dig2,recp2);
152    AliITSClusterFinderSSD *rec2=new AliITSClusterFinderSSD(seg2,dig2);
153    ITS->SetReconstructionModel(2,rec2);
154    // test
155    printf("SSD dimensions %f %f \n",seg2->Dx(),seg2->Dz());
156    printf("SSD nstrips %d %d \n",seg2->Npz(),seg2->Npx());
157
158    
159 //
160 //   Loop over events
161 //
162
163
164    Int_t Nh=0;
165    Int_t Nh1=0;
166    for (int nev=0; nev<= evNumber2; nev++) {
167      Int_t nparticles = 0;
168      nparticles = gAlice->GetEvent(nev);
169      cout << "nev         " << nev <<endl;
170      cout << "nparticles  " << nparticles <<endl;
171      if (nev < evNumber1) continue;
172      if (nparticles <= 0) return;
173      
174      AliITShit *itsHit;
175      AliITSRecPoint *itsPnt = 0;
176      AliITSRawClusterSSD *itsClu = 0;
177      
178      // Get Hit, Cluster & Recpoints Tree Pointers
179
180      TTree *TH = gAlice->TreeH();
181      Int_t nenthit=TH->GetEntries();
182      printf("Found %d entries in the Hit tree (must be one per track per event!)\n",nenthit);
183
184      ITS->GetTreeC(nev);
185      TTree *TC=ITS->TreeC();
186      Int_t nentclu=TC->GetEntries();
187      printf("Found %d entries in the Cluster tree (must be one per module per event!)\n",nentclu);
188
189      TTree *TR = gAlice->TreeR();
190      Int_t nentrec=TR->GetEntries();
191      printf("Found %d entries in the RecPoints tree\n",nentrec);
192
193      // Get Pointers to Clusters & Recpoints TClonesArrays
194
195      TClonesArray *ITSclu  = ITS->ClustersAddress(2); 
196      printf ("ITSclu %p \n",ITSclu);
197      TClonesArray *ITSrec  = ITS->RecPoints(); 
198      printf ("ITSrec %p \n",ITSrec);
199
200      // check recpoints
201
202      //Int_t nbytes = 0;
203      Int_t totpoints = 0;
204      Int_t totclust = 0;
205
206      // check hits
207      
208      Int_t nmodules=0;
209      Int_t mod;
210      
211      ITS->InitModules(-1,nmodules); 
212      ITS->FillModules(nev,0,nmodules,"","");
213      
214      TObjArray *fITSmodules = ITS->GetModules();
215      
216      Int_t first0 = geom->GetStartDet(0);  // SPD
217      Int_t last0 = geom->GetLastDet(0);    // SPD
218      Int_t first1 = geom->GetStartDet(1);  // SDD
219      Int_t last1 = geom->GetLastDet(1);    // SDD
220      Int_t first2 = geom->GetStartDet(2);  // SSD
221      Int_t last2 = geom->GetLastDet(2);    // SSD
222
223      //  For the SPD: first0 = 0, last0 = 239     (240 modules);  
224      //  for the SDD: first1 = 240, last1 = 499   (260 modules);  
225      //  for the SSD: first2 = 500, last2 = 2269  (1770 modules).  
226
227      printf("det type %d first0, last0 %d %d \n",0,first0,last0);
228      printf("det type %d first1, last1 %d %d \n",1,first1,last1);
229      printf("det type %d first2, last2 %d %d \n",2,first2,last2);
230
231      // module loop for the SSD
232      for (mod=first2; mod<last2+1; mod++) {  // for the "ALL" option
233      //for (mod=0; mod<last2-first2+1; mod++) { //for the "SSD" option
234
235        TTree *TR = gAlice->TreeR();
236        Int_t nentrec=TR->GetEntries();
237        //printf("Found %d entries in the RecPoints tree\n",nentrec);
238       
239               //cout << "CLUSTERS: reset" << endl;
240        ITS->ResetClusters();
241        //cout << "CLUSTERS: get" << endl;
242        TC->GetEvent(mod);
243        //cout << "RECPOINTS: reset" << endl;
244        ITS->ResetRecPoints();
245        //cout << "RECPOINTS: get" << endl;
246        //TR->GetEvent(mod+1);   // for the V3.04 AliRoot
247        TR->GetEvent(mod);       // for the V3.05 AliRoot
248
249        Int_t nrecp = ITSrec->GetEntries();
250        totpoints += nrecp;
251        if (nrecp) printf("Found %d rec points for module %d\n",nrecp,mod);
252        //if (!nrecp) continue;
253        Int_t nclusters = ITSclu->GetEntries();
254        totclust += nclusters;
255        //if (nclusters) printf("Found %d clusters for module %d\n",nrecc,mod);
256        
257        //AliITSmodule *Mod = (AliITSmodule *)fITSmodules->At(mod+first2);
258        // for the "SSD" option
259
260        AliITSmodule *Mod = (AliITSmodule *)fITSmodules->At(mod);
261        // for the "ALL" option
262
263        //       printf("Mod: %X\n",Mod);
264        Int_t nhits = Mod->GetNhits();
265        Float_t epart = 0;
266        //cout <<" module,nrecp,nclusters,nhits ="<<mod<<","<<nrecp<<","<<nclusters<<","<<nhits<< endl;
267
268        // ---------------- cluster/hit analysis ---------------------
269
270
271      Float_t pathInSSD = 300.;
272
273        // ---- Recpoint loop
274        for (Int_t pnt=0;pnt<nrecp;pnt++) {
275          itsPnt  = (AliITSRecPoint*)ITSrec->At(pnt);
276          if(!itsPnt) continue;
277          itsClu  = (AliITSRawClusterSSD*)ITSclu->At(pnt);
278          if(!itsClu) continue;
279
280          Int_t nxP = itsClu->fMultiplicity;
281          Int_t nxN = itsClu->fMultiplicityN;
282          Float_t qclP = itsClu->fSignalP;     // in ADC
283          Float_t qclN = itsClu->fSignalN;     // in ADC
284          //Float_t dq = qclP - qclN;
285          Float_t qcut = itsClu->fQErr;        // abs(dq)/signal,
286                                               // where signal is
287                                               // max of qclP,qclN        
288          Float_t xrec = 10000*itsPnt->GetX();
289          Float_t zrec = 10000*itsPnt->GetZ();
290          Float_t qrec = itsPnt->GetQ();      // in ADC, maximum from fSignalP/N
291          //Float_t dedx = itsPnt->GetdEdX();   // in KeV (ADC * 2.16)
292          Float_t dedx = itsPnt->fdEdX;   // in KeV (ADC * 2.16)
293          Int_t ii = 0;
294          Int_t tr1 = itsPnt->GetLabel(ii);
295          Int_t ii = 1;
296          Int_t tr2 = itsPnt->GetLabel(ii);
297          Int_t ii = 2;
298          Int_t tr3 = itsPnt->GetLabel(ii);
299
300          // fill ntuple2
301              ntuple2_st.nxP = nxP;
302              ntuple2_st.nxN = nxN;
303              ntuple2_st.x = xrec/1000;
304              ntuple2_st.z = zrec/1000;
305
306              if(qcut < 0.18) ntuple2->Fill();
307
308
309           Int_t noverlaps = 0;
310           Int_t noverprim = 0;
311           Int_t flaghit = 0;
312           Float_t xhit0 = 1e+7;
313           Float_t yhit0 = 1e+7;
314           Float_t zhit0 = 1e+7;
315
316        // Hit loop
317         for (Int_t hit=0;hit<nhits;hit++) {
318
319          itsHit   = (AliITShit*)Mod->GetHit(hit);
320
321          Int_t flagtrack = 0;
322          Int_t hitlayer = itsHit->GetLayer();
323          Int_t hitladder= itsHit->GetLadder();
324          Int_t hitdet= itsHit->GetDetector();
325
326          Int_t track = itsHit->GetTrack();
327          Int_t dray = 0;
328          Int_t hitstat = itsHit->GetTrackStatus();
329
330           Float_t zhit = 10000*itsHit->GetZL();
331           Float_t xhit = 10000*itsHit->GetXL();
332           Float_t yhit = 10000*itsHit->GetYL();
333           Float_t ehit = 1.0e+6*itsHit->GetIonization(); // hit energy, KeV 
334
335            Int_t parent = itsHit->GetParticle()->GetFirstMother();
336            Int_t partcode = itsHit->GetParticle()->GetPdgCode();
337
338    //  partcode (pdgCode): 11 - e-, 13 - mu-, 22 - gamma, 111 - pi0, 211 - i+
339    //  310 - K0s, 321 - K+, 2112 - n, 2212 - p, 3122 - lambda
340
341            Float_t pmod = itsHit->GetParticle()->P(); // the momentum at the
342                                                       // vertex
343            pmod *= 1.0e+3;
344
345           if(hitstat == 66 && yhit < -146.) {  // entering hit
346             xhit0 = xhit;
347             yhit0 = yhit;
348             zhit0 = zhit;
349           }
350
351           if(hitstat == 66) continue; // Take the not entering hits only 
352
353           if(xhit0 > 9e+6 || zhit0 > 9e+6 || yhit0 > 9e+6) {
354             //cout<<"default xhit0,zhit0,yhit0 ="<<xhit0<<","<<zhit0<<","<<yhit0<<endl;
355             continue;
356           }
357
358
359
360           // Consider the hits only with the track number equaled to one
361           // of the recpoint
362           if(track == tr1) flagtrack = 1;
363
364          if(flagtrack == 1) {     // the hit corresponds to the recpoint
365
366            flaghit = 1;
367
368            //Float_t px = itsHit->GetPXL(); // the momenta at this GEANT point
369            //Float_t py = itsHit->GetPYL();
370            //Float_t pz = itsHit->GetPZL();
371
372          Int_t hitprim = 0;
373
374          if(partcode == 11 && pmod < 6) dray = 1; // delta ray is e-
375                                                   // at p < 6 MeV/c
376
377          if((hitstat == 68 || hitstat == 33) && dray == 0)  noverlaps=noverlaps + 1;
378                                                   // overlapps for all hits but
379                                                   // not for delta ray which
380                                                   // also went out from the
381                                                   // detector and returned
382                                                   // again
383
384
385           // x,z resolution colculation
386           if(hitstat == 68 || hitsat == 33) {
387              Float_t xmed = (xhit + xhit0)/2;
388              Float_t zmed = (zhit + zhit0)/2;
389              Float_t xdif = xmed - xrec;
390              Float_t zdif = zmed - zrec;
391
392             if(parent < 0)  {
393               hitprim = 1; // hitprim=1 for the primery particles
394               noverprim += 1;
395             }
396              pathInSSD = TMath::Sqrt((xhit0-xhit)*(xhit0-xhit)+(yhit0-yhit)*(yhit0-yhit)+(zhit0-zhit)*(zhit0-zhit));
397
398              //cout<<"lay,pnt,hit,xmed,xrec,xdif,zmed,zrec,zdif ="<<hitlayer<<","<<pnt<<","<<hit<<","<<xmed<<","<<xrec<<","<<xdif<<","<<zmed<<","<<zrec<<","<<zdif<<endl;
399
400          // fill ntuple
401              ntuple_st.lay = hitlayer;
402              ntuple_st.nxP = nxP;
403              ntuple_st.nxN = nxN;
404              ntuple_st.hitprim = hitprim;
405              ntuple_st.partcode = partcode;
406              ntuple_st.x = xrec/1000;
407              ntuple_st.z = zrec/1000;
408              ntuple_st.dx = xdif;
409              ntuple_st.dz = zdif;
410              ntuple_st.pmod = pmod;
411
412              //if(qcut < 0.18) ntuple->Fill();
413              ntuple->Fill();
414
415              //if(hitlayer == 5 && qcut < 0.18) {
416             if(hitlayer == 5 ) {
417              Xres5->Fill(xdif);
418              Zres5->Fill(zdif);
419              Path5->Fill(pathInSSD);
420             }
421             //if(hitlayer == 6 && qcut < 0.18) {
422             if(hitlayer == 6) {
423              Xres6->Fill(xdif);
424              Zres6->Fill(zdif);
425              Path6->Fill(pathInSSD);
426             }
427           } // hitstat 68/33
428          } else {       // non correspondent hit
429           xhit0 = 1e+7;
430           zhit0 = 1e+7;
431          } // end of hit-recpoint correspondence
432         } // hit loop       
433
434         if(flaghit == 1) {
435
436           if(noverlaps == 0) noverlaps = 1; // cluster contains one or more
437           // delta rays only
438
439           // fill ntuple1
440           ntuple1_st.lay = hitlayer;
441           ntuple1_st.lad = hitladder;
442           ntuple1_st.det = hitdet;
443           ntuple1_st.nxP = nxP;
444           ntuple1_st.nxN = nxN;
445           ntuple1_st.qclP = qclP*300/pathInSSD; 
446           ntuple1_st.qclN = qclN*300/pathInSSD; 
447           ntuple1_st.qrec = qrec*300/pathInSSD; 
448           ntuple1_st.dx = xdif;
449           ntuple1_st.dz = zdif;
450           noverlaps -= 1;
451           noverprim -= 1;
452           ntuple1_st.noverlaps = noverlaps;
453           ntuple1_st.noverprim = noverprim;
454
455           //if(qcut < 0.18) ntuple1->Fill();
456           ntuple1->Fill();
457
458           Float_t de = dedx*300./pathInSSD;
459           dEdX->Fill(de);
460             if(hitlayer == 5 ) {
461              adcPadcN5all->Fill(qclP,qclN);
462             }
463             if(hitlayer == 6 ) {
464              adcPadcN6all->Fill(qclP,qclN);
465             }
466             if(hitlayer == 5 && qcut < 0.18) {
467              adcPadcN5cut->Fill(qclP,qclN);
468              NxP5->Fill(nxP);
469              NxN5->Fill(nxN);
470             }
471             if(hitlayer == 6 && qcut < 0.18) {
472              adcPadcN6cut->Fill(qclP,qclN);
473              NxP6->Fill(nxP);
474              NxN6->Fill(nxN);
475             }
476         } // flaghit = 1
477        } //b.b. recpoint loop
478      } //b.b. module loop
479    } //b.b. evnt loop
480
481    TFile fhistos("SSD_his.root","RECREATE");
482
483    ntuple->Write();
484    ntuple1->Write();
485    ntuple2->Write();
486    NxP5->Write();
487    NxN5->Write();
488    NxP6->Write();
489    NxN6->Write();
490    Xres5->Write();
491    Zres5->Write();
492    Xres6->Write();
493    Zres6->Write();
494    Path5->Write();
495    Path6->Write();
496    adcPadcN5all->Write();
497    adcPadcN6all->Write();
498    adcPadcN5cut->Write();
499    adcPadcN6cut->Write();
500    dEdX->Write();
501
502    fhistos.Close();
503
504    cout<<"!!! Histogramms and ntuples were written"<<endl;
505
506    TCanvas *c1 = new TCanvas("c1","ITS clusters",400,10,600,700);
507    c1->Divide(2,2);
508    c1->cd(1);
509    gPad->SetFillColor(33);
510          Xres5->SetFillColor(42);
511          Xres5->Draw();
512    c1->cd(2);
513    gPad->SetFillColor(33);
514          Zres5->SetFillColor(46);
515          Zres5->Draw();
516    c1->cd(3);
517    gPad->SetFillColor(33);
518          Xres6->SetFillColor(42);
519          Xres6->Draw();
520    c1->cd(4);
521    gPad->SetFillColor(33);
522          Zres6->SetFillColor(46);
523          Zres6->Draw();
524
525    cout<<"END  test for clusters and hits "<<endl;
526
527 }
528
529
530
531
532
533