Fix bug in reading ADC values in Digits2SDigits
[u/mrichter/AliRoot.git] / JETAN / AliFastJetFinder.cxx
CommitLineData
a17e6965 1/**************************************************************************
2 * Copyright(c) 1998-1999, 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
392c9b47 16
a17e6965 17//---------------------------------------------------------------------
392c9b47 18// FastJet v2.3.4 finder algorithm interface
8838ab7a 19// Last modification: Neutral cell energy included in the jet reconstruction
20//
21// Authors: Rafael.Diaz.Valdes@cern.ch
22// Magali.estienne@subatech.in2p3.fr (neutral part + bg subtraction option)
392c9b47 23//
a17e6965 24//---------------------------------------------------------------------
25
8838ab7a 26
a17e6965 27#include <Riostream.h>
28#include <TLorentzVector.h>
29#include <TFile.h>
30#include <TH1F.h>
31#include <TH2F.h>
32#include <TArrayF.h>
a17e6965 33#include <TClonesArray.h>
34
35#include "AliFastJetFinder.h"
8838ab7a 36#include "AliFastJetHeaderV1.h"
a17e6965 37#include "AliJetReaderHeader.h"
38#include "AliJetReader.h"
8838ab7a 39#include "AliJetUnitArray.h"
d1993270 40#include "AliFastJetInput.h"
41#include "AliJetBkg.h"
42#include "AliAODPWG4JetEventBackground.h"
392c9b47 43
44#include "fastjet/PseudoJet.hh"
45#include "fastjet/ClusterSequenceArea.hh"
46#include "fastjet/AreaDefinition.hh"
47#include "fastjet/JetDefinition.hh"
48// get info on how fastjet was configured
49#include "fastjet/config.h"
50
51#ifdef ENABLE_PLUGIN_SISCONE
52#include "fastjet/SISConePlugin.hh"
53#endif
54
55#include<sstream> // needed for internal io
56#include<vector>
57#include <cmath>
58
59using namespace std;
a17e6965 60
61
9157b9c9 62ClassImp(AliFastJetFinder)
8838ab7a 63
64
392c9b47 65//____________________________________________________________________________
a17e6965 66
67AliFastJetFinder::AliFastJetFinder():
392c9b47 68 AliJetFinder()
a17e6965 69{
70 // Constructor
71}
72
392c9b47 73//____________________________________________________________________________
a17e6965 74
75AliFastJetFinder::~AliFastJetFinder()
a17e6965 76{
77 // destructor
78}
79
392c9b47 80//______________________________________________________________________________
a17e6965 81void AliFastJetFinder::FindJets()
a17e6965 82{
d1993270 83 cout<<"----------in AliFastJetFinder::FindJets() ------------------"<<endl;
392c9b47 84 //pick up fastjet header
8838ab7a 85 AliFastJetHeaderV1 *header = (AliFastJetHeaderV1*)fHeader;
86 Bool_t debug = header->GetDebug(); // debug option
87 Bool_t bgMode = header->GetBGMode(); // choose to subtract BG or not
392c9b47 88
89 // check if we are reading AOD jets
90 TRefArray *refs = 0;
91 Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
92 if (fromAod) { refs = fReader->GetReferences(); }
93
94 // RUN ALGORITHM
95 // read input particles -----------------------------
d1993270 96
97 vector<fastjet::PseudoJet> inputParticles=fInputFJ->GetInputParticles();
98
99
392c9b47 100 // create an object that represents your choice of jet algorithm, and
101 // the associated parameters
3dda898d 102 double rParam = header->GetRparam();
392c9b47 103 fastjet::Strategy strategy = header->GetStrategy();
3dda898d 104 fastjet::RecombinationScheme recombScheme = header->GetRecombScheme();
392c9b47 105 fastjet::JetAlgorithm algorithm = header->GetAlgorithm();
d1993270 106 fastjet::JetDefinition jetDef(algorithm, rParam, recombScheme, strategy);
8838ab7a 107
392c9b47 108 // create an object that specifies how we to define the area
3dda898d 109 fastjet::AreaDefinition areaDef;
110 double ghostEtamax = header->GetGhostEtaMax();
111 double ghostArea = header->GetGhostArea();
112 int activeAreaRepeats = header->GetActiveAreaRepeats();
392c9b47 113
114 // now create the object that holds info about ghosts
d1993270 115 fastjet::GhostedAreaSpec ghostSpec(ghostEtamax, activeAreaRepeats, ghostArea);
392c9b47 116 // and from that get an area definition
3dda898d 117 fastjet::AreaType areaType = header->GetAreaType();
d1993270 118 areaDef = fastjet::AreaDefinition(areaType,ghostSpec);
392c9b47 119
8838ab7a 120 if(bgMode) // BG subtraction
121 {
122 //***************************** JETS FINDING AND EXTRACTION
123 // run the jet clustering with the above jet definition
d1993270 124 fastjet::ClusterSequenceArea clust_seq(inputParticles, jetDef, areaDef);
392c9b47 125
8838ab7a 126 // save a comment in the header
127
128 TString comment = "Running FastJet algorithm with the following setup. ";
129 comment+= "Jet definition: ";
d1993270 130 comment+= TString(jetDef.description());
8838ab7a 131 comment+= ". Area definition: ";
3dda898d 132 comment+= TString(areaDef.description());
8838ab7a 133 comment+= ". Strategy adopted by FastJet: ";
134 comment+= TString(clust_seq.strategy_string());
135 header->SetComment(comment);
136 if(debug){
137 cout << "--------------------------------------------------------" << endl;
138 cout << comment << endl;
139 cout << "--------------------------------------------------------" << endl;
140 }
141 //header->PrintParameters();
142
143
144 // extract the inclusive jets with pt > ptmin, sorted by pt
145 double ptmin = header->GetPtMin();
3dda898d 146 vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets(ptmin);
8838ab7a 147
148 //cout << "Number of unclustered particles: " << clust_seq.unclustered_particles().size() << endl;
149
150
151 //subtract background // ===========================================
152 // set the rapididty , phi range within which to study the background
3dda898d 153 double rapMax = header->GetRapMax();
154 double rapMin = header->GetRapMin();
155 double phiMax = header->GetPhiMax();
156 double phiMin = header->GetPhiMin();
157 fastjet::RangeDefinition range(rapMin, rapMax, phiMin, phiMax);
8838ab7a 158
159 // subtract background
3dda898d 160 vector<fastjet::PseudoJet> subJets = clust_seq.subtracted_jets(range,ptmin);
8838ab7a 161
162 // print out
163 //cout << "Printing inclusive sub jets with pt > "<< ptmin<<" GeV\n";
164 //cout << "---------------------------------------\n";
165 //cout << endl;
166 //printf(" ijet rap phi Pt area +- err\n");
167
168 // sort jets into increasing pt
3dda898d 169 vector<fastjet::PseudoJet> jets = sorted_by_pt(subJets);
8838ab7a 170 for (size_t j = 0; j < jets.size(); j++) { // loop for jets
171
172 double area = clust_seq.area(jets[j]);
3dda898d 173 double areaError = clust_seq.area_error(jets[j]);
8838ab7a 174
3dda898d 175 printf("Jet found %5d %9.5f %8.5f %10.3f %8.3f +- %6.3f\n", (Int_t)j,jets[j].rap(),jets[j].phi(),jets[j].perp(), area, areaError);
392c9b47 176
177 // go to write AOD info
8838ab7a 178 AliAODJet aodjet (jets[j].px(), jets[j].py(), jets[j].pz(), jets[j].E());
179 //cout << "Printing jet " << endl;
180 if(debug) aodjet.Print("");
181 //cout << "Adding jet ... " ;
182 AddJet(aodjet);
183 //cout << "added \n" << endl;
184
185 }
186 }
187 else { // No BG subtraction
d1993270 188
189 TClonesArray* fUnit = fReader->GetUnitArray();
190 if(fUnit == 0) { cout << "Could not get the momentum array" << endl; return; }
191 Int_t nIn = fUnit->GetEntries();
192 cout<<"===== check Unit Array in AliFastJetFinder ========="<<endl;
193 Int_t ppp=0;
194 for(Int_t ii=0; ii<nIn; ii++)
195 {
196 AliJetUnitArray *uArray = (AliJetUnitArray*)fUnit->At(ii);
197 if(uArray->GetUnitEnergy()>0.){
198 Float_t eta = uArray->GetUnitEta();
199 Float_t phi = uArray->GetUnitPhi();
200 cout<<"ipart = "<<ppp<<" eta="<<eta<<" phi="<<phi<<endl;
201 ppp++;
202 }
203 }
392c9b47 204
d1993270 205 //fastjet::ClusterSequence clust_seq(inputParticles, jetDef);
206 fastjet::ClusterSequenceArea clust_seq(inputParticles, jetDef, areaDef);
207
8838ab7a 208 // save a comment in the header
209
210 TString comment = "Running FastJet algorithm with the following setup. ";
211 comment+= "Jet definition: ";
d1993270 212 comment+= TString(jetDef.description());
8838ab7a 213 comment+= ". Strategy adopted by FastJet: ";
214 comment+= TString(clust_seq.strategy_string());
215 header->SetComment(comment);
216 if(debug){
217 cout << "--------------------------------------------------------" << endl;
218 cout << comment << endl;
219 cout << "--------------------------------------------------------" << endl;
220 }
221 //header->PrintParameters();
222
223 // extract the inclusive jets with pt > ptmin, sorted by pt
224 double ptmin = header->GetPtMin();
3dda898d 225 vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets(ptmin);
8838ab7a 226
227 //cout << "Number of unclustered particles: " << clust_seq.unclustered_particles().size() << endl;
392c9b47 228
3dda898d 229 vector<fastjet::PseudoJet> jets = sorted_by_pt(inclusiveJets); // Added by me
8838ab7a 230 for (size_t j = 0; j < jets.size(); j++) { // loop for jets // Added by me
231
232 printf("Jet found %5d %9.5f %8.5f %10.3f \n",(Int_t)j,jets[j].rap(),jets[j].phi(),jets[j].perp());
d1993270 233
234 vector<fastjet::PseudoJet> constituents = clust_seq.constituents(jets[j]);
235 int nCon= constituents.size();
236 TArrayI ind(nCon);
237 Double_t area=clust_seq.area(jets[j]);
238 cout<<"area = "<<area<<endl;
8838ab7a 239 // go to write AOD info
240 AliAODJet aodjet (jets[j].px(), jets[j].py(), jets[j].pz(), jets[j].E());
d1993270 241 aodjet.SetEffArea(area,0);
8838ab7a 242 //cout << "Printing jet " << endl;
243 if(debug) aodjet.Print("");
d1993270 244 // cout << "Adding jet ... " <<j<<endl;
245 for (int i=0; i < nCon; i++)
246 {
247 fastjet::PseudoJet mPart=constituents[i];
248 ind[i]=mPart.user_index();
249 //cout<<i<<" index="<<ind[i]<<endl;
250
251 //internal oop over all the unit cells
252 Int_t ipart = 0;
253 for(Int_t ii=0; ii<nIn; ii++)
254 {
255 AliJetUnitArray *uArray = (AliJetUnitArray*)fUnit->At(ii);
256 if(uArray->GetUnitEnergy()>0.){
257 uArray->SetUnitTrackID(ipart);//used to have the index available in AliJEtBkg
258 if(ipart==ind[i]){
259 aodjet.AddTrack(uArray);
260 }
261 ipart++;
262 }
263 }
264 }
265
8838ab7a 266 AddJet(aodjet);
267 //cout << "added \n" << endl;
d1993270 268
269
270 ///----> set in the aod the reference to the unit cells
271 // in FastJetFinder: 1) loop over the unit array. 2) select those unit cells belonging to the jet (via user_index). 3) use AliAODJet->AddTrack(unitRef)
272 // in AliJetBkg: 1) loop over the unit arrays --> get ind of the unit cell 2) internal loop on jet unit cells; 3) check if i_cell = UID of the trackRefs of the AODJet
273 // should work hopefully
274
275
8838ab7a 276
277 } // end loop for jets
278 }
279
a17e6965 280}
281
392c9b47 282//____________________________________________________________________________
283void AliFastJetFinder::RunTest(const char* datafile)
a17e6965 284
a17e6965 285{
a17e6965 286
392c9b47 287 // This simple test run the kt algorithm for an ascii file testdata.dat
288 // read input particles -----------------------------
3dda898d 289 vector<fastjet::PseudoJet> inputParticles;
392c9b47 290 Float_t px,py,pz,en;
291 ifstream in;
292 Int_t nlines = 0;
293 // we assume a file basic.dat in the current directory
294 // this file has 3 columns of float data
295 in.open(datafile);
296 while (1) {
297 in >> px >> py >> pz >> en;
298 if (!in.good()) break;
299 //printf("px=%8f, py=%8f, pz=%8fn",px,py,pz);
300 nlines++;
3dda898d 301 inputParticles.push_back(fastjet::PseudoJet(px,py,pz,en));
a17e6965 302 }
392c9b47 303 //printf(" found %d pointsn",nlines);
304 in.close();
305 //////////////////////////////////////////////////
306
307 // create an object that represents your choice of jet algorithm, and
308 // the associated parameters
3dda898d 309 double rParam = 1.0;
392c9b47 310 fastjet::Strategy strategy = fastjet::Best;
3dda898d 311 fastjet::RecombinationScheme recombScheme = fastjet::BIpt_scheme;
d1993270 312 fastjet::JetDefinition jetDef(fastjet::kt_algorithm, rParam, recombScheme, strategy);
392c9b47 313
314
315
316 // create an object that specifies how we to define the area
3dda898d 317 fastjet::AreaDefinition areaDef;
318 double ghostEtamax = 7.0;
319 double ghostArea = 0.05;
320 int activeAreaRepeats = 1;
392c9b47 321
322
323 // now create the object that holds info about ghosts
d1993270 324 fastjet::GhostedAreaSpec ghostSpec(ghostEtamax, activeAreaRepeats, ghostArea);
392c9b47 325 // and from that get an area definition
d1993270 326 areaDef = fastjet::AreaDefinition(fastjet::active_area,ghostSpec);
392c9b47 327
328
329 // run the jet clustering with the above jet definition
d1993270 330 fastjet::ClusterSequenceArea clust_seq(inputParticles, jetDef, areaDef);
392c9b47 331
332
333 // tell the user what was done
334 cout << "--------------------------------------------------------" << endl;
d1993270 335 cout << "Jet definition was: " << jetDef.description() << endl;
3dda898d 336 cout << "Area definition was: " << areaDef.description() << endl;
392c9b47 337 cout << "Strategy adopted by FastJet was "<< clust_seq.strategy_string()<<endl<<endl;
338 cout << "--------------------------------------------------------" << endl;
339
340
341 // extract the inclusive jets with pt > 5 GeV, sorted by pt
342 double ptmin = 5.0;
3dda898d 343 vector<fastjet::PseudoJet> inclusiveJets = clust_seq.inclusive_jets(ptmin);
392c9b47 344
345 cout << "Number of unclustered particles: " << clust_seq.unclustered_particles().size() << endl;
346
347
348 //subtract background // ===========================================
349 // set the rapididty range within which to study the background
3dda898d 350 double rapMax = ghostEtamax - rParam;
351 fastjet::RangeDefinition range(rapMax);
392c9b47 352 // subtract background
3dda898d 353 vector<fastjet::PseudoJet> subJets = clust_seq.subtracted_jets(range,ptmin);
392c9b47 354
355 // print them out //================================================
356 cout << "Printing inclusive jets after background subtraction \n";
357 cout << "------------------------------------------------------\n";
358 // sort jets into increasing pt
3dda898d 359 vector<fastjet::PseudoJet> jets = sorted_by_pt(subJets);
392c9b47 360
361 printf(" ijet rap phi Pt area +- err\n");
362 for (size_t j = 0; j < jets.size(); j++) {
363
364 double area = clust_seq.area(jets[j]);
3dda898d 365 double areaError = clust_seq.area_error(jets[j]);
392c9b47 366
8838ab7a 367 printf("%5d %9.5f %8.5f %10.3f %8.3f +- %6.3f\n",(Int_t)j,jets[j].rap(),
3dda898d 368 jets[j].phi(),jets[j].perp(), area, areaError);
392c9b47 369 }
370 cout << endl;
371 // ================================================================
a17e6965 372
392c9b47 373
374
a17e6965 375}
376
392c9b47 377//____________________________________________________________________________
a17e6965 378
379void AliFastJetFinder::WriteJHeaderToFile()
380{
a17e6965 381 fHeader->Write();
382}
383
9157b9c9 384//____________________________________________________________________________
8838ab7a 385
386Float_t AliFastJetFinder::EtaToTheta(Float_t arg)
387{
388 // return (180./TMath::Pi())*2.*atan(exp(-arg));
389 return 2.*atan(exp(-arg));
390
391
392}
393
394//____________________________________________________________________________
395
396void AliFastJetFinder::InitTask(TChain *tree)
397{
398
399 printf("Fast jet finder initialization ******************");
400 fReader->CreateTasks(tree);
401
402}
d1993270 403
404Bool_t AliFastJetFinder::ProcessEvent2()
405{
406 //
407 // Process one event
408 // Charged only or charged+neutral jets
409 //
410
411 TRefArray* ref = new TRefArray();
412 Bool_t procid = kFALSE;
413 Bool_t ok = fReader->ExecTasks(procid,ref);
414
415 // Delete reference pointer
416 if (!ok) {delete ref; return kFALSE;}
417
418 // Leading particles
419 fInputFJ->SetHeader(fHeader);
420 fInputFJ->SetReader(fReader);
421 fInputFJ->FillInput();
422
423 // Jets
424 FindJets();
425
426 fJetBkg->SetHeader(fHeader);
427 fJetBkg->SetReader(fReader);
428 fJetBkg->SetFastJetInput(fInputFJ);
429 Double_t bkg1=fJetBkg->BkgFastJet();
430 Double_t bkg2=fJetBkg->BkgChargedFastJet();
431 Double_t bkg3=fJetBkg->BkgFastJetCone(fAODjets);
432 Double_t bkg4=fJetBkg->BkgRemoveJetLeading(fAODjets);
433
434 fAODEvBkg->SetBackground(0,bkg1);
435 fAODEvBkg->SetBackground(1,bkg2);
436 fAODEvBkg->SetBackground(2,bkg3);
437 fAODEvBkg->SetBackground(3,bkg4);
438
439 Int_t nEntRef = ref->GetEntries();
440
441 for(Int_t i=0; i<nEntRef; i++)
442 {
443 // Reset the UnitArray content which were referenced
444 ((AliJetUnitArray*)ref->At(i))->SetUnitTrackID(0);
445 ((AliJetUnitArray*)ref->At(i))->SetUnitEnergy(0.);
446 ((AliJetUnitArray*)ref->At(i))->SetUnitCutFlag(kPtSmaller);
447 ((AliJetUnitArray*)ref->At(i))->SetUnitCutFlag2(kPtSmaller);
448 ((AliJetUnitArray*)ref->At(i))->SetUnitSignalFlag(kBad);
449 ((AliJetUnitArray*)ref->At(i))->SetUnitSignalFlagC(kTRUE,kBad);
450 ((AliJetUnitArray*)ref->At(i))->SetUnitDetectorFlag(kTpc);
451 ((AliJetUnitArray*)ref->At(i))->SetUnitFlag(kOutJet);
452 ((AliJetUnitArray*)ref->At(i))->ClearUnitTrackRef();
453
454 // Reset process ID
455 AliJetUnitArray* uA = (AliJetUnitArray*)ref->At(i);
456 uA->ResetBit(kIsReferenced);
457 uA->SetUniqueID(0);
458 }
459
460 // Delete the reference pointer
461 ref->Delete();
462 delete ref;
463
464 Reset();
465
466 return kTRUE;
467}