]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muon/CpMacroWithFilter.C
Custom Calib Task for PMD: sjena
[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)
50     {
51       std::cerr << "Could not get filter ??? Filename does not look right !!!" << std::endl;
52       return -2;
53     }
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     }
62
63     alirootPath += "/";
64     alirootPath += alirootVersion;
65
66     // check the aliroot version required actually exists on cvmfs...
67     if ( gSystem->AccessPathName(alirootPath.Data()) ) 
68     {
69         std::cerr << "Requested AliRoot version not found (looking into " << alirootPath.Data() << ")" << std::endl;
70         return -2;
71     }
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()) )
77     {
78       std::cerr << "Could not find requested filter macro (looking into " << filterMacroFullPath.Data() << ")" << std::endl;
79       return -3;
80     }
81
82     // check that the companion macro (to load the relevant libraries 
83     // and set the additional include paths, if needed) exists
84
85     filterRootLogonFullPath.Form("%s/PWG/muon/%s_rootlogon.C",alirootPath.Data(),filterName.Data());
86
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()));
117         
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());
122         
123     std::cout << gSystem->GetIncludePath() << std::endl;
124         
125     // finally delegate the work to the required filter
126       
127     std::cout << Form("Will compile filter %s+(\"%s\",\"%s\")",filterMacroFullPath.Data(),from.Data(),to.Data()) << std::endl;
128
129     Int_t fail = gROOT->LoadMacro(Form("%s.C+",filterName.Data()));
130       
131     if ( fail )
132     {
133       std::cout << Form("Failed to load/compile macro %s+",filterMacroFullPath.Data()) << std::endl;
134       return -6;
135     }
136         
137     std::cout << Form("Will execute filter %s(\"%s\",\"%s\")",filterName.Data(),from.Data(),to.Data()) << std::endl;
138
139     return (Int_t)gROOT->ProcessLine(Form("%s(\"%s\",\"%s\")",filterName.Data(),from.Data(),to.Data()));
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 }