some more updates
[u/mrichter/AliRoot.git] / PWGPP / macros / rawmerge.C
CommitLineData
a65a7e70 1//
2// Macro to create the "raw" data file with selected events
a65a7e70 3
983b8aef 4void rawmerge( TString inputFileName="wn.xml",
5 TString fullEventListFileName="event.list",
6 TString outputFileNameTMP="filtered.root")
a65a7e70 7{
983b8aef 8 // Create the filtered raw data files using a file with list of raw chunks with event numbers.
9 // inputFileName - either the text file with chunkname+event number+triggerType or
10 // xml collection
11 // fullEventListFileName - if 1st arg is an xml collection, this is the full list of
12 // chunks and events to be filtered acoording the xml contents
13 //
14 // format of the event list: /path/to/chunk eventNumber offlineTriggerType
15 //
16 // e.g. /alice/data/2013/LHC13b/000195390/raw/13000195390000.10.root 218 V0s
17 // /alice/data/2013/LHC13b/000195390/raw/13000195390000.10.root 219 highPt
18 // /alice/data/2013/LHC13b/000195390/raw/13000195390000.10.root 246 V0s
19 //
20 // triggerType can be: highPt, V0s, CosmicPairs, Laser
21 //
22 //
23 // if the file list is an xml collection (for running on alien),
24 // first extract the available chunks and event numbers from the
25 // reference file, and use that as input
26 if (inputFileName.Contains(".xml"))
27 {
28 TString tmp=inputFileName+".list";
29 makeAlienInputEventList(tmp,fullEventListFileName,inputFileName);
30 inputFileName=tmp;
31 }
32
33 TGrid * alien = TGrid::Connect("alien://",0,0,"t");
34
35 Int_t eventNumber;
36 ifstream files;
37 files.open(inputFileName.Data());
38 if (!files.is_open())
39 {
40 fprintf(stderr,"error: could not read event list file \"%s\". Exiting.\n",inputFileName.Data());
41 return;
42 }
43
44 //TSeqCollection* listOfFiles = (TSeqCollection*)gROOT->GetListOfFiles();
45 TList* listOfFiles = new TList();
46 TString outputFileName;
47 TString line;
48 TString iURI;
49 TString triggerType;
50 TString iURIold;
51 TFile *ifile=0;
52 TFile *ofile=0;
53 TTree *itree=0;
54 TTree *otree=0;
55 Long64_t ievent=0;
56 Int_t ofilenumber=0;
57 Int_t lineNumber=0;
58 Int_t eventold=0;
59
60 while (files.good())
61 {
62 ++lineNumber;
63 //read the line, do some checks
64 line.ReadLine(files);
65 TObjArray* entries = line.Tokenize(" ");
66 TObjString* iURIobjstr = (TObjString*)entries->At(0);
67 iURI.Clear();
68 if (iURIobjstr) iURI=iURIobjstr->String();
69 TObjString* ieventobjstr = (TObjString*)entries->At(1);
70 if (ieventobjstr) ievent = ieventobjstr->String().Atoi();
71 if (iURI.IsNull() || !ieventobjstr)
72 {
73 printf("malformed line: %s, skipping...\n",line.Data());
74 continue;
75 }
76 TObjString* triggerTypeobjstr = (TObjString*)entries->At(2);
77 triggerType.Clear();
78 if (triggerTypeobjstr) triggerType=triggerTypeobjstr->String();
79
80 printf("> processing \"%s\" event %d trigger \"%s\"...\n",iURI.Data(),ievent,triggerType.Data());
81 if (ievent==eventold && iURI==iURIold)
82 {
83 printf("duplicated continue\n");
84 continue;
85 }
86 //
87 if (!iURIold.Contains(iURI.Data()))
88 {
89 //if a new file - open it
90 printf("new file: %s\n",iURI.Data());
91 delete ifile;
92 ifile=0;
93 ifile=TFile::Open(iURI.Data());
94 if (!ifile)
95 {
96 fprintf(stderr,"warning: could not open file for event \"%s\", skipping it...\n",iURI.Data());
97 continue;
98 }
99 }
100 else
101 {
102 //if same file, reuse it
103 printf("using already open file: %s\n",iURI.Data());
a65a7e70 104 }
105 iURIold=iURI;
983b8aef 106 //
107 TTree *itree=dynamic_cast<TTree*>(ifile->Get("RAW"));
108 if (!itree)
109 {
110 fprintf(stderr,"warning: could not find RAW tree for event \"%s\", skipping it...\n",iURI.Data());
111 continue;
112 }
a65a7e70 113
983b8aef 114 // manage output files
115 if (!triggerType.IsNull()) triggerType.Prepend("_");
116 outputFileName=outputFileNameTMP;
117 outputFileName.ReplaceAll(".root","");
118 outputFileName+=triggerType;
119 outputFileName+=".root";
120
121 ofile=dynamic_cast<TFile*>(listOfFiles->FindObject(outputFileName.Data()));
122 if (!ofile)
123 {
124 printf("< creating output file \"%s\"\n",outputFileName.Data());
125 ofile=TFile::Open(outputFileName,"recreate");
126 if (!ofile)
127 {
128 fprintf(stderr,"error: could not create output file: \"%s\" Exiting.\n",outputFileName.Data());
129 return;
130 }
131 listOfFiles->Add(ofile);
132 }
133 ofile->cd();
134 otree=dynamic_cast<TTree*>(ofile->Get("RAW"));
135 if (!otree)
136 {
137 otree=itree->CloneTree(0);
a65a7e70 138 }
a65a7e70 139
983b8aef 140 otree->CopyAddresses(itree);
141 itree->GetEntry(ievent);
142 eventold=ievent;
143 printf("filling event %i in file %s\n",ievent,ofile->GetName());
144 otree->Fill();
145 //otree->CopyEntries(itree,Form("Entry$==%d",ievent),1);
a65a7e70 146
983b8aef 147 // reset input
148 itree->ResetBranchAddresses();
149 }
150
151 //close the files
152 for (Int_t i=0; i<listOfFiles->GetEntries(); i++)
153 {
154 ofile=dynamic_cast<TFile*>(listOfFiles->At(i));
155 if (!ofile) {continue;}
156 otree=dynamic_cast<TTree*>(ofile->Get("RAW"));
157 Long64_t nEntries=0;
158 if (otree) { nEntries = otree->GetEntries(); }
159
160 //write the file and close
161 ofile->Write();
162 printf("closing file: %s with %i entries\n",ofile->GetName(),nEntries);
163 delete ofile;
164
165 //remove empty files
166 if (nEntries==0)
167 {
168 gSystem->Unlink(ofile->GetName());
169 }
170 }
171}
172
173Bool_t makeAlienInputEventList( TString outputFileName="wn.list",
174 TString referenceFileName="filteredEvents.list",
175 TString inputCollectionName="wn.xml" )
176{
177 TAlienCollection *coll = TAlienCollection::Open(inputCollectionName);
178 if (!coll)
179 {
180 ::Error("makeAlienInputEventList", "Cannot open collection from %s", xmlfile);
181 return NULL;
182 }
183 TString configLine;
184 TString chunkPath;
185 TString chunkName;
186 ifstream referenceFile;
187 referenceFile.open(referenceFileName.Data());
188 if (!referenceFile.is_open())
189 {
190 printf("could not open %s\n",referenceFileName.Data());
191 return kFALSE;
192 }
193 gSystem->Unlink(outputFileName);
194 ofstream outputFile;
195 outputFile.open(outputFileName.Data());
196 if (!outputFile.is_open())
197 {
198 printf("could not open %s\n",referenceFileName.Data());
199 return kFALSE;
200 }
201
202 while (coll->Next())
203 {
204 chunkPath = coll->GetTURL();
205 TObjArray* a = chunkPath.Tokenize("/");
206 TObjString* objstr = dynamic_cast<TObjString*>(a->At(a->GetEntries()-1));
207 if (!objstr)
208 {
209 printf("empty chunkPath from collection!\n");
210 return kFALSE;
211 }
212 TString chunkName = objstr->String();
213 chunkName.ReplaceAll("alien://","");
214 while (referenceFile.good())
215 {
216 configLine.ReadLine(referenceFile);
217 if (configLine.Contains(chunkName))
218 {
219 configLine=configLine.Strip(TString::kBoth);
220 if (!configLine.BeginsWith("alien://"))
221 {
222 configLine.Prepend("alien://");
223 }
224 outputFile << configLine << endl;
225 cout << configLine << endl;
226 }
227 }
228 //jump to beginning and clear flags
229 referenceFile.clear();
230 referenceFile.seekg(0,ios::beg);
231 }
232 outputFile.close();
233 referenceFile.close();
234 return kTRUE;
a65a7e70 235}
236