1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
20 Realisation of an AliVEventPool based on THnSparse
24 Possible usage: three steps
25 1) Creation of a XML tag collection in aliensh
27 aliensh:> find -x charm /alice/sim/PDC_08/LHC08x/180001 tag.root > 180001.xml
29 2) Merging the tag files
31 TGrid::Connect("alien://");
32 TAlienCollection *collection = TAlienCollection::Open(xmlfile);
33 TGridResult* result = collection->GetGridResult("",0);
34 AliTagCreator *t = new AliTagCreator();
35 t->MergeTags("ESD",result);
37 3) Chain the merged tag files and test the event pool
39 void testpool(const char * dirname = ".", const char * pattern = "Run180001") {
41 gSystem->Load("libANALYSIS.so");
42 gSystem->Load("libANALYSISalice.so");
45 TChain * fChain = new TChain("T");
48 // Chain the tag files in the working directory
51 // Open the working directory
52 void * dirp = gSystem->OpenDirectory(dirname);
53 const char * name = 0x0;
54 // Add all files matching *pattern* to the chain
55 while((name = gSystem->GetDirEntry(dirp))) {
57 if (strstr(name,pattern)) {
58 fTagFilename = dirname;
62 fChain->Add(fTagFilename);
67 Int_t nruns = fChain->GetEntries();
69 cout << nruns << " run(s) found in the tag chain." << endl;
72 const char * vars[] = {"fNumberOfPositiveTracks","fNumberOfNegativeTracks","fPrimaryVertexZ"};
73 Int_t nbins[] = {10,10,10};
74 Double_t xmin[] ={-0.5,-0.5,-20};
75 Double_t xmax[] ={49/5,49.5,20};
76 Int_t chunksize = 100;
78 AliEventPoolSparse * pool =
79 new AliEventPoolSparse("test", "test", fChain, dim, vars, nbins, xmin, xmax, chunksize);
83 TChain * esdchain = 0x0;
85 while (esdchain=pool->GetNextChain()) {
86 cout << "Chain: "<< ichain <<" Events: " << esdchain->GetEntries() << endl;
96 #include "AliEventPoolSparse.h"
97 #include "AliRunTag.h"
98 #include "AliEventTag.h"
100 #include "AliRunTagCuts.h"
101 #include "AliLHCTagCuts.h"
102 #include "AliDetectorTagCuts.h"
103 #include "AliEventTagCuts.h"
105 #include <TObjArray.h>
107 #include <TTreeFormula.h>
110 #include <Riostream.h>
113 ClassImp(AliEventPoolSparse)
115 // _________________________________________________________________________
116 AliEventPoolSparse::AliEventPoolSparse() :
118 fChunkSize(1024 * 16),
130 fDetectorTagCut(0x0),
134 // Default constructor. Initializes the THnSparseI,
135 // the initial size of the array and the array itself
137 fPool = new TEntryList * [fN];
138 memset(fPool,0x0,fN*sizeof(TEntryList*));
141 // _________________________________________________________________________
142 AliEventPoolSparse::AliEventPoolSparse(const char* name, const char* title, TChain * tagchain, Int_t dim,
143 const char ** vars, const Int_t* nbins, const Double_t* xmin,
144 const Double_t* xmax, Int_t chunksize):
145 fHnSparseI(name, title, dim, nbins, xmin, xmax, chunksize),
146 fChunkSize(chunksize),
158 fDetectorTagCut(0x0),
161 // Constructor. Initializes the THnSparseI,
162 // the initial size of the pool array and the array itself
163 // It uses the provided array of variables to create TTreeFormulas
164 // that are used when the pools are filled. This is the reason to require the input
165 // tag chain in the constructor.
168 fPool = new TEntryList * [fN];
169 memset(fPool,0x0,fN*sizeof(TArrayI*));
172 fVars = new TTreeFormula*[dim];
174 for (Int_t ivar=0; ivar<dim; ++ivar) {
175 fVars[ivar] = new TTreeFormula(vars[ivar],vars[ivar],fTagChain);
181 // _________________________________________________________________________
182 AliEventPoolSparse::~AliEventPoolSparse() {
183 // Destructor. Delete the pool, the array of TTreeFormula
184 // and the pointers to cuts
185 for (Int_t i=0; i<fN; ++i) delete fPool[i];
186 if (fN>0) delete [] fPool;
188 Int_t ndim = fHnSparseI.GetNdimensions();
189 for (Int_t i=0; i<ndim; ++i) delete fVars[i];
199 delete fDetectorTagCut;
205 // Implementation of the interface functions
206 // _________________________________________________________________________
207 TChain* AliEventPoolSparse::GetNextChain(){
208 // Return the chains one by one. The output is 0x0 if the pool is not initialized
209 // or the last chain is already reached
211 AliError("The event pool is not initialized");
215 if (fCurrentBin>=fHnSparseI.GetNbins()) { // Check if >= or >
216 AliInfo("No more chains");
222 fChain->SetEntryList(fPool[fCurrentBin++],"ne");
226 // _________________________________________________________________________
227 void AliEventPoolSparse::GetCurrentBin(Float_t* xbin) {
228 // This method fills the center of the current bin in xbin
231 AliError("The event pool is not initialized");
235 Int_t ndim = fHnSparseI.GetNdimensions();
236 Int_t * coord = new Int_t[ndim];
237 fHnSparseI.GetBinContent(fCurrentBin,coord);
239 TObjArray * axes = fHnSparseI.GetListOfAxes();
240 for (Int_t i=0; i<ndim; ++i)
241 xbin[i]=((TAxis*)axes->At(i+1))->GetBinCenter(coord[i]);
246 // _________________________________________________________________________
247 void AliEventPoolSparse::Init(){
248 // Loop on the tag chain and select the events according
249 // to the Run, LHC, detector, and event cuts.
250 // Fill the THnSparse bin and add the event to the corresponding pool
251 // Taken and modified from AliAnalysisTag
254 AliError("Please provide a tag chain!");
258 Int_t ndim = fHnSparseI.GetNdimensions();
261 Double_t * x = new Double_t[ndim];
264 AliRunTag *tag = new AliRunTag;
265 AliEventTag *evTag = 0;
266 fTagChain->SetBranchAddress("AliTAG",&tag);
272 Int_t current = -1; // Current tree number
273 for(Int_t iTagFiles = 0; iTagFiles < fTagChain->GetEntries(); iTagFiles++) {
274 fTagChain->GetEntry(iTagFiles);
276 if (current != fTagChain->GetTreeNumber()) {
277 // Update the formula leaves if a new file is processed by the chain
278 // if (fRunCut) fRunCut->UpdateFormulaLeaves();
279 // if (fLHCCut) fLHCCut->UpdateFormulaLeaves();
280 // if (fDetCut) fDetCut->UpdateFormulaLeaves();
281 // if (fEvCut) fEvCut->UpdateFormulaLeaves();
283 if (fEventTagCut) fEventTagCut->InitializeTriggerClasses(tag->GetActiveTriggerClasses());
285 for (Int_t ivar=0; ivar<fHnSparseI.GetNdimensions(); ++ivar)
286 if (fVars[ivar]) fVars[ivar]->UpdateFormulaLeaves();
288 // Create the ESD/AOD chain if not done
290 // Decide if we have ESD or AOD
291 TFile * tagfile = fTagChain->GetFile();
292 if (strstr(tagfile->GetName(),"ESD")) fChain = new TChain("esdTree");
293 else if (strstr(tagfile->GetName(),"AOD")) fChain = new TChain("aodTree");
295 AliError("Only ESD and AOD type is implemented!!!");
301 // Update the tree number
302 current = fTagChain->GetTreeNumber();
305 // Deprecated use of TTreeFormulas
306 // // Apply Run, LHC, and detector cuts if they exist
307 // if(!fRunCut || fRunCut->EvalInstance(iTagFiles) == 1) {
308 // if(!fLHCCut || fLHCCut->EvalInstance(iTagFiles) == 1) {
309 // if(!fDetCut || fDetCut->EvalInstance(iTagFiles) == 1) {
312 // // Get access to the event data in the TTreeFormula
313 // if (fEvCut) fEvCut->GetNdata();
314 // for (Int_t ivar=0; ivar<fHnSparseI.GetNdimensions(); ++ivar)
315 // if (fVars[ivar]) fVars[ivar]->GetNdata();
318 // // const TClonesArray *tagList = tag->GetEventTags();
319 // Int_t iFiles = tag->GetNFiles();
320 // for (int ifs = 0; ifs<iFiles; ifs++) {
321 // AliFileTag *eftag = (AliFileTag *) tag->GetFileTag(ifs);
323 // guid = eftag->GetGUID();
324 // turl = eftag->GetTURL();
325 // path = eftag->GetPath();
327 // Int_t iEvents = eftag->GetNEvents();
328 // for(Int_t i = 0; i < iEvents; i++) {
329 // evTag = (AliEventTag *) eftag->GetEventTag(i);
332 // if(!fEvCut || fEvCut->EvalInstance(i) == 1) {
333 // TEntryList *fLocalList = new TEntryList();
334 // fLocalList->SetTreeName(fChain->GetName());
335 // fLocalList->SetFileName(turl.Data());
336 // fLocalList->Enter(i);
339 // // Add this event to the corresponding pool
341 // // Increment the bin content corrresponding to the vector "x" by "w",
342 // // and store the event index iev to the array associated with the bin,
343 // // then return the bin index.
345 // for (Int_t ivar=0; ivar<ndim; ++ivar) x[ivar] = fVars[ivar]->EvalInstance(i);
347 // Int_t bin = fHnSparseI.Fill(x);
348 // // Check if we have to enlarge the array of pointers
349 // if (bin>=fN) Set(bin+fChunkSize);
350 // // Allocate the TEntryList if this is the first use of it
351 // if (!fPool[bin]) fPool[bin] = new TEntryList();
352 // // Add the event iev to the corresponding bin
353 // fPool[bin]->Add(fLocalList);
358 // for (Int_t ipool=0; ipool<fHnSparseI.GetNbins(); ++ipool)
359 // fPool[ipool]->OptimizeStorage();
361 // // Add the current file to the ESD/AOD chain
362 // if(!path.IsNull()) fChain->AddFile(path);
363 // else if(!turl.IsNull()) fChain->AddFile(turl);
365 // }//detector tag cuts
369 // Apply Run, LHC, and detector cuts if they exist
370 if(!fRunTagCut || fRunTagCut->IsAccepted(tag)) {
371 if(!fLHCTagCut || fLHCTagCut->IsAccepted(tag->GetLHCTag())) {
372 if(!fDetectorTagCut || fDetectorTagCut->IsAccepted(tag->GetDetectorTags())) {
374 // // Get access to the event data in the TTreeFormula
375 // if (fEvCut) fEvCut->GetNdata();
376 for (Int_t ivar=0; ivar<fHnSparseI.GetNdimensions(); ++ivar)
377 if (fVars[ivar]) fVars[ivar]->GetNdata();
380 // const TClonesArray *tagList = tag->GetEventTags();
381 Int_t iFiles = tag->GetNFiles();
382 for (int ifs = 0; ifs<iFiles; ifs++) {
383 AliFileTag *eftag = (AliFileTag *) tag->GetFileTag(ifs);
385 guid = eftag->GetGUID();
386 turl = eftag->GetTURL();
387 path = eftag->GetPath();
389 Int_t iEvents = eftag->GetNEvents();
390 for(Int_t i = 0; i < iEvents; i++) {
391 evTag = (AliEventTag *) eftag->GetEventTag(i);
394 if(!fEventTagCut || fEventTagCut->IsAccepted(evTag)) {
395 TEntryList *fLocalList = new TEntryList();
396 fLocalList->SetTreeName(fChain->GetName());
397 fLocalList->SetFileName(turl.Data());
398 fLocalList->Enter(i);
401 // Add this event to the corresponding pool
403 // Increment the bin content corrresponding to the vector "x" by "w",
404 // and store the event index iev to the array associated with the bin,
405 // then return the bin index.
407 for (Int_t ivar=0; ivar<ndim; ++ivar) x[ivar] = fVars[ivar]->EvalInstance(i);
409 Int_t bin = fHnSparseI.Fill(x);
410 // Check if we have to enlarge the array of pointers
411 if (bin>=fN) Set(bin+fChunkSize);
412 // Allocate the TEntryList if this is the first use of it
413 if (!fPool[bin]) fPool[bin] = new TEntryList();
414 // Add the event iev to the corresponding bin
415 fPool[bin]->Add(fLocalList);
420 for (Int_t ipool=0; ipool<fHnSparseI.GetNbins(); ++ipool)
421 fPool[ipool]->OptimizeStorage();
423 // Add the current file to the ESD/AOD chain
424 if(!path.IsNull()) fChain->AddFile(path);
425 else if(!turl.IsNull()) fChain->AddFile(turl);
434 fCurrentBin = 0; // Initialize the current bin
437 // _________________________________________________________________________
438 void AliEventPoolSparse::SetRunCut(const char * cut){
439 // Run selection cuts
440 if (fRunCut) delete fRunCut;
441 fRunCut = new TTreeFormula("fRun",cut,fTagChain);
444 // _________________________________________________________________________
445 void AliEventPoolSparse::SetLHCCut(const char * cut){
446 // LHC selection cuts
447 if (fLHCCut) delete fLHCCut;
448 fLHCCut = new TTreeFormula("fLHC",cut,fTagChain);
451 // _________________________________________________________________________
452 void AliEventPoolSparse::SetDetCut(const char * cut){
453 // Detector selection cuts
454 if (fDetCut) delete fDetCut;
455 fDetCut = new TTreeFormula("fDet",cut,fTagChain);
458 // _________________________________________________________________________
459 void AliEventPoolSparse::SetEventCut(const char * cut){
460 // Event selection cuts
461 if (fEvCut) delete fEvCut;
462 fEvCut = new TTreeFormula("fEv",cut,fTagChain);
465 // _________________________________________________________________________
466 void AliEventPoolSparse::SetRunCut(AliRunTagCuts* cut)
470 // _________________________________________________________________________
471 void AliEventPoolSparse::SetEventCut(AliEventTagCuts* cut)
475 // _________________________________________________________________________
476 void AliEventPoolSparse::SetDetectorCut(AliDetectorTagCuts* cut)
478 fDetectorTagCut = cut;
480 // _________________________________________________________________________
481 void AliEventPoolSparse::SetLHCCut(AliLHCTagCuts* cut)
486 // _________________________________________________________________________
487 void AliEventPoolSparse::Set(Int_t n){
488 // Set size of the array of pointers to n.
489 // A new array is created, the old contents copied to the new array,
490 // then the old array is deleted.
491 // This function is taken from TArrayI
495 TEntryList **temp = fPool;
497 fPool = new TEntryList*[n];
498 if (n < fN) memcpy(fPool,temp, n*sizeof(TEntryList*));
500 memcpy(fPool,temp,fN*sizeof(TEntryList*));
501 memset(&fPool[fN],0x0,(n-fN)*sizeof(TEntryList*));
506 if (fN) delete [] temp;