]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/CalibMacros/CPass0/mergeByComponent.C
Merge branch 'master', remote branch 'origin' into TPCdev
[u/mrichter/AliRoot.git] / PWGPP / CalibMacros / CPass0 / 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
60    AliTPCcalibTimeGain::SetMergeEntriesCut(500000);
61    AliTPCcalibGainMult::SetMergeEntriesCut(500000);
62    AliTPCcalibAlign::SetMergeEntriesCut(5000000);
63    AliTPCcalibTracks::SetMergeEntriesCut(5000000);
64
65   AliFileMerger merger;
66   /* select what to merge */
67   merger.SetNoTrees(kFALSE);
68   if (component == "ALL")
69     merger.AddReject("esdFriend");
70   else
71     merger.AddAccept(component.Data());
72   /* merge */
73   merger.IterTXT(list, outputFileName.Data(), kFALSE);
74   /* notify */
75   gSystem->Exec(Form("touch %s_merge_done", component.Data()));
76   return;
77 }
78
79 //___________________________________________________________________
80 void MakeFileList(const char *searchdir, const char *pattern, const char* outputFileName="calib.list", Int_t timeOut=10)
81 {
82   gSystem->Setenv("XRDCLIENTMAXWAIT",Form("%d",timeOut));
83   gEnv->SetValue("XNet.RequestTimeout", timeOut);
84   gEnv->SetValue("XNet.ConnectTimeout", timeOut);
85   gEnv->SetValue("XNet.TransactionTimeout", timeOut);
86   TFile::SetOpenTimeout(timeOut);
87
88   TGrid::Connect("alien");
89
90   TString command;
91   command = Form("find %s %s", searchdir, pattern);
92   cerr<<"command: "<<command<<endl;
93   TGridResult *res = gGrid->Command(command);
94   if (!res) return;
95   TIter nextmap(res);
96   TMap *map = 0;
97
98   ofstream outputFile;
99   outputFile.open(Form(outputFileName));
100
101   //first identify the largest file and put it at the beginning
102   Int_t largestFileSize=0;
103   TString largestFile;
104   TObject* largestObject;
105   while((map=(TMap*)nextmap()))
106   {
107     TObjString *objs = dynamic_cast<TObjString*>(map->GetValue("turl"));
108     TObjString *objsSize = dynamic_cast<TObjString*>(map->GetValue("size"));
109     if (!objs || !objs->GetString().Length()) continue;
110     if (!objsSize || !objsSize->GetString().Length()) continue;
111
112     Int_t currentFileSize=objsSize->GetString().Atoi();
113     if (currentFileSize>largestFileSize) 
114     {
115       largestFileSize=currentFileSize;
116       largestFile=objs->GetString();
117       largestObject=map;
118     }
119   }
120   outputFile << largestFile.Data()<< endl;
121   res->Remove(largestObject);
122   
123   //then write the rest of the entries to the file
124   nextmap.Reset();
125   while((map=(TMap*)nextmap()))
126   {
127     TObjString *objs = dynamic_cast<TObjString*>(map->GetValue("turl"));
128     if (!objs || !objs->GetString().Length())
129     {
130       delete res;
131       break;
132     }
133
134     TString src=Form("%s",objs->GetString().Data());
135     outputFile << src.Data()<< endl;
136   }
137   outputFile.close();
138   return;
139 }
140
141 //___________________________________________________________________
142 void CopyCPass(const char* alienFileList="alien.list", const char* outputFileList="local.list", Int_t timeOut=10)
143 {
144   //copy all the alien files to local
145   gSystem->Setenv("XRDCLIENTMAXWAIT",Form("%d",timeOut));
146   gEnv->SetValue("XNet.RequestTimeout", timeOut);
147   gEnv->SetValue("XNet.ConnectTimeout", timeOut);
148   gEnv->SetValue("XNet.TransactionTimeout", timeOut);
149   TFile::SetOpenTimeout(timeOut);
150
151   TGrid::Connect("alien");
152
153   ifstream inputFile;
154   inputFile.open(alienFileList);
155   ofstream outputFile;
156   outputFile.open(Form(outputFileList));
157
158   if (!inputFile.is_open())
159   {
160     printf("input file %s not found! exiting...\n",alienFileList);
161     exit(1);
162   }
163
164   Int_t counter=0;
165   while( inputFile.good())
166   {
167     TString src("");
168     src.ReadLine(inputFile);
169     if (src.IsNull()) continue;
170     TString dst=src;
171     dst.ReplaceAll("alien:///","");
172     dst.ReplaceAll("/","_");
173     Bool_t result = TFile::Cp(src.Data(),dst.Data(),kTRUE);
174     AliSysInfo::AddStamp(dst.Data(),counter, result);
175     if (result) 
176     {
177       outputFile << dst.Data()<< endl;
178       counter++;
179     }
180   }
181   cout<<counter<<" files copied!"<<endl;
182
183   inputFile.close();
184   outputFile.close();
185   gSystem->Exec("touch copy_done");
186 }
187