]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONrecoNtuple.C
Removing memory allocation in constructor AliMUONData(name,title)
[u/mrichter/AliRoot.git] / MUON / MUONrecoNtuple.C
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 /* $Id$ */
17
18
19
20 // Macro MUONrecoNtuple.C (TO BE COMPILED)
21 // for testing the C++ reconstruction code
22 // and producing an Ntuple with reconstructed tracks
23 // in output file "MUONtrackReco.root".
24 // An example for using the Ntuple is in the macro MUONmassPlot.C
25
26 // Arguments:
27 //   FirstEvent (default 0)
28 //   LastEvent (default 0)
29 //   RecGeantHits (1 to reconstruct GEANT hits) (default 0)
30 //   FileName (for signal) (default "galice.root")
31 //   BkgGeantFileName (for background),
32 //      needed only if RecGeantHits = 1 and background to be added
33
34 // IMPORTANT NOTICE FOR USERS:
35 // under "root" or "root.exe", execute the following commands:
36 // 1. "gSystem->SetIncludePath("-I$ALICE_ROOT/MUON -I$ALICE_ROOT/STEER -I$ROOTSYS/include")" to get the right path at compilation time
37 // 2. ".x loadlibs.C" to load the shared libraries
38 // 3. ".x MUONrecoNtuple.C+()" with the right arguments, without forgetting the "+" which implies the compilation of the macro before its execution
39
40 #include <iostream.h>
41
42 #include <TClassTable.h>
43 #include <TClonesArray.h>
44 #include <TFile.h>
45 #include <TParticle.h>
46 #include <TROOT.h>
47 #include <TTree.h>
48
49 #include "AliHeader.h"
50 #include "AliRun.h"
51
52 #include "AliMUON.h"
53 #include "AliMUONEventReconstructor.h"
54 #include "AliMUONTrack.h"
55 #include "AliMUONTrackHit.h"
56 #include "AliMUONTrackParam.h"
57
58 // Classes for Ntuple ////////////////////////////////////////////////////
59
60 class AliMUONTrackRecNtuple : public TObject {
61  public:
62   // for direct access to members
63   Int_t fCharge; // charge of reconstructed track (+/- 1)
64   Float_t fPxRec; // Px of reconstructed track at vertex (GeV/c)
65   Float_t fPyRec; // Py of reconstructed track at vertex (GeV/c)
66   Float_t fPzRec; // Pz of reconstructed track at vertex (GeV/c)
67   Float_t fZRec; // Z of reconstructed track at vertex (cm)
68   Float_t fZRec1; // Z of reconstructed track at first hit (cm)
69   Int_t fNHits; // number of hits
70   Float_t fChi2; // chi2 of fit
71   Float_t fPxGen; // Px of best compatible generated track at vertex (GeV/c)
72   Float_t fPyGen; // Py of best compatible generated track at vertex (GeV/c)
73   Float_t fPzGen; // Pz of best compatible generated track at vertex (GeV/c)
74   AliMUONTrackRecNtuple(){;} // Constructor
75   virtual ~AliMUONTrackRecNtuple(){;} // Destructor
76  protected:
77  private:
78   ClassDef(AliMUONTrackRecNtuple, 1) // AliMUONTrackRecNtuple
79     };
80
81 class AliMUONHeaderRecNtuple : public TObject {
82  public:
83   // for direct access to members
84   Int_t fEvent; // event number
85   AliMUONHeaderRecNtuple(){;} // Constructor
86   virtual ~AliMUONHeaderRecNtuple(){;} // Destructor
87  protected:
88  private:
89   ClassDef(AliMUONHeaderRecNtuple, 1) // AliMUONHeaderRecNtuple
90     };
91
92 ClassImp(AliMUONTrackRecNtuple) // Class implementation in ROOT context
93 ClassImp(AliMUONHeaderRecNtuple) // Class implementation in ROOT context
94
95   //__________________________________________________________________________
96 void AliMUONEventRecNtupleFill(AliMUONEventReconstructor *Reco, Int_t FillWrite = 0)
97 {
98   // Fill Ntuple for reconstructed event pointed to by "Reco"
99   // if "FillWrite" different from -1.
100   // Ntuple is created automatically before filling first event.
101   // If "FillWrite" = -1, write and close the file
102
103   static Bool_t firstTime = kTRUE;
104   static TTree *ntuple;
105   static AliMUONHeaderRecNtuple *header = new AliMUONHeaderRecNtuple();
106   static TClonesArray *recTracks = new TClonesArray("AliMUONTrackRecNtuple",5);
107
108   Int_t trackIndex;
109   AliMUONTrack *track;
110   AliMUONTrackParam *trackParam;
111   AliMUONTrackRecNtuple *recTrackNt;
112   Double_t bendingSlope, nonBendingSlope, pYZ;
113
114   if (FillWrite == -1) {
115     printf(">>> Writing Ntuple of reconstructed tracks\n");
116     // better to create the file before the Ntuple ????
117     TFile *file = new TFile("MUONtrackReco.root","recreate");
118     ntuple->Write();
119     file->Write();
120     file->Close();
121   }
122
123   if (firstTime) {
124     firstTime = kFALSE;
125     // first call: create tree for Ntuple...
126     printf(">>> Creating Ntuple of reconstructed tracks\n");
127     ntuple = new TTree("MUONtrackReco", "MUONtrackReco");
128     ntuple->Branch("Header","AliMUONHeaderRecNtuple", &header);
129     ntuple->Branch("Tracks", &recTracks);
130   }
131
132   // header
133   
134   header->fEvent = gAlice->GetHeader()->GetEvent();
135
136   TClonesArray *recoTracksPtr = Reco->GetRecTracksPtr();
137   recoTracksPtr->Compress(); // for simple loop without "Next" since no hole
138   recTracks->Clear(); // to reset the TClonesArray of tracks to be put in the ntuple
139   // Loop over reconstructed tracks
140   for (trackIndex = 0; trackIndex < Reco->GetNRecTracks(); trackIndex++) {
141     track = (AliMUONTrack*) ((*recoTracksPtr)[trackIndex]);
142     recTrackNt = (AliMUONTrackRecNtuple*)
143       new ((*recTracks)[trackIndex]) AliMUONTrackRecNtuple();
144     // track parameters at Vertex
145     trackParam = track->GetTrackParamAtVertex();
146     recTrackNt->fCharge =
147       Int_t(TMath::Sign(1., trackParam->GetInverseBendingMomentum()));
148     bendingSlope = trackParam->GetBendingSlope();
149     nonBendingSlope = trackParam->GetNonBendingSlope();
150     pYZ = 1/TMath::Abs(trackParam->GetInverseBendingMomentum());
151     recTrackNt->fPzRec = pYZ / TMath::Sqrt(1.0 + bendingSlope * bendingSlope);
152     recTrackNt->fPxRec = recTrackNt->fPzRec * nonBendingSlope;
153     recTrackNt->fPyRec = recTrackNt->fPzRec * bendingSlope;
154     recTrackNt->fZRec = trackParam->GetZ();
155     // track parameters at first hit
156     trackParam = ((AliMUONTrackHit*)
157                   (track->GetTrackHitsPtr()->First()))->GetTrackParam();
158     recTrackNt->fZRec1 = trackParam->GetZ();
159     // chi2
160     recTrackNt->fChi2 = track->GetFitFMin();
161     // number of hits
162     recTrackNt->fNHits = track->GetNTrackHits();
163     printf("test> Px %f Py %f Pz %f \n", recTrackNt->fPxRec,  recTrackNt->fPyRec,  recTrackNt->fPzRec);
164     // track parameters at vertex of best compatible generated track:
165     // in fact muon with the right charge
166     TTree* mtreeK=gAlice->TreeK();
167     TBranch *brparticle = mtreeK->GetBranch("Particles");
168     Int_t nPart = brparticle->GetEntries();
169     TParticle *particle = new TParticle();
170     mtreeK->SetBranchAddress("Particles",&particle);
171     for (Int_t iPart = 0; iPart < nPart; iPart++) {
172        brparticle->GetEntry(iPart);
173        //cout << "Code Particle: " << particle->GetPdgCode() << "\n";
174        if ((particle->GetPdgCode() * recTrackNt->fCharge) == -13) {
175         recTrackNt->fPxGen = particle->Px();
176         recTrackNt->fPyGen = particle->Py();
177         recTrackNt->fPzGen = particle->Pz();
178         printf("Gen: Px %f Py %f Pz %f \n", recTrackNt->fPxGen, recTrackNt->fPyGen, recTrackNt->fPzGen);
179        }  
180    }
181   } // for (trackIndex = 0;...
182
183   printf(">>> Filling Ntuple of reconstructed tracks\n");
184   ntuple->Fill();
185
186   return;
187 }
188
189 void MUONrecoNtuple (Int_t FirstEvent = 0, Int_t LastEvent = 0, Int_t RecGeantHits = 0, Text_t *FileName = "galice.root", Text_t *BkgGeantFileName = "")
190 {
191   //
192   cout << "MUON_recoNtuple" << endl;
193   cout << "FirstEvent " << FirstEvent << endl;
194   cout << "LastEvent " << LastEvent << endl;
195   cout << "RecGeantHits " << RecGeantHits << endl;
196   cout << "FileName ``" << FileName << "''" << endl;
197   cout << "BkgGeantFileName ``" << BkgGeantFileName << "''" << endl;
198 //   // Dynamically link some shared libs                    
199 //   if (gClassTable->GetID("AliRun") < 0) {
200 //     gROOT->LoadMacro("loadlibs.C");
201 //     loadlibs();
202 //   }
203
204
205   // Creating Run Loader and openning file containing Hits, Digits and RecPoints
206   AliRunLoader * RunLoader = AliRunLoader::Open(FileName,"Event","UPDATE");
207   if (RunLoader ==0x0) {
208     printf(">>> Error : Error Opening %s file \n",FileName);
209     return;
210   }
211   // Loading AliRun master
212   RunLoader->LoadgAlice();
213   gAlice = RunLoader->GetAliRun();
214   RunLoader->LoadKinematics("READ");
215   
216   // Loading MUON subsystem
217   AliMUON * MUON = (AliMUON *) gAlice->GetDetector("MUON");
218   AliLoader * MUONLoader = RunLoader->GetLoader("MUONLoader");
219   MUONLoader->LoadHits("READ");
220   MUONLoader->LoadRecPoints("READ");
221
222   Int_t ievent, nevents;
223   nevents = RunLoader->GetNumberOfEvents();
224
225   // Initializations
226   // AliMUON *MUON  = (AliMUON*) gAlice->GetModule("MUON"); // necessary ????
227   MUON->SetTreeAddress(); 
228   AliMUONEventReconstructor *Reco = new AliMUONEventReconstructor();
229
230   Reco->SetRecGeantHits(RecGeantHits);
231
232   // The right place for changing AliMUONEventReconstructor parameters
233   // with respect to the default ones
234 //   Reco->SetMaxSigma2Distance(100.0);
235 //  Reco->SetPrintLevel(20);
236    Reco->SetPrintLevel(1);
237 //   Reco->SetBendingResolution(0.0);
238 //   Reco->SetNonBendingResolution(0.0);
239   cout << "AliMUONEventReconstructor: actual parameters" << endl;
240   Reco->Dump();
241 //   gObjectTable->Print();
242   // Loop over events
243   if (LastEvent>nevents) LastEvent = nevents;
244   for (Int_t event = FirstEvent; event < LastEvent; event++) {
245     cout << "Event: " << event << endl;
246     RunLoader->GetEvent(event);   
247     //     Int_t nparticles = gAlice->GetEvent(event);
248     //      cout << "nparticles: " << nparticles << endl;
249     // prepare background file and/or event if necessary
250     if (RecGeantHits == 1) {
251       if (event == FirstEvent) Reco->SetBkgGeantFile(BkgGeantFileName);
252       if (Reco->GetBkgGeantFile())Reco->NextBkgGeantEvent();
253     }
254     Reco->EventReconstruct();
255     // Dump current event
256     Reco->EventDump();
257     // Fill Ntuple
258     AliMUONEventRecNtupleFill(Reco, 0);
259 //     gObjectTable->Print();
260     MUON->ResetRawClusters();
261   } // Event loop
262     // Write Ntuple
263     AliMUONEventRecNtupleFill(Reco, -1);
264 }