]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/CalibMacros/CPass1/mergeByComponent.C
c6b8ede93fbf4e4840668cd6a434fd88cbf0f98d
[u/mrichter/AliRoot.git] / PWGPP / CalibMacros / CPass1 / mergeByComponent.C
1
2 void mergeByComponent(TString       component,
3                       const Char_t* fileList="calib.list",
4                       const Char_t* apath=0, 
5                       const Char_t* apattern=0, 
6                       Int_t         fileDownloadTimeOut=10, 
7                       const Char_t* localFileList="calib.list",
8                       const Char_t* mergedFileName="CalibObjects.root" )
9 {
10   // merging procedure
11   // component can be COPY, MAKEALIENLIST, component name or ALL
12   // ALL will merge the full file
13   // selecting components will only merge the selected top level 
14   //   objects in the file
15   // COPY only copies the the alien files from fileList then 
16   //   saves a list of local downloaded files in localFileList.
17   // MAKEALIENLIST produces a list of files on alien, uses root
18   //   to connect in case alien_xx utilities are not available 
19   //   liek on the alien nodes
20   /* load libs */
21   printf("Executing mergeByComponent.C\n");
22   gROOT->Macro("$ALICE_ROOT/PWGPP/CalibMacros/CPass0/LoadLibraries.C");
23   TH1::AddDirectory(0);
24
25   Int_t fileDownloadTimeOut=10;
26
27   /* copy only */ 
28   if (component == "COPY") {
29     if (!apath || ! apattern){
30       printf("Alien find path or pattern not specified");
31       exit(1);
32     }
33     CopyCPass(fileList, localFileList, fileDownloadTimeOut);
34     return;
35   }
36   /* make file list only */ 
37   if (component == "MAKEALIENLIST") {
38     if (!apath || ! apattern){
39       printf("Alien find path or pattern not specified");
40       exit(1);
41     }
42     MakeFileList(apath, apattern, fileList);
43     return;
44   }
45
46   /* merge component */
47   MergeCPass(fileList, component, mergedFileName);
48 }
49
50 //___________________________________________________________________
51
52 void MergeCPass(const Char_t *list, TString component, TString outputFileName="CalibObjects.root")
53 {
54   //AliTPCcalibTimeGain::SetMergeEntriesCut(2000000);
55   //AliTPCcalibGainMult::SetMergeEntriesCut(2000000);
56   //AliTPCcalibAlign::SetMergeEntriesCut(10000000);
57   //AliTPCcalibTracks::SetMergeEntriesCut(10000000);
58   //AliTPCcalibTime::SetResHistoMergeCut(10000000);
59   AliFileMerger merger;
60   /* select what to merge */
61   merger.SetNoTrees(kFALSE);
62   if (component == "ALL")
63     merger.AddReject("esdFriend");
64   else
65     merger.AddAccept(component.Data());
66   /* merge */
67   merger.IterTXT(list, outputFileName.Data(), kFALSE);
68   /* notify */
69   gSystem->Exec(Form("touch %s_merge_done", component.Data()));
70   return;
71 }
72
73 //___________________________________________________________________
74 void MakeFileList(const char *searchdir, const char *pattern, const char* outputFileName="calib.list", Int_t timeOut=10)
75 {
76   gSystem->Setenv("XRDCLIENTMAXWAIT",Form("%d",timeOut));
77   gEnv->SetValue("XNet.RequestTimeout", timeOut);
78   gEnv->SetValue("XNet.ConnectTimeout", timeOut);
79   gEnv->SetValue("XNet.TransactionTimeout", timeOut);
80   TFile::SetOpenTimeout(timeOut);
81
82   TGrid::Connect("alien");
83
84   TString command;
85   command = Form("find %s %s", searchdir, pattern);
86   cerr<<"command: "<<command<<endl;
87   TGridResult *res = gGrid->Command(command);
88   if (!res) return;
89   TIter nextmap(res);
90   TMap *map = 0;
91
92   ofstream outputFile;
93   outputFile.open(Form(outputFileName));
94
95   //first identify the largest file and put it at the beginning
96   Int_t largestFileSize=0;
97   TString largestFile;
98   TObject* largestObject;
99   while((map=(TMap*)nextmap()))
100   {
101     TObjString *objs = dynamic_cast<TObjString*>(map->GetValue("turl"));
102     TObjString *objsSize = dynamic_cast<TObjString*>(map->GetValue("size"));
103     if (!objs || !objs->GetString().Length()) continue;
104     if (!objsSize || !objsSize->GetString().Length()) continue;
105
106     Int_t currentFileSize=objsSize->GetString().Atoi();
107     if (currentFileSize>largestFileSize) 
108     {
109       largestFileSize=currentFileSize;
110       largestFile=objs->GetString();
111       largestObject=map;
112     }
113   }
114   outputFile << largestFile.Data()<< endl;
115   res->Remove(largestObject);
116   
117   //then write the rest of the entries to the file
118   nextmap.Reset();
119   while((map=(TMap*)nextmap()))
120   {
121     TObjString *objs = dynamic_cast<TObjString*>(map->GetValue("turl"));
122     if (!objs || !objs->GetString().Length())
123     {
124       delete res;
125       break;
126     }
127
128     TString src=Form("%s",objs->GetString().Data());
129     outputFile << src.Data()<< endl;
130   }
131   outputFile.close();
132   return;
133 }
134
135 //___________________________________________________________________
136 void CopyCPass(const char* alienFileList="alien.list", const char* outputFileList="local.list", Int_t timeOut=10)
137 {
138   //copy all the alien files to local
139   gSystem->Setenv("XRDCLIENTMAXWAIT",Form("%d",timeOut));
140   gEnv->SetValue("XNet.RequestTimeout", timeOut);
141   gEnv->SetValue("XNet.ConnectTimeout", timeOut);
142   gEnv->SetValue("XNet.TransactionTimeout", timeOut);
143   TFile::SetOpenTimeout(timeOut);
144
145   TGrid::Connect("alien");
146
147   ifstream inputFile;
148   inputFile.open(alienFileList);
149   ofstream outputFile;
150   outputFile.open(Form(outputFileList));
151
152   if (!inputFile.is_open())
153   {
154     printf("input file %s not found! exiting...\n",alienFileList);
155     exit(1);
156   }
157
158   Int_t counter=0;
159   while( inputFile.good())
160   {
161     TString src("");
162     src.ReadLine(inputFile);
163     if (src.IsNull()) continue;
164     TString dst=src;
165     dst.ReplaceAll("alien:///","");
166     dst.ReplaceAll("/","_");
167     Bool_t result = TFile::Cp(src.Data(),dst.Data(),kTRUE);
168     AliSysInfo::AddStamp(dst.Data(),counter, result);
169     if (result) 
170     {
171       outputFile << dst.Data()<< endl;
172       counter++;
173     }
174   }
175   cout<<counter<<" files copied!"<<endl;
176
177   inputFile.close();
178   outputFile.close();
179   gSystem->Exec("touch copy_done");
180 }
181