]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muon/CpMacroWithFilter.C
ATO-98 Possibility to scale ExBBShpae correction - needed for fitting and test purposes
[u/mrichter/AliRoot.git] / PWG / muon / CpMacroWithFilter.C
1 //
2 //  CpMacroWithFilter.C
3 //
4 //  Created by Laurent Aphecetche
5 //
6
7 #if !defined(__CINT__) || defined(__MAKECINT__)
8
9 #include "Riostream.h"
10 #include "TString.h"
11 #include "TSystem.h"
12 #include "TROOT.h"
13 #include "TGrid.h"
14 #include "TFile.h"
15 #include "TObjString.h"
16
17 #endif
18
19 Int_t CpMacroWithFilter(TString from, TString to)
20 {
21   /// Copy one file from url "from" into url "to".
22   ///
23   /// If from contains one of the filter keywords
24   /// (in the form of e.g. AliAOD.FILTER_ZZZZ_WITH_ALIROOT_YYYY.root)
25   /// then we call the external filter macro
26   ///
27   /// otherwise we do a simple TFile::Cp(from,to)
28   ///
29
30   const char* swBaseDir = "/cvmfs/alice.cern.ch/x86_64-2.6-gnu-4.1.2/Packages/AliRoot";
31   
32   if (from.IsNull() || to.IsNull()) return -1;
33   
34   std::cout << Form("Entering CpMacroWithFilter(\"%s\",\"%s\")",from.Data(),to.Data()) << std::endl;
35   
36   TString filterName;
37   TString alirootVersion;
38   TString alirootPath(swBaseDir);
39   TString filterMacroFullPath;
40   TString filterRootLogonFullPath;
41
42   Int_t ix = from.Index("FILTER_");
43   
44   if ( ix > 0)
45   {
46     TString part = from(ix,from.Length()-ix+1);
47     TObjArray* tmp = part.Tokenize(".");
48     TObjString* ostr = static_cast<TObjString*>(tmp->First());
49     if (!ostr) break;
50     filterName = ostr->String();
51     delete tmp;
52     ix = filterName.Index("_WITH_");
53     if ( ix > 0 )
54     {
55       alirootVersion = filterName(ix+strlen("_WITH_ALIROOT_"),part.Length()-ix-strlen("_WITH_ALIROOT_"));
56       filterName = filterName(0,ix);
57     }
58
59     alirootPath += "/";
60     alirootPath += alirootVersion;
61
62     // check the aliroot version required actually exists on cvmfs...
63     if ( gSystem->AccessPathName(alirootPath.Data()) ) 
64     {
65         std::cerr << "Requested AliRoot version not found (looking into " << alirootPath.Data() << ")" << std::endl;
66         return -2;
67     }
68
69     // check the filter required actually exists
70     filterMacroFullPath.Form("%s/PWG/muon/%s.C",alirootPath.Data(),filterName.Data());
71    
72     if ( gSystem->AccessPathName(filterMacroFullPath.Data()) )
73     {
74       std::cerr << "Could not find requested filter macro (looking into " << filterMacroFullPath.Data() << ")" << std::endl;
75       return -3;
76     }
77
78     // check that the companion macro (to load the relevant libraries 
79     // and set the additional include paths, if needed) exists
80
81     filterRootLogonFullPath.Form("%s/PWG/muon/%s_rootlogon.C",alirootPath.Data(),filterName.Data());
82
83     if ( gSystem->AccessPathName(filterRootLogonFullPath.Data()) )
84     {
85       std::cerr << "Could not find requested filter companion macro (looking into " << filterRootLogonFullPath.Data() << ")" << std::endl;
86       return -4;
87     }
88
89     from.ReplaceAll(filterName.Data(),"");
90     from.ReplaceAll("_WITH_ALIROOT_","");
91     from.ReplaceAll(alirootVersion,"");
92     from.ReplaceAll("..",".");
93
94     std::cout << "Will filter file=" << from.Data() << std::endl;
95   }
96   
97   if (from.Contains("alien://")) TGrid::Connect("alien://");
98
99   if (!gGrid)
100     {
101       std::cerr << "Cannot get gGrid !" << std::endl;
102       return -5;
103     }
104
105   if ( !filterName.IsNull() ) 
106   {
107     // most probably the filter will require AliRoot libs, so add the dynamic path here
108     // as well as the include path and the macro path.
109     //
110     gSystem->AddDynamicPath(Form("%s/lib/tgt_%s",alirootPath.Data(),gSystem->GetBuildArch()));
111     gSystem->SetIncludePath(Form("-I%s/include -I%s/PWG/muon",alirootPath.Data(),alirootPath.Data()));
112     gROOT->SetMacroPath(Form("%s:%s/PWG/muon",gROOT->GetMacroPath(),alirootPath.Data()));
113         
114     // execute the companion macro
115     std::cout << Form("Will load companion macro %s(\"%s\",\"%s\")",filterRootLogonFullPath.Data(),from.Data(),to.Data()) << std::endl;
116
117     gROOT->Macro(filterRootLogonFullPath.Data());
118         
119     std::cout << gSystem->GetIncludePath() << std::endl;
120         
121     // finally delegate the work to the required filter
122       
123     std::cout << Form("Will compile filter %s+(\"%s\",\"%s\")",filterMacroFullPath.Data(),from.Data(),to.Data()) << std::endl;
124
125     Int_t fail = gROOT->LoadMacro(Form("%s.C+",filterName.Data()));
126       
127     if ( fail )
128     {
129       std::cout << Form("Failed to load/compile macro %s+",filterMacroFullPath.Data()) << std::endl;
130       return -6;
131     }
132         
133     std::cout << Form("Will execute filter %s(\"%s\",\"%s\")",filterName.Data(),from.Data(),to.Data()) << std::endl;
134
135     return (Int_t)gROOT->ProcessLine(Form("%s(\"%s\",\"%s\")",filterName.Data(),from.Data(),to.Data()));
136   }
137   else
138   {
139     // "normal" case of a simple copy
140     //
141     // ! operator since TFile::Cp returns kTRUE(1) in case of success
142     std::cout << "Performing a simple TFile::Cp" << std::endl;
143     return (!TFile::Cp(from.Data(),to.Data()));
144   }
145   
146 }