Storing the trigger class mask into the raw data header. Should be ok for TPC,PHOS...
[u/mrichter/AliRoot.git] / ZDC / AliZDCMerger.cxx
CommitLineData
359cdddc 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16
17///////////////////////////////////////////////////////////////////////////////
18// //
19// ZDC event merging class //
20// //
21///////////////////////////////////////////////////////////////////////////////
22
23// --- ROOT system
ef0750c2 24#include <Riostream.h>
359cdddc 25#include <TTree.h>
26#include <TFile.h>
27#include <TDirectory.h>
1acbaaa8 28#include <TNtuple.h>
53c30603 29#include <TSystem.h>
fe107ada 30#include <TROOT.h>
359cdddc 31
32// --- AliRoot header files
33#include "AliZDCMerger.h"
34#include "AliZDC.h"
35#include "AliZDCHit.h"
1acbaaa8 36#include "AliZDCMergedHit.h"
359cdddc 37#include "AliZDCDigit.h"
38#include "AliZDCFragment.h"
39#include "AliRun.h"
40#include "AliDetector.h"
41#include "AliHeader.h"
42#include "AliGenEventHeader.h"
53c30603 43#include "AliGenHijingEventHeader.h"
359cdddc 44
45ClassImp(AliZDCMerger)
46
1450a7cd 47//int comp(const void *i,const void *j) {return *(int *)i - *(int *)j;}
48
359cdddc 49//____________________________________________________________________________
50AliZDCMerger::AliZDCMerger()
51{
52// Default constructor
1450a7cd 53 //fMerge = kDigitize -> Only digitization
54 //fMerge = kMerge -> Digitization + Merging
55 fMerge = kMerge;
359cdddc 56 fFnBgr = 0;
359cdddc 57 fBgrFile = 0;
1acbaaa8 58 fNEvBgr = 0;
2685bf00 59 fHitsBgr = 0;
359cdddc 60 fImpPar = 0;
61 fSpecn = 0;
62 fSpecp = 0;
63 fFreeSpn = 0;
64 fFreeSpp = 0;
65 fFnSpecn = 0;
359cdddc 66 fSpecnFile = 0;
67 fFnSpecp = 0;
359cdddc 68 fSpecpFile = 0;
1acbaaa8 69 fNMhits = 0;
1450a7cd 70
359cdddc 71}
72
73//____________________________________________________________________________
74AliZDCMerger::~AliZDCMerger()
75{
fe107ada 76 // Destructor
359cdddc 77 if (fSpecnFile) delete fSpecnFile;
78 if (fSpecpFile) delete fSpecpFile;
79}
80
81//____________________________________________________________________________
82void AliZDCMerger::InitMerging()
83{
84 // Hits tree, impact parameter, num. of spectators n & p
85 // in background (full Hijing) event
86 Float_t b;
1450a7cd 87 Background(b, fSpecn, fSpecp);
359cdddc 88
89 // Production of nuclear fragments -> num. of FREE spectators n & p
1450a7cd 90 Fragmentation(b, fSpecn, fSpecp, fFreeSpn, fFreeSpp);
359cdddc 91
92 // Extract from spectators distribution the signal events:
93 // NFreeSpectatorN spectator n & NFreeSpectatorP spectator p
1acbaaa8 94 Mixing();
359cdddc 95}
96
97//____________________________________________________________________________
98void AliZDCMerger::Background(Float_t &fImpPar, Int_t &fSpecn, Int_t &fSpecp)
99{
100
101 // --- Open the background file
fe107ada 102 if (fMerge && !fBgrFile) fBgrFile = OpenBgrFile();
359cdddc 103
104 // --- Read from the TreeE impact parameter (b),
105 // # of spectators n and p (fSpecn, fSpecp)
106 fBgrFile->cd();
107
1acbaaa8 108// // Get AliRun object from file or create it if not on file
359cdddc 109// gAlice = (AliRun*)fBgrFile->Get("gAlice");
1acbaaa8 110// if (!gAlice) {
111// gAlice = (AliRun*)fBgrFile->Get("gAlice");
112// if (gAlice) printf("AliRun object found on file\n");
113// if (!gAlice) {
114// printf("\n create new gAlice object");
115// gAlice = new AliRun("gAlice","Alice test program");
116// }
117// }
1450a7cd 118
fe107ada 119 // gAlice->GetEvent(fNEvBgr); this is done in the steering macro
359cdddc 120 AliHeader *header = gAlice->GetHeader();
121 AliGenEventHeader* mcHeader = header->GenEventHeader();
122 fImpPar = ((AliGenHijingEventHeader*) mcHeader)->ImpactParameter();
85a96223 123 fSpecn = ((AliGenHijingEventHeader*) mcHeader)->ProjSpectatorsn();
124 fSpecp = ((AliGenHijingEventHeader*) mcHeader)->ProjSpectatorsp();
125 //
1450a7cd 126 printf("\n HIJING ev. #%d - b = %f fm, Nspecn = %d, Nspecp = %d\n",
127 fNEvBgr,fImpPar,fSpecn,fSpecp);
359cdddc 128}
129
130//____________________________________________________________________________
131TFile* AliZDCMerger::OpenBgrFile()
132{
133 // Initialise background event
fe107ada 134 TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(fFnBgr);
135 if(!file)cerr<<"AliZDCMerger: background file "<<fFnBgr<<" not found\n";
136 // TFile *file = new TFile(fFnBgr,"UPDATE");
1acbaaa8 137 printf("\n AliZDCMerger --- Background event -> %s file opened \n", fFnBgr);
359cdddc 138 fHitsBgr = new TClonesArray("AliZDCHit",1000);
1acbaaa8 139 fMHits = new TClonesArray("AliZDCMergedHit",1000);
359cdddc 140 return file;
141}
142
143//____________________________________________________________________________
144void AliZDCMerger::Fragmentation(Float_t fImpPar, Int_t fSpecn, Int_t fSpecp,
145 Int_t &fFreeSpn, Int_t &fFreeSpp)
146{
1450a7cd 147 //printf("\n Fragmentation -> fSpecn = %d, fSpecp = %d\n",fSpecn,fSpecp);
86aea133 148 Int_t j, zz[100], nn[100], nAlpha, ztot, ntot;
359cdddc 149 AliZDCFragment *frag = new AliZDCFragment(fImpPar);
150 for(j=0; j<=99; j++){
151 zz[j] =0;
152 nn[j] =0;
153 }
154
155 // Fragments generation
156 frag->GenerateIMF(zz, nAlpha);
157
158 // Attach neutrons
86aea133 159 ztot=0;
160 ntot=0;
161 frag->AttachNeutrons(zz, nn, ztot, ntot);
162 fFreeSpn = fSpecn-ntot-2*nAlpha;
163 fFreeSpp = fSpecp-ztot-2*nAlpha;
1450a7cd 164 if(fFreeSpn<0) fFreeSpn=0;
165 if(fFreeSpp<0) fFreeSpp=0;
86aea133 166 //printf("\n 2*nAlpha = %d, ztot = %d, ntot = %d\n",2*nAlpha, ztot, ntot);
1acbaaa8 167 printf("\n Fragmentation -> FreeSpn = %d, FreeSpp = %d\n",fFreeSpn,fFreeSpp);
359cdddc 168}
169
170//____________________________________________________________________________
1acbaaa8 171void AliZDCMerger::Mixing()
359cdddc 172{
1acbaaa8 173
1450a7cd 174 //printf("\n AliZDCMerger->Mixing\n");
1acbaaa8 175
359cdddc 176 // ### Background event Hits ###########################################
177 fBgrFile->cd();
178// fBgrFile->ls();
179
86aea133 180 AliZDC *zdc = (AliZDC *)gAlice->GetModule("ZDC");
181// if(zdc) printf("\n Ho trovato lo ZDC!\n");
359cdddc 182
1acbaaa8 183// fNEvBgr = 0; // Let's suppose to have 1 full Hijing event per file
1acbaaa8 184 // Hits tree
359cdddc 185 char treeBgrName[20];
186 sprintf(treeBgrName,"TreeH%d",fNEvBgr);
1acbaaa8 187 fTrHBgr = (TTree*)gDirectory->Get(treeBgrName);
359cdddc 188 if(!fTrHBgr){
189 printf("\n ERROR -> Can't find TreeH%d in background file\n",fNEvBgr);
190 }
1acbaaa8 191// fTrHBgr->Print();
192
193 // Branch address
194 TBranch *branch;
195 char branchname[20];
86aea133 196 sprintf(branchname,"%s",zdc->GetName());
1acbaaa8 197 if(fTrHBgr && fHitsBgr){
198// printf("\n fTrHBgr!=0 && fHitsBgr!=0\n");
199 branch = fTrHBgr->GetBranch(branchname);
200 if(branch) branch->SetAddress(&fHitsBgr);
201 }
202
359cdddc 203 Int_t ntracks = (Int_t) fTrHBgr->GetEntries();
1450a7cd 204 //printf("\n --- ntracks = %d\n\n", ntracks);
1acbaaa8 205
206 Int_t itrack, nhits, ihit, j, sector[2];
207 AliZDCHit* zdcHit;
86aea133 208 AliZDCMergedHit *mHit;
209 Float_t mHits[7];
1acbaaa8 210 TClonesArray &sdigits = *fMHits; // SDigits TCArray
211 fNMhits = 0;
359cdddc 212
359cdddc 213 // --- Tracks loop
214 for(itrack=0; itrack<ntracks; itrack++){
359cdddc 215 fTrHBgr->GetEvent(itrack);
216
1acbaaa8 217 nhits = fHitsBgr->GetEntries();
1acbaaa8 218// printf(" nhits = %d \n", nhits);
219 for(ihit=0; ihit<nhits; ihit++){
220 zdcHit = (AliZDCHit*) fHitsBgr->UncheckedAt(ihit);
221
222 for(j=0; j<2; j++) sector[j] = zdcHit->GetVolume(j);
86aea133 223 mHits[0] = zdcHit->GetPrimKinEn();
224 mHits[1] = zdcHit->GetXImpact();
225 mHits[2] = zdcHit->GetYImpact();
226 mHits[3] = zdcHit->GetSFlag();
227 mHits[4] = zdcHit->GetLightPMQ();
228 mHits[5] = zdcHit->GetLightPMC();
229 mHits[6] = zdcHit->GetEnergy();
230 mHit = new AliZDCMergedHit(sector, mHits);
231// mHit->Print("");
232 new((*fMHits)[fNMhits]) AliZDCMergedHit(*mHit);
233 new (sdigits[fNMhits]) AliZDCMergedHit(*mHit);
234 delete mHit;
1acbaaa8 235 fNMhits++;
236 }//Hits loop
237
359cdddc 238 } // Tracks loop
1450a7cd 239 //printf(" fNMhits (after bckg) = %d, \n",fNMhits);
1acbaaa8 240
241 // ### Signal event Hits ###########################################
242 // --- Neutrons
243 ExtractSignal(1);
359cdddc 244
1acbaaa8 245 // --- Protons
246 ExtractSignal(2);
1450a7cd 247 //printf(" fNMhits (after signal) = %d \n",fNMhits);
359cdddc 248
249}
250
251//____________________________________________________________________________
1acbaaa8 252void AliZDCMerger::ExtractSignal(Int_t SpecType)
359cdddc 253{
254
1acbaaa8 255// printf("\n Entering in Extract Signal\n");
256
86aea133 257 Int_t numEvents = 0;
1acbaaa8 258 if(SpecType == 1){ // --- Signal for spectator neutrons
259 fFnSpecn = gSystem->ExpandPathName("$ALICE/$ALICE_LEVEL/ZDC/ZNsignalntu.root");
260 fSpecnFile = TFile::Open(fFnSpecn,"R");
261 fSpecnFile->cd();
1450a7cd 262 printf("\n --- ExtractSignal x n: file %s opened\n", fFnSpecn);
86aea133 263 numEvents = fFreeSpn;
1acbaaa8 264 }
265 else if(SpecType == 2){ // --- Signal for spectator protons
266 fFnSpecp = gSystem->ExpandPathName("$ALICE/$ALICE_LEVEL/ZDC/ZPsignalntu.root");
267 fSpecpFile = TFile::Open(fFnSpecp,"R");
268 fSpecpFile->cd();
1450a7cd 269 printf("\n --- ExtractSignal x p: file %s opened\n", fFnSpecp);
86aea133 270 numEvents = fFreeSpp;
1acbaaa8 271 }
86aea133 272 //printf("\n # of free spectators = %d\n", numEvents);
1450a7cd 273 //printf("\n fNMhits (before adding signal) = %d\n",fNMhits);
1acbaaa8 274
86aea133 275 TNtuple *zdcSignal = (TNtuple*) gDirectory->Get("ZDCSignal");
276 Int_t nentries = (Int_t) zdcSignal->GetEntries();
1450a7cd 277 //printf("\n # entries = %d\n", nentries);
1acbaaa8 278
86aea133 279 AliZDCMergedHit *mHit;
280 Float_t *entry, hitsSpec[7];
281 Int_t pl, i, j, k, iev=0, rnd[125], volume[2];
1450a7cd 282 for(pl=0;pl<125;pl++){
283 rnd[pl] = 0;
284 }
86aea133 285 for(pl=0;pl<numEvents;pl++){
1450a7cd 286 rnd[pl] = (Int_t) (9999*gRandom->Rndm());
85a96223 287 if(rnd[pl] >= 9999) rnd[pl] = 9998;
1450a7cd 288 //printf(" rnd[%d] = %d\n",pl,rnd[pl]);
289 }
290 // Sorting vector in ascending order with C function QSORT
86aea133 291 qsort((void*)rnd,numEvents,sizeof(Int_t),comp);
292 //for(pl=0;pl<numEvents;pl++){
1450a7cd 293 ////printf(" rnd[%d] = %d\n",pl,rnd[pl]);
294 //}
295 do{
1acbaaa8 296 for(i=0; i<nentries; i++){
86aea133 297 zdcSignal->GetEvent(i);
298 entry = zdcSignal->GetArgs();
1450a7cd 299 if(entry[0] == rnd[iev]){
86aea133 300 for(k=0; k<2; k++) volume[k] = (Int_t) entry[k+1];
1acbaaa8 301 for(j=0; j<7; j++){
86aea133 302 hitsSpec[j] = entry[j+3];
1acbaaa8 303 }
1450a7cd 304 //printf("\n i = %d, iev = %d, entry[0] = %f, rnd[%d] = %d ",i,iev,entry[0],iev,rnd[iev]);
86aea133 305 mHit = new AliZDCMergedHit(volume, hitsSpec);
306 new((*fMHits)[fNMhits++]) AliZDCMergedHit(*mHit);
307 delete mHit;
1acbaaa8 308 }
1450a7cd 309 else if(entry[0] > rnd[iev]){
310 iev++;
311 continue;
312 }
1acbaaa8 313 }
86aea133 314 }while(iev<numEvents);
1acbaaa8 315
316 if(SpecType ==1){
1450a7cd 317 //printf("\n fNMhits (after n signal) = %d\n",fNMhits);
1acbaaa8 318 fSpecnFile->Close();
319 }
320 else if(SpecType == 2){
1450a7cd 321 //printf("\n fNMhits (after p signal) = %d\n",fNMhits);
1acbaaa8 322 fSpecpFile->Close();
323 }
324
359cdddc 325}