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