ROOT logo
/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/* $Id$ */

/// \ingroup macros
/// \file fastMUONSim.C
/// \brief An example how to do the fast simulation and storing
/// the muons that survive the reconstruction
///
/// \author A. De Falco, H. Woehri, INFN Cagliari, April 2007
///
/// An example how to do the fast simulation and storing
/// the muons that survive the reconstruction in 
/// AliMUONTrackLight and AliMUONPairLight objects for
/// further analysis. Should be used together with the
/// macro fastMUONGen.C

#if !defined(__CINT__) || defined(__MAKECINT__)
//STEER includes
#include "AliStack.h"
#include "AliRun.h"
#include "AliHeader.h"

//FASTSIM includes
#include "AliFastMuonTriggerEff.h"
#include "AliFastMuonTrackingAcc.h"
#include "AliFastMuonTrackingEff.h"
#include "AliFastMuonTrackingRes.h"
#include "AliFastDetector.h"

//MUON includes
#include "AliMUONTrackLight.h"
#include "AliMUONPairLight.h"

//ROOT includes
#include "TParticle.h"
#include "TRandom.h"
#include "TFile.h"
#include "TClonesArray.h"
#include "TTree.h"

#endif

Float_t fThetaMin = 171., fThetaMax = 178.;
//====================================================
void fastMUONSim(Float_t bkg=0, char* outFileName = "fastSim_pp.root"){

  //setting up the fast simulator
  AliFastMuonTriggerEff *trigeff = new AliFastMuonTriggerEff();
  AliFastMuonTrackingAcc *acc = new AliFastMuonTrackingAcc();
  AliFastMuonTrackingEff *eff = new AliFastMuonTrackingEff();
  AliFastMuonTrackingRes *res = new AliFastMuonTrackingRes();
  acc->SetBackground(bkg);
  eff->SetBackground(bkg);
  res->SetBackground(bkg);  
  acc->Init(); 
  eff->Init(); 
  res->Init(); 
  trigeff->Init();

  trigeff->SetCut(0); // 0... trigger pt cut at 1 GeV
                      // 1... trigger pt cut at 2 GeV

  //prepare the arrays to store the generated/simulated muons
  TClonesArray *muonArray   = new TClonesArray("AliMUONTrackLight",100); 
  TClonesArray *dimuonArray = new TClonesArray("AliMUONPairLight",100); 
  TTree *treeOut = new TTree("tree","tree"); 
  TFile *fout = new TFile(outFileName,"recreate"); 
  fout->cd(); 
  treeOut->Branch("muons",&muonArray); 
  treeOut->Branch("dimuons",&dimuonArray); 
  //
  // Stack of particle for each event
  AliStack* stack;
  // Creating Run Loader and opening file containing Hits
  // AliRunLoader * RunLoader = AliRunLoader::Open("galice.root","MUONFolder","READ");
  AliRunLoader *RunLoader = AliRunLoader::Open("galice.root");
  if (RunLoader == 0x0) {
    printf(">>> Error : Error Opening %s file \n", "galice.root");
    return;
  }

  RunLoader->LoadKinematics("READ");
  Int_t nevents = RunLoader->GetNumberOfEvents();
  printf("nevents %d\n", nevents);

  TParticle *part = 0, *partMother=0;
  AliMUONTrackLight *muon = 0;
  Double_t radeg =  180./TMath::Pi(); 
  for(int iev = 0; iev < nevents; iev++){

    if(iev%1000==0) printf("Event %d\n", iev);
    RunLoader->GetEvent(iev); 
    stack = RunLoader->Stack();
    //stack->DumpPStack();

    //reset muon info
    Int_t nMuons = 0;
    muonArray->Clear();     // clean muon and dimuon arrays 
    dimuonArray->Clear(); 
    //
    Int_t nMuonsStored = 0; 
    Int_t nPart = stack->GetNtrack();
    for(int iPart = 0; iPart < nPart; iPart++){

      part = stack->Particle(iPart);
      if(fabs(part->GetPdgCode()) == 13){ //muon found

	nMuons++;
	Double_t xyz[3] = {part->Vx(),part->Vy(),part->Vz()};
	// do not take muons that were decayed after the absorber
	if(xyz[2] < -(90.+ 40.)) continue; //decay muons after 90cm + 1 int. length
	Int_t charge = (part->GetPdgCode() > 0) ? -1 : +1;
	Double_t px = part->Px();
	Double_t py = part->Py();
	Double_t pz = part->Pz();
	Double_t E = part->Energy();
	TLorentzVector pGen(px,py,pz,E);
	//
	//fast simulation code
	res->SetCharge(charge);
	eff->SetCharge(charge);
	acc->SetCharge(charge);
	Float_t p = (Float_t) pGen.P();
	Float_t pt = (Float_t) pGen.Pt();
	Float_t theta = (Float_t) radeg*pGen.Theta();
	Float_t phi = (Float_t) radeg*pGen.Phi(); 
	
	//continue only if we have a muon within the MUON acceptance
	if(theta < fThetaMin || theta > fThetaMax) continue;

	theta = 180. - theta; // correct by hand the 'new' coordinates frame
	phi   = 180. - phi; // correct by hand the 'new' coordinates frame
	Float_t prec = 0, thetarec = 0, phirec = 0; 
	res->Evaluate(p, theta ,phi, prec, thetarec, phirec);
	Float_t precx = prec * TMath::Sin(thetarec/radeg) * TMath::Cos(phirec/radeg); 
	Float_t precy = prec * TMath::Sin(thetarec/radeg) * TMath::Sin(phirec/radeg); 
	Float_t precz = prec * TMath::Cos(thetarec/radeg);
	precz = -precz; // correct by hand the 'new' coordinates frame
	precx   = -precx; // correct by hand the 'new' coordinates frame

  	Float_t trkeff = eff->Evaluate(charge, pt, theta, phi);
 	Float_t accep  = acc->Evaluate(charge, pt, theta, phi);
 	Float_t trgeff = trigeff->Evaluate(charge,pt,theta,phi);
	if(trkeff > 1.) printf("tracking efficiency > 1\n");
	if(accep > 1.) printf("acceptance efficiency > 1\n");
	if(trgeff > 1.) printf("trigger efficiency > 1\n");
	if (gRandom->Rndm() > trkeff || gRandom->Rndm() > accep) continue; 

	//only if we have a muon in the acceptance, store it as an
	//AliMUONTrackLight object and use it to form dimuons...
	muon = new AliMUONTrackLight();
	muon->SetCharge(charge);
	muon->SetTrackPDGCode(part->GetPdgCode()); //must be set, otherwise -999
	muon->SetTrackPythiaLine(iPart);//must be set, otherwise -999
	muon->SetPGen(pGen);
  	muon->FillMuonHistory(stack, part);
	// set vertex to mother's vx,vy,vz to have the primary vertex

	Int_t idMother = -1; 
	Int_t id2 = 0; 
	while (idMother < 0) idMother = muon->GetParentPythiaLine(id2++); 
	partMother = stack->Particle(idMother);
	Double_t xyzMother[3] = {partMother->Vx(),
				 partMother->Vy(),
				 partMother->Vz()};
	muon->SetVertex(xyzMother);
	if (gRandom->Rndm() > trgeff) muon->SetTriggered(kFALSE); 
	else muon->SetTriggered(kTRUE);
	muon->SetPxPyPz(precx,precy,precz);

	//store the referenced track in the muonArray:
	TClonesArray &muons = *muonArray;
	new (muons[nMuonsStored++]) AliMUONTrackLight(*muon);
	delete muon;
      }

    }//part

    Int_t nmuons = muonArray->GetEntriesFast(); 
    Int_t ndimuons = 0; 
    for(Int_t nMu1 = 0; nMu1 < nmuons-1; nMu1++) { 
      AliMUONTrackLight* mu1 = (AliMUONTrackLight*) muonArray->At(nMu1); 
      for(Int_t nMu2 = nMu1+1; nMu2 < nmuons; nMu2++){
	AliMUONTrackLight* mu2 = (AliMUONTrackLight*) muonArray->At(nMu2); 
	AliMUONPairLight *dimuLight = new AliMUONPairLight(); 
	dimuLight->SetMuons(*mu1, *mu2);
	TClonesArray &dimuons = *dimuonArray;
	new (dimuons[ndimuons++]) AliMUONPairLight(*dimuLight);
      }
    }// end dimuons
    treeOut->Fill(); 
    stack->Reset();
  }//end of events

  RunLoader->UnloadKinematics();
  fout->cd(); 
  treeOut->Write();
}
 fastMUONSim.C:1
 fastMUONSim.C:2
 fastMUONSim.C:3
 fastMUONSim.C:4
 fastMUONSim.C:5
 fastMUONSim.C:6
 fastMUONSim.C:7
 fastMUONSim.C:8
 fastMUONSim.C:9
 fastMUONSim.C:10
 fastMUONSim.C:11
 fastMUONSim.C:12
 fastMUONSim.C:13
 fastMUONSim.C:14
 fastMUONSim.C:15
 fastMUONSim.C:16
 fastMUONSim.C:17
 fastMUONSim.C:18
 fastMUONSim.C:19
 fastMUONSim.C:20
 fastMUONSim.C:21
 fastMUONSim.C:22
 fastMUONSim.C:23
 fastMUONSim.C:24
 fastMUONSim.C:25
 fastMUONSim.C:26
 fastMUONSim.C:27
 fastMUONSim.C:28
 fastMUONSim.C:29
 fastMUONSim.C:30
 fastMUONSim.C:31
 fastMUONSim.C:32
 fastMUONSim.C:33
 fastMUONSim.C:34
 fastMUONSim.C:35
 fastMUONSim.C:36
 fastMUONSim.C:37
 fastMUONSim.C:38
 fastMUONSim.C:39
 fastMUONSim.C:40
 fastMUONSim.C:41
 fastMUONSim.C:42
 fastMUONSim.C:43
 fastMUONSim.C:44
 fastMUONSim.C:45
 fastMUONSim.C:46
 fastMUONSim.C:47
 fastMUONSim.C:48
 fastMUONSim.C:49
 fastMUONSim.C:50
 fastMUONSim.C:51
 fastMUONSim.C:52
 fastMUONSim.C:53
 fastMUONSim.C:54
 fastMUONSim.C:55
 fastMUONSim.C:56
 fastMUONSim.C:57
 fastMUONSim.C:58
 fastMUONSim.C:59
 fastMUONSim.C:60
 fastMUONSim.C:61
 fastMUONSim.C:62
 fastMUONSim.C:63
 fastMUONSim.C:64
 fastMUONSim.C:65
 fastMUONSim.C:66
 fastMUONSim.C:67
 fastMUONSim.C:68
 fastMUONSim.C:69
 fastMUONSim.C:70
 fastMUONSim.C:71
 fastMUONSim.C:72
 fastMUONSim.C:73
 fastMUONSim.C:74
 fastMUONSim.C:75
 fastMUONSim.C:76
 fastMUONSim.C:77
 fastMUONSim.C:78
 fastMUONSim.C:79
 fastMUONSim.C:80
 fastMUONSim.C:81
 fastMUONSim.C:82
 fastMUONSim.C:83
 fastMUONSim.C:84
 fastMUONSim.C:85
 fastMUONSim.C:86
 fastMUONSim.C:87
 fastMUONSim.C:88
 fastMUONSim.C:89
 fastMUONSim.C:90
 fastMUONSim.C:91
 fastMUONSim.C:92
 fastMUONSim.C:93
 fastMUONSim.C:94
 fastMUONSim.C:95
 fastMUONSim.C:96
 fastMUONSim.C:97
 fastMUONSim.C:98
 fastMUONSim.C:99
 fastMUONSim.C:100
 fastMUONSim.C:101
 fastMUONSim.C:102
 fastMUONSim.C:103
 fastMUONSim.C:104
 fastMUONSim.C:105
 fastMUONSim.C:106
 fastMUONSim.C:107
 fastMUONSim.C:108
 fastMUONSim.C:109
 fastMUONSim.C:110
 fastMUONSim.C:111
 fastMUONSim.C:112
 fastMUONSim.C:113
 fastMUONSim.C:114
 fastMUONSim.C:115
 fastMUONSim.C:116
 fastMUONSim.C:117
 fastMUONSim.C:118
 fastMUONSim.C:119
 fastMUONSim.C:120
 fastMUONSim.C:121
 fastMUONSim.C:122
 fastMUONSim.C:123
 fastMUONSim.C:124
 fastMUONSim.C:125
 fastMUONSim.C:126
 fastMUONSim.C:127
 fastMUONSim.C:128
 fastMUONSim.C:129
 fastMUONSim.C:130
 fastMUONSim.C:131
 fastMUONSim.C:132
 fastMUONSim.C:133
 fastMUONSim.C:134
 fastMUONSim.C:135
 fastMUONSim.C:136
 fastMUONSim.C:137
 fastMUONSim.C:138
 fastMUONSim.C:139
 fastMUONSim.C:140
 fastMUONSim.C:141
 fastMUONSim.C:142
 fastMUONSim.C:143
 fastMUONSim.C:144
 fastMUONSim.C:145
 fastMUONSim.C:146
 fastMUONSim.C:147
 fastMUONSim.C:148
 fastMUONSim.C:149
 fastMUONSim.C:150
 fastMUONSim.C:151
 fastMUONSim.C:152
 fastMUONSim.C:153
 fastMUONSim.C:154
 fastMUONSim.C:155
 fastMUONSim.C:156
 fastMUONSim.C:157
 fastMUONSim.C:158
 fastMUONSim.C:159
 fastMUONSim.C:160
 fastMUONSim.C:161
 fastMUONSim.C:162
 fastMUONSim.C:163
 fastMUONSim.C:164
 fastMUONSim.C:165
 fastMUONSim.C:166
 fastMUONSim.C:167
 fastMUONSim.C:168
 fastMUONSim.C:169
 fastMUONSim.C:170
 fastMUONSim.C:171
 fastMUONSim.C:172
 fastMUONSim.C:173
 fastMUONSim.C:174
 fastMUONSim.C:175
 fastMUONSim.C:176
 fastMUONSim.C:177
 fastMUONSim.C:178
 fastMUONSim.C:179
 fastMUONSim.C:180
 fastMUONSim.C:181
 fastMUONSim.C:182
 fastMUONSim.C:183
 fastMUONSim.C:184
 fastMUONSim.C:185
 fastMUONSim.C:186
 fastMUONSim.C:187
 fastMUONSim.C:188
 fastMUONSim.C:189
 fastMUONSim.C:190
 fastMUONSim.C:191
 fastMUONSim.C:192
 fastMUONSim.C:193
 fastMUONSim.C:194
 fastMUONSim.C:195
 fastMUONSim.C:196
 fastMUONSim.C:197
 fastMUONSim.C:198
 fastMUONSim.C:199
 fastMUONSim.C:200
 fastMUONSim.C:201
 fastMUONSim.C:202
 fastMUONSim.C:203
 fastMUONSim.C:204
 fastMUONSim.C:205
 fastMUONSim.C:206
 fastMUONSim.C:207
 fastMUONSim.C:208
 fastMUONSim.C:209
 fastMUONSim.C:210
 fastMUONSim.C:211
 fastMUONSim.C:212
 fastMUONSim.C:213