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"),
86 if(hits_not_trackref) {
98 //_____________________________________________________________________
100 void AliFMDBackgroundCorrection::GenerateBackgroundCorrection(Bool_t from_hits,
101 const Int_t nvtxbins,
103 const Int_t nBinsEta,
107 const Char_t* filename,
111 const Char_t* infilename) {
113 //TGrid::Connect("alien:",0,0,"t");
117 //AliCDBManager::Instance()->SetDefaultStorage("alien://Folder=/alice/data/2008/LHC08d/OCDB/");
118 AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
119 AliCDBManager::Instance()->SetRun(runNo);
121 #if defined(__CINT__)
122 gSystem->Load("liblhapdf");
123 gSystem->Load("libEGPythia6");
124 gSystem->Load("libpythia6");
125 gSystem->Load("libAliPythia6");
126 gSystem->Load("libgeant321");
131 //Setting up the geometry
132 //-----------------------------------------------
133 if (AliGeomManager::GetGeometry() == NULL)
134 AliGeomManager::LoadGeometry();
136 AliFMDGeometry* geo = AliFMDGeometry::Instance();
138 geo->InitTransformations();
140 AliInfo("Processing hits and primaries ");
142 AliFMDInputBG input(from_hits);
146 input.SetVtxCutZ(zvtxcut);
147 input.SetNvtxBins(nvtxbins);
148 input.SetNbinsEta(nBinsEta);
152 AliInfo(Form("Found %d primaries and %d hits.", input.GetNprim(),input.GetNhits()));
153 TObjArray* hitArray ;
154 TObjArray* primaryArray;
156 TFile* infile = TFile::Open(infilename);
157 hitArray = new TObjArray();
158 primaryArray = new TObjArray();
160 for(Int_t det =1; det<=3;det++)
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++)
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++)
176 TH2F* hHits = (TH2F*)infile->Get(Form("hHits_FMD%d%c_vtx%d",det,ringChar,v));
179 vtxArrayHits->AddAtAndExpand(hHits,v);
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++) {
193 TH2F* hPrimary = (TH2F*)infile->Get(Form("hPrimary_FMD_%c_vtx%d",ringChar,v));
194 ringArray->AddAtAndExpand(hPrimary,v);
201 hitArray = input.GetHits();
202 primaryArray = input.GetPrimaries();
204 fCorrectionArray.SetName("FMD_bg_correction");
205 fCorrectionArray.SetOwner();
207 TList* primaryList = new TList();
208 primaryList->SetName("primaries");
210 TList* hitList = new TList();
211 hitList->SetName("hits");
212 TList* corrList = new TList();
213 corrList->SetName("corrections");
215 AliFMDAnaCalibBackgroundCorrection* background = new AliFMDAnaCalibBackgroundCorrection();
217 for(Int_t det= 1; det <=3; det++) {
218 Int_t nRings = (det==1 ? 1 : 2);
220 TObjArray* detArrayCorrection = new TObjArray();
221 detArrayCorrection->SetName(Form("FMD%d",det));
222 fCorrectionArray.AddAtAndExpand(detArrayCorrection,det);
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);
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);
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);
250 TAxis refAxis(nvtxbins,-1*zvtxcut,zvtxcut);
252 TFile* infile = TFile::Open(infilename);
253 TAxis* refaxis = (TAxis*)infile->Get("vertexbins");
254 background->SetRefAxis(refaxis);
257 else background->SetRefAxis(&refAxis);
259 TFile* fout = new TFile(filename,"RECREATE");
260 refAxis.Write("vertexbins");
263 primaryList->Write();
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);
275 AliCDBManager* cdb = AliCDBManager::Instance();
276 cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
277 AliCDBId id(AliFMDAnaParameters::GetBackgroundPath(),runNo,endRunNo);
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);
294 //_____________________________________________________________________
295 void AliFMDBackgroundCorrection::Simulate(Int_t nEvents) {
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:");
305 sim.RunSimulation(nEvents);
311 //_____________________________________________________________________
313 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessHit(AliFMDHit* h,
319 Bool_t retval = ProcessEvent(h->Detector(),
326 // std::cout<<"Length: "<<h->Length()<<std::endl;
327 // std::cout<<"Is stopped "<<h->IsStop()<<" "<<kTRUE<<std::endl;
330 //_____________________________________________________________________
332 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessTrackRef(AliTrackReference* tr, TParticle* p)
336 UShort_t det,sec,strip;
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);
347 //_____________________________________________________________________
349 AliFMDBackgroundCorrection::AliFMDInputBG::ProcessEvent(UShort_t det,
360 ((nTrack != fPrevTrack) ||
361 (det != fPrevDetector))){ ||
362 (ring != fPrevRing))){ ||
363 (sec != fPrevSec))) {*/
364 /*Float_t nstrips = (ring =='O' ? 256 : 512);
367 Float_t prevStripTrack = -1;
369 prevStripTrack = fLastTrackByStrip.operator()(det,ring,sec,strip-1);
371 Float_t nextStripTrack = -1;
372 if(strip < (nstrips - 1))
373 nextStripTrack = fLastTrackByStrip.operator()(det,ring,sec,strip+1);
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;
380 // std::cout<<"Track # "<<nTrack<<" passed the cut in "<<det<<" "<<ring<<" "<<sec<<" "<<strip<<std::endl;
381 if(charge != 0 && nTrack != thisStripTrack) {
383 fHitMap.operator()(det,ring,sec,strip) += 1;
385 Float_t nstrips = (ring =='O' ? 256 : 512);
386 fLastTrackByStrip.operator()(det,ring,sec,strip) = (Float_t)nTrack;
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;
404 //_____________________________________________________________________
405 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Init()
407 fLastTrackByStrip.Reset(-1);
409 fPrimaryArray.SetOwner();
410 fPrimaryArray.SetName("FMD_primary");
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");
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++) {
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());
431 ringArray->AddAtAndExpand(hPrimary,v);
436 fHitArray.SetOwner();
437 fHitArray.SetName("FMD_hits");
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());
455 vtxArrayHits->AddAtAndExpand(hHits,v);
465 //_____________________________________________________________________
467 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::Begin(Int_t event )
470 Bool_t retVal = AliFMDInput::Begin(event);
471 AliStack* partStack = fLoader->Stack();
472 Int_t nTracks = partStack->GetNtrack();
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());
482 if(phi<0) phi = phi+2*TMath::Pi();
483 // if(p->Theta() == 0) continue;
484 Float_t eta = p->Eta();
486 // std::cout<<-1*TMath::Log(TMath::Tan(0.5*p->Theta()))<<std::endl;
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) {
494 fPrimaryMapInner.Fill(eta,phi);
495 fPrimaryMapOuter.Fill(eta,phi);
501 //_____________________________________________________________________
502 Bool_t AliFMDBackgroundCorrection::AliFMDInputBG::End() {
504 Bool_t retval = AliFMDInput::End();
506 AliGenEventHeader* genHeader = fLoader->GetHeader()->GenEventHeader();
508 genHeader->PrimaryVertex(vertex);
510 if(TMath::Abs(vertex.At(2)) > fZvtxCut)
513 Double_t delta = 2*fZvtxCut/fNvtxBins;
514 Double_t vertexBinDouble = (vertex.At(2) + fZvtxCut) / delta;
515 Int_t vertexBin = (Int_t)vertexBinDouble;
517 TObjArray* innerArray = (TObjArray*)fPrimaryArray.At(0);
518 TObjArray* outerArray = (TObjArray*)fPrimaryArray.At(1);
520 TH2F* hPrimaryInner = (TH2F*)innerArray->At(vertexBin);
521 TH2F* hPrimaryOuter = (TH2F*)outerArray->At(vertexBin);
523 hPrimaryInner->Add(&fPrimaryMapInner);
524 hPrimaryOuter->Add(&fPrimaryMapOuter);
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);
534 for(UShort_t sec =0; sec < nsec; sec++) {
536 for(UShort_t strip = 0; strip < nstr; strip++) {
538 if(fHitMap.operator()(det,ring,sec,strip) > 0) {
539 //std::cout<<fHitMap.operator()(det,ring,sec,strip)<<std::endl;
541 AliFMDGeometry* fmdgeo = AliFMDGeometry::Instance();
542 fmdgeo->Detector2XYZ(det,ring,sec,strip,x,y,z);
544 Int_t iring = (ring == 'I' ? 0 : 1);
546 TObjArray* detArray = (TObjArray*)fHitArray.At(det);
547 TObjArray* vtxArray = (TObjArray*)detArray->At(iring);
548 TH2F* hHits = (TH2F*)vtxArray->At(vertexBin);
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));
563 fPrimaryMapInner.Reset();
564 fPrimaryMapOuter.Reset();
566 fLastTrackByStrip.Reset(-1);