Extacting the OCDB in a separate module. The detectors have write permission in the...
[u/mrichter/AliRoot.git] / TRD / TRDcalib / AliTRDcalib.cxx
CommitLineData
da7d31b7 1#define AliTRDcalib_cxx
2// The class definition in Calib.h has been generated automatically
3// by the ROOT utility TTree::MakeSelector(). This class is derived
4// from the ROOT class TSelector. For more information on the TSelector
5// framework see $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
6
7// The following methods are defined in this file:
8// Begin(): called everytime a loop on the tree starts,
9// a convenient place to create your histograms.
10// SlaveBegin(): called after Begin(), when on PROOF called only on the
11// slave servers.
12// Process(): called for each event, in this function you decide what
13// to read and fill your histograms.
14// SlaveTerminate: called at the end of the loop on the tree, when on PROOF
15// called only on the slave servers.
16// Terminate(): called at the end of the loop on the tree,
17// a convenient place to draw/fit your histograms.
18//
19// To use this file, try the following session on your Tree T:
20//
21// Root > T->Process("AliTRDcalib.cxx")
22// Root > T->Process("AliTRDcalib.cxx","some options")
23// Root > T->Process("AliTRDcalib.cxx+")
24//
25
26#include "AliTRDcalib.h"
27#include <TTree.h>
28#include <TObject.h>
29#include <TH2.h>
30#include <TStyle.h>
31#include <TH2I.h>
32#include <TProfile2D.h>
33#include <TCanvas.h>
34#include <TStyle.h>
35
2729f359 36#include <AliESD.h>
da7d31b7 37#include <AliESDEvent.h>
38#include <AliESDfriend.h>
39#include <AliESDtrack.h>
40#include <AliESDfriendTrack.h>
41
42#include <AliTRDgeometry.h>
43#include <AliTRDtrack.h>
da7d31b7 44#include <AliCDBManager.h>
45#include <AliTRDCalibraFillHisto.h>
46#include <AliTRDCalibraVdriftLinearFit.h>
47
48
49AliTRDcalib::AliTRDcalib(TTree *) :
50 TSelector(),
2729f359 51 fESD(0),
da7d31b7 52 fev(0),
53 fevf(0),
54 fo(0),
55 ft(0),
da7d31b7 56 fesdTrack(0),
57 ffriendTrack(0),
58 fFileNo(0)
59 {
60 //G__SetCatchException(0);
61 }
62//_____________________________________________________________________
63void AliTRDcalib::Begin(TTree * /*tree*/)
64{
65 // The Begin() function is called at the start of the query.
66 // When running with PROOF Begin() is only called on the client.
67 // The tree argument is deprecated (on PROOF 0 is passed).
68
69 //TString option = GetOption();
70
71}
72//______________________________________________________________________
73void AliTRDcalib::SlaveBegin(TTree * tree)
74{
75 // The SlaveBegin() function is called after the Begin() function.
76 // When running with PROOF SlaveBegin() is called on each slave server.
77 // The tree argument is deprecated (on PROOF 0 is passed).
78
79 //printf("Slave Begin\n");
80
81 //TString option = GetOption();
82 if(tree) Init(tree);
83
84 fo = 0x0;
85 ft = 0x0;
da7d31b7 86 fesdTrack = 0x0;
87 ffriendTrack = 0x0;
88
89 AliCDBManager *cdbManager = AliCDBManager::Instance();
162637e4 90 cdbManager->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
da7d31b7 91 //cdbManager->SetSpecificStorage("TRD/Calib/FEE","local:///u/bailhach/aliroot/database30head/");
92 cdbManager->SetRun(0);
93
94 // instance calibration
95 fcalib = AliTRDCalibraFillHisto::Instance();
96 fcalib->SetNz(0,0);
97 fcalib->SetNrphi(0,0);
98 fcalib->SetNz(1,0);
99 fcalib->SetNrphi(1,0);
da7d31b7 100 fcalib->SetHisto2d();
101 fcalib->SetVector2d();
102 fcalib->SetLinearFitterOn();
103 fcalib->SetLinearFitterDebugOn();
104 fcalib->SetCH2dOn();
105 fcalib->SetPH2dOn();
106 fcalib->SetPRF2dOn();
2729f359 107 fcalib->Init2Dhistos();
da7d31b7 108 //fcalib->SetDebugLevel(1);
109 fcalib->SetNumberClusters(14);
110
111}
112//__________________________________________________________________________
113void AliTRDcalib::CleanESD(){
114 //
115 //printf("CleanESD\n");
116 if (fev!=0){
117 delete fev;
118 fev = 0;
119 }
120 if (fevf!=0){
121 delete fevf;
122 fevf =0;
123 }
124}
125//_________________________________________________________________________________
126Bool_t AliTRDcalib::Process(Long64_t entry)
127{
128 // The Process() function is called for each entry in the tree (or possibly
129 // keyed object in the case of PROOF) to be processed. The entry argument
130 // specifies which entry in the currently loaded tree is to be processed.
131 // It can be passed to either Calib::GetEntry() or TBranch::GetEntry()
132 // to read either all or the required parts of the data. When processing
133 // keyed objects with PROOF, the object is already loaded and is available
134 // via the fObject pointer.
135 //
136 // This function should contain the "body" of the analysis. It can contain
137 // simple or elaborate selection criteria, run algorithms on the data
138 // of the event and typically fill histograms.
139 //
140 // The processing can be stopped by calling Abort().
141 //
142 // Use fStatus to set the return value of TTree::Process().
143 //
144 // The return value is currently not used.
145 //printf("process\n");
2729f359 146
147
148 ////////////////////////////////
149 // Get the stuff
150 //////////////////////////////
151 Int_t status = ReadEvent(entry);
152 if (status<0) return status;
da7d31b7 153 Int_t ntr = fev->GetNumberOfTracks();
2729f359 154 Int_t nTRDcls = 0;
155
156 //////////////////////////////
157 // Loop over AliESDtrack
158 //////////////////////////////
da7d31b7 159
160 if(ntr>0){
161
162 fev->SetESDfriend(fevf);
163
164 //printf("Number of friends tracks %d\n",fevf->GetNumberOfTracks());
165
166
167 for(int itrk=0; itrk<fev->GetNumberOfTracks(); itrk++){
168
169 fesdTrack = fev->GetTrack(itrk);
170 if(!(nTRDcls = fesdTrack->GetTRDncls())) continue;
171 if(!(fesdTrack->GetFriendTrack())) continue;
172 ffriendTrack = new AliESDfriendTrack(*(fesdTrack->GetFriendTrack()));
173
174 Int_t icalib=0;
175 while((fo = (TObject *)(ffriendTrack->GetCalibObject(icalib++)))){
176 //printf("Name of calibObject %s\n",fo->IsA()->GetName());
177 if(strcmp(fo->IsA()->GetName(), "AliTRDtrack") != 0) continue;
178 //printf("\tfound %s @ 0x%x; calib object %d\n", fo->IsA()->GetName(), fo, icalib-1);
179 ft = (AliTRDtrack *)fo;
180
181 fcalib->UpdateHistograms(ft);
182 }
183 }
184 }
185
186 //CleanESD();
187 return kTRUE;
188}
189//______________________________________________________________________________________________
190void AliTRDcalib::SlaveTerminate()
191{
192 // The SlaveTerminate() function is called after all entries or objects
193 // have been processed. When running with PROOF SlaveTerminate() is called
194 // on each slave server.
195
196 if(!fOutput)
197 {
198 printf("ERROR: Output list not initialized\n");
199 return;
200 }
201
202 fCH2d = new TH2I(*(fcalib->GetCH2d()));
203 fPH2d = new TProfile2D(*(fcalib->GetPH2d()));
204 fPRF2d = new TProfile2D(*(fcalib->GetPRF2d()));
205
206 AliTRDCalibraVdriftLinearFit *ju = fcalib->GetVdriftLinearFit();
207 for(Int_t det = 0; det < 540; det++){
208 fVdriftLinear[det] = new TH2F(*(ju->GetLinearFitterHisto(det,kTRUE)));
209 }
210
211
212
213 fOutput->Add(fCH2d);
214 fOutput->Add(fPH2d);
215 fOutput->Add(fPRF2d);
216 for(Int_t det = 0; det < 540; det++){
217 fOutput->Add(fVdriftLinear[det]);
218 }
219
220 fcalib->Destroy();
221
222}
223//____________________________________________________________________________________
224void AliTRDcalib::Terminate()
225{
226 // The Terminate() function is the last function to be called during
227 // a query. It always runs on the client, it can be used to present
228 // the results graphically or save the results to file.
229
230
231 printf("InTerminate()\n");
232 if (!fOutput) return;
233
234 //fOutput->Print();
235
236 fCH2d = dynamic_cast<TH2I*>(fOutput->FindObject("CH2d"));
237
238 if (!fCH2d)
239 {
240 printf("Error: %s not returned\n","fCH2d");
241 return;
242 }
243
244 fPH2d = dynamic_cast<TProfile2D*>(fOutput->FindObject("PH2d"));
245
246 if (!fPH2d)
247 {
248 printf("Error: %s not returned\n","fPH2d");
249 return;
250 }
251
252 fPRF2d = dynamic_cast<TProfile2D*>(fOutput->FindObject("PRF2d"));
253
254 if (!fPRF2d)
255 {
256 printf("Error: %s not returned\n","fPRF2d");
257 return;
258 }
259
260
261 const char * Name = 0x0;
262 Name = "LFDV%dversion0";
263 TObjArray array(540);
264
265 for(Int_t det = 0; det < 540; det++){
266
267 TString Namehisto (Form(Name, det));
268 fVdriftLinear[det] = dynamic_cast<TH2F*>(fOutput->FindObject((const char *)Namehisto));
269 if (!fVdriftLinear[det])
270 {
271 printf("Error: %s not returned\n",(const char *)Namehisto);
272 return;
273 }
274 array.AddAt(fVdriftLinear[det],det);
275 }
276
277 AliTRDCalibraVdriftLinearFit vdriftlinearfit = AliTRDCalibraVdriftLinearFit(array);
278 vdriftlinearfit.FillPEArray();
279
280 gStyle->SetPalette(1);
281 gStyle->SetOptStat(1111);
282 gStyle->SetPadBorderMode(0);
283 gStyle->SetCanvasColor(10);
284 gStyle->SetPadLeftMargin(0.13);
285 gStyle->SetPadRightMargin(0.10);
286
287
288 printf("There are %d files analysed\n",fFileNo);
289
290
291 TCanvas *u = new TCanvas("u","",50,50,600,800);
292 u->Divide(3,1);
293 u->cd(1);
294 fCH2d->DrawCopy("lego");
295 u->cd(2);
296 fPH2d->DrawCopy("lego");
297 u->cd(3);
298 fPRF2d->DrawCopy("lego");
299
300 TObjArray *arrayE = vdriftlinearfit.GetEArray();
301
302 Double_t totalsum = 0.0;
303 for(Int_t k = 0; k < 540; k++){
304 TVectorD *h = (TVectorD *) arrayE->UncheckedAt(k);
305 if(h){
306 totalsum += (*h)[2];
307 }
308 }
309
310 TFile file("Output.root","recreate");
311 fOutput->Write();
312
313}
314//___________________________________________________________________________________________
315void AliTRDcalib::Init(TTree *tree)
316{
2729f359 317 // The Init() function is called when the selector needs to initialize
318 // a new tree or chain. Typically here the branch addresses and branch
319 // pointers of the tree will be set.
320 // It is normaly not necessary to make changes to the generated
321 // code, but the routine can be extended by the user if needed.
322 // Init() will be called many times when running on PROOF
323 // (once per file to be processed).
324
325 // Set branch addresses and branch pointers
326 if (!tree) return;
327 fChain = tree;
328 //if (counter>1) return;
329 tree->SetBranchStatus("*",1);
330 //
331 // New AliESDevent format
332 //
333 if (!fChain->GetBranch("ESD")){
334 //
335 //
336 //
337 if (fev) delete fev;
338 fev = new AliESDEvent();
339 fev->ReadFromTree(tree); // Attach the branch with ESD friends
340 fevf = (AliESDfriend*)fev->FindListObject("AliESDfriend");
341 tree->SetBranchAddress("ESDfriend.",&fevf);
342 return;
343 }
344
345 fChain->SetBranchAddress("ESD",&fESD);
346 Info("Init","Enter");
347 Bool_t isOK=kFALSE;
348 if (fChain->GetBranch("ESDfriend")) {
349 fChain->SetBranchAddress("ESDfriend",&fevf);
350 Info("Init","V0-ESDfriend.");
351 isOK=kTRUE;
352 }
353 if (fChain->GetBranch("ESDfriend.")){
354 Info("Init","V1-ESDfriend.");
355 fChain->SetBranchAddress("ESDfriend.",&fevf);
356 isOK=kTRUE;
357 }
358 return;
da7d31b7 359
360}
361//___________________________________________________________________________________________
362Bool_t AliTRDcalib::Notify()
363{
364 // The Notify() function is called when a new file is opened. This
365 // can be either for a new TTree in a TChain or when when a new TTree
366 // is started when using PROOF. It is normaly not necessary to make changes
367 // to the generated code, but the routine can be extended by the
368 // user if needed. The return value is currently not used.
369
370 ++fFileNo;
371 printf ("Processing file no %d\n",fFileNo);
372
373 return kTRUE;
374}
2729f359 375//__________________________________________________________________________________
376Int_t AliTRDcalib::ReadEvent(Long64_t entry){
377 //
378 //
379 //
380
381
382 if (!fChain) return -1;
383 if (!fChain->GetTree()) return -1;
384 try {
385 fChain->GetTree()->GetEntry(entry);
386 } catch (std::bad_alloc) {
387 printf("Pica vyjebana pojebany skurveny kokot piciak\n");
388 return -1;
389 }
390 if (!fESD && !fev) {
391 return -2;
392 }
393 Int_t ntracks = (fESD) ? fESD->GetNumberOfTracks() : fev->GetNumberOfTracks();
394
395 if (fev){
396 fev->SetESDfriend(fevf);
397 }
398
399
400 if (!fevf || fevf->GetNumberOfTracks() != ntracks) {
401 try {
402 delete fESD;
403 }
404 catch (std::bad_alloc) {
405 printf("Pica vyjebana pojebany skurveny kokot piciak\n");
406 fESD =0;
407 return -1;
408 }
409 return -3;
410 }
411 if (fESD) fESD->SetESDfriend(fevf);
412 return 0;
413}