]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDBackgroundCorrection.cxx
Fixes of warnings and upgrade of analysis to include Pb+Pb analysis. Background corre...
[u/mrichter/AliRoot.git] / FMD / AliFMDBackgroundCorrection.cxx
1 /**************************************************************************
2  * Copyright(c) 2004, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // Thil class computes background corrections for the FMD. The correction is computed 
17 // in eta,phi cells and the objects stored can be put into alien to use with analysis.
18 // It is based on the AliFMDInput class that is used to loop over hits and primaries.
19 //
20 // Author: Hans Hjersing Dalsgaard, NBI, hans.dalsgaard@cern.ch
21 //
22 //
23
24 #include "AliSimulation.h"
25 #include "TStopwatch.h"
26 #include "iostream"
27 #include "TGrid.h"
28 #include "AliRunLoader.h"
29 #include "AliGeomManager.h"
30 #include "AliFMDGeometry.h"
31 #include "AliStack.h"
32 #include "TParticle.h"
33 #include "TDatabasePDG.h"
34 #include "TParticlePDG.h"
35 #include "AliRun.h"
36 #include "AliFMDBackgroundCorrection.h"
37 #include "TSystem.h"
38 #include "AliCDBManager.h"
39 #include "TTree.h"
40 #include "TClonesArray.h"
41 #include "TBranch.h"
42 #include "AliFMDHit.h"
43 #include "AliLoader.h" 
44 #include "AliFMD.h"
45 #include "AliGenEventHeader.h"
46 #include "AliHeader.h"
47 #include "TFile.h"
48 #include "TAxis.h"
49 #include "AliCDBId.h"
50 #include "AliCDBMetaData.h"
51 #include "TROOT.h"
52 #include "AliFMDParameters.h"
53 #include "AliLog.h"
54 #include "TList.h"
55 #include "AliFMDAnaParameters.h"
56 #include "AliFMDAnaCalibBackgroundCorrection.h"
57 #include "AliTrackReference.h"
58 #include "AliFMDStripIndex.h"
59
60 ClassImp(AliFMDBackgroundCorrection)
61 //_____________________________________________________________________
62 AliFMDBackgroundCorrection::AliFMDBackgroundCorrection() : 
63   TNamed(),
64   fCorrectionArray(),
65   fPrimaryList()
66 {} 
67
68 //_____________________________________________________________________
69 AliFMDBackgroundCorrection::AliFMDInputBG::AliFMDInputBG(Bool_t hits_not_trackref) : 
70   AliFMDInput("galice.root"),
71   fPrimaryArray(),
72   fHitArray(),
73   fHitMap(),
74   fLastTrackByStrip(),
75   fPrim(0),
76   fHits(0),
77   fZvtxCut(0),
78   fNvtxBins(0),
79   fPrevTrack(-1),
80   fPrevDetector(-1),
81   fPrevRing('Q'),
82   fPrevSec(-1),
83   fNbinsEta(100)
84 {
85
86   if(hits_not_trackref) {
87     AddLoad(kHits);
88     AddLoad(kTracks); 
89   }
90   else
91     AddLoad(kTrackRefs);
92   AddLoad(kKinematics); 
93   AddLoad(kHeader);
94   
95   
96 }
97
98 //_____________________________________________________________________
99
100 void AliFMDBackgroundCorrection::GenerateBackgroundCorrection(Bool_t from_hits,
101                                                               const Int_t nvtxbins,
102                                                               Float_t zvtxcut, 
103                                                               const Int_t nBinsEta, 
104                                                               Bool_t storeInAlien, 
105                                                               Int_t runNo,
106                                                               Int_t endRunNo, 
107                                                               const Char_t* filename, 
108                                                               Bool_t simulate,
109                                                               Int_t nEvents,
110                                                               Bool_t inFile,
111                                                               const Char_t* infilename) {
112   
113   //TGrid::Connect("alien:",0,0,"t");
114   if(simulate)
115     Simulate(nEvents);
116   else {
117     //AliCDBManager::Instance()->SetDefaultStorage("alien://Folder=/alice/data/2008/LHC08d/OCDB/");
118     AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
119     AliCDBManager::Instance()->SetRun(runNo);
120     
121 #if defined(__CINT__)
122     gSystem->Load("liblhapdf");
123     gSystem->Load("libEGPythia6");
124     gSystem->Load("libpythia6");
125     gSystem->Load("libAliPythia6");
126     gSystem->Load("libgeant321");
127 #endif
128       // 
129   }  
130   
131   //Setting up the geometry
132   //-----------------------------------------------
133   if (AliGeomManager::GetGeometry() == NULL)
134     AliGeomManager::LoadGeometry();
135   
136   AliFMDGeometry* geo = AliFMDGeometry::Instance();
137   geo->Init();
138   geo->InitTransformations();
139     
140   AliInfo("Processing hits and primaries ");
141   
142   AliFMDInputBG input(from_hits);
143   
144   if(!inFile) {
145   
146     input.SetVtxCutZ(zvtxcut);
147     input.SetNvtxBins(nvtxbins);
148     input.SetNbinsEta(nBinsEta);
149     input.Run();
150   }
151   
152   AliInfo(Form("Found %d primaries and %d hits.", input.GetNprim(),input.GetNhits()));
153   TObjArray* hitArray ;
154   TObjArray* primaryArray;
155   if(inFile) {
156     TFile* infile = TFile::Open(infilename);
157     hitArray     = new TObjArray();
158     primaryArray = new TObjArray();
159     
160     for(Int_t det =1; det<=3;det++)
161       {
162         TObjArray* detArrayHits = new TObjArray();
163         detArrayHits->SetName(Form("FMD%d",det));
164         hitArray->AddAtAndExpand(detArrayHits,det);
165         Int_t nRings = (det==1 ? 1 : 2);
166         for(Int_t ring = 0;ring<nRings;ring++)
167           {
168             
169             Char_t ringChar = (ring == 0 ? 'I' : 'O');
170             TObjArray* vtxArrayHits = new TObjArray();
171             vtxArrayHits->SetName(Form("FMD%d%c",det,ringChar));
172             detArrayHits->AddAtAndExpand(vtxArrayHits,ring);
173             for(Int_t v=0; v<nvtxbins;v++)
174               {
175               
176                 TH2F* hHits          = (TH2F*)infile->Get(Form("hHits_FMD%d%c_vtx%d",det,ringChar,v));
177                 
178                 
179               vtxArrayHits->AddAtAndExpand(hHits,v);
180                       
181             } 
182         }
183       
184     }
185     
186     for(Int_t iring = 0; iring<2;iring++) {
187       Char_t ringChar = (iring == 0 ? 'I' : 'O');
188       TObjArray* ringArray = new TObjArray();
189       ringArray->SetName(Form("FMD_%c",ringChar));
190       primaryArray->AddAtAndExpand(ringArray,iring);
191       for(Int_t v=0; v<nvtxbins;v++) {
192         
193         TH2F* hPrimary       = (TH2F*)infile->Get(Form("hPrimary_FMD_%c_vtx%d",ringChar,v));
194         ringArray->AddAtAndExpand(hPrimary,v);
195       }
196     }
197     
198     
199   }
200   else {
201     hitArray     = input.GetHits();
202     primaryArray = input.GetPrimaries();
203   }
204   fCorrectionArray.SetName("FMD_bg_correction");
205   fCorrectionArray.SetOwner();
206    
207   TList* primaryList     = new TList();
208   primaryList->SetName("primaries");
209   
210   TList* hitList     = new TList();
211   hitList->SetName("hits");
212   TList* corrList    = new TList();
213   corrList->SetName("corrections");
214   
215   AliFMDAnaCalibBackgroundCorrection* background = new AliFMDAnaCalibBackgroundCorrection();
216   
217   for(Int_t det= 1; det <=3; det++) {
218     Int_t nRings = (det==1 ? 1 : 2);
219     
220     TObjArray* detArrayCorrection = new TObjArray();
221     detArrayCorrection->SetName(Form("FMD%d",det));
222     fCorrectionArray.AddAtAndExpand(detArrayCorrection,det);
223     
224     
225     for(Int_t iring = 0; iring<nRings; iring++) {
226       TObjArray* primRingArray = (TObjArray*)primaryArray->At(iring);
227       Char_t ringChar = (iring == 0 ? 'I' : 'O');
228       TObjArray* vtxArrayCorrection = new TObjArray();
229       vtxArrayCorrection->SetName(Form("FMD%d%c",det,ringChar));
230       detArrayCorrection->AddAtAndExpand(vtxArrayCorrection,iring);
231       
232       for(Int_t vertexBin=0;vertexBin<nvtxbins;vertexBin++) {
233         TObjArray* detArray  = (TObjArray*)hitArray->At(det);
234         TObjArray* vtxArray  = (TObjArray*)detArray->At(iring);
235         TH2F* hHits          = (TH2F*)vtxArray->At(vertexBin);
236         hitList->Add(hHits);
237         TH2F* hPrimary  = (TH2F*)primRingArray->At(vertexBin);
238         primaryList->Add(hPrimary);
239         TH2F* hCorrection = (TH2F*)hHits->Clone(Form("FMD%d%c_vtxbin_%d_correction",det,ringChar,vertexBin));
240         hCorrection->Divide(hPrimary);
241         corrList->Add(hCorrection);
242         hCorrection->SetTitle(hCorrection->GetName());
243         vtxArrayCorrection->AddAtAndExpand(hCorrection,vertexBin);
244         background->SetBgCorrection(det,ringChar,vertexBin,hCorrection);
245       }
246       
247     }
248   }
249   
250   TAxis refAxis(nvtxbins,-1*zvtxcut,zvtxcut);
251   if(inFile) {
252     TFile* infile = TFile::Open(infilename);
253     TAxis* refaxis = (TAxis*)infile->Get("vertexbins");
254     background->SetRefAxis(refaxis);
255       
256   }
257   else background->SetRefAxis(&refAxis);
258   
259   TFile*  fout = new TFile(filename,"RECREATE");
260   refAxis.Write("vertexbins");
261   
262   hitList->Write();
263   primaryList->Write();
264   corrList->Write();
265    
266   TObjArray* container = new TObjArray();
267   container->SetOwner();
268   container->AddAtAndExpand(&refAxis,0);
269   container->AddAtAndExpand(&fCorrectionArray,1);
270   container->AddAtAndExpand(hitArray,2);
271   container->AddAtAndExpand(primaryArray,3);
272   
273   
274   if(storeInAlien) {
275     AliCDBManager* cdb = AliCDBManager::Instance();
276     cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
277     AliCDBId      id(AliFMDAnaParameters::GetBackgroundPath(),runNo,endRunNo);
278     
279     AliCDBMetaData* meta = new AliCDBMetaData;                          
280     meta->SetResponsible(gSystem->GetUserInfo()->fRealName.Data());     
281     meta->SetAliRootVersion(gROOT->GetVersion());                       
282     meta->SetBeamPeriod(1);                                             
283     meta->SetComment("Background Correction for FMD");
284     meta->SetProperty("key1", background );
285     cdb->Put(background, id, meta);
286     
287   }
288   
289   fout->Close();
290  
291   
292   }
293
294 //_____________________________________________________________________
295 void AliFMDBackgroundCorrection::Simulate(Int_t nEvents) {
296   
297   AliSimulation sim ; 
298   sim.SetRunNumber(0);
299   TGrid::Connect("alien:",0,0,"t");
300   sim.SetDefaultStorage("alien://Folder=/alice/data/2008/LHC08d/OCDB/");
301   sim.SetConfigFile("Config.C");
302   sim.SetRunQA("FMD:");
303   TStopwatch timer;
304   timer.Start();
305   sim.RunSimulation(nEvents);    
306   timer.Stop();
307   timer.Print();
308   
309 }
310
311 //_____________________________________________________________________
312 Bool_t 
313 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessHit(AliFMDHit* h, 
314                                                       TParticle* /*p*/) 
315 {
316   
317   if(!h)
318     return kTRUE;
319   Bool_t retval = ProcessEvent(h->Detector(),
320                                h->Ring(),
321                                h->Sector(),
322                                h->Strip(),
323                                h->Track(),
324                                h->Q());
325   
326   // std::cout<<"Length: "<<h->Length()<<std::endl;
327   // std::cout<<"Is stopped "<<h->IsStop()<<"    "<<kTRUE<<std::endl;
328   return retval;
329 }
330 //_____________________________________________________________________
331 Bool_t 
332 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessTrackRef(AliTrackReference* tr, TParticle* p) 
333 {
334   if(!tr)
335     return kTRUE;
336   UShort_t det,sec,strip;
337   Char_t   ring;
338   AliFMDStripIndex::Unpack(tr->UserId(),det,ring,sec,strip);
339   Int_t         nTrack  = tr->GetTrack();
340   TDatabasePDG* pdgDB   = TDatabasePDG::Instance();
341   TParticlePDG* pdgPart = pdgDB->GetParticle(p->GetPdgCode());
342   Float_t       charge  = (pdgPart ? pdgPart->Charge() : 0);
343   Bool_t        retval  = ProcessEvent(det,ring,sec,strip,nTrack,charge);
344   return retval;
345   
346 }
347 //_____________________________________________________________________
348 Bool_t 
349 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessEvent(UShort_t det,
350                                                         Char_t   ring, 
351                                                         UShort_t sec, 
352                                                         UShort_t strip,
353                                                         Int_t    nTrack,
354                                                         Float_t  charge)
355 {
356   
357   
358   
359   /*  if(charge !=  0 && 
360      ((nTrack != fPrevTrack) || 
361       (det != fPrevDetector))){ || 
362       (ring != fPrevRing))){    ||
363                                   (sec != fPrevSec))) {*/
364   /*Float_t nstrips = (ring =='O' ? 256 : 512);
365   
366   
367   Float_t prevStripTrack = -1;
368   if(strip >0)
369     prevStripTrack = fLastTrackByStrip.operator()(det,ring,sec,strip-1);
370   
371   Float_t nextStripTrack = -1;
372   if(strip < (nstrips - 1))
373     nextStripTrack = fLastTrackByStrip.operator()(det,ring,sec,strip+1);
374   */
375   
376   Float_t thisStripTrack = fLastTrackByStrip.operator()(det,ring,0,0);
377   // if(nTrack == fLastTrackByStrip.operator()(det,ring,sec,strip))
378   //  std::cout<<"Track # "<<nTrack<<"  failed the cut in "<<det<<"   "<<ring<<"   "<<sec<<"  "<<strip<<std::endl;
379   // else
380   //  std::cout<<"Track # "<<nTrack<<"  passed the cut in "<<det<<"   "<<ring<<"   "<<sec<<"  "<<strip<<std::endl;
381   if(charge != 0 && nTrack != thisStripTrack) {
382     
383     fHitMap.operator()(det,ring,sec,strip) += 1;
384     fHits++;
385     Float_t nstrips = (ring =='O' ? 256 : 512);
386     fLastTrackByStrip.operator()(det,ring,sec,strip) = (Float_t)nTrack;
387     if(strip >0)
388       fLastTrackByStrip.operator()(det,ring,sec,strip-1) = (Float_t)nTrack;
389     if(strip < (nstrips - 1))
390       fLastTrackByStrip.operator()(det,ring,sec,strip+1) = (Float_t)nTrack;
391     
392     fPrevDetector = det;
393     fPrevRing     = ring;
394     fPrevSec      = sec;
395     fPrevTrack    = nTrack;
396   }
397   
398   
399   
400   return kTRUE;
401
402 }
403
404 //_____________________________________________________________________
405 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Init() 
406 {
407   fLastTrackByStrip.Reset(-1);
408   
409   fPrimaryArray.SetOwner();
410   fPrimaryArray.SetName("FMD_primary");
411   
412   fPrimaryMapInner.SetBins(fNbinsEta, -6,6, 20, 0, 2*TMath::Pi());
413   fPrimaryMapOuter.SetBins(fNbinsEta, -6,6, 40, 0, 2*TMath::Pi());
414   fPrimaryMapInner.SetName("fPrimaryMapInner");
415   fPrimaryMapInner.SetName("fPrimaryMapOuter");
416   
417   fPrimaryMapInner.Sumw2();
418   fPrimaryMapOuter.Sumw2();
419   for(Int_t iring = 0; iring<2;iring++) {
420     Char_t ringChar = (iring == 0 ? 'I' : 'O');
421     TObjArray* ringArray = new TObjArray();
422     ringArray->SetName(Form("FMD_%c",ringChar));
423     fPrimaryArray.AddAtAndExpand(ringArray,iring);
424     Int_t nSec = (iring == 1 ? 40 : 20);
425     for(Int_t v=0; v<fNvtxBins;v++) {
426
427       TH2F* hPrimary       = new TH2F(Form("hPrimary_FMD_%c_vtx%d",ringChar,v),
428                                       Form("hPrimary_FMD_%c_vtx%d",ringChar,v),
429                                       fNbinsEta, -6,6, nSec, 0,2*TMath::Pi());
430       hPrimary->Sumw2();
431       ringArray->AddAtAndExpand(hPrimary,v);
432     }
433   }
434   
435   
436   fHitArray.SetOwner();
437   fHitArray.SetName("FMD_hits");
438    
439   for(Int_t det =1; det<=3;det++) {
440     TObjArray* detArrayHits = new TObjArray();
441     detArrayHits->SetName(Form("FMD%d",det));
442     fHitArray.AddAtAndExpand(detArrayHits,det);
443     Int_t nRings = (det==1 ? 1 : 2);
444     for(Int_t ring = 0;ring<nRings;ring++) {
445       Int_t nSec = (ring == 1 ? 40 : 20);
446       Char_t ringChar = (ring == 0 ? 'I' : 'O');
447       TObjArray* vtxArrayHits = new TObjArray();
448       vtxArrayHits->SetName(Form("FMD%d%c",det,ringChar));
449       detArrayHits->AddAtAndExpand(vtxArrayHits,ring);
450       for(Int_t v=0; v<fNvtxBins;v++) {
451         TH2F* hHits = new TH2F(Form("hHits_FMD%d%c_vtx%d", det,ringChar,v),
452                                Form("hHits_FMD%d%c_vtx%d", det,ringChar,v),
453                                fNbinsEta, -6,6, nSec, 0, 2*TMath::Pi());
454         hHits->Sumw2();
455         vtxArrayHits->AddAtAndExpand(hHits,v);
456         
457       } 
458     }
459   }
460
461   AliFMDInput::Init();
462   
463   return kTRUE;
464 }
465 //_____________________________________________________________________
466
467 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Begin(Int_t event ) 
468 {
469
470   Bool_t             retVal    = AliFMDInput::Begin(event); 
471   AliStack*          partStack = fLoader->Stack();
472   Int_t              nTracks   = partStack->GetNtrack();
473   
474   
475   for(Int_t j=0;j<nTracks;j++) {
476     TParticle* p           = partStack->Particle(j);
477     TDatabasePDG* pdgDB    = TDatabasePDG::Instance();
478     TParticlePDG* pdgPart  = pdgDB->GetParticle(p->GetPdgCode());
479     Float_t       charge   = (pdgPart ? pdgPart->Charge() : 0);
480     Float_t       phi      = TMath::ATan2(p->Py(),p->Px());
481     
482     if(phi<0) phi = phi+2*TMath::Pi();
483     // if(p->Theta() == 0) continue;
484     Float_t eta   = p->Eta();   
485     
486     // std::cout<<-1*TMath::Log(TMath::Tan(0.5*p->Theta()))<<std::endl;
487     
488     Bool_t primary = partStack->IsPhysicalPrimary(j);
489     //(charge!=0)&&(TMath::Abs(p->Vx() - vertex.At(0))<0.01)&&(TMath::Abs(p->Vy() - vertex.At(1))<0.01)&&(TMath::Abs(p->Vz() - vertex.At(2))<0.01);
490     if(primary && charge !=0) {
491       
492       fPrim++;
493       
494       fPrimaryMapInner.Fill(eta,phi);
495       fPrimaryMapOuter.Fill(eta,phi);
496     }
497   }
498   
499   return retVal;
500 }
501 //_____________________________________________________________________
502 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::End()  {
503   
504   Bool_t retval = AliFMDInput::End();
505   
506   AliGenEventHeader* genHeader = fLoader->GetHeader()->GenEventHeader();
507   TArrayF vertex;
508   genHeader->PrimaryVertex(vertex);
509   
510   if(TMath::Abs(vertex.At(2)) > fZvtxCut) 
511     return kTRUE;
512   
513   Double_t delta           = 2*fZvtxCut/fNvtxBins;
514   Double_t vertexBinDouble = (vertex.At(2) + fZvtxCut) / delta;
515   Int_t    vertexBin       = (Int_t)vertexBinDouble;
516   //Primaries
517   TObjArray* innerArray = (TObjArray*)fPrimaryArray.At(0);
518   TObjArray* outerArray = (TObjArray*)fPrimaryArray.At(1);
519   
520   TH2F* hPrimaryInner  = (TH2F*)innerArray->At(vertexBin);
521   TH2F* hPrimaryOuter  = (TH2F*)outerArray->At(vertexBin);
522   
523   hPrimaryInner->Add(&fPrimaryMapInner);
524   hPrimaryOuter->Add(&fPrimaryMapOuter);
525   
526   //Hits
527   for(UShort_t det=1;det<=3;det++) {
528     Int_t nRings = (det==1 ? 1 : 2);
529     for (UShort_t ir = 0; ir < nRings; ir++) {
530       Char_t   ring = (ir == 0 ? 'I' : 'O');
531       UShort_t nsec = (ir == 0 ? 20  : 40);
532       UShort_t nstr = (ir == 0 ? 512 : 256);
533       
534       for(UShort_t sec =0; sec < nsec;  sec++) {
535         
536         for(UShort_t strip = 0; strip < nstr; strip++) {
537           
538           if(fHitMap.operator()(det,ring,sec,strip) > 0) {
539             //std::cout<<fHitMap.operator()(det,ring,sec,strip)<<std::endl;
540             Double_t x,y,z;
541             AliFMDGeometry* fmdgeo = AliFMDGeometry::Instance();
542             fmdgeo->Detector2XYZ(det,ring,sec,strip,x,y,z);
543             
544             Int_t iring = (ring == 'I' ? 0 : 1);
545             
546             TObjArray* detArray  = (TObjArray*)fHitArray.At(det);
547             TObjArray* vtxArray  = (TObjArray*)detArray->At(iring);
548             TH2F* hHits          = (TH2F*)vtxArray->At(vertexBin);
549             
550             Float_t   phi   = TMath::ATan2(y,x);
551             if(phi<0) phi   = phi+2*TMath::Pi();
552             Float_t   r     = TMath::Sqrt(TMath::Power(x,2)+TMath::Power(y,2));
553             Float_t   theta = TMath::ATan2(r,z-vertex.At(2));
554             Float_t   eta   = -1*TMath::Log(TMath::Tan(0.5*theta));
555             hHits->Fill(eta,phi,fHitMap.operator()(det,ring,sec,strip));
556           }
557           
558         }
559       }
560     }
561   }
562   
563   fPrimaryMapInner.Reset();
564   fPrimaryMapOuter.Reset();
565   fHitMap.Reset(0);
566   fLastTrackByStrip.Reset(-1);
567   
568   return retval;
569 }
570
571 //
572 // EOF
573 //
574
575