1 /**************************************************************************
2 * Copyright(c) 2004, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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.
20 // Author: Hans Hjersing Dalsgaard, NBI, hans.dalsgaard@cern.ch
24 #include "AliSimulation.h"
25 #include "TStopwatch.h"
28 #include "AliRunLoader.h"
29 #include "AliGeomManager.h"
30 #include "AliFMDGeometry.h"
32 #include "TParticle.h"
33 #include "TDatabasePDG.h"
34 #include "TParticlePDG.h"
36 #include "AliFMDBackgroundCorrection.h"
38 #include "AliCDBManager.h"
40 #include "TClonesArray.h"
42 #include "AliFMDHit.h"
43 #include "AliLoader.h"
45 #include "AliGenEventHeader.h"
46 #include "AliHeader.h"
50 #include "AliCDBMetaData.h"
52 #include "AliFMDParameters.h"
55 #include "AliFMDAnaParameters.h"
56 #include "AliFMDAnaCalibBackgroundCorrection.h"
57 #include "AliTrackReference.h"
58 #include "AliFMDStripIndex.h"
60 ClassImp(AliFMDBackgroundCorrection)
61 //_____________________________________________________________________
62 AliFMDBackgroundCorrection::AliFMDBackgroundCorrection() :
68 //_____________________________________________________________________
69 AliFMDBackgroundCorrection::AliFMDInputBG::AliFMDInputBG(Bool_t hits_not_trackref) :
70 AliFMDInput("galice.root"),
95 //_____________________________________________________________________
97 void AliFMDBackgroundCorrection::GenerateBackgroundCorrection(Bool_t from_hits,
100 const Int_t nBinsEta,
104 const Char_t* filename,
108 const Char_t* infilename) {
110 //TGrid::Connect("alien:",0,0,"t");
114 //AliCDBManager::Instance()->SetDefaultStorage("alien://Folder=/alice/data/2008/LHC08d/OCDB/");
115 AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
116 AliCDBManager::Instance()->SetRun(runNo);
118 #if defined(__CINT__)
119 gSystem->Load("liblhapdf");
120 gSystem->Load("libEGPythia6");
121 gSystem->Load("libpythia6");
122 gSystem->Load("libAliPythia6");
123 gSystem->Load("libgeant321");
128 //Setting up the geometry
129 //-----------------------------------------------
130 if (AliGeomManager::GetGeometry() == NULL)
131 AliGeomManager::LoadGeometry();
133 AliFMDGeometry* geo = AliFMDGeometry::Instance();
135 geo->InitTransformations();
137 AliInfo("Processing hits and primaries ");
139 AliFMDInputBG input(from_hits);
143 input.SetVtxCutZ(zvtxcut);
144 input.SetNvtxBins(nvtxbins);
145 input.SetNbinsEta(nBinsEta);
149 AliInfo(Form("Found %d primaries and %d hits.", input.GetNprim(),input.GetNhits()));
150 TObjArray* hitArray ;
151 TObjArray* primaryArray;
153 TFile* infile = TFile::Open(infilename);
154 hitArray = new TObjArray();
155 primaryArray = new TObjArray();
157 for(Int_t det =1; det<=3;det++)
159 TObjArray* detArrayHits = new TObjArray();
160 detArrayHits->SetName(Form("FMD%d",det));
161 hitArray->AddAtAndExpand(detArrayHits,det);
162 Int_t nRings = (det==1 ? 1 : 2);
163 for(Int_t ring = 0;ring<nRings;ring++)
166 Char_t ringChar = (ring == 0 ? 'I' : 'O');
167 TObjArray* vtxArrayHits = new TObjArray();
168 vtxArrayHits->SetName(Form("FMD%d%c",det,ringChar));
169 detArrayHits->AddAtAndExpand(vtxArrayHits,ring);
170 for(Int_t v=0; v<nvtxbins;v++)
173 TH2F* hHits = (TH2F*)infile->Get(Form("hHits_FMD%d%c_vtx%d",det,ringChar,v));
176 vtxArrayHits->AddAtAndExpand(hHits,v);
183 for(Int_t iring = 0; iring<2;iring++) {
184 Char_t ringChar = (iring == 0 ? 'I' : 'O');
185 TObjArray* ringArray = new TObjArray();
186 ringArray->SetName(Form("FMD_%c",ringChar));
187 primaryArray->AddAtAndExpand(ringArray,iring);
188 for(Int_t v=0; v<nvtxbins;v++) {
190 TH2F* hPrimary = (TH2F*)infile->Get(Form("hPrimary_FMD_%c_vtx%d",ringChar,v));
191 ringArray->AddAtAndExpand(hPrimary,v);
198 hitArray = input.GetHits();
199 primaryArray = input.GetPrimaries();
201 fCorrectionArray.SetName("FMD_bg_correction");
202 fCorrectionArray.SetOwner();
204 TList* primaryList = new TList();
205 primaryList->SetName("primaries");
207 TList* hitList = new TList();
208 hitList->SetName("hits");
209 TList* corrList = new TList();
210 corrList->SetName("corrections");
212 AliFMDAnaCalibBackgroundCorrection* background = new AliFMDAnaCalibBackgroundCorrection();
214 for(Int_t det= 1; det <=3; det++) {
215 Int_t nRings = (det==1 ? 1 : 2);
217 TObjArray* detArrayCorrection = new TObjArray();
218 detArrayCorrection->SetName(Form("FMD%d",det));
219 fCorrectionArray.AddAtAndExpand(detArrayCorrection,det);
222 for(Int_t iring = 0; iring<nRings; iring++) {
223 TObjArray* primRingArray = (TObjArray*)primaryArray->At(iring);
224 Char_t ringChar = (iring == 0 ? 'I' : 'O');
225 TObjArray* vtxArrayCorrection = new TObjArray();
226 vtxArrayCorrection->SetName(Form("FMD%d%c",det,ringChar));
227 detArrayCorrection->AddAtAndExpand(vtxArrayCorrection,iring);
229 for(Int_t vertexBin=0;vertexBin<nvtxbins;vertexBin++) {
230 TObjArray* detArray = (TObjArray*)hitArray->At(det);
231 TObjArray* vtxArray = (TObjArray*)detArray->At(iring);
232 TH2F* hHits = (TH2F*)vtxArray->At(vertexBin);
234 TH2F* hPrimary = (TH2F*)primRingArray->At(vertexBin);
235 primaryList->Add(hPrimary);
236 TH2F* hCorrection = (TH2F*)hHits->Clone(Form("FMD%d%c_vtxbin_%d_correction",det,ringChar,vertexBin));
237 hCorrection->Divide(hPrimary);
238 corrList->Add(hCorrection);
239 hCorrection->SetTitle(hCorrection->GetName());
240 vtxArrayCorrection->AddAtAndExpand(hCorrection,vertexBin);
241 background->SetBgCorrection(det,ringChar,vertexBin,hCorrection);
247 TAxis refAxis(nvtxbins,-1*zvtxcut,zvtxcut);
249 TFile* infile = TFile::Open(infilename);
250 TAxis* refaxis = (TAxis*)infile->Get("vertexbins");
251 background->SetRefAxis(refaxis);
254 else background->SetRefAxis(&refAxis);
256 TFile* fout = new TFile(filename,"RECREATE");
257 refAxis.Write("vertexbins");
260 primaryList->Write();
263 TObjArray* container = new TObjArray();
264 container->SetOwner();
265 container->AddAtAndExpand(&refAxis,0);
266 container->AddAtAndExpand(&fCorrectionArray,1);
267 container->AddAtAndExpand(hitArray,2);
268 container->AddAtAndExpand(primaryArray,3);
272 AliCDBManager* cdb = AliCDBManager::Instance();
273 cdb->SetDefaultStorage("local://$ALICE_ROOT");
274 AliCDBId id(AliFMDAnaParameters::GetBackgroundPath(),runNo,endRunNo);
276 AliCDBMetaData* meta = new AliCDBMetaData;
277 meta->SetResponsible(gSystem->GetUserInfo()->fRealName.Data());
278 meta->SetAliRootVersion(gROOT->GetVersion());
279 meta->SetBeamPeriod(1);
280 meta->SetComment("Background Correction for FMD");
281 meta->SetProperty("key1", background );
282 cdb->Put(background, id, meta);
291 //_____________________________________________________________________
292 void AliFMDBackgroundCorrection::Simulate(Int_t nEvents) {
296 TGrid::Connect("alien:",0,0,"t");
297 sim.SetDefaultStorage("alien://Folder=/alice/data/2008/LHC08d/OCDB/");
298 sim.SetConfigFile("Config.C");
299 sim.SetRunQA("FMD:");
302 sim.RunSimulation(nEvents);
308 //_____________________________________________________________________
310 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessHit(AliFMDHit* h,
316 Bool_t retval = ProcessEvent(h->Detector(),
325 //_____________________________________________________________________
327 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessTrackRef(AliTrackReference* tr,
332 UShort_t det,sec,strip;
334 AliFMDStripIndex::Unpack(tr->UserId(),det,ring,sec,strip);
335 Int_t nTrack = tr->GetTrack();
336 TDatabasePDG* pdgDB = TDatabasePDG::Instance();
337 TParticlePDG* pdgPart = pdgDB->GetParticle(p->GetPdgCode());
338 Float_t charge = (pdgPart ? pdgPart->Charge() : 0);
339 Bool_t retval = ProcessEvent(det,ring,sec,strip,nTrack,charge);
343 //_____________________________________________________________________
345 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessEvent(UShort_t det,
356 ((nTrack != fPrevTrack) ||
357 (det != fPrevDetector) ||
358 (ring != fPrevRing) ||
359 (sec != fPrevSec))) {
360 fHitMap.operator()(det,ring,sec,strip) = 1;
374 //_____________________________________________________________________
375 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Init()
377 fPrimaryArray.SetOwner();
378 fPrimaryArray.SetName("FMD_primary");
380 fPrimaryMapInner.SetBins(fNbinsEta, -6,6, 20, 0, 2*TMath::Pi());
381 fPrimaryMapOuter.SetBins(fNbinsEta, -6,6, 40, 0, 2*TMath::Pi());
382 fPrimaryMapInner.SetName("fPrimaryMapInner");
383 fPrimaryMapInner.SetName("fPrimaryMapOuter");
385 fPrimaryMapInner.Sumw2();
386 fPrimaryMapOuter.Sumw2();
387 for(Int_t iring = 0; iring<2;iring++) {
388 Char_t ringChar = (iring == 0 ? 'I' : 'O');
389 TObjArray* ringArray = new TObjArray();
390 ringArray->SetName(Form("FMD_%c",ringChar));
391 fPrimaryArray.AddAtAndExpand(ringArray,iring);
392 Int_t nSec = (iring == 1 ? 40 : 20);
393 for(Int_t v=0; v<fNvtxBins;v++) {
395 TH2F* hPrimary = new TH2F(Form("hPrimary_FMD_%c_vtx%d",ringChar,v),
396 Form("hPrimary_FMD_%c_vtx%d",ringChar,v),
397 fNbinsEta, -6,6, nSec, 0,2*TMath::Pi());
399 ringArray->AddAtAndExpand(hPrimary,v);
404 fHitArray.SetOwner();
405 fHitArray.SetName("FMD_hits");
407 for(Int_t det =1; det<=3;det++) {
408 TObjArray* detArrayHits = new TObjArray();
409 detArrayHits->SetName(Form("FMD%d",det));
410 fHitArray.AddAtAndExpand(detArrayHits,det);
411 Int_t nRings = (det==1 ? 1 : 2);
412 for(Int_t ring = 0;ring<nRings;ring++) {
413 Int_t nSec = (ring == 1 ? 40 : 20);
414 Char_t ringChar = (ring == 0 ? 'I' : 'O');
415 TObjArray* vtxArrayHits = new TObjArray();
416 vtxArrayHits->SetName(Form("FMD%d%c",det,ringChar));
417 detArrayHits->AddAtAndExpand(vtxArrayHits,ring);
418 for(Int_t v=0; v<fNvtxBins;v++) {
419 TH2F* hHits = new TH2F(Form("hHits_FMD%d%c_vtx%d", det,ringChar,v),
420 Form("hHits_FMD%d%c_vtx%d", det,ringChar,v),
421 fNbinsEta, -6,6, nSec, 0, 2*TMath::Pi());
423 vtxArrayHits->AddAtAndExpand(hHits,v);
433 //_____________________________________________________________________
435 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Begin(Int_t event )
438 Bool_t retVal = AliFMDInput::Begin(event);
439 AliStack* partStack = fLoader->Stack();
440 Int_t nTracks = partStack->GetNtrack();
443 for(Int_t j=0;j<nTracks;j++) {
444 TParticle* p = partStack->Particle(j);
445 TDatabasePDG* pdgDB = TDatabasePDG::Instance();
446 TParticlePDG* pdgPart = pdgDB->GetParticle(p->GetPdgCode());
447 Float_t charge = (pdgPart ? pdgPart->Charge() : 0);
448 Float_t phi = TMath::ATan2(p->Py(),p->Px());
450 if(phi<0) phi = phi+2*TMath::Pi();
451 // if(p->Theta() == 0) continue;
452 Float_t eta = p->Eta();
454 // std::cout<<-1*TMath::Log(TMath::Tan(0.5*p->Theta()))<<std::endl;
456 Bool_t primary = partStack->IsPhysicalPrimary(j);
457 //(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);
458 if(primary && charge !=0) {
464 fPrimaryMapInner.Fill(eta,phi);
465 fPrimaryMapOuter.Fill(eta,phi);
471 //_____________________________________________________________________
472 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::End() {
474 Bool_t retval = AliFMDInput::End();
476 AliGenEventHeader* genHeader = fLoader->GetHeader()->GenEventHeader();
478 genHeader->PrimaryVertex(vertex);
480 if(TMath::Abs(vertex.At(2)) > fZvtxCut)
483 Double_t delta = 2*fZvtxCut/fNvtxBins;
484 Double_t vertexBinDouble = (vertex.At(2) + fZvtxCut) / delta;
485 Int_t vertexBin = (Int_t)vertexBinDouble;
487 TObjArray* innerArray = (TObjArray*)fPrimaryArray.At(0);
488 TObjArray* outerArray = (TObjArray*)fPrimaryArray.At(1);
490 TH2F* hPrimaryInner = (TH2F*)innerArray->At(vertexBin);
491 TH2F* hPrimaryOuter = (TH2F*)outerArray->At(vertexBin);
493 hPrimaryInner->Add(&fPrimaryMapInner);
494 hPrimaryOuter->Add(&fPrimaryMapOuter);
497 for(UShort_t det=1;det<=3;det++) {
498 Int_t nRings = (det==1 ? 1 : 2);
499 for (UShort_t ir = 0; ir < nRings; ir++) {
500 Char_t ring = (ir == 0 ? 'I' : 'O');
501 UShort_t nsec = (ir == 0 ? 20 : 40);
502 UShort_t nstr = (ir == 0 ? 512 : 256);
504 for(UShort_t sec =0; sec < nsec; sec++) {
506 for(UShort_t strip = 0; strip < nstr; strip++) {
508 if(fHitMap.operator()(det,ring,sec,strip) > 0) {
511 AliFMDGeometry* fmdgeo = AliFMDGeometry::Instance();
512 fmdgeo->Detector2XYZ(det,ring,sec,strip,x,y,z);
514 Int_t iring = (ring == 'I' ? 0 : 1);
516 TObjArray* detArray = (TObjArray*)fHitArray.At(det);
517 TObjArray* vtxArray = (TObjArray*)detArray->At(iring);
518 TH2F* hHits = (TH2F*)vtxArray->At(vertexBin);
520 Float_t phi = TMath::ATan2(y,x);
521 if(phi<0) phi = phi+2*TMath::Pi();
522 Float_t r = TMath::Sqrt(TMath::Power(x,2)+TMath::Power(y,2));
523 Float_t theta = TMath::ATan2(r,z-vertex.At(2));
524 Float_t eta = -1*TMath::Log(TMath::Tan(0.5*theta));
525 hHits->Fill(eta,phi);
533 fPrimaryMapInner.Reset();
534 fPrimaryMapOuter.Reset();