Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibViewer.cxx
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: AliTRDCalibViewer.cxx 40390 2010-04-14 09:43:23Z cblume $ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Class which implements AliBaseCalibViewer for the TRD                    //
21 //  used for the calibration monitor                                         //
22 //                                                                           //
23 //  Authors:     Marian Ivanov (Marian.Ivanov@cern.ch)                       //
24 //               Jens Wiechula (Jens.Wiechula@cern.ch)                       //
25 //               Ionut Arsene  (iarsene@cern.ch)                             //
26 //                                                                           //
27 ///////////////////////////////////////////////////////////////////////////////
28
29 #include <iostream>
30 #include <fstream>
31 #include <TString.h>
32 #include <TRandom.h>
33 #include <TLegend.h>
34 #include <TLine.h>
35 #include <TCanvas.h>
36 #include <TROOT.h>
37 #include <TStyle.h>
38 #include <TH1.h> 
39 #include <TH1F.h>
40 #include <TMath.h>
41 #include <TVectorD.h>
42 #include <THashTable.h>
43 #include <TObjString.h>
44 #include <TTimeStamp.h>
45 #include <TObjString.h>
46 #include <TTreeStream.h>
47 #include <TFile.h>
48 #include <TKey.h>
49 #include <TGraph.h>
50 #include <TDirectory.h>
51 #include <TFriendElement.h>
52 #include <TGrid.h>
53 #include <TGeoManager.h>
54 #include "AliTRDCalDet.h"
55 #include "AliTRDCalPad.h"
56 #include "AliTRDCalROC.h"
57 #include "AliTRDCalChamberStatus.h"
58 #include "AliTRDCalSingleChamberStatus.h"
59 #include "AliTRDCalPadStatus.h"
60 #include "AliTRDCalDCSv2.h"
61 #include "AliTRDCalDCSFEEv2.h"
62 #include "AliTRDcalibDB.h"
63 #include "AliCDBManager.h"
64 #include "AliCDBStorage.h"
65 #include "AliCDBEntry.h"
66 #include "AliGRPObject.h"
67 #include "AliTRDalignment.h"
68 #include "AliTRDgeometry.h"
69 #include "AliTRDpadPlane.h"
70
71 #include "AliTRDCalibViewer.h"
72
73
74 using namespace std;
75
76 ClassImp(AliTRDCalibViewer)
77
78 //_____________________________________________________________________________
79 AliTRDCalibViewer::AliTRDCalibViewer()
80                   :AliBaseCalibViewer()
81 {
82   //
83   // Default constructor (just call base class constructor)
84   //
85 }
86
87 //_____________________________________________________________________________
88 AliTRDCalibViewer::AliTRDCalibViewer(const AliTRDCalibViewer &c)
89                   :AliBaseCalibViewer(c)
90 {
91   //
92   // copy constructor (just call base class copy constructor)
93   // 
94 }
95
96 //_____________________________________________________________________________
97 AliTRDCalibViewer::AliTRDCalibViewer(TTree* tree)
98                   :AliBaseCalibViewer(tree)
99 {
100   //
101   // Constructor (just call the corresponding base constructor)
102   //
103 }
104
105 //_____________________________________________________________________________
106 AliTRDCalibViewer::AliTRDCalibViewer(const char* fileName, const char* treeName)
107                   :AliBaseCalibViewer(fileName, treeName)                   
108 {
109    //
110    // Constructor (just call the corresponding base constructor)
111    //
112 }
113
114 //_____________________________________________________________________________
115 AliTRDCalibViewer& AliTRDCalibViewer::operator=(const AliTRDCalibViewer& param) 
116 {
117   //
118   // assignment operator
119   //
120
121   if (this == &param) {
122     return *this;
123   }
124
125   fTree = param.fTree;
126   fTreeMustBeDeleted = param.fTreeMustBeDeleted;
127   fListOfObjectsToBeDeleted = param.fListOfObjectsToBeDeleted;
128   fAbbreviation = param.fAbbreviation;
129   fAppendString = param.fAppendString;
130
131   return(*this);
132
133 }
134
135 //_____________________________________________________________________________
136 AliTRDCalibViewer::~AliTRDCalibViewer()
137 {
138    //
139    // AliTRDCalibViewer destructor
140    // do nothing, the base class destructor will do the job
141 }
142
143 /*
144 //_____________________________________________________________________________
145 void AliTRDCalibViewer::GetTimeInfoOCDB(const Char_t* runList, const Char_t* outFile,
146                                         Int_t firstRun, Int_t lastRun, UInt_t infoFlags,
147                                         const Char_t* ocdbStorage) {
148 //
149 //  Get time information from OCDB by calling the DumpOCDBtoTree.C macro
150 //
151   DumpOCDBtoTree(runList, outFile, firstRun, lastRun,
152                  TESTBIT(infoFlags,1), TESTBIT(infoFlags,2), 
153                  TESTBIT(infoFlags,3), TESTBIT(infoFlags,4),
154                  ocdbStorage);
155 }
156 */
157
158 //_____________________________________________________________________________
159 const char* AliTRDCalibViewer::AddAbbreviations(char* c, Bool_t printDrawCommand){ 
160    // Replace all "<variable>" with "<variable><fAbbreviation>" (Adds forgotten "~")
161    // but take care on the statistical information, like "CEQmean_Mean"
162    // and also take care on correct given variables, like "CEQmean~"
163    // 
164    // For each variable out of "listOfVariables":
165    // - 'Save' correct items:
166    //   - form <replaceString>, take <variable>'s first char, add <removeString>, add rest of <variable>, e.g. "C!#EQmean" (<removeString> = "!#")
167    //   - For each statistical information in "listOfNormalizationVariables":
168    //     - ReplaceAll <variable><statistical_Information> with <replaceString><statistical_Information>
169    //   - ReplaceAll <variable><abbreviation> with <replaceString><abbreviation>, e.g. "CEQmean~" -> "C!#EQmean~"
170    //   - ReplaceAll <variable><appendStr> with <replaceString><appendStr>, e.g. "CEQmean.fElements" -> "C!#EQmean.fElements"
171    //
172    // - Do actual replacing:
173    //   - ReplaceAll <variable> with <variable><fAbbreviation>, e.g. "CEQmean" -> "CEQmean~"
174    //
175    // - Undo saving:
176    //   - For each statistical information in "listOfNormalizationVariables":
177    //     - ReplaceAll <replaceString><statistical_Information> with <variable><statistical_Information> 
178    //   - ReplaceAll <replaceString><abbreviation> with <variable><abbreviation>, e.g. "C!#EQmean~" -> "CEQmean~"
179    //   - ReplaceAll <replaceString><appendStr> with <variable><appendStr>, e.g. "C!#EQmean.fElements" -> "CEQmean.fElements"
180    // 
181    // Now all the missing "~" should be added.
182    
183    TString str(c);
184    TString removeString = "!#";  // very unprobable combination of chars
185    TString replaceString = "";
186    TString searchString = "";
187    TString normString = "";
188    TObjArray *listOfVariables = GetListOfVariables();
189    // variables used for mapping the pads, mcms, ...
190    listOfVariables->Add(new TObjString("SuperModule"));
191    listOfVariables->Add(new TObjString("Layer"));
192    listOfVariables->Add(new TObjString("Stack"));
193    listOfVariables->Add(new TObjString("Channel"));
194    listOfVariables->Add(new TObjString("Row"));
195    listOfVariables->Add(new TObjString("Column"));
196    listOfVariables->Add(new TObjString("Chamber"));
197    listOfVariables->Add(new TObjString("PadSuperRow"));
198    listOfVariables->Add(new TObjString("PadSuperColumn"));
199    listOfVariables->Add(new TObjString("MCMSuperRow"));
200    listOfVariables->Add(new TObjString("MCMSuperColumn"));
201    listOfVariables->Add(new TObjString("ROB"));
202    listOfVariables->Add(new TObjString("MCM"));
203    TObjArray *listOfNormalizationVariables = GetListOfNormalizationVariables();
204    Int_t nVariables = listOfVariables->GetEntriesFast();
205    Int_t nNorm = listOfNormalizationVariables->GetEntriesFast();
206    
207    Int_t *varLengths = new Int_t[nVariables];
208    for (Int_t i = 0; i < nVariables; i++) {
209       varLengths[i] = ((TObjString*)listOfVariables->At(i))->String().Length();
210    }
211    Int_t *normLengths = new Int_t[nNorm];
212    for (Int_t i = 0; i < nNorm; i++) {
213       normLengths[i] = ((TObjString*)listOfNormalizationVariables->At(i))->String().Length();
214    }
215    Int_t *varSort = new Int_t[nVariables+1];
216    TMath::Sort(nVariables, varLengths, varSort, kTRUE);
217    Int_t *normSort = new Int_t[nNorm+1];
218    TMath::Sort(nNorm, normLengths, normSort, kTRUE);
219       
220    for (Int_t ivar = 0; ivar < nVariables; ivar++) {
221       // ***** save correct tokens *****
222       // first get the next variable:
223       searchString = ((TObjString*)listOfVariables->At(varSort[ivar]))->String();
224       // form replaceString:
225       replaceString = "";
226       for (Int_t i = 0; i < searchString.Length(); i++) {
227          replaceString.Append(searchString[i]);
228          if (i == 0) replaceString.Append(removeString);
229       }
230       // go through normalization:
231       for (Int_t inorm = 0; inorm < nNorm; inorm++) {
232         normString = ((TObjString*)listOfNormalizationVariables->At(normSort[inorm]))->String();
233         str.ReplaceAll(searchString + normString, replaceString + normString);
234         // like: str.ReplaceAll("CEQmean_Mean", "C!EQmean_Mean");
235       }
236       str.ReplaceAll(searchString + fAbbreviation, replaceString + fAbbreviation);
237       // like: str.ReplaceAll("CEQmean~", "C!EQmean~");
238       str.ReplaceAll(searchString + fAppendString,    replaceString + fAppendString);
239       // like: str.ReplaceAll("CEQmean.fElements", "C!EQmean.fElements");
240       
241       // ***** add missing extensions *****
242       str.ReplaceAll(searchString, replaceString + fAbbreviation);
243       // like: str.ReplaceAll("CEQmean", "C!EQmean~");
244    }
245    
246    // ***** undo saving *****
247    str.ReplaceAll(removeString, "");
248   
249    if (printDrawCommand) std::cout << "The string looks now like: " << str.Data() << std::endl;
250    delete [] varSort;
251    delete [] normSort;
252    return str.Data();
253 }
254
255 //_____________________________________________________________________________
256 TObjArray* AliTRDCalibViewer::GetListOfVariables(Bool_t printList) {
257   //
258   // scan the tree  - produces a list of available variables in the tree
259   // printList: print the list to the screen, after the scan is done
260   //
261   TObjArray* arr = new TObjArray();
262   TObjString* str = 0;
263   if (!fTree) {
264     return 0;
265   }
266   Int_t nentries = fTree->GetListOfBranches()->GetEntries();
267   for (Int_t i = 0; i < nentries; i++) {
268     str = new TObjString(fTree->GetListOfBranches()->At(i)->GetName());
269     str->String().ReplaceAll("_Mean", "");
270     str->String().ReplaceAll("_RMS", "");
271     str->String().ReplaceAll("_Median", "");
272     str->String().ReplaceAll(".", "");
273     str->String().ReplaceAll("_Run", "");
274     str->String().ReplaceAll("_SuperModule", "");
275     str->String().ReplaceAll("_Chamber", "");
276     // add all the variables in the tree to a list
277     // exception make variables which are used for mapping, specified in AddAbbreviations()
278     // These two functions should be kept synchronized with respect to the mapping variables 
279     if (!arr->FindObject(str) && 
280         !(str->String() == "run" || 
281           str->String() == "SuperModule" || str->String() == "Layer" || str->String() == "Stack" || 
282           str->String() == "Chamber" ||
283           str->String() == "Channel" || str->String() == "Row" || str->String() == "Column" || 
284           str->String() == "PadSuperRow" || str->String() == "PadSuperColumn" || str->String() == "MCMSuperRow"
285           || str->String() == "MCMSuperColumn" || str->String() == "MCM" || str->String() == "ROB")) {
286       arr->Add(str);
287     }
288   }
289   
290   // loop over all friends (if there are some) and add them to the list
291   if (fTree->GetListOfFriends()) {
292     for (Int_t ifriend = 0; ifriend < fTree->GetListOfFriends()->GetEntries(); ifriend++){
293       // printf("iterating through friendlist, currently at %i\n", ifriend);
294       // printf("working with %s\n", fTree->GetListOfFriends()->At(ifriend)->ClassName());
295       if (TString(fTree->GetListOfFriends()->At(ifriend)->ClassName()) != "TFriendElement") continue; // no friendElement found
296       TFriendElement *friendElement = (TFriendElement*)fTree->GetListOfFriends()->At(ifriend);
297       if (friendElement->GetTree() == 0) continue; // no tree found in friendElement
298       // printf("friend found \n");
299       for (Int_t i = 0; i < friendElement->GetTree()->GetListOfBranches()->GetEntries(); i++) {
300         // printf("iterating through friendelement entries, currently at %i\n", i);
301         str = new TObjString(friendElement->GetTree()->GetListOfBranches()->At(i)->GetName());
302         str->String().ReplaceAll("_Mean", "");
303         str->String().ReplaceAll("_RMS", "");
304         str->String().ReplaceAll("_Median", "");
305         str->String().ReplaceAll(".", "");
306         str->String().ReplaceAll("_Run", "");
307         str->String().ReplaceAll("_SuperModule", "");
308         str->String().ReplaceAll("_Chamber", "");
309         if (!(str->String() == "run" || 
310               str->String() == "SuperModule" || str->String() == "Layer" || str->String() == "Stack" || 
311               str->String() == "Chamber" ||
312               str->String() == "Channel" || str->String() == "Row" || str->String() == "Column" || 
313               str->String() == "PadSuperRow" || str->String() == "PadSuperColumn" || str->String() == "MCMSuperRow"
314               || str->String() == "MCMSuperColumn" || str->String() == "MCM" || str->String() == "ROB")){
315           // insert "<friendName>." at the beginning: (<friendName> is per default "R")
316           str->String().Insert(0, ".");
317           str->String().Insert(0, friendElement->GetName());
318           if (!arr->FindObject(str)) {
319             arr->Add(str);
320           }
321           // printf("added string %s \n", str->String().Data());
322         }
323       }
324     }
325   } // if (fTree->GetListOfFriends())
326   
327   arr->Sort();
328     
329   if (printList) {
330     TIterator* iter = arr->MakeIterator();
331     iter->Reset();
332     TObjString* currentStr = 0;
333     while ( (currentStr = (TObjString*)(iter->Next())) ) {
334       std::cout << currentStr->GetString().Data() << std::endl;
335     }
336     delete iter;
337   }
338   return arr;
339 }
340
341 TObjArray* AliTRDCalibViewer::GetListOfNormalizationVariables(Bool_t printList) const{
342   //
343   // produces a list of available variables for normalization in the tree
344   // printList: print the list to the screen, after the scan is done
345   //
346    TObjArray* arr = new TObjArray();
347    arr->Add(new TObjString("_Mean_Run"));
348    arr->Add(new TObjString("_Mean_SuperModule"));
349    arr->Add(new TObjString("_Mean_Chamber"));
350    arr->Add(new TObjString("_Median_Run"));
351    arr->Add(new TObjString("_Median_SuperModule"));
352    arr->Add(new TObjString("_Median_Chamber"));
353    
354    if (printList) {
355      TIterator* iter = arr->MakeIterator();
356      iter->Reset();
357      TObjString* currentStr = 0;
358      while ((currentStr = (TObjString*)(iter->Next()))) {
359        std::cout << currentStr->GetString().Data() << std::endl;
360      }
361      delete iter;
362    }
363    return arr;
364 }
365
366 void AliTRDCalibViewer::GetLayerSectorStack(TString trdString, Int_t& layerNo, Int_t& sectorNo, Int_t& stackNo) const {
367   // Get the layer, sector and stack numbers out of a string
368   // encoded with the following format:
369   // Layer%dSector%dStack%d
370
371   //sscanf(trdString.Data(), "Layer%1dSector%02dStack%1d", &layerNo, &sectorNo, &stackNo);
372
373   // Coverity compliant solution (bit more cumbersome, but works) CBL
374   TString cName = trdString.Data();
375   Char_t  cLayer[2];
376   Char_t  cSector[3];
377   Char_t  cStack[2];
378
379   cLayer[0]  = cName[5];
380   cLayer[1]  = 0;
381   cSector[0] = cName[12];
382   cSector[1] = cName[13];
383   cSector[2] = 0;
384   cStack[0]  = cName[19];
385   cStack[1]  = 0;
386
387   layerNo    = atoi(cLayer);
388   sectorNo   = atoi(cSector);
389   stackNo    = atoi(cStack);
390
391   return;
392 }
393
394 //_____________________________________________________________________________
395 Int_t AliTRDCalibViewer::EasyDraw(const char* drawCommand, const char* sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
396   //
397   // easy drawing of data, use '~' for abbreviation of '.fElements'
398   // example: EasyDraw("CETmean~-CETmean_mean", "A", "(CETmean~-CETmean_mean)>0")
399   // sector: sector-number - only the specified sector will be drwawn
400   //         'A'/'C' or 'a'/'c' - side A/C will be drawn
401   //         'ALL' - whole TPC will be drawn, projected on one side
402   // cuts: specifies cuts
403   // drawOptions: draw options like 'same'
404   // writeDrawCommand: write the command, that is passed to TTree::Draw
405   //
406
407   TString drawStr(drawCommand);
408
409   TString sectorStr(sector);
410   Int_t layerNo = -1; 
411   Int_t sectorNo = -1; 
412   Int_t stackNo = -1;
413   GetLayerSectorStack(sectorStr, layerNo, sectorNo, stackNo);
414   if(layerNo==-1) {
415      Warning("EasyDraw", "The sector string must always contain the Layer number!");
416      return -1;
417    }
418    if(layerNo<0 || layerNo>5) {
419      Warning("EasyDraw", "The Layer number must be in the range [0,5] !");
420      return -1;
421    }
422    if(sectorNo!=-1 && (sectorNo<0 || sectorNo>17)) {
423      Warning("EasyDraw", "The SuperModule number must be in the range [0,17] !");
424      return -1;
425    }
426    if(stackNo!=-1 && (stackNo<0 || stackNo>4)) {
427      Warning("EasyDraw", "The Stack number must be in the range [0,4] !");
428      return -1;
429    }
430
431    TString cutStr("");
432
433    Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
434    if (dangerousToDraw) {
435       Warning("EasyDraw", "The draw string must not contain ':' or '>>'. Using only first variable for drawing!");
436       drawStr.Resize(drawStr.First(":"));
437    }
438
439    TString drawOptionsStr("");
440    TRandom rnd(0);
441    Int_t rndNumber = rnd.Integer(10000);
442
443    if (drawOptions && strcmp(drawOptions, "") != 0)
444       drawOptionsStr += drawOptions;
445    else
446       drawOptionsStr += "profcolz";
447
448    const Int_t gkNRows[ 5] = {16, 16, 12, 16, 16};  // number of pad rows in the chambers from each of the 5 stacks
449
450    // draw calibration stuff
451    if(drawStr.Contains("Status") || drawStr.Contains("Gain") || drawStr.Contains("Noise") ||
452       drawStr.Contains("Vdrift") || drawStr.Contains("T0") || 
453       drawStr.Contains("gain") || drawStr.Contains("chiSquare")) {
454      if(sectorNo==-1 && stackNo==-1) {    // plot the entire layer
455        drawStr += Form(":PadSuperColumn%s:PadSuperRow%s>>prof", fAppendString.Data(), fAppendString.Data());
456        drawStr += rndNumber;
457        drawStr += "(76,-0.5,75.5,2592,-0.5,2591.5)";
458        cutStr += Form("Layer==%d", layerNo);
459      }
460      else if(sectorNo!=-1 && stackNo==-1) {     // plot a sector from a layer
461        drawStr += Form(":Column%s:PadSuperRow%s>>prof", fAppendString.Data(), fAppendString.Data());
462        drawStr += rndNumber;
463        drawStr += "(76,-0.5,75.5,144,-0.5,143.5)";
464        cutStr += Form("Layer==%d && SuperModule==%d", layerNo, sectorNo);
465      }
466      else if(sectorNo==-1 && stackNo!=-1) {       // plot a stack from a layer
467        drawStr += Form(":PadSuperColumn%s:Row%s>>prof", fAppendString.Data(), fAppendString.Data());
468        drawStr += rndNumber;
469        drawStr += Form("(%d,-0.5,%d-0.5,2592,-0.5,2591.5)", gkNRows[stackNo], gkNRows[stackNo]);
470        cutStr += Form("Layer==%d && Stack==%d", layerNo, stackNo);
471      }
472      else {                // the layer, sector and stack are determined -> so plot a chamber
473        drawStr += Form(":Column%s:Row%s>>prof", fAppendString.Data(), fAppendString.Data());
474        drawStr += rndNumber;
475        drawStr += Form("(%d,-0.5,%d-0.5,144,-0.5,143.5)", gkNRows[stackNo], gkNRows[stackNo]);
476        cutStr += Form("Layer==%d && SuperModule==%d && Stack==%d", layerNo, sectorNo, stackNo);
477      }
478    }
479    // draw FEE stuff
480    else if(drawStr.Contains("SORandEOR") || 
481            drawStr.Contains("gsmSOR") || drawStr.Contains("gsmDelta") ||
482            drawStr.Contains("nimSOR") || drawStr.Contains("nimDelta") ||
483            drawStr.Contains("nevSOR") || drawStr.Contains("nevDelta") ||
484            drawStr.Contains("nptSOR") || drawStr.Contains("nptDelta")) {
485      if(sectorNo==-1 && stackNo==-1) {    // plot the entire layer
486        drawStr += Form(":MCMSuperColumn%s:MCMSuperRow%s>>prof", fAppendString.Data(), fAppendString.Data());
487        drawStr += rndNumber;
488        drawStr += "(76,-0.5,75.5,144,-0.5,143.5)";
489        cutStr += Form("Layer==%d", layerNo);
490      }
491      else if(sectorNo!=-1 && stackNo==-1) {     // plot a sector from a layer
492        drawStr += Form(":MCMSuperColumn%s:MCMSuperRow%s>>prof", fAppendString.Data(), fAppendString.Data());
493        drawStr += rndNumber;
494        drawStr += "(76,-0.5,75.5,144,-0.5,143.5)";
495        cutStr += Form("Layer==%d && SuperModule==%d", layerNo, sectorNo);
496      }
497      else if(sectorNo==-1 && stackNo!=-1) {       // plot a stack from a layer
498        drawStr += Form(":MCMSuperColumn%s:MCMSuperRow%s>>prof", fAppendString.Data(), fAppendString.Data());
499        drawStr += rndNumber;
500        //       drawStr += Form("(%d,-0.5,%d-0.5,2592,-0.5,2591.5)", gkNRows[stackNo], gkNRows[stackNo]);
501        drawStr += "(76,-0.5,75.5,144,-0.5,143.5)";
502        cutStr += Form("Layer==%d && Stack==%d", layerNo, stackNo);
503      }
504      else {                // the layer, sector and stack are determined -> so plot a chamber
505        drawStr += Form(":ROB%s:MCM%s>>prof", fAppendString.Data(), fAppendString.Data());
506        drawStr += rndNumber;
507        drawStr += Form("(16,-0.5,15.5,%d,-0.5,%d-0.5)", gkNRows[stackNo]/2, gkNRows[stackNo]/2);
508        cutStr += Form("Layer==%d && SuperModule==%d && Stack==%d", layerNo, sectorNo, stackNo);
509      }
510    }
511    // draw alignment stuff
512    else if(drawStr.Contains("Align")) {
513      if(sectorNo==-1 && stackNo==-1) {  // plot the entire layer
514        drawStr += ":SuperModule:Stack>>prof";
515        drawStr += rndNumber;
516        drawStr += "(5,-0.5,4.5,18,-0.5,17.5)";
517        cutStr += Form("Layer==%d", layerNo);
518      }
519      else if(sectorNo!=-1 && stackNo==-1) {     // plot a sector from a layer
520        drawStr += ":SuperModule:Stack>>prof";
521        drawStr += rndNumber;
522        drawStr += Form("(5,-0.5,4.5,1,%f,%f)", sectorNo-0.5, sectorNo+0.5);
523        cutStr += Form("Layer==%d && SuperModule==%d", layerNo, sectorNo);
524      }
525      else if(sectorNo==-1 && stackNo!=-1) {       // plot a stack from a layer
526        drawStr += ":SuperModule:Stack>>prof";
527        drawStr += rndNumber;
528        drawStr += Form("(1,%f,%f,18,-0.5,17.5)", stackNo-0.5, stackNo+0.5);
529        cutStr += Form("Layer==%d && Stack==%d", layerNo, stackNo);
530      }
531      else {                // the layer, sector and stack are determined -> so plot a chamber
532        drawStr += ":SuperModule:Stack>>prof";
533        drawStr += rndNumber;
534        drawStr += Form("(1,%f,%f,1,%f,%f)", stackNo-0.5, stackNo+0.5, sectorNo-0.5, sectorNo+0.5);
535        cutStr += Form("Layer==%d && SuperModule==%d && Stack==%d", layerNo, sectorNo, stackNo);
536      }
537    }
538
539
540    if (cuts && cuts[0] != 0) {
541       if (cutStr.Length() != 0) cutStr += "&& ";
542       cutStr += "(";
543       cutStr += cuts;
544       cutStr += ")";
545    }
546    drawStr.ReplaceAll(fAbbreviation, fAppendString);
547    cutStr.ReplaceAll(fAbbreviation, fAppendString);
548    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
549    Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
550    TString profName("prof");
551    profName += rndNumber;
552    TObject *obj = gDirectory->Get(profName.Data());
553    // set the names of the axes 
554    TH1 *histObj = (TH1*)obj;
555    if(drawStr.Contains("Status") || drawStr.Contains("Gain") || drawStr.Contains("Noise") ||
556       drawStr.Contains("Vdrift") || drawStr.Contains("T0") ||
557       drawStr.Contains("gain") || drawStr.Contains("chiSquare")) {
558      histObj->GetXaxis()->SetTitle("Row");
559      histObj->GetYaxis()->SetTitle("Column");
560    }
561    else if(drawStr.Contains("SORandEOR") || 
562            drawStr.Contains("gsmSOR") || drawStr.Contains("gsmDelta") ||
563            drawStr.Contains("nimSOR") || drawStr.Contains("nimDelta") ||
564            drawStr.Contains("nevSOR") || drawStr.Contains("nevDelta") ||
565            drawStr.Contains("nptSOR") || drawStr.Contains("nptDelta")) {
566      histObj->GetXaxis()->SetTitle("MCM Row");
567      histObj->GetYaxis()->SetTitle("MCM Column");
568    }
569    else if(drawStr.Contains("Align")) {
570      histObj->GetXaxis()->SetTitle("Stack");
571      histObj->GetYaxis()->SetTitle("Sector");
572    }
573
574    if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
575    return returnValue;
576 }
577
578 //_____________________________________________________________________________
579 Int_t AliTRDCalibViewer::EasyDraw1D(const char* drawCommand, const char* sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
580   //
581   // easy drawing of data, use '~' for abbreviation of '.fElements'
582   // example: EasyDraw("CETmean~-CETmean_mean", "A", "(CETmean~-CETmean_mean)>0")
583   // sector: sector-number - the specified sector will be drwawn
584   //         'A'/'C' or 'a'/'c' - side A/C will be drawn
585   //         'ALL' - whole TPC will be drawn, projected on one side
586   // cuts: specifies cuts
587   // drawOptions: draw options like 'same'
588   // writeDrawCommand: write the command, that is passed to TTree::Draw
589   //
590
591    TString drawStr(drawCommand);
592
593    TString sectorStr(sector);
594    Int_t layerNo = -1; 
595    Int_t sectorNo = -1; 
596    Int_t stackNo = -1;
597    GetLayerSectorStack(sectorStr, layerNo, sectorNo, stackNo);
598    if(layerNo==-1) {
599      Warning("EasyDraw", "The sector string must always contain the Layer number!");
600      return -1;
601    }
602    if(layerNo<0 || layerNo>5) {
603      Warning("EasyDraw", "The Layer number must be in the range [0,5] !");
604      return -1;
605    }
606    if(sectorNo!=-1 && (sectorNo<0 || sectorNo>17)) {
607      Warning("EasyDraw", "The Sector number must be in the range [0,17] !");
608      return -1;
609    }
610    if(stackNo!=-1 && (stackNo<0 || stackNo>4)) {
611      Warning("EasyDraw", "The Stack number must be in the range [0,4] !");
612      return -1;
613    }
614
615    TString drawOptionsStr(drawOptions);
616    TString cutStr("");
617
618    if(sectorNo==-1 && stackNo==-1)     // plot the entire layer
619      cutStr += Form("Layer==%d", layerNo);
620    else if(sectorNo!=-1 && stackNo==-1)      // plot a sector from a layer
621      cutStr += Form("Layer==%d && SuperModule==%d", layerNo, sectorNo);
622    else if(sectorNo==-1 && stackNo!=-1)        // plot a stack from a layer
623      cutStr += Form("Layer==%d && Stack==%d", layerNo, stackNo);
624    else                 // the layer, sector and stack are determined -> so plot a chamber
625      cutStr += Form("Layer==%d && SuperModule==%d && Stack==%d", layerNo, sectorNo, stackNo);
626    
627    if(cuts && cuts[0] != 0) {
628       if (cutStr.Length() != 0) cutStr += "&& ";
629       cutStr += "(";
630       cutStr += cuts;
631       cutStr += ")";
632    }
633
634    drawStr.ReplaceAll(fAbbreviation, fAppendString);
635    cutStr.ReplaceAll(fAbbreviation, fAppendString);
636    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
637    Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
638    if (returnValue == -1) return -1;
639    
640    TObject *obj = (gPad) ? gPad->GetPrimitive("htemp") : 0; 
641    if (!obj) obj = (TH1F*)gDirectory->Get("htemp");
642    if (!obj) obj = gPad->GetPrimitive("tempHist");
643    if (!obj) obj = (TH1F*)gDirectory->Get("tempHist");
644    if (!obj) obj = gPad->GetPrimitive("Graph");
645    if (!obj) obj = (TH1F*)gDirectory->Get("Graph");
646    if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
647    return returnValue;
648 }
649
650 //_____________________________________________________________________________
651 Int_t AliTRDCalibViewer::EasyDraw(const char* drawCommand, Int_t chamber, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
652   //
653   // easy drawing of data, use '~' for abbreviation of '.fElements'
654   // example: EasyDraw("CETmean~-CETmean_mean", 34, "(CETmean~-CETmean_mean)>0")
655   // sector: sector-number - only the specified sector will be drwawn
656   // cuts: specifies cuts
657   // drawOptions: draw options like 'same'
658   // writeDrawCommand: write the command, that is passed to TTree::Draw
659   //
660   if(chamber >= 0 && chamber < 540) {
661     Int_t superModuleNo = chamber/30;
662     Int_t stackNo = (chamber%30)/6;
663     Int_t layerNo = (chamber%30)%6;
664     char sectorChr[22];
665     snprintf(sectorChr,22, "Layer%iSector%iStack%i", layerNo, superModuleNo, stackNo);
666     return EasyDraw(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
667   }
668   Error("EasyDraw","The TRD contains only chamber from 0 to 539");
669   return -1;
670 }
671
672 //_____________________________________________________________________________
673 Int_t AliTRDCalibViewer::EasyDraw1D(const char* drawCommand, Int_t chamber, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
674   //
675   // easy drawing of data, use '~' for abbreviation of '.fElements'
676   // example: EasyDraw("CETmean~-CETmean_mean", 34, "(CETmean~-CETmean_mean)>0")
677   // sector: sector-number - the specified sector will be drwawn
678   // cuts: specifies cuts
679   // drawOptions: draw options like 'same'
680   // writeDrawCommand: write the command, that is passed to TTree::Draw
681   //
682
683   if (chamber >= 0 && chamber < 539) {
684     Int_t superModuleNo = chamber/30;
685     Int_t stackNo = (chamber%30)/6;
686     Int_t layerNo = (chamber%30)%6;
687     char sectorChr[22];
688     snprintf(sectorChr,22, "Layer%iSector%iStack%i", layerNo, superModuleNo, stackNo);
689     return EasyDraw1D(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
690   }
691   Error("EasyDraw1D","The TRD contains only chambers from 0 to 539");
692   return -1;
693 }
694
695 //_____________________________________________________________________________
696 Bool_t AliTRDCalibViewer::DumpOCDBtoTreeDetails(const Char_t* runListFilename,
697                                                 const Char_t* outFilename,
698                                                 Int_t firstRun, Int_t lastRun,
699                                                 const Char_t* storage,
700                                                 Int_t version,
701                                                 Int_t subVersion,
702                                                 Bool_t getCalibs,
703                                                 Bool_t getDCS,
704                                                 Bool_t getAlign) {
705   //
706   // Retrieve TRD OCDB information for a given run list/range
707   //
708
709   if(runListFilename[0]!='\0' && firstRun==-1 && lastRun==-1) {
710     cout << "AliTRDCalibViewer::DumpOCDBtoTreeDetails(): You must provide at least a run range or an ascii filename with run numbers" 
711          << endl;
712     return kFALSE;
713   }
714   // initialize the OCDB manager
715   TString storageString = storage;
716   if(storageString.Contains("alien://")) {
717     TGrid::Connect("alien://");
718   }
719   AliCDBManager *manager = AliCDBManager::Instance();
720   if(storage[0]!='\0') {
721     manager->SetDefaultStorage(storage);
722   }
723   else {
724     if(!manager->IsDefaultStorageSet()) {
725       cout << "AliTRDCalibViewer::DumpOCDBtoTreeDetails(): Default OCDB storage not set!!" << endl;
726       return kFALSE;
727     }
728   }
729   manager->SetRun(1);
730
731   // open the ascii file
732   ifstream in;
733   if(runListFilename[0]!='\0')
734     in.open(runListFilename);
735
736   // initialize the tree streamer
737   if(outFilename[0]=='\0') outFilename = "trdDetails.root";
738   TString calibFilename = outFilename;
739   
740   TTreeSRedirector *treeStreamer = new TTreeSRedirector(calibFilename.Data());
741
742   Int_t currRun;
743   if(runListFilename[0]=='\0' && firstRun!=-1 && lastRun!=-1)
744     currRun = firstRun;
745
746   TVectorD runs;
747
748   // loop over runs
749   while(1) {
750     if(runListFilename[0]!='\0') {
751       if(!(in>>currRun)) continue;
752       if(currRun < (firstRun==-1 ? 0 : firstRun) ||
753          currRun > (lastRun==-1 ? 999999999 : lastRun))
754         continue;
755     }
756     else {
757       if(currRun>lastRun) break;
758     }
759     cout << "run = " << currRun << endl;
760     manager->SetRun(currRun);
761
762     // Get GRP data. If there is no proper GRP object for this run than
763     // this run is aborted
764     AliCDBEntry *entry = manager->Get("GRP/GRP/Data");
765     AliGRPObject* grpObject = 0;
766     if(entry) {
767       entry->SetOwner(kFALSE);
768       grpObject = dynamic_cast<AliGRPObject*>(entry->GetObject());
769     }
770     else {
771       currRun++;
772       //      continue;
773       //      return kFALSE;
774     }
775     if(!grpObject)
776       cout << "No GRP info available for this run " << endl;
777
778     // Coverity
779     //time_t startTimeGRP = 0;
780     TObjString runType("");
781     if(grpObject) {
782       // Coverity
783       //startTimeGRP = grpObject->GetTimeStart();
784       TTimeStamp start(grpObject->GetTimeStart());
785       TTimeStamp end(grpObject->GetTimeEnd());
786       cout << "Start time: " << start.GetDate()/10000 << "/" 
787            << (start.GetDate()/100)-(start.GetDate()/10000)*100 << "/" 
788            << start.GetDate()%100 << "   "
789            << start.GetTime()/10000 << ":"
790            << (start.GetTime()/100)-(start.GetTime()/10000)*100 << ":" 
791            << start.GetTime()%100 << endl;
792       cout << "End time: " << end.GetDate()/10000 << "/" 
793            << (end.GetDate()/100)-(end.GetDate()/10000)*100 << "/" 
794            << end.GetDate()%100 << "   "
795            << end.GetTime()/10000 << ":"
796            << (end.GetTime()/100)-(end.GetTime()/10000)*100 << ":"
797            << end.GetTime()%100 << endl;
798       cout << "Run type = " << grpObject->GetRunType().Data() << endl;
799       runType = grpObject->GetRunType().Data();
800     }
801
802     // gain
803     AliTRDCalDet *chamberGainFactor = 0;
804     if(getCalibs) {
805       entry = manager->Get("TRD/Calib/ChamberGainFactor", currRun, version, subVersion);
806       if(entry) {
807         entry->SetOwner(kFALSE);
808         chamberGainFactor = (AliTRDCalDet*)entry->GetObject();
809       }
810     }
811     AliTRDCalPad *padGainFactor = 0;
812     if(getCalibs) {
813       entry = manager->Get("TRD/Calib/LocalGainFactor", currRun, version, subVersion);
814       if(entry) {
815         entry->SetOwner(kFALSE);
816         padGainFactor = (AliTRDCalPad*)entry->GetObject();
817       }
818     }
819     Double_t runMeanGain, runRMSGain;
820     TVectorD chamberMeanGain(AliTRDcalibDB::kNdet);
821     TVectorD chamberRMSGain(AliTRDcalibDB::kNdet);
822     TVectorD smMeanGain(AliTRDcalibDB::kNsector);
823     TVectorD smRMSGain(AliTRDcalibDB::kNsector);
824     for(Int_t iNdet=0; iNdet<AliTRDcalibDB::kNdet; iNdet++) {chamberMeanGain[iNdet] = 0.0; chamberRMSGain[iNdet] = 0.0;}
825     for(Int_t iSm=0; iSm<AliTRDcalibDB::kNsector; iSm++) {smMeanGain[iSm] = 0.0; smRMSGain[iSm] = 0.0;}
826     TString parName("Gain");
827     if(getCalibs)
828       ProcessTRDCalibArray(chamberGainFactor, padGainFactor, 
829                            parName,
830                            runMeanGain, runRMSGain,
831                            chamberMeanGain, chamberRMSGain,
832                            smMeanGain, smRMSGain);
833
834     // noise/pedestals
835     AliTRDCalDet *chamberNoise = 0;
836     if(getCalibs) {
837       entry = manager->Get("TRD/Calib/DetNoise", currRun, version, subVersion);
838       if(entry) {
839         entry->SetOwner(kFALSE);
840         chamberNoise = (AliTRDCalDet*)entry->GetObject();
841       }
842     }
843     AliTRDCalPad *padNoise = 0;
844     if(getCalibs) {
845       entry = manager->Get("TRD/Calib/PadNoise", currRun, version, subVersion);
846       if(entry) {
847         entry->SetOwner(kFALSE);
848         padNoise = (AliTRDCalPad*)entry->GetObject();
849       }
850     }
851     Double_t runMeanNoise, runRMSNoise;
852     TVectorD chamberMeanNoise(AliTRDcalibDB::kNdet);
853     TVectorD chamberRMSNoise(AliTRDcalibDB::kNdet);
854     TVectorD smMeanNoise(AliTRDcalibDB::kNsector);
855     TVectorD smRMSNoise(AliTRDcalibDB::kNsector);
856     for(Int_t iNdet=0; iNdet<AliTRDcalibDB::kNdet; iNdet++) {chamberMeanNoise[iNdet] = 0.0; chamberRMSNoise[iNdet] = 0.0;}
857     for(Int_t iSm=0; iSm<AliTRDcalibDB::kNsector; iSm++) {smMeanNoise[iSm] = 0.0; smRMSNoise[iSm] = 0.0;}
858     parName = "Noise";
859     if(getCalibs)
860       ProcessTRDCalibArray(chamberNoise, padNoise, 
861                            parName,
862                            runMeanNoise, runRMSNoise,
863                            chamberMeanNoise, chamberRMSNoise,
864                            smMeanNoise, smRMSNoise);
865
866     // vdrift
867     AliTRDCalDet *chamberVdrift = 0;
868     if(getCalibs) {
869       entry = manager->Get("TRD/Calib/ChamberVdrift", currRun, version, subVersion);
870       if(entry) {
871         entry->SetOwner(kFALSE);
872         chamberVdrift = (AliTRDCalDet*)entry->GetObject();
873       }
874     }
875     AliTRDCalPad *padVdrift = 0;
876     if(getCalibs) {
877       entry = manager->Get("TRD/Calib/LocalVdrift", currRun, version, subVersion);
878       if(entry) {
879         entry->SetOwner(kFALSE);
880         padVdrift = (AliTRDCalPad*)entry->GetObject();
881       }
882     }
883     Double_t runMeanVdrift, runRMSVdrift;
884     TVectorD chamberMeanVdrift(AliTRDcalibDB::kNdet);
885     TVectorD chamberRMSVdrift(AliTRDcalibDB::kNdet);
886     TVectorD smMeanVdrift(AliTRDcalibDB::kNsector);
887     TVectorD smRMSVdrift(AliTRDcalibDB::kNsector);
888     for(Int_t iNdet=0; iNdet<AliTRDcalibDB::kNdet; iNdet++) {chamberMeanVdrift[iNdet] = 0.0; chamberRMSVdrift[iNdet] = 0.0;}
889     for(Int_t iSm=0; iSm<AliTRDcalibDB::kNsector; iSm++) {smMeanVdrift[iSm] = 0.0; smRMSVdrift[iSm] = 0.0;}
890     parName = "Vdrift";
891     if(getCalibs)
892       ProcessTRDCalibArray(chamberVdrift, padVdrift, 
893                            parName,
894                            runMeanVdrift, runRMSVdrift,
895                            chamberMeanVdrift, chamberRMSVdrift,
896                            smMeanVdrift, smRMSVdrift);
897
898     // T0
899     AliTRDCalDet *chamberT0 = 0;
900     if(getCalibs) {
901       entry = manager->Get("TRD/Calib/ChamberT0", currRun, version, subVersion);
902       if(entry) {
903         entry->SetOwner(kFALSE);
904         chamberT0 = (AliTRDCalDet*)entry->GetObject();
905       }
906     }
907     AliTRDCalPad *padT0 = 0;
908     if(getCalibs) {
909       entry = manager->Get("TRD/Calib/LocalT0", currRun, version, subVersion);
910       if(entry) {
911         entry->SetOwner(kFALSE);
912         padT0 = (AliTRDCalPad*)entry->GetObject();
913       }
914     }
915     Double_t runMeanT0, runRMST0;
916     TVectorD chamberMeanT0(AliTRDcalibDB::kNdet);
917     TVectorD chamberRMST0(AliTRDcalibDB::kNdet);
918     TVectorD smMeanT0(AliTRDcalibDB::kNsector);
919     TVectorD smRMST0(AliTRDcalibDB::kNsector);
920     for(Int_t iNdet=0; iNdet<AliTRDcalibDB::kNdet; iNdet++) {chamberMeanT0[iNdet] = 0.0; chamberRMST0[iNdet] = 0.0;}
921     for(Int_t iSm=0; iSm<AliTRDcalibDB::kNsector; iSm++) {smMeanT0[iSm] = 0.0; smRMST0[iSm] = 0.0;}
922     parName = "T0";
923     if(getCalibs)
924       ProcessTRDCalibArray(chamberT0, padT0, 
925                            parName,
926                            runMeanT0, runRMST0,
927                            chamberMeanT0, chamberRMST0,
928                            smMeanT0, smRMST0);
929
930     // status
931     AliTRDCalChamberStatus* chamberStatus = 0;
932     if(getCalibs) {
933       entry = manager->Get("TRD/Calib/ChamberStatus", currRun, version, subVersion);
934       if(entry) {
935         entry->SetOwner(kFALSE);
936         chamberStatus = (AliTRDCalChamberStatus*)entry->GetObject();
937       }
938     }
939     AliTRDCalPadStatus *padStatus = 0;
940     if(getCalibs) {
941       entry = manager->Get("TRD/Calib/PadStatus", currRun, version, subVersion);
942       if(entry) {
943         entry->SetOwner(kFALSE);
944         padStatus = (AliTRDCalPadStatus*)entry->GetObject();
945       }
946     }
947
948     // DCS FEE information
949     TObjArray *dcsArray = 0;
950     if(getDCS) {
951       entry = manager->Get("TRD/Calib/DCS");
952       if(entry) {
953         entry->SetOwner(kTRUE);
954         dcsArray = (TObjArray*)entry->GetObject();
955       }
956     }
957     AliTRDCalDCSv2 *dcsSOR = 0;
958     AliTRDCalDCSv2 *dcsEOR = 0;
959     if(getDCS && dcsArray) {
960       dcsSOR = (AliTRDCalDCSv2*)dcsArray->At(0);
961       dcsEOR = (AliTRDCalDCSv2*)dcsArray->At(1);
962     }
963
964     // Alignment information
965     // get the geometry from OCDB
966     TGeoManager *geoMan = 0x0;
967     if(getAlign) {
968       entry=manager->Get("GRP/Geometry/Data");
969       if(entry)
970         geoMan=(TGeoManager*)entry->GetObject();
971       else
972         cout << "Cannot get an entry for the geometry storage" << endl;
973     }
974     // get the alignment from OCDB
975     AliTRDalignment *alignMan=0;
976     if(getAlign && geoMan) {
977       entry=manager->Get("TRD/Align/Data", currRun, version, subVersion);
978       if(entry) {
979         alignMan = new AliTRDalignment();
980         cout << "storage for alignment = " << manager->GetDefaultStorage()->GetURI().Data() << endl;
981         alignMan->ReadDB(manager->GetDefaultStorage()->GetURI().Data(), "TRD/Align/Data", currRun, version, subVersion);
982       }
983       else {
984         cout << "Cannot get an entry for the alignment info" << endl;
985       }
986     }
987
988     Int_t kSuperModuleStatus[18] = {1, 1, 0, 0, 0, 0,     // super module status (1- installed, 0- not installed)
989                                     0, 1, 1, 1, 1, 0, 
990                                     0, 0, 0, 0, 0, 1};
991     Int_t kNRows[ 5] = {16, 16, 12, 16, 16};  // number of pad rows in the chambers from each of the 5 stacks
992     Int_t kNCols = 144;          // number of pad columns in the chambers from each of the 18 supermodules
993     Int_t kROB[5] = {8, 8, 6, 8, 8};   // number of read out boards(ROB) per chamber (6 in stack 2 and 8 in the rest)
994     Int_t kMCM = 16;                   // number of MCMs per ROB
995     for(Short_t iLayer=0; iLayer<AliTRDgeometry::kNlayer; iLayer++) {   // loop over layers
996       for(Short_t iSector=0; iSector<AliTRDgeometry::kNsector; iSector++) {  // loop over supermodules
997         if(kSuperModuleStatus[iSector]==0) 
998           continue;
999         Double_t alignSMPars[6];
1000         for(Int_t ipar=0; ipar<6; ipar++) alignSMPars[ipar]=0.0;
1001         if(getAlign && alignMan)
1002           alignMan->GetSm(iSector, alignSMPars);
1003         for(Short_t iStack=0; iStack<AliTRDgeometry::kNstack; iStack++) {    // loop over stacks
1004           Short_t chamberNo = AliTRDgeometry::GetDetector(iLayer, iStack, iSector);
1005           AliTRDCalROC *gainROC = 0;
1006           if(padGainFactor) gainROC = padGainFactor->GetCalROC(chamberNo);
1007           AliTRDCalROC *noiseROC = 0;
1008           if(padNoise) noiseROC = padNoise->GetCalROC(chamberNo);
1009           AliTRDCalROC *vdriftROC = 0;
1010           if(padVdrift) vdriftROC = padVdrift->GetCalROC(chamberNo);
1011           AliTRDCalROC *t0ROC = 0;
1012           if(t0ROC) t0ROC = padT0->GetCalROC(chamberNo);
1013           AliTRDCalSingleChamberStatus *statusROC = 0;
1014           if(padStatus) statusROC = padStatus->GetCalROC(chamberNo);
1015           TVectorD channelVector(kNRows[iStack]*kNCols);
1016           TVectorD rowVector(kNRows[iStack]*kNCols);
1017           TVectorD colVector(kNRows[iStack]*kNCols);
1018           TVectorD statusVector(kNRows[iStack]*kNCols);
1019           TVectorD gainVector(kNRows[iStack]*kNCols);
1020           TVectorD noiseVector(kNRows[iStack]*kNCols);
1021           TVectorD vdriftVector(kNRows[iStack]*kNCols);
1022           TVectorD t0Vector(kNRows[iStack]*kNCols);
1023           TVectorD padSuperRowVector(kNRows[iStack]*kNCols);
1024           TVectorD padSuperColumnVector(kNRows[iStack]*kNCols);
1025           for(Int_t ipar=0; ipar<kNRows[iStack]*kNCols; ipar++) {
1026             channelVector[ipar] = 0; rowVector[ipar] = 0; colVector[ipar] = 0; 
1027             statusVector[ipar] = 0; gainVector[ipar] = 0; noiseVector[ipar] = 0; 
1028             vdriftVector[ipar] = 0; t0Vector[ipar] = 0; padSuperRowVector[ipar] = 0; 
1029             padSuperColumnVector[ipar] = 0;
1030           }
1031           Int_t index = 0;
1032           if(getCalibs) {
1033             for(Short_t iRow=0; iRow<kNRows[iStack]; iRow++) {   // loop over pad rows
1034               for(Short_t iCol=0; iCol<kNCols; iCol++) {    // loop over pad columns
1035                 Short_t padSuperRow = iRow;
1036                 for(Int_t i=0; i<iStack; i++) padSuperRow = padSuperRow + kNRows[i]; 
1037                 padSuperRowVector[index] = padSuperRow;
1038                 Short_t padSuperColumn = iCol;
1039                 for(Int_t i=0; i<iSector; i++) padSuperColumn = padSuperColumn + kNCols;
1040                 padSuperColumnVector[index] = padSuperColumn;
1041                 Short_t channelNo = -1;
1042                 Float_t gain = -99.;
1043                 if(gainROC && chamberGainFactor) {
1044                   channelNo = gainROC->GetChannel(iCol, iRow);
1045                   gain = chamberGainFactor->GetValue(chamberNo) * gainROC->GetValue(iCol, iRow);
1046                 }
1047                 Float_t noise = -99.;
1048                 if(noiseROC && chamberNoise)
1049                   noise = chamberNoise->GetValue(chamberNo) * noiseROC->GetValue(iCol, iRow);
1050                 Float_t vdrift = -99.;
1051                 if(vdriftROC && chamberVdrift)
1052                   vdrift = chamberVdrift->GetValue(chamberNo) * vdriftROC->GetValue(iCol, iRow);
1053                 Float_t t0 = -99.;
1054                 if(t0ROC && chamberT0)
1055                   t0 = chamberT0->GetValue(chamberNo) + t0ROC->GetValue(iCol, iRow);
1056                 Int_t status = -99;
1057                 if(statusROC)
1058                   status = statusROC->GetStatus(iCol, iRow);
1059                 channelVector[index] = channelNo;
1060                 rowVector[index] = iRow;
1061                 colVector[index] = iCol;
1062                 statusVector[index] = status;
1063                 gainVector[index] = gain;
1064                 noiseVector[index] = noise;
1065                 vdriftVector[index] = vdrift;
1066                 t0Vector[index] = t0;
1067                 index++;
1068               }  // end loop over pad columns
1069             }  // end loop over pad rows
1070           }   // end if(getCalibs)
1071
1072           // get the dcs information
1073           AliTRDCalDCSFEEv2 *dcsfeeSOR = 0;
1074           AliTRDCalDCSFEEv2 *dcsfeeEOR = 0;
1075           if(getDCS) {
1076             if(dcsSOR) dcsfeeSOR = dcsSOR->GetCalDCSFEEObj(chamberNo);
1077             if(dcsEOR) dcsfeeEOR = dcsEOR->GetCalDCSFEEObj(chamberNo);
1078           }
1079           
1080           Bool_t sorAndEor = kFALSE;
1081           if(getDCS && dcsfeeSOR && dcsfeeEOR) sorAndEor = kTRUE;
1082           if(getDCS && !dcsfeeSOR && dcsfeeEOR) dcsfeeSOR = dcsfeeEOR;
1083           TVectorD robVector(kROB[iStack]*kMCM);
1084           TVectorD mcmVector(kROB[iStack]*kMCM);
1085           TVectorD sorandeorVector(kROB[iStack]*kMCM);
1086           TVectorD gsmSorVector(kROB[iStack]*kMCM);
1087           TVectorD gsmDeltaVector(kROB[iStack]*kMCM);
1088           TVectorD nimSorVector(kROB[iStack]*kMCM);
1089           TVectorD nimDeltaVector(kROB[iStack]*kMCM);
1090           TVectorD nevSorVector(kROB[iStack]*kMCM);
1091           TVectorD nevDeltaVector(kROB[iStack]*kMCM);
1092           TVectorD nptSorVector(kROB[iStack]*kMCM);
1093           TVectorD nptDeltaVector(kROB[iStack]*kMCM);
1094           TVectorD mcmSuperRowVector(kROB[iStack]*kMCM);
1095           TVectorD mcmSuperColumnVector(kROB[iStack]*kMCM);
1096           for(Int_t ipar=0; ipar<kROB[iStack]*kMCM; ipar++) {
1097             robVector[ipar] = 0; mcmVector[ipar] = 0; sorandeorVector[ipar] = 0; 
1098             gsmSorVector[ipar] = 0; gsmDeltaVector[ipar] = 0; nimSorVector[ipar] = 0; 
1099             nimDeltaVector[ipar] = 0; nevSorVector[ipar] = 0; nevDeltaVector[ipar] = 0; 
1100             nptSorVector[ipar] = 0; nptDeltaVector[ipar] = 0; mcmSuperRowVector[ipar] = 0; 
1101             mcmSuperColumnVector[ipar] = 0;
1102           }
1103
1104           Int_t robsRowDirection = kNRows[iStack]/4;    // 4 or 3 ROBs per chamber in row direction
1105           Int_t index1 = 0; 
1106           if(getDCS && (dcsfeeSOR || dcsfeeEOR) && dcsfeeSOR->GetStatusBit()==0) {
1107             for(Int_t iROB=0; iROB<kROB[iStack]; iROB++) { // loop over ROBs
1108               for(Int_t iMCM=0; iMCM<kMCM; iMCM++) {  // loop over MCMs
1109                 Short_t superRowMCM = iMCM%4;            // 4 MCMs per ROB in row direction
1110                 superRowMCM += 4*(iROB%robsRowDirection);   // now we have the row of this MCM inside one chamber
1111                 for(Int_t kk=0; kk<iStack; kk++) superRowMCM += kNRows[kk];   // add number of rows in previous stacks
1112                 Short_t superColumnMCM = iMCM/4;        // 4 MCMs per ROB in column direction
1113                 superColumnMCM += 4*(iROB/robsRowDirection);    // should yield 0 or 1 (2 ROBs per chamber in col direction)
1114                 superColumnMCM += iSector*8;
1115                 mcmSuperRowVector[index1] = superRowMCM;
1116                 mcmSuperColumnVector[index1] = superColumnMCM;
1117                 Int_t gsm = dcsfeeSOR->GetMCMGlobalState(iROB, iMCM);
1118                 Int_t nim = dcsfeeSOR->GetMCMStateNI(iROB, iMCM);
1119                 Int_t nev = dcsfeeSOR->GetMCMEventCnt(iROB, iMCM);
1120                 Int_t npt = dcsfeeSOR->GetMCMPtCnt(iROB, iMCM);
1121                 Int_t dgsm = -100000;
1122                 Int_t dnim = -100000;
1123                 Int_t dnev = -100000;
1124                 Int_t dnpt = -100000;
1125                 if(sorAndEor) {
1126                   dgsm = gsm - dcsfeeEOR->GetMCMGlobalState(iROB, iMCM);
1127                   dnim = nim - dcsfeeEOR->GetMCMStateNI(iROB, iMCM);
1128                   dnev = nev - dcsfeeEOR->GetMCMEventCnt(iROB, iMCM);
1129                   dnpt = npt - dcsfeeEOR->GetMCMPtCnt(iROB, iMCM);
1130                   if(gsm==-1 && dgsm==0) dgsm = -100000;
1131                   if(nim==-1 && dnim==0) dnim = -100000;
1132                   if(nev==-1 && dnev==0) dnev = -100000;
1133                   if(npt==-1 && dnpt==0) dnpt = -100000;
1134                 }
1135                 robVector[index1] = iROB;
1136                 mcmVector[index1] = iMCM;
1137                 sorandeorVector[index1] = sorAndEor;
1138                 gsmSorVector[index1] = gsm;
1139                 gsmDeltaVector[index1] = dgsm;
1140                 nimSorVector[index1] = nim;
1141                 nimDeltaVector[index1] = dnim;
1142                 nevSorVector[index1] = nev;
1143                 nevDeltaVector[index1] = dnev;
1144                 nptSorVector[index1] = npt;
1145                 nptDeltaVector[index1] = dnpt;
1146                 index1++;
1147               }  // end loop over MCMs
1148             }  // end loop over ROBs
1149           }  // end if(getDCS ...)
1150
1151           Double_t alignChamberPars[6];
1152           for(Int_t ipar=0; ipar<6; ipar++) alignChamberPars[ipar]=0;
1153           if(getAlign && alignMan)
1154             alignMan->GetCh(chamberNo, alignChamberPars);
1155
1156           (*treeStreamer)<< "TRDcalibDetails"
1157                          << "run=" << currRun
1158                          << "SuperModule=" << iSector
1159                          << "Stack=" << iStack
1160                          << "Layer=" << iLayer
1161                          << "Chamber=" << chamberNo;
1162           if(getAlign)
1163             (*treeStreamer)<< "TRDcalibDetails"
1164                            << "Align_SM_ShiftRphi=" << alignSMPars[0]
1165                            << "Align_SM_ShiftZ=" << alignSMPars[1]
1166                            << "Align_SM_ShiftR=" << alignSMPars[2]
1167                            << "Align_SM_RotRphi=" << alignSMPars[3]
1168                            << "Align_SM_RotZ=" << alignSMPars[4]
1169                            << "Align_SM_RotR=" << alignSMPars[5]
1170                            << "Align_Ch_ShiftRphi=" << alignChamberPars[0]
1171                            << "Align_Ch_ShiftZ=" << alignChamberPars[1]
1172                            << "Align_Ch_ShiftR=" << alignChamberPars[2]
1173                            << "Align_Ch_RotRphi=" << alignChamberPars[3]
1174                            << "Align_Ch_RotZ=" << alignChamberPars[4]
1175                            << "Align_Ch_RotR=" << alignChamberPars[5];
1176           if(getCalibs)
1177             (*treeStreamer)<< "TRDcalibDetails"
1178                            << "Gain_Mean_Run=" << runMeanGain
1179                            << "Gain_RMS_Run=" << runRMSGain
1180                            << "Gain_Mean_SuperModule=" << smMeanGain[iSector]
1181                            << "Gain_RMS_SuperModule=" << smRMSGain[iSector]
1182                            << "Gain_Mean_Chamber=" << chamberMeanGain[chamberNo]
1183                            << "Gain_RMS_Chamber=" << chamberRMSGain[chamberNo]
1184                            << "Noise_Mean_Run=" << runMeanNoise
1185                            << "Noise_RMS_Run=" << runRMSNoise
1186                            << "Noise_Mean_SuperModule=" << smMeanNoise[iSector]
1187                            << "Noise_RMS_SuperModule=" << smRMSNoise[iSector]
1188                            << "Noise_Mean_Chamber=" << chamberMeanNoise[chamberNo]
1189                            << "Noise_RMS_Chamber=" << chamberRMSNoise[chamberNo]
1190                            << "Vdrift_Mean_Run=" << runMeanVdrift
1191                            << "Vdrift_RMS_Run=" << runRMSVdrift
1192                            << "Vdrift_Mean_SuperModule=" << smMeanVdrift[iSector]
1193                            << "Vdrift_RMS_SuperModule=" << smRMSVdrift[iSector]
1194                            << "Vdrift_Mean_Chamber=" << chamberMeanVdrift[chamberNo]
1195                            << "Vdrift_RMS_Chamber=" << chamberRMSVdrift[chamberNo]
1196                            << "T0_Mean_Run=" << runMeanT0
1197                            << "T0_RMS_Run=" << runRMST0
1198                            << "T0_Mean_SuperModule=" << smMeanT0[iSector]
1199                            << "T0_RMS_SuperModule=" << smRMST0[iSector]
1200                            << "T0_Mean_Chamber=" << chamberMeanT0[chamberNo]
1201                            << "T0_RMS_Chamber=" << chamberRMST0[chamberNo]
1202                            << "Channel.=" << &channelVector
1203                            << "Row.=" << &rowVector
1204                            << "Column.=" << &colVector
1205                            << "PadSuperRow.=" << &padSuperRowVector
1206                            << "PadSuperColumn.=" << &padSuperColumnVector
1207                            << "Status.=" << &statusVector
1208                            << "Gain.=" << &gainVector
1209                            << "Noise.=" << &noiseVector
1210                            << "Vdrift.=" << &vdriftVector
1211                            << "T0.=" << &t0Vector;
1212           if(getDCS)
1213             (*treeStreamer)<< "TRDcalibDetails"
1214                            << "ROB.=" << &robVector
1215                            << "MCM.=" << &mcmVector
1216                            << "MCMSuperRow.=" << &mcmSuperRowVector
1217                            << "MCMSuperColumn.=" << &mcmSuperColumnVector
1218                            << "SORandEOR.=" << &sorandeorVector
1219                            << "gsmSOR.=" << &gsmSorVector
1220                            << "gsmDelta.=" << &gsmDeltaVector
1221                            << "nimSOR.=" << &nimSorVector
1222                            << "nimDelta.=" << &nimDeltaVector
1223                            << "nevSOR.=" << &nevSorVector
1224                            << "nevDelta.=" << &nevDeltaVector
1225                            << "nptSOR.=" << &nptSorVector
1226                            << "nptDelta.=" << &nptDeltaVector;
1227           (*treeStreamer)<< "TRDcalibDetails"
1228                          << "\n";
1229         }  // end loop over stacks
1230       }  // end loop over supermodules
1231     }  // end loop over layers
1232     
1233     // add the run number to the list of runs
1234     runs.ResizeTo(runs.GetNoElements()+1);
1235     runs[runs.GetNoElements()-1] = currRun;
1236     
1237     // do cleaning
1238     if(chamberGainFactor) delete chamberGainFactor;
1239     if(padGainFactor) delete padGainFactor;
1240     if(chamberNoise) delete chamberNoise;
1241     if(padNoise) delete padNoise;
1242     if(chamberVdrift) delete chamberVdrift;
1243     if(padVdrift) delete padVdrift;
1244     if(chamberT0) delete chamberT0;
1245     if(padT0) delete padT0;
1246     if(chamberStatus) delete chamberStatus;
1247     if(padStatus) delete padStatus;
1248
1249     // check if we still have run numbers in the file or provided range
1250     if(runListFilename[0]=='\0' && firstRun!=-1 && lastRun!=-1) {
1251       currRun++;
1252       if(currRun>lastRun) break;
1253     }
1254     if(runListFilename[0]!='\0' && in.eof())
1255       break;
1256   }   // end loop over runs
1257
1258   treeStreamer->GetFile()->cd();
1259   runs.Write("runs");
1260   delete treeStreamer;
1261   return kTRUE;
1262   //  delete treeStreamerDCS;
1263 }
1264
1265 //_________________________________________________________________________
1266 void AliTRDCalibViewer::DumpCalibToTree(const Char_t* inFilename, const Char_t* outFilename)
1267 {
1268   //
1269   //  extract info from CalPad objects and dump them into a tree to be viewed
1270   //
1271   TTreeSRedirector *treeStreamer = new TTreeSRedirector(outFilename);
1272   //open file and retrieve list of calPad objects
1273   TFile f(inFilename);
1274   TList *l=(TList*)f.GetListOfKeys();
1275
1276   TObjArray arrCalPads;
1277   TObjArray arrSMmean;
1278   TObjArray arrSMrms;
1279   arrCalPads.SetOwner();
1280   arrSMmean.SetOwner();
1281   arrSMrms.SetOwner();
1282
1283   TIter next(l);
1284   TKey *k=0x0;
1285   while ( (k=(TKey*)next()) ){
1286     AliTRDCalPad *pad=dynamic_cast<AliTRDCalPad*>(k->ReadObj());
1287     if (!pad) continue;
1288     arrCalPads.Add(pad);
1289
1290     TVectorD *smMean=new TVectorD(AliTRDcalibDB::kNsector);
1291     TVectorD *smRMS=new TVectorD(AliTRDcalibDB::kNsector);
1292
1293     arrSMmean.Add(smMean);
1294     arrSMrms.Add(smRMS);
1295
1296     ProcessTRDCalibArray(pad, *smMean, *smRMS);
1297   }
1298
1299   Int_t kSuperModuleStatus[18] = {1, 1, 0, 0, 0, 0,     // super module status (1- installed, 0- not installed)
1300       0, 1, 1, 1, 1, 0,
1301       0, 0, 0, 0, 0, 1};
1302
1303   AliTRDgeometry trdGeom;
1304   Int_t kNRows[5] = {16, 16, 12, 16, 16};  // number of pad rows in the chambers from each of the 5 stacks
1305   
1306   for(Short_t iLayer=0; iLayer<AliTRDgeometry::kNlayer; iLayer++) {   // loop over layers
1307     for(Short_t iSM=0; iSM<AliTRDgeometry::kNsector; iSM++) {  // loop over supermodules
1308       if(kSuperModuleStatus[iSM]==0)
1309         continue;
1310       
1311       for(Short_t iStack=0; iStack<AliTRDgeometry::kNstack; iStack++) {    // loop over stacks
1312         AliTRDpadPlane &plane=*trdGeom.GetPadPlane(iLayer, iStack);
1313
1314         Short_t chamberNo = AliTRDgeometry::GetDetector(iLayer, iStack, iSM);
1315         const Int_t nrows=plane.GetNrows();
1316         const Int_t ncols=plane.GetNcols();
1317         const Int_t nchannels=nrows*ncols;
1318 //         printf("chamberNo: %d (%03d-%03d-%03d)\n", chamberNo,nrows,ncols,nchannels);
1319         
1320         TVectorD channelVector(nchannels);
1321         TVectorD rowVector(nchannels);
1322         TVectorD colVector(nchannels);
1323         
1324         TVectorD gxVector(nchannels);
1325         TVectorD gyVector(nchannels);
1326         TVectorD gzVector(nchannels);
1327         
1328         TVectorD padSuperRowVector(nchannels);
1329         TVectorD padSuperColumnVector(nchannels);
1330         
1331         Int_t index = 0;
1332         for(Short_t iRow=0; iRow<nrows; iRow++) {   // loop over pad rows
1333           for(Short_t iCol=0; iCol<ncols; iCol++) {    // loop over pad columns
1334             Short_t padSuperRow = iRow;
1335             for(Int_t i=0; i<iStack; i++) padSuperRow = padSuperRow + kNRows[i];
1336             padSuperRowVector.GetMatrixArray()[index] = padSuperRow;
1337             
1338             Short_t padSuperColumn = iCol;
1339             for(Int_t i=0; i<iSM; i++) padSuperColumn = padSuperColumn + ncols;
1340             padSuperColumnVector.GetMatrixArray()[index] = padSuperColumn;
1341             
1342             rowVector.GetMatrixArray()[index] = iRow;
1343             colVector.GetMatrixArray()[index] = iCol;
1344             
1345             index++;
1346           }  // end loop over pad columns
1347         }  // end loop over pad rows
1348         
1349         (*treeStreamer)<< "TRDcalibDetails"
1350           << "SuperModule=" << iSM
1351           << "Stack=" << iStack
1352           << "Layer=" << iLayer
1353           << "Chamber=" << chamberNo
1354             //geographical information
1355           << "Channel.=" << &channelVector
1356           << "Row.=" << &rowVector
1357           << "Column.=" << &colVector
1358           << "PadSuperRow.=" << &padSuperRowVector
1359           << "PadSuperColumn.=" << &padSuperColumnVector;
1360 //          << "gx.=" << &gxVector
1361 //          << "gy.=" << &gyVector
1362 //          << "gz.=" << &gzVector;
1363           
1364         //
1365         // pad calibrations
1366         //
1367         TObjArray arrTrash;
1368         arrTrash.SetOwner();
1369         Int_t ncalib=arrCalPads.GetEntriesFast();
1370         for (Int_t iCalib=0; iCalib<ncalib; ++iCalib){
1371           AliTRDCalPad *pad=(AliTRDCalPad*)arrCalPads.UncheckedAt(iCalib);
1372           AliTRDCalROC *calROC=pad->GetCalROC(chamberNo);
1373           
1374           TVectorD &smMean=*((TVectorD*)arrSMmean.UncheckedAt(iCalib));
1375           TVectorD &smRMS=*((TVectorD*)arrSMrms.UncheckedAt(iCalib));
1376           
1377           TString calibName=pad->GetName();
1378           
1379           TVectorD *valueVector=new TVectorD(nchannels);
1380           arrTrash.Add(valueVector);
1381           
1382           Double_t rocMean=0;
1383           Double_t rocRMS=0;
1384           Double_t rocMedian=0;
1385           
1386           if (calROC){
1387             Int_t index2 = 0;
1388             for(Short_t iRow=0; iRow<nrows; iRow++) {
1389               for(Short_t iCol=0; iCol<ncols; iCol++) {
1390                 valueVector->GetMatrixArray()[index2] = calROC->GetValue(iCol,iRow);
1391                 index2++;
1392               }
1393             }
1394             rocMean   = calROC->GetMean();
1395             rocRMS    = calROC->GetRMS();
1396             rocMedian = calROC->GetMedian();
1397             //check for NaN
1398             if ( !(rocMean<1e30) ) rocMean=0;
1399             if ( !(rocRMS<1e30) ) rocRMS=0;
1400             
1401 //             printf("mean:   %f\n",rocMean);
1402 //             printf("rms:    %f\n",rocRMS);
1403 //             printf("median: %f\n",rocMedian);
1404           }
1405           
1406           (*treeStreamer)<< "TRDcalibDetails"
1407             // statistical information
1408             << (Char_t*)((calibName+"_Mean_SuperModule=").Data()) << smMean[iSM]
1409             << (Char_t*)((calibName+"_RMS_SuperModule=").Data())  << smRMS[iSM]
1410             << (Char_t*)((calibName+"_Mean_Chamber=").Data())     << rocMean
1411             << (Char_t*)((calibName+"_RMS_Chamber=").Data())      << rocRMS
1412             << (Char_t*)((calibName+"_Median_Chamber=").Data())   << rocMedian
1413             //pad by pad information
1414             << (Char_t*)((calibName+".=").Data()) << valueVector;
1415           
1416         }   // end loop over calib objects
1417         
1418         (*treeStreamer)<< "TRDcalibDetails"
1419           << "\n";
1420         arrTrash.Delete();
1421       }  // end loop over stacks
1422     }  // end loop over supermodules
1423   }  // end loop over layers
1424   delete treeStreamer;
1425 }
1426
1427 //_____________________________________________________________________________
1428 void AliTRDCalibViewer::ProcessTRDCalibArray(AliTRDCalDet* chamberCalib, AliTRDCalPad *padCalib,
1429                                              TString parName,
1430                                              Double_t &runValue, Double_t &runRMS,
1431                                              TVectorD &chamberValues, TVectorD &chamberValuesRMS,
1432                                              TVectorD &superModuleValues, TVectorD &superModuleValuesRMS) {
1433   // Process the calibrations for a given run.
1434   // Calculates the run and chamber wise averages
1435   //
1436   if(!chamberCalib) return;
1437   if(!padCalib) return;
1438   Int_t kSuperModuleStatus[18] = {1, 1, 0, 0, 0, 0,     // super module status (1- installed, 0- not installed)
1439                                   0, 1, 1, 1, 1, 0, 
1440                                   0, 0, 0, 0, 0, 1};
1441
1442   // initialize the histograms used for extracting the mean and RMS
1443   TH1F *runWiseHisto = new TH1F("runHisto", "runHisto", 200, -10.0, 10.0);
1444   TH1F *superModuleWiseHisto = new TH1F("smHisto", "smHisto", 200, -10.0, 10.0);
1445   TH1F *chamberWiseHisto = new TH1F("chamberHisto", "chamberHisto", 200, -10.0, 10.0);
1446
1447   // check if the calibration parameter is multiplicative or additive
1448   Bool_t multiplicative = kTRUE;
1449   if(!parName.CompareTo("T0")) multiplicative = kFALSE;
1450
1451   // first iteration (calculate all averages and RMS without discrimination on the SM average)
1452   runWiseHisto->Reset();
1453   for(Int_t iSM = 0; iSM<AliTRDcalibDB::kNsector; iSM++) {   // loop over supermodules
1454     // reset the super module histogram
1455     superModuleWiseHisto->Reset();
1456     // check if SM is installed
1457     if(!kSuperModuleStatus[iSM]) continue;
1458     for(Int_t iChamber=iSM*AliTRDcalibDB::kNstack*AliTRDcalibDB::kNlayer; 
1459         iChamber < (iSM+1)*AliTRDcalibDB::kNstack*AliTRDcalibDB::kNlayer; 
1460         iChamber++) {  // loop over chambers in this supermodule
1461       // get the chamber value
1462       Float_t chamberValue = chamberCalib->GetValue(iChamber);
1463       // get the ROC object
1464       AliTRDCalROC *chamberROC = padCalib->GetCalROC(iChamber);
1465       if(!chamberROC) 
1466         continue;
1467       chamberWiseHisto->Reset();
1468       for(Int_t iChannel = 0; iChannel < chamberROC->GetNchannels(); iChannel++){ // loop over channels
1469         // calculate the calibration parameter for this pad
1470         Float_t padValue;
1471         if(multiplicative)
1472           padValue = chamberValue * chamberROC->GetValue(iChannel);
1473         else
1474           padValue = chamberValue + chamberROC->GetValue(iChannel);
1475         // fill the run, SM and chamber wise histograms
1476         chamberWiseHisto->Fill(padValue);
1477         // if the parameter is Noise then check if the pad value is not a default one
1478         // Default value is now 1.2!!!! Check with Raphaelle for more informations
1479         if(parName.Contains("Noise") &&
1480            TMath::Abs(padValue-1.2)<0.00001) continue;
1481         superModuleWiseHisto->Fill(padValue);
1482         runWiseHisto->Fill(padValue);
1483       }  // end loop over channels
1484       // get the chamber wise mean and RMS
1485       chamberValues[iChamber] = chamberWiseHisto->GetMean();
1486       chamberValuesRMS[iChamber] = chamberWiseHisto->GetRMS();
1487     }  // end loop over chambers
1488     // SM wise mean and RMS
1489     superModuleValues[iSM] = superModuleWiseHisto->GetMean();
1490     superModuleValuesRMS[iSM] = superModuleWiseHisto->GetRMS();
1491   }  // end loop over supermodules
1492   // run wise mean and RMS
1493   runValue = runWiseHisto->GetMean();
1494   runRMS = runWiseHisto->GetRMS();
1495
1496   // Noise and Gain are finished processing
1497   if(parName.Contains("Noise") || parName.Contains("Gain"))
1498     return;
1499   // second iteration (calculate SM and run wise averages and RMS for Vdrift and T0)
1500   // The pads with calib parameter equal to the SM average are discarded (default value)
1501   runWiseHisto->Reset();
1502   for(Int_t iSM = 0; iSM<AliTRDcalibDB::kNsector; iSM++) {   // loop over supermodules
1503     superModuleWiseHisto->Reset();
1504     // eliminate the uninstalled super modules
1505     if(!kSuperModuleStatus[iSM]) continue;
1506     for(Int_t iChamber=iSM*AliTRDcalibDB::kNstack*AliTRDcalibDB::kNlayer; 
1507         iChamber < (iSM+1)*AliTRDcalibDB::kNstack*AliTRDcalibDB::kNlayer; 
1508         iChamber++) {  // loop over chambers
1509       // the chamber value
1510       Float_t chamberValue = chamberCalib->GetValue(iChamber);
1511       AliTRDCalROC *chamberROC = padCalib->GetCalROC(iChamber);
1512       if(!chamberROC) 
1513         continue;
1514       
1515       for(Int_t iChannel = 0; iChannel < chamberROC->GetNchannels(); iChannel++){ // loop over channels in a chamber
1516         // get the pad value
1517         Float_t padValue;
1518         if(multiplicative)
1519           padValue = chamberValue * chamberROC->GetValue(iChannel);
1520         else
1521           padValue = chamberValue + chamberROC->GetValue(iChannel);
1522         // eliminate from the average and RMS calculation all pads which
1523         // have the calib parameter equal with the SM average
1524         if((parName.Contains("Vdrift") || parName.Contains("T0")) && 
1525            TMath::Abs(padValue-superModuleValues[iSM])<0.00001) continue;
1526         superModuleWiseHisto->Fill(padValue);
1527         runWiseHisto->Fill(padValue);
1528       }   // end loop over channels
1529     }   // end loop over chambers 
1530     
1531     superModuleValues[iSM] = superModuleWiseHisto->GetMean();
1532     superModuleValuesRMS[iSM] = superModuleWiseHisto->GetRMS();
1533   }   // end loop over super modules
1534   runValue = runWiseHisto->GetMean();
1535   runRMS = runWiseHisto->GetRMS();
1536
1537   delete chamberWiseHisto;
1538   delete superModuleWiseHisto;
1539   delete runWiseHisto;
1540
1541   return;
1542 }
1543
1544
1545 //_____________________________________________________________________________
1546 void AliTRDCalibViewer::ProcessTRDCalibArray(AliTRDCalPad *padCalib,
1547                                              TVectorD &superModuleValues, TVectorD &superModuleValuesRMS)
1548 {
1549 // Process the calibrations for a given run.
1550 // Calculates the run and chamber wise averages
1551   
1552   if(!padCalib) return;
1553   Int_t kSuperModuleStatus[18] = {1, 1, 0, 0, 0, 0,     // super module status (1- installed, 0- not installed)
1554       0, 1, 1, 1, 1, 0,
1555       0, 0, 0, 0, 0, 1};
1556
1557   for(Int_t iSM = 0; iSM<AliTRDcalibDB::kNsector; iSM++) {   // loop over supermodules
1558     Double_t mean=0;
1559     Double_t rms=0;
1560     Double_t count=0;
1561     if(!kSuperModuleStatus[iSM]) continue;
1562
1563     // loop over chambers in this supermodule
1564     const Int_t nchambers=AliTRDcalibDB::kNstack*AliTRDcalibDB::kNlayer;
1565     for(Int_t iChamber=iSM*nchambers; iChamber < (iSM+1)*nchambers; iChamber++) {
1566       AliTRDCalROC *chamberROC = padCalib->GetCalROC(iChamber);
1567       if(!chamberROC)
1568         continue;
1569
1570       // loop over channels
1571       for(Int_t iChannel = 0; iChannel < chamberROC->GetNchannels(); iChannel++){ 
1572         mean+=chamberROC->GetValue(iChannel);
1573         rms+=chamberROC->GetValue(iChannel)*chamberROC->GetValue(iChannel);
1574         ++count;
1575       }  // end loop over channels
1576     }  // end loop over chambers
1577
1578     //calculate mean and rms
1579     if (count>0){
1580       mean/=count;
1581       rms/=count;
1582       rms=TMath::Sqrt(TMath::Abs(mean*mean-rms));
1583     }
1584     // SM wise mean and RMS
1585     superModuleValues[iSM]    = mean;
1586     superModuleValuesRMS[iSM] = rms;
1587   }  // end loop over supermodules
1588 }