4 Macro for making pileup events in pp. Remember to have the MC
5 optione switched on and that the resulting digit files will
6 not be RLE so use the PileUp option when tracking.
10 root>MakePileup(path,triggerevent);
12 Remember to have a "digitfile" link to the root
13 file containing the pp events in "path".
15 Authors: Roland Bramm & Anders Vestbo & Constantin Loizides
19 #include "AliHLTLogger.h"
20 #include "AliHLTFileHandler.h"
21 #include "AliHLTDigitData.h"
22 #include "AliHLTTransform.h"
30 void QSort(void **dPt,Int_t first,Int_t last);
31 Int_t CompareDigits(void *dPt1,void *dPt2);
34 /* Nice script I am using for the generation of pileup events:
36 --------------------------------------------------------
45 ln -s -f /data1/AliRoot/PythiaPP_1000Events_TPConly_ev0-375_digits.root $dir/digitfile.root
46 cd ~/l3/level3code/exa
48 for i in `seq $from $to`; do
52 aliroot -b runMakePileup.C\(\"$dir\",0,375,\"$pdir\",101\);
54 --------------------------------------------------------
56 Path should point to the path in which there is a link to the root digitsfile.
57 Startev and Endev mark the beginning and ending of valid event numbers in that file.
58 Pileupdir is the directory in which the binary pileuped data will be written
59 Npiles specifies the number of pileups wanted.
60 TriggerEvent specifies the triggerevent in the linear mode or if -1 is given the random mode.
61 Good_tpc_ev_filename is the rootfile name where the offline good-tracks are stored (see fill.C)
64 void MakePileup(Char_t *path,Int_t startev,Int_t endev,Char_t *pileupdir="pileup",Int_t npiles=25,Int_t triggerevent=-1,Char_t *good_tpc_ev_filename=0)
66 Int_t ntotalev=endev-startev+1;
69 cerr << "Total number of events in root file must be at least equal to the number of pp events to be piledup." <<endl;
72 if(npiles<1 || npiles % 2 != 1 ){
73 cerr << "npiles must be at least one and a odd number." <<endl;
77 Int_t const minTriggerEvents=5;
78 //Int_t RefOffset[25] = {30,60,90,120,150,180,210,240,270,300,330,360,-30,-60,-90,-120,-150,-180,-210,-240,-270,-300,-330,-360,-390};
79 Int_t tshift=30*25/npiles;
80 Int_t *Offset=new Int_t[npiles];
82 for(Int_t i=1;i<=(npiles-1)/2;i++)
84 for(Int_t i=1;i<=(npiles-1)/2;i++)
85 Offset[i+(npiles-1)/2] = -i*tshift;
87 Int_t *EventNumbers=new Int_t[npiles];
89 Int_t evdiff=endev-startev;
91 Int_t iseed = (abs((12*1000+1)*time(&t)))%900000000;
92 TRandom *rand=new TRandom(iseed);
94 while(triggerevent<0){
95 Double_t dummy=evdiff*rand->Rndm();
97 if(good_tpc_ev_filename){
98 TFile goodfile=TFile(good_tpc_ev_filename,"READ");
99 if(!goodfile.IsOpen()){
100 cerr << "Could not open good tpc tracks file "<<good_tpc_ev_filename<<endl;
103 Char_t gooddummy[1000];
104 sprintf(gooddummy,"good-pptracks-%d",(Int_t)dummy+startev);
105 TNtuple *ngtuppel = (TNtuple*)goodfile.Get(gooddummy);
106 if(!ngtuppel) continue;
107 Int_t ngoodevents=(Int_t)ngtuppel->GetEntries();
108 if(ngoodevents>=minTriggerEvents) triggerevent=(Int_t)dummy+startev;
111 triggerevent=(Int_t)dummy+startev;
112 } //found a trigger event
113 for(Int_t i=1;i<npiles;i++){
114 Int_t ev=triggerevent;
115 while(ev==triggerevent){
116 Double_t dummy=evdiff*rand->Rndm()+startev;
124 for(Int_t i=1;i<npiles;i++){
125 if(ev==triggerevent) ev++;
130 EventNumbers[0]=triggerevent;
131 cout << "Events: ";for(Int_t i=0;i<npiles;i++) cout << EventNumbers[i] << " "; cout << endl;
132 cout << "Offsets: ";for(Int_t i=0;i<npiles;i++) cout << Offset[i] << " ";cout << endl;
139 Int_t patch = -1; //All of slice.
141 Int_t NEvents = npiles; //for compatibility with old version
142 AliHLTFileHandler **hand=new AliHLTFileHandler*[NEvents];
143 AliHLTDigitRowData **data=new AliHLTDigitRowData*[NEvents];
144 AliHLTDigitData **rowData=new AliHLTDigitData*[NEvents];
145 AliHLTDigitData *test;
150 Int_t DigitsPerRow = 0;
153 const Int_t maxdigits=50000;
154 AliHLTDigitData *temp[maxdigits];
155 AliHLTDigitData *temp2[maxdigits];
157 //Create 1 filehander per event:
158 sprintf(Carry,"%s/digitfile.root",path);
159 for(Int_t event=0; event<NEvents; event++)
161 hand[event] = new AliHLTFileHandler();
162 if(!hand[event]->SetAliInput(Carry))
163 cerr<<" Error opening file :"<<Carry<<endl;
166 //Looping over slices and the over events
167 for(Int_t slice=0 ; slice<36 ; slice++){
168 cout<<"\nLoading slice "<<slice<<" "<<endl;
169 for(Int_t event = 0; event < NEvents ; event++){
170 Int_t eventno=EventNumbers[event];
171 hand[event]->Init(slice,patch); //give merge option to FileHandler
172 data[event] = hand[event]->AliAltroDigits2Memory(size,eventno,kTRUE);
177 //Find out how many digits to allocate per patch
179 Int_t sign = slice < 18 ? -1 : 1;
180 for(Int_t row = 0 ; row < AliHLTTransform::GetNRows(patch) ; row++){
181 for(Int_t event = 0; event < NEvents ; event++){
182 rowData[event] = (AliHLTDigitData *)data[event]->fDigitData;
183 for(UInt_t dig=0; dig<data[event]->fNDigit; dig++)
185 Int_t time = rowData[event][dig].fTime + sign*Offset[event];
186 if(time < AliHLTTransform::GetNTimeBins() && time >= 0)
190 hand[event]->UpdateRowPointer(data[event]);
194 //cout << "Try to allocate : " << DigitsTot << endl;
195 Int_t AllDigitsSize = sizeof(AliHLTDigitData) * DigitsTot + sizeof(AliHLTDigitRowData) * AliHLTTransform::GetNRows(patch);
196 NData = new Byte_t[AllDigitsSize];
197 memset(NData,0,AllDigitsSize);
198 AliHLTDigitRowData *AllRowData = (AliHLTDigitRowData*)NData;
199 //cout << "Allocated " << endl;
201 //Reset the data pointers, because they changed when doing UpdateRowPointer.
202 for(Int_t event=0; event<NEvents; event++)
203 data[event] = (AliHLTDigitRowData*)hand[event]->GetDataPointer(size);
207 for(Int_t row = 0 ; row < AliHLTTransform::GetNRows(patch) ; row++){
209 for(Int_t event = 0; event < NEvents ; event++){
210 rowData[event] = (AliHLTDigitData *)data[event]->fDigitData;
211 for(UInt_t dig=0; dig<data[event]->fNDigit; dig++)
213 Int_t time = rowData[event][dig].fTime + sign*Offset[event];
214 if(time < AliHLTTransform::GetNTimeBins() && time >= 0)
218 //cout << "Found : " << DigitsPerRow << " in all Events, Row :" << row << endl;
220 //Copy the digits to a temporary storage,
221 //because we have to sort them before storing them :
223 for(Int_t event = 0; event < NEvents ; event++){
224 rowData[event] = (AliHLTDigitData *)data[event]->fDigitData;
225 for(UInt_t digit = 0; digit < data[event]->fNDigit ; digit++){
227 Int_t time = rowData[event][digit].fTime + sign*Offset[event];
228 if(time >= AliHLTTransform::GetNTimeBins() || time < 0)
231 temp[MeVDigit] = &rowData[event][digit];
232 temp[MeVDigit]->fTime = temp[MeVDigit]->fTime + sign*Offset[event];
233 //cout << MeVDigit << " : " << digit << endl;
236 hand[event]->UpdateRowPointer(data[event]);
240 QSort((void**)temp,0,MeVDigit);
242 //Now, loop and check for overlaps:
244 for(Int_t c=0; c<MeVDigit; c++)
247 if(final_count>0 && temp[c]->fTime == temp[c-1]->fTime && temp[c]->fPad == temp[c-1]->fPad)
249 //this one is overlapping with the previous one:
250 temp2[final_count-1]->fCharge += temp[c]->fCharge;
251 if(temp2[final_count-1]->fCharge>=1024)
252 temp2[final_count-1]->fCharge=1023; //Saturation
254 Int_t testev=( (temp[c]->fTrackID[0])>>22 ) & 0x3ff;
255 if( testev == triggerevent)
257 //This digit comes from trigger event, so store the mcid
258 temp2[final_count-1]->fTrackID[0] = temp[c]->fTrackID[0];
259 temp2[final_count-1]->fTrackID[1] = temp[c]->fTrackID[1];
260 temp2[final_count-1]->fTrackID[2] = temp[c]->fTrackID[2];
265 temp2[final_count] = temp[c];
270 tot_dig+=final_count;
271 AllRowData->fRow = row;
272 AllRowData->fNDigit = final_count;
273 test = (AliHLTDigitData *) AllRowData->fDigitData;
275 //and copy them to the new event:
276 for(Int_t c=0; c<final_count; c++)
278 test[c].fCharge = temp2[c]->fCharge;
279 test[c].fPad = temp2[c]->fPad;
280 test[c].fTime = temp2[c]->fTime;
283 Int_t testev=( (temp2[c]->fTrackID[0])>>22 ) & 0x3ff;
286 //Only store the mc corresponding to the trigger event:
287 if ( testev == triggerevent)
289 test[c].fTrackID[0] = ( (temp2[c]->fTrackID[0]) & 0x3fffff ) - 128;
290 test[c].fTrackID[1] = ( (temp2[c]->fTrackID[1]) & 0x3fffff ) - 128;
291 test[c].fTrackID[2] = ( (temp2[c]->fTrackID[2]) & 0x3fffff ) - 128;
293 else //Digit not corresponding to the triggerevent, so mark it as noise.
295 test[c].fTrackID[0]=-1;
296 test[c].fTrackID[1]=-1;
297 test[c].fTrackID[2]=-1;
299 if(c>0 && test[c].fTime == test[c-1].fTime && test[c].fPad == test[c-1].fPad)
300 cerr<<"Overlap at row "<<row<<" pad "<<(int)test[c].fPad<<" time "<<(int)test[c].fTime<<endl;
301 //cout<<"Copying back, pad "<<(int)test[c].fPad<<" time "<<(int)test[c].fTime<<" charge "<<(int)test[c].fCharge<<endl;
304 if(MeVDigit!=DigitsPerRow)
305 cerr<<endl<<"Error: "<<MeVDigit<<" "<<DigitsPerRow<<endl;
306 if(final_count > MeVDigit)
307 cerr<<"Error; final_count "<<final_count<<" MeVDigit "<<MeVDigit<<endl;
309 Byte_t *tmp = (Byte_t *)AllRowData;
310 Int_t UpdateSize = sizeof(AliHLTDigitRowData) + sizeof(AliHLTDigitData)*final_count;
312 AllRowData = (AliHLTDigitRowData *) tmp;
313 }//end looping over row
315 if(tot_dig>DigitsTot)
316 cerr<<endl<<"Mismatching digitcount "<<tot_dig<<" "<<DigitsTot<<endl;
318 //cout << "Clearing data structures. " << endl;
319 for(Int_t event = 0; event < NEvents ; event++)
323 hand[event]->FreeDigitsTree();
326 AliHLTFileHandler *Out = new AliHLTFileHandler();
327 AllRowData = (AliHLTDigitRowData*)NData;
329 if(pileupdir[0]=='/')
330 sprintf(Carry,"mkdir -p %s/",pileupdir);
332 sprintf(Carry,"mkdir -p %s/%s/",path,pileupdir);
333 gSystem->Exec(Carry);
335 if(pileupdir[0]=='/')
336 sprintf(Carry,"%s/digits_%d_%d_%d.raw",pileupdir,triggerevent,slice,patch);
338 sprintf(Carry,"%s/%s/digits_%d_%d_%d.raw",path,pileupdir,triggerevent,slice,patch);
341 if(!Out->SetBinaryOutput(Carry))
343 cerr<<"Cannot open file: "<<Carry<<endl;
346 cout << "Writing to file: " << Carry <<endl;
347 Out->Memory2Binary(AliHLTTransform::GetNRows(patch),AllRowData);
348 Out->CloseBinaryOutput();
353 for(Int_t event = 0; event < NEvents ; event++)
361 void QSort(void **dPt,Int_t first,Int_t last)
363 //General sorting routine. only sorting the pointers.
364 AliHLTDigitData **a = (AliHLTDigitData**)dPt;
366 AliHLTDigitData *tmp;
370 while (last - first > 1) {
374 while (++i < last && CompareDigits((void*)a[i], (void*)a[first]) < 0)
376 while (--j > first && CompareDigits((void*)a[j], (void*)a[first]) > 0)
392 if (j - first < last - (j + 1)) {
393 QSort((void**)a, first, j);
394 first = j + 1; // QSort(j + 1, last);
396 QSort((void**)a, j + 1, last);
397 last = j; // QSort(first, j);
402 Int_t CompareDigits(void *dPt1,void *dPt2)
404 AliHLTDigitData *a = (AliHLTDigitData*)dPt1;
405 AliHLTDigitData *b = (AliHLTDigitData*)dPt2;
406 if(a->fPad==b->fPad && a->fTime == b->fTime) return 0;
408 if(a->fPad<b->fPad) return -1;
409 if(a->fPad==b->fPad && a->fTime<b->fTime) return -1;