LCOV - code coverage report
Current view: top level - ANALYSIS/ANALYSISalice - AliEventPoolSparse.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 176 0.6 %
Date: 2016-06-14 17:26:59 Functions: 1 20 5.0 %

          Line data    Source code
       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             : 
      16             : /* $Id$ */
      17             :  
      18             : 
      19             : /*
      20             :   Realisation of an AliVEventPool based on THnSparse
      21             :   Author Peter Hristov
      22             :   Peter.Hristov@cern.ch
      23             : 
      24             :   Possible usage: three steps
      25             :   1) Creation of a XML tag collection in aliensh
      26             : 
      27             :   aliensh:> find -x charm /alice/sim/PDC_08/LHC08x/180001 tag.root > 180001.xml
      28             : 
      29             :   2) Merging the tag files
      30             : 
      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);
      36             : 
      37             :   3) Chain the merged tag files and test the event pool
      38             : 
      39             : void testpool(const char * dirname = ".", const char * pattern = "Run180001") {
      40             : 
      41             :   gSystem->Load("libANALYSIS");
      42             :   gSystem->Load("libANALYSISalice");
      43             : 
      44             :   // Create a chain
      45             :   TChain * fChain = new TChain("T");
      46             : 
      47             : 
      48             :   // Chain the tag files in the working directory
      49             :   TString fTagFilename;
      50             :   
      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))) {
      56             :     cout << name << endl;
      57             :     if (strstr(name,pattern)) { 
      58             :       fTagFilename = dirname;
      59             :       fTagFilename += "/";
      60             :       fTagFilename += name;
      61             :                 
      62             :       fChain->Add(fTagFilename);  
      63             :     }//pattern check
      64             :   }//directory loop
      65             : 
      66             : 
      67             :   Int_t nruns = fChain->GetEntries();
      68             : 
      69             :   cout << nruns << " run(s) found in the tag chain." << endl;
      70             : 
      71             :   Int_t dim = 3;
      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;
      77             : 
      78             :   AliEventPoolSparse * pool = 
      79             :     new AliEventPoolSparse("test", "test", fChain, dim, vars, nbins, xmin, xmax, chunksize);
      80             : 
      81             :   pool->Init();
      82             : 
      83             :   TChain * esdchain = 0x0;
      84             :   Int_t ichain = 0;
      85             :   while (esdchain=pool->GetNextChain()) {
      86             :     cout << "Chain: "<< ichain <<" Events: " << esdchain->GetEntries() << endl;
      87             :     ichain++;
      88             :   }
      89             : 
      90             :   delete fChain;
      91             : 
      92             : }
      93             : 
      94             : */
      95             : 
      96             : #include "AliEventPoolSparse.h"
      97             : #include "AliRunTag.h"
      98             : #include "AliEventTag.h"
      99             : #include "AliLog.h"
     100             : #include "AliRunTagCuts.h"
     101             : #include "AliLHCTagCuts.h"
     102             : #include "AliDetectorTagCuts.h"
     103             : #include "AliEventTagCuts.h"
     104             : 
     105             : #include <TObjArray.h>
     106             : #include <TAxis.h>
     107             : #include <TTreeFormula.h>
     108             : #include <TChain.h>
     109             : #include <TFile.h>
     110             : #include <Riostream.h>
     111             : #include <cstring>
     112             : 
     113         170 : ClassImp(AliEventPoolSparse)
     114             : 
     115             : // _________________________________________________________________________
     116           0 : AliEventPoolSparse::AliEventPoolSparse() :
     117           0 :   fHnSparseI(),
     118           0 :   fChunkSize(1024 * 16),
     119           0 :   fN(0),
     120           0 :   fPool(0x0),
     121           0 :   fCurrentBin(-1),
     122           0 :   fTagChain(0x0),
     123           0 :   fVars(0x0),
     124           0 :   fRunCut(0x0),
     125           0 :   fLHCCut(0x0),
     126           0 :   fDetCut(0x0),
     127           0 :   fEvCut(0x0),
     128           0 :   fRunTagCut(0x0),
     129           0 :   fEventTagCut(0x0),
     130           0 :   fDetectorTagCut(0x0),
     131           0 :   fLHCTagCut(0x0),
     132           0 :   fBinNumber(0)
     133           0 : {
     134             :   // Default constructor. Initializes the THnSparseI,
     135             :   // the initial size of the array and the array itself
     136           0 :   fN = fChunkSize;
     137           0 :   fPool = new TEntryList * [fN];
     138           0 :   memset(fPool,0x0,fN*sizeof(TEntryList*));
     139           0 : }
     140             : 
     141             : // _________________________________________________________________________
     142           0 : 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           0 :   fHnSparseI(name, title, dim, nbins, xmin, xmax, chunksize),
     146           0 :   fChunkSize(chunksize),
     147           0 :   fN(0),
     148           0 :   fPool(0x0),
     149           0 :   fCurrentBin(-1),
     150           0 :   fTagChain(tagchain),
     151           0 :   fVars(0x0),
     152           0 :   fRunCut(0x0),
     153           0 :   fLHCCut(0x0),
     154           0 :   fDetCut(0x0),
     155           0 :   fEvCut(0x0),
     156           0 :   fRunTagCut(0x0),
     157           0 :   fEventTagCut(0x0),
     158           0 :   fDetectorTagCut(0x0),
     159           0 :   fLHCTagCut(0x0),
     160           0 :   fBinNumber(0){
     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.
     166             : 
     167           0 :   fN = fChunkSize;
     168           0 :   fPool = new TEntryList * [fN];
     169           0 :   memset(fPool,0x0,fN*sizeof(TArrayI*));
     170             : 
     171             :   // Pool variables
     172           0 :   fVars = new TTreeFormula*[dim];
     173             : 
     174           0 :   for (Int_t ivar=0; ivar<dim; ++ivar) {
     175           0 :     fVars[ivar] = new TTreeFormula(vars[ivar],vars[ivar],fTagChain);
     176             :   }
     177             : 
     178             : 
     179           0 : }
     180             : 
     181             : // _________________________________________________________________________
     182           0 : AliEventPoolSparse::~AliEventPoolSparse() {
     183             :   // Destructor. Delete the pool, the array of TTreeFormula
     184             :   // and the pointers to cuts
     185           0 :   for (Int_t i=0; i<fN; ++i) delete fPool[i];
     186           0 :   if (fN>0) delete [] fPool;
     187             : 
     188           0 :   Int_t ndim = fHnSparseI.GetNdimensions();
     189           0 :   for (Int_t i=0; i<ndim; ++i) delete fVars[i];
     190           0 :   delete [] fVars;
     191             : 
     192           0 :   delete fRunCut;
     193           0 :   delete fLHCCut;
     194           0 :   delete fDetCut;
     195           0 :   delete fEvCut;
     196             : 
     197           0 :   delete fRunTagCut;
     198           0 :   delete fEventTagCut;
     199           0 :   delete fDetectorTagCut;
     200           0 :   delete fLHCTagCut;
     201             : 
     202           0 : }
     203             : 
     204             : 
     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
     210           0 :   if (fCurrentBin<0) {
     211           0 :     AliError("The event pool is not initialized");
     212           0 :     return 0x0;
     213             :   }
     214             : 
     215           0 :   if (fCurrentBin>=fHnSparseI.GetNbins()) { // Check if >= or >
     216           0 :     AliInfo("No more chains");
     217           0 :     return 0x0;
     218             :   }
     219             : 
     220           0 :   fBinNumber++;
     221             :   
     222           0 :   fChain->SetEntryList(fPool[fCurrentBin++],"ne"); 
     223           0 :   return fChain;
     224           0 : }
     225             : 
     226             : // _________________________________________________________________________
     227             : void  AliEventPoolSparse::GetCurrentBin(Float_t* xbin) {
     228             :   // This method fills the center of the current bin in xbin
     229             : 
     230           0 :   if (fCurrentBin<0) {
     231           0 :     AliError("The event pool is not initialized");
     232           0 :     return;
     233             :   }
     234             : 
     235           0 :   Int_t ndim = fHnSparseI.GetNdimensions();
     236           0 :   Int_t * coord = new Int_t[ndim]; 
     237           0 :   fHnSparseI.GetBinContent(fCurrentBin,coord);
     238             : 
     239           0 :   TObjArray * axes = fHnSparseI.GetListOfAxes();
     240           0 :   for (Int_t i=0; i<ndim; ++i) 
     241           0 :     xbin[i]=((TAxis*)axes->At(i+1))->GetBinCenter(coord[i]);
     242             : 
     243           0 :   delete [] coord;
     244           0 : }
     245             : 
     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
     252             : 
     253           0 :   if (!fTagChain) {
     254           0 :     AliError("Please provide a tag chain!");
     255           0 :     return;
     256             :   }
     257             : 
     258           0 :   Int_t ndim = fHnSparseI.GetNdimensions();
     259           0 :   if (ndim<=0) return;
     260             : 
     261           0 :   Double_t * x = new Double_t[ndim];
     262             :   
     263             :   // Tag objects.
     264           0 :   AliRunTag *tag = new AliRunTag;        
     265             :   AliEventTag *evTag = 0;  
     266           0 :   fTagChain->SetBranchAddress("AliTAG",&tag);   
     267             :   
     268           0 :   TString guid("");    
     269           0 :   TString turl("");    
     270           0 :   TString path("");    
     271             :   
     272             :   Int_t current = -1;    // Current tree number
     273           0 :   for(Int_t iTagFiles = 0; iTagFiles < fTagChain->GetEntries(); iTagFiles++) {     
     274           0 :     fTagChain->GetEntry(iTagFiles);   
     275             : 
     276           0 :     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();
     282             : 
     283           0 :       if (fEventTagCut) fEventTagCut->InitializeTriggerClasses(tag->GetActiveTriggerClasses());
     284             : 
     285           0 :       for (Int_t ivar=0; ivar<fHnSparseI.GetNdimensions(); ++ivar)
     286           0 :         if (fVars[ivar]) fVars[ivar]->UpdateFormulaLeaves();
     287             : 
     288             :       // Create the ESD/AOD chain if not done
     289           0 :       if (!fChain) {
     290             :         // Decide if we have ESD or AOD
     291           0 :         TFile * tagfile = fTagChain->GetFile();
     292           0 :         if (strstr(tagfile->GetName(),"ESD")) fChain = new TChain("esdTree");
     293           0 :         else if (strstr(tagfile->GetName(),"AOD")) fChain = new TChain("aodTree");
     294             :         else {
     295           0 :           AliError("Only ESD and AOD type is implemented!!!");
     296           0 :           delete [] x;
     297           0 :           return;
     298             :         }
     299           0 :       }
     300             :       
     301             :       // Update the tree number
     302           0 :       current = fTagChain->GetTreeNumber();
     303           0 :     }
     304             :     
     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) {
     310             :          
     311             : 
     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();
     316             :          
     317             : //        // Loop on events
     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);
     322             : 
     323             : //          guid = eftag->GetGUID();          
     324             : //          turl = eftag->GetTURL();          
     325             : //          path = eftag->GetPath();          
     326             :             
     327             : //          Int_t iEvents = eftag->GetNEvents();
     328             : //          for(Int_t i = 0; i < iEvents; i++) {      
     329             : //            evTag = (AliEventTag *) eftag->GetEventTag(i);          
     330             :               
     331             :               
     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);
     337             :                 
     338             :                 
     339             : //              // Add this event to the corresponding pool
     340             : //              {
     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.
     344             :                   
     345             : //                for (Int_t ivar=0; ivar<ndim; ++ivar) x[ivar] = fVars[ivar]->EvalInstance(i);
     346             :                   
     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);
     354             : //              }
     355             : //            }
     356             : //          }//event loop        
     357             :           
     358             : //          for (Int_t ipool=0; ipool<fHnSparseI.GetNbins(); ++ipool) 
     359             : //            fPool[ipool]->OptimizeStorage();
     360             :             
     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);
     364             : //        }
     365             : //      }//detector tag cuts
     366             : //       }//lhc tag cuts
     367             : //     }//run tag cut    
     368             : 
     369             :     // Apply Run, LHC, and detector cuts if they exist
     370           0 :     if(!fRunTagCut || fRunTagCut->IsAccepted(tag)) {          
     371           0 :       if(!fLHCTagCut || fLHCTagCut->IsAccepted(tag->GetLHCTag())) {        
     372           0 :         if(!fDetectorTagCut || fDetectorTagCut->IsAccepted(tag->GetDetectorTags())) {
     373             :          
     374             : //        // Get access to the event data in the TTreeFormula
     375             : //        if (fEvCut) fEvCut->GetNdata();
     376           0 :           for (Int_t ivar=0; ivar<fHnSparseI.GetNdimensions(); ++ivar)
     377           0 :             if (fVars[ivar]) fVars[ivar]->GetNdata();
     378             :          
     379             :           // Loop on events
     380             :           //      const TClonesArray *tagList = tag->GetEventTags();
     381           0 :           Int_t iFiles = tag->GetNFiles();
     382           0 :           for (int ifs = 0; ifs<iFiles; ifs++) {
     383           0 :             AliFileTag *eftag = (AliFileTag *) tag->GetFileTag(ifs);
     384             : 
     385           0 :             guid = eftag->GetGUID();          
     386           0 :             turl = eftag->GetTURL();          
     387           0 :             path = eftag->GetPath();          
     388             :             
     389           0 :             Int_t iEvents = eftag->GetNEvents();
     390           0 :             for(Int_t i = 0; i < iEvents; i++) {      
     391           0 :               evTag = (AliEventTag *) eftag->GetEventTag(i);          
     392             :               
     393             :               
     394           0 :               if(!fEventTagCut || fEventTagCut->IsAccepted(evTag)) {
     395           0 :                 TEntryList *fLocalList = new TEntryList();
     396           0 :                 fLocalList->SetTreeName(fChain->GetName());
     397           0 :                 fLocalList->SetFileName(turl.Data());
     398           0 :                 fLocalList->Enter(i);
     399             :                 
     400             :                 
     401             :                 // Add this event to the corresponding pool
     402             :                 {
     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.
     406             :                   
     407           0 :                   for (Int_t ivar=0; ivar<ndim; ++ivar) x[ivar] = fVars[ivar]->EvalInstance(i);
     408             :                   
     409           0 :                   Int_t bin =  fHnSparseI.Fill(x);
     410             :                   // Check if we have to enlarge the array of pointers
     411           0 :                   if (bin>=fN) Set(bin+fChunkSize);
     412             :                   // Allocate the TEntryList if this is the first use of it
     413           0 :                   if (!fPool[bin]) fPool[bin] = new TEntryList();
     414             :                   // Add the event iev to the corresponding bin
     415           0 :                   fPool[bin]->Add(fLocalList);
     416             :                 }
     417           0 :               }
     418             :             }//event loop        
     419             :           
     420           0 :             for (Int_t ipool=0; ipool<fHnSparseI.GetNbins(); ++ipool) 
     421           0 :               fPool[ipool]->OptimizeStorage();
     422             :             
     423             :             // Add the current file to the ESD/AOD chain
     424           0 :             if(!path.IsNull()) fChain->AddFile(path);         
     425           0 :             else if(!turl.IsNull()) fChain->AddFile(turl);
     426             :           }
     427           0 :         }//detector tag cuts
     428             :       }//lhc tag cuts
     429             :     }//run tag cut       
     430             : 
     431             :   }//tag file loop       
     432             : 
     433           0 :   delete [] x;
     434           0 :   fCurrentBin = 0; // Initialize the current bin
     435           0 : }
     436             : 
     437             : // _________________________________________________________________________
     438             : void AliEventPoolSparse::SetRunCut(const char * cut){
     439             :   // Run selection cuts
     440           0 :   if (fRunCut) delete fRunCut;
     441           0 :   fRunCut = new TTreeFormula("fRun",cut,fTagChain);
     442           0 : }
     443             : 
     444             : // _________________________________________________________________________
     445             : void AliEventPoolSparse::SetLHCCut(const char * cut){
     446             :   // LHC selection cuts
     447           0 :   if (fLHCCut) delete fLHCCut;
     448           0 :   fLHCCut = new TTreeFormula("fLHC",cut,fTagChain);
     449           0 : }
     450             : 
     451             : // _________________________________________________________________________
     452             : void AliEventPoolSparse::SetDetCut(const char * cut){
     453             :   // Detector selection cuts
     454           0 :   if (fDetCut) delete fDetCut;
     455           0 :   fDetCut = new TTreeFormula("fDet",cut,fTagChain);
     456           0 : }
     457             : 
     458             : // _________________________________________________________________________
     459             : void AliEventPoolSparse::SetEventCut(const char * cut){
     460             :   // Event selection cuts
     461           0 :   if (fEvCut) delete fEvCut;
     462           0 :   fEvCut = new TTreeFormula("fEv",cut,fTagChain);
     463           0 : }
     464             : 
     465             : // _________________________________________________________________________
     466             : void AliEventPoolSparse::SetRunCut(AliRunTagCuts* cut)
     467             : {
     468           0 :   fRunTagCut = cut;
     469           0 : }
     470             : // _________________________________________________________________________
     471             : void AliEventPoolSparse::SetEventCut(AliEventTagCuts* cut)
     472             : {
     473           0 :   fEventTagCut = cut;
     474           0 : }
     475             : // _________________________________________________________________________
     476             : void AliEventPoolSparse::SetDetectorCut(AliDetectorTagCuts* cut)
     477             : {
     478           0 :   fDetectorTagCut = cut;
     479           0 : }
     480             : // _________________________________________________________________________
     481             : void AliEventPoolSparse::SetLHCCut(AliLHCTagCuts* cut)
     482             : {
     483           0 :   fLHCTagCut = cut;
     484           0 : }
     485             : 
     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
     492             : 
     493           0 :   if (n < 0) return;
     494           0 :   if (n != fN) {
     495           0 :     TEntryList **temp = fPool;
     496           0 :     if (n != 0) {
     497           0 :       fPool = new TEntryList*[n];
     498           0 :       if (n < fN) memcpy(fPool,temp, n*sizeof(TEntryList*));
     499             :       else {
     500           0 :         memcpy(fPool,temp,fN*sizeof(TEntryList*));
     501           0 :         memset(&fPool[fN],0x0,(n-fN)*sizeof(TEntryList*));
     502             :       }
     503             :     } else {
     504           0 :       fPool = 0x0;
     505             :     }
     506           0 :     if (fN) delete [] temp;
     507           0 :     fN = n;
     508           0 :   }
     509           0 : }

Generated by: LCOV version 1.11