]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/TRDcalib/AliTRDcalibV1.cxx
Extacting the OCDB in a separate module. The detectors have write permission in the...
[u/mrichter/AliRoot.git] / TRD / TRDcalib / AliTRDcalibV1.cxx
1 #define AliTRDcalibV1_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("AliTRDcalibV1.cxx")
22 // Root > T->Process("AliTRDcalibV1.cxx","some options")
23 // Root > T->Process("AliTRDcalibV1.cxx+")
24 //
25
26 #include "AliTRDcalibV1.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
36 #include <AliESD.h>
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>
44 #include <AliCDBManager.h>
45 #include <AliTRDCalibraFillHisto.h>
46 #include <AliTRDCalibraVdriftLinearFit.h>
47
48
49 AliTRDcalibV1::AliTRDcalibV1(TTree *) : 
50    TSelector(),
51    fESD(0),
52    fev(0),
53    fevf(0),
54    fo(0),
55    ft(0),
56    fesdTrack(0),
57    ffriendTrack(0),
58    fFileNo(0)     
59  {
60    //G__SetCatchException(0);     
61  }  
62 //_____________________________________________________________________
63 void AliTRDcalibV1::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 //______________________________________________________________________
73 void AliTRDcalibV1::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;
86   fesdTrack = 0x0;
87   ffriendTrack = 0x0;
88
89   AliCDBManager *cdbManager = AliCDBManager::Instance();
90   cdbManager->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
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);
100   fcalib->SetHisto2d();
101   fcalib->SetVector2d();
102   fcalib->SetLinearFitterOn();
103   fcalib->SetLinearFitterDebugOn();
104   fcalib->SetCH2dOn();
105   fcalib->SetPH2dOn();
106   fcalib->SetPRF2dOn();
107   fcalib->Init2Dhistos();
108   //fcalib->SetDebugLevel(1);
109   fcalib->SetNumberClusters(14);
110
111 }
112 //__________________________________________________________________________
113 void   AliTRDcalibV1::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 //_________________________________________________________________________________
126 Bool_t AliTRDcalibV1::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");
146  
147   if (!fChain) return kFALSE;  
148   //printf("process1\n");
149   Int_t nBytes;
150   Int_t nTRDcls = 0;
151   nBytes = fChain->GetTree()->GetEntry(entry);
152   //printf("There are %d bytes for these event\n",nBytes);
153   if (!fev || (nBytes == 0)) { 
154     return kFALSE;
155   }
156   if(fev->GetAliESDOld()) fev->CopyFromOldESD();
157   //printf("process2\n");
158   Int_t ntr = fev->GetNumberOfTracks();
159   //printf("Tracks new %d\n",ntr);
160   
161   if (!fevf || (fevf->GetNumberOfTracks()!=ntr)) {
162     return kFALSE;
163   }
164   
165   if(ntr>0){
166     
167     fev->SetESDfriend(fevf);
168     
169     //printf("Number of friends tracks %d\n",fevf->GetNumberOfTracks());
170     
171     
172     for(int itrk=0; itrk<fev->GetNumberOfTracks(); itrk++){
173      
174       fesdTrack = fev->GetTrack(itrk);
175       if(!(nTRDcls = fesdTrack->GetTRDncls())) continue;
176       if(!(fesdTrack->GetFriendTrack())) continue;
177       //ffriendTrack = new AliESDfriendTrack(*(fesdTrack->GetFriendTrack()));
178       ffriendTrack = fevf->GetTrack(itrk);        
179          
180       Int_t icalib=0;
181       while((fo = (TObject *)(ffriendTrack->GetCalibObject(icalib++)))){
182         //printf("Name of calibObject %s\n",fo->IsA()->GetName());
183         if(strcmp(fo->IsA()->GetName(), "AliTRDtrackV1") != 0) continue;
184         //printf("\tfound %s @ 0x%x; calib object %d\n", fo->IsA()->GetName(), fo, icalib-1);
185         ft = (AliTRDtrackV1 *)fo;
186         
187         fcalib->UpdateHistogramsV1(ft);
188       }
189     }
190   }
191   
192   //CleanESD();
193   return kTRUE;
194 }
195 //______________________________________________________________________________________________
196 void AliTRDcalibV1::SlaveTerminate()
197 {
198    // The SlaveTerminate() function is called after all entries or objects
199    // have been processed. When running with PROOF SlaveTerminate() is called
200    // on each slave server.
201
202   if(!fOutput)
203     {
204       printf("ERROR: Output list not initialized\n");
205       return;
206     }
207   
208   fCH2d = new TH2I(*(fcalib->GetCH2d()));
209   fPH2d = new TProfile2D(*(fcalib->GetPH2d()));
210   fPRF2d = new TProfile2D(*(fcalib->GetPRF2d()));
211
212   AliTRDCalibraVdriftLinearFit *ju = fcalib->GetVdriftLinearFit();
213   for(Int_t det = 0; det < 540; det++){
214     fVdriftLinear[det] = new TH2F(*(ju->GetLinearFitterHisto(det,kTRUE)));
215   }
216   
217
218
219   fOutput->Add(fCH2d);
220   fOutput->Add(fPH2d);
221   fOutput->Add(fPRF2d);
222   for(Int_t det = 0; det < 540; det++){
223     fOutput->Add(fVdriftLinear[det]);
224   }
225
226   fcalib->Destroy();
227  
228 }
229 //____________________________________________________________________________________
230 void AliTRDcalibV1::Terminate()
231 {
232    // The Terminate() function is the last function to be called during
233    // a query. It always runs on the client, it can be used to present
234    // the results graphically or save the results to file.
235
236  
237   printf("InTerminate()\n");
238   if (!fOutput) return;
239  
240   //fOutput->Print();
241   
242   fCH2d = dynamic_cast<TH2I*>(fOutput->FindObject("CH2d"));
243   
244   if (!fCH2d)
245     {
246       printf("Error: %s not returned\n","fCH2d");
247       return;
248     }
249
250   fPH2d = dynamic_cast<TProfile2D*>(fOutput->FindObject("PH2d"));
251   
252   if (!fPH2d)
253     {
254       printf("Error: %s not returned\n","fPH2d");
255       return;
256     }
257
258   fPRF2d = dynamic_cast<TProfile2D*>(fOutput->FindObject("PRF2d"));
259   
260   if (!fPRF2d)
261     {
262       printf("Error: %s not returned\n","fPRF2d");
263       return;
264     }
265
266
267   const char * Name = 0x0;
268   Name = "LFDV%dversion0";
269   TObjArray array(540);
270
271   for(Int_t det = 0; det < 540; det++){
272
273     TString Namehisto (Form(Name, det));
274     fVdriftLinear[det] = dynamic_cast<TH2F*>(fOutput->FindObject((const char *)Namehisto));
275     if (!fVdriftLinear[det])
276       {
277         printf("Error: %s not returned\n",(const char *)Namehisto);
278         return;
279       }
280     array.AddAt(fVdriftLinear[det],det);
281   }
282
283   AliTRDCalibraVdriftLinearFit vdriftlinearfit = AliTRDCalibraVdriftLinearFit(array);
284   vdriftlinearfit.FillPEArray();
285
286   gStyle->SetPalette(1);
287   gStyle->SetOptStat(1111);
288   gStyle->SetPadBorderMode(0);
289   gStyle->SetCanvasColor(10);
290   gStyle->SetPadLeftMargin(0.13);
291   gStyle->SetPadRightMargin(0.10);
292
293  
294   printf("There are %d files analysed\n",fFileNo);
295   
296   
297   TCanvas *u = new TCanvas("u","",50,50,600,800);
298   u->Divide(3,1);
299   u->cd(1);
300   fCH2d->DrawCopy("lego");
301   u->cd(2);
302   fPH2d->DrawCopy("lego");
303   u->cd(3);
304   fPRF2d->DrawCopy("lego");
305
306   TObjArray *arrayE = vdriftlinearfit.GetEArray();
307
308   Double_t totalsum = 0.0;
309   for(Int_t k = 0; k < 540; k++){
310     TVectorD *h = (TVectorD *) arrayE->UncheckedAt(k);
311     if(h){
312       totalsum += (*h)[2];
313     }
314   }
315
316   TFile file("Output.root","recreate");
317   fOutput->Write();  
318
319 }
320 //___________________________________________________________________________________________
321 void AliTRDcalibV1::Init(TTree *tree)
322 {
323    // The Init() function is called when the selector needs to initialize
324    // a new tree or chain. Typically here the branch addresses and branch
325    // pointers of the tree will be set.
326    // It is normaly not necessary to make changes to the generated
327    // code, but the routine can be extended by the user if needed.
328    // Init() will be called many times when running on PROOF
329    // (once per file to be processed).
330
331   // Set branch addresses and branch pointers
332   if (!tree) return;
333   fChain = tree;
334   //if (counter>1) return;
335   tree->SetBranchStatus("*",1);
336   //
337   // New AliESDevent format
338   //
339   if (!fChain->GetBranch("ESD")){
340     //
341     //
342     //
343     if (fev) delete fev;
344     fev = new AliESDEvent();
345     fev->ReadFromTree(tree); // Attach the branch with ESD friends
346     fevf = (AliESDfriend*)fev->FindListObject("AliESDfriend");
347     tree->SetBranchAddress("ESDfriend.",&fevf); 
348     return;
349   }
350   
351   fChain->SetBranchAddress("ESD",&fESD);
352   Info("Init","Enter");
353   Bool_t isOK=kFALSE;
354   if (fChain->GetBranch("ESDfriend")) {
355     fChain->SetBranchAddress("ESDfriend",&fevf);
356     Info("Init","V0-ESDfriend.");
357     isOK=kTRUE;
358   }
359   if (fChain->GetBranch("ESDfriend.")){
360     Info("Init","V1-ESDfriend.");
361     fChain->SetBranchAddress("ESDfriend.",&fevf);
362     isOK=kTRUE;
363   }
364   return;
365
366 }
367 //___________________________________________________________________________________________
368 Bool_t AliTRDcalibV1::Notify()
369 {
370    // The Notify() function is called when a new file is opened. This
371    // can be either for a new TTree in a TChain or when when a new TTree
372    // is started when using PROOF. It is normaly not necessary to make changes
373    // to the generated code, but the routine can be extended by the
374    // user if needed. The return value is currently not used.
375
376   ++fFileNo;
377   printf ("Processing file no %d\n",fFileNo);
378   
379   return kTRUE;
380 }
381 //__________________________________________________________________________________
382 Int_t AliTRDcalibV1::ReadEvent(Long64_t entry){
383   //
384   //
385   //
386
387
388   if (!fChain) return -1;  
389   if (!fChain->GetTree()) return -1; 
390   try {
391     fChain->GetTree()->GetEntry(entry);
392   } catch (std::bad_alloc) {
393     printf("Pica vyjebana pojebany skurveny kokot piciak\n");
394     return -1;
395   }
396   if (!fESD && !fev) { 
397     return -2;
398   }
399   Int_t ntracks = (fESD) ? fESD->GetNumberOfTracks() : fev->GetNumberOfTracks();   
400
401   if (fev){
402     fev->SetESDfriend(fevf);   
403   }
404
405   
406   if (!fevf || fevf->GetNumberOfTracks() != ntracks) {
407     try {
408       delete fESD;
409     }
410     catch (std::bad_alloc) {
411       printf("Pica vyjebana pojebany skurveny kokot piciak\n");
412       fESD =0;
413       return -1;
414     }
415     return -3;
416   }
417   if (fESD) fESD->SetESDfriend(fevf);
418   return 0;
419 }