AliPhysics  58f3d52 (58f3d52)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Trending2ELoss.C
Go to the documentation of this file.
1 #ifndef __CINT__
2 # include <TClass.h>
3 # include <TDirectory.h>
4 # include <TCollection.h>
5 # include <TParameter.h>
6 # include <TH1.h>
7 # include <TAxis.h>
8 # include <TError.h>
9 # include <TGrid.h>
10 # include <TClonesArray.h>
11 # include <TFile.h>
12 # include <TF1.h>
13 # include <TLine.h>
14 # include "AliFMDCorrELossFit.h"
16 #else
17 class TClass;
18 class TCollection;
19 class TDirectory;
20 class TH1;
21 class TAxis;
22 #endif
23 
24 // ===================================================================
26  TClass* cls, Bool_t verbose=true)
27 {
28  if (!cls) return true;
29  if (!o->IsA()->InheritsFrom(cls)) {
30  if (verbose)
31  Warning("CheckClass", "Object \"%s\" from \"%s\" is not a %s but a %s",
32  o->GetName(), dir->GetName(), cls->GetName(), o->ClassName());
33  return false;
34  }
35  return true;
36 }
37 
38 // ===================================================================
39 TObject*
40 GetObject(TDirectory* dir, const char* name, TClass* cls,
41  Bool_t verbose=true)
42 {
43  if (!dir) {
44  if (verbose) Warning("GetObject", "No directory passed");
45  return 0;
46  }
47  TObject* o = dir->Get(name);
48  if (!o) {
49  if (verbose) Warning("GetObject", "Object \"%s\" not found in \"%s\"",
50  name, dir->GetName());
51  // dir->ls();
52  return 0;
53  }
54  if (!CheckClass(o, dir, cls, verbose)) return 0;
55  return o;
56 }
57 
58 // ___________________________________________________________________
59 TObject*
60 GetObject(const TCollection* dir, const char* name, TClass* cls,
61  Bool_t verbose=true)
62 {
63  if (!dir) {
64  if (verbose) Warning("GetObject", "No collection passed");
65  return 0;
66  }
67  TObject* o = dir->FindObject(name);
68  if (!o) {
69  if (verbose)
70  Warning("GetObject", "Object \"%s\" not found in \"%s\"",
71  name, dir->GetName());
72  // dir->ls();
73  return 0;
74  }
75  if (!CheckClass(o, dir, cls,verbose)) return 0;
76  return o;
77 }
78 // ___________________________________________________________________
80 GetCollection(TDirectory* dir, const char* name, Bool_t verbose=true)
81 {
82  return static_cast<TCollection*>(GetObject(dir, name, TCollection::Class(),
83  verbose));
84 }
85 // ___________________________________________________________________
87 GetCollection(const TCollection* dir, const char* name, Bool_t verbose=true)
88 {
89  return static_cast<TCollection*>(GetObject(dir, name, TCollection::Class(),
90  verbose));
91 }
92 // ___________________________________________________________________
93 TH1* GetH1(TDirectory* dir, const char* name, Bool_t verbose=true)
94 {
95  return static_cast<TH1*>(GetObject(dir, name, TH1::Class(),verbose));
96 }
97 // ___________________________________________________________________
98 TH1* GetH1(const TCollection* dir, const char* name, Bool_t verbose=true)
99 {
100  return static_cast<TH1*>(GetObject(dir, name, TH1::Class(),verbose));
101 }
102 // ___________________________________________________________________
103 TAxis* GetAxis(TDirectory* dir, const char* name, Bool_t verbose=true)
104 {
105  return static_cast<TAxis*>(GetObject(dir, name, TAxis::Class(),verbose));
106 }
107 // ___________________________________________________________________
108 TAxis* GetAxis(const TCollection* dir, const char* name, Bool_t verbose=true)
109 {
110  return static_cast<TAxis*>(GetObject(dir, name, TAxis::Class(),verbose));
111 }
112 // ___________________________________________________________________
113 Int_t GetInt(TDirectory* dir, const char* name, Int_t def=0,
114  Bool_t verbose=true)
115 {
116  TParameter<int>* p =
117  static_cast<TParameter<int>*>(GetObject(dir,name,TParameter<int>::Class(),
118  verbose));
119  if (!p) return def;
120  return p->GetVal();
121 }
122 // ___________________________________________________________________
123 Int_t GetInt(const TCollection* dir, const char* name,
124  Int_t def=0, Bool_t verbose=true)
125 {
126  TParameter<int>* p =
127  static_cast<TParameter<int>*>(GetObject(dir,name,TParameter<int>::Class(),
128  verbose));
129  if (!p) return def;
130  return p->GetVal();
131 }
132 // ___________________________________________________________________
133 Long_t GetLong(TDirectory* dir, const char* name,
134  Long_t def=0, Bool_t verbose=true)
135 {
136  TParameter<long>* p =
137  static_cast<TParameter<long>*>(GetObject(dir,name,
139  verbose));
140  if (!p) return def;
141  return p->GetVal();
142 }
143 // ___________________________________________________________________
144 Long_t GetLong(const TCollection* dir, const char* name,
145  Long_t def=0, Bool_t verbose=true)
146 {
147  TParameter<long>* p =
148  static_cast<TParameter<long>*>(GetObject(dir,name,
150  verbose));
151  if (!p) return def;
152  return p->GetVal();
153 }
154 // ___________________________________________________________________
155 Double_t GetDouble(TDirectory* dir, const char* name,
156  Double_t def=0, Bool_t verbose=true)
157 {
158  TParameter<double>* p =
159  static_cast<TParameter<double>*>(GetObject(dir,name,
161  verbose));
162  if (!p) return def;
163  return p->GetVal();
164 }
165 // ___________________________________________________________________
166 Double_t GetDouble(const TCollection* dir, const char* name,
167  Double_t def=0, Bool_t verbose=true)
168 {
169  TParameter<double>* p =
170  static_cast<TParameter<double>*>(GetObject(dir,name,
172  verbose));
173  if (!p) return def;
174  return p->GetVal();
175 }
176 // ___________________________________________________________________
177 Bool_t GetBool(TDirectory* dir, const char* name,
178  Bool_t def=false, Bool_t verbose=true)
179 {
180  TParameter<bool>* p =
181  static_cast<TParameter<bool>*>(GetObject(dir,name,
183  verbose));
184  if (!p) return def;
185  return p->GetVal();
186 }
187 // ___________________________________________________________________
188 Bool_t GetBool(const TCollection* dir, const char* name,
189  Bool_t def=false, Bool_t verbose=true)
190 {
191  TParameter<bool>* p =
192  static_cast<TParameter<bool>*>(GetObject(dir,name,
194  verbose));
195  if (!p) return def;
196  return p->GetVal();
197 }
198 
199 // ===================================================================
200 Bool_t Fail(const char* msg)
201 {
202  Warning("Trending2ELoss", msg);
203  TFile* stamp = TFile::Open("bad.root", "RECREATE");
204  TNamed* status = new TNamed("status", "Bad run");
205  status->Write();
206  stamp->Write();
207  stamp->Close();
208  return false;
209 }
210 
211 // ===================================================================
212 Bool_t
213 Trending2ELoss(const char* fileName="trending.root",
214  Double_t minRate=.7,
215  Int_t maxGap=3)
216 {
217  if (!gROOT->GetClass("AliFMDCorrELossFit")) {
218  const char* fwd = "$ALICE_PHYSICS/PWGLF/FORWARD/analysis2";
219  gROOT->Macro(Form("%s/scripts/LoadLibs.C", fwd));
220  }
221  TString fname(fileName);
222  if (fname.BeginsWith("alien:")) {
223  TGrid::Connect("alien:");
224  if (!gGrid) return Fail(Form("Failed to connect to grid"));
225  }
226 
227  TFile* file = TFile::Open(fname, "READ");
228  if (!file) return Fail(Form("Failed to open \"%s\"", fileName));
229 
230  TCollection* top = GetCollection(file, "forwardQAResults");
231  if (!top) return false;
232 
233  TCollection* ins = GetCollection(top, "fmdEventInspector");
234  if (!ins) return false;
235 
236  UInt_t run = GetLong(ins, "runNo", 0);
237  UShort_t sys = GetInt(ins, "sys", 0);
238  UShort_t sNN = GetInt(ins, "sNN", 0);
239  Short_t fld = GetInt(ins, "field", 9999);
240  Bool_t sat = GetBool(ins, "satellite", false);
241  Bool_t mc = GetBool(ins, "mc", false);
242  TH1* acc = GetH1(ins, "nEventsAccepted");
243 
244  if (run <= 0 || sys <= 0 || sNN <= 0 || fld > 100)
245  return Fail(Form("Unknown run (%d) system (%d), energy (%d), or field (%d)",
246  sys, sNN, fld));
247 
248  Long_t minEvents = 10000;
249  if (sys == 1) minEvents = 1000000;
250  else if (sys == 3) minEvents = 100000;
251  else if (sys == 4) minEvents = 100000;
252  if (!acc || acc->GetEntries() < minEvents)
253  return Fail(Form("%09d: Too (%ld) few (<%ld) events for sys=%d",
254  run, acc ? Long_t(acc->GetEntries()) : 0, minEvents, sys));
255 
256  TCollection* enf = GetCollection(top, "fmdEnergyFitter");
257  if (!enf) return false;
258 
259  UShort_t nps = GetInt(enf, "nParticles", 0);
260  Double_t low = GetDouble(enf, "lowCut", 0);
261  Double_t mxc = GetDouble(enf, "maxChi2PerNDF", 0);
262  Double_t mre = GetDouble(enf, "maxRelPerError", 0);
263  Double_t lwt = GetDouble(enf, "minWeight", 0);
264  Bool_t shf = GetBool(enf, "deltaShift", false);
265  TH1* het = GetH1(enf, "etaAxis");
266 
267  if (nps <= 0) return Fail(Form("%09d: Too (%d) few peaks fitted", run, nps));
268  if (low <= 0) return Fail(Form("%09d: Lower bound (%f) too low", run, low));
269  if (mxc <= 0 || mre <= 0 || lwt <= 0)
270  return Fail(Form("%09d: Max chi^2/nu (%f), max dp/p (%f), "
271  "least weight (%f) invalid",
272  run, mxc, mre, lwt));
273  if (!het) return Fail("No eta axis defined");
274 
276  corr->SetEtaAxis(*(het->GetXaxis()));
277  corr->SetLowCut(low);
278  corr->SetBit(AliFMDCorrELossFit::kHasShift, shf);
279 
280  const TAxis& eta = corr->GetEtaAxis();
281 
282  TClonesArray tmp("AliFMDCorrELossFit::ELossFit");
283  Int_t ir = 0;
284  TH1* hRate = new TH1D("rate", "Success rate", 5, .5, 5.5);
285  TH1* hTotal = new TH1I("total","Total fits", 5, .5, 5.5);
286  TH1* hMax = new TH1I("max", "Max gap", 5, .5, 5.5);
287  hRate ->SetFillColor(kRed+2); hRate ->SetFillStyle(3001);
288  hTotal->SetFillColor(kGreen+2); hTotal->SetFillStyle(3001);
289  hMax ->SetFillColor(kBlue+2); hMax ->SetFillStyle(3001);
290  hRate ->SetDirectory(0);
291  hTotal->SetDirectory(0);
292  hMax ->SetDirectory(0);
293  hRate ->GetListOfFunctions()->Add(new TLine(.5,100*minRate,5.5,100*minRate));
294  hMax ->GetListOfFunctions()->Add(new TLine(.5,maxGap,5.5,maxGap));
295  hTotal->GetListOfFunctions()->Add(new TLine(.5,15,5.5,15));
296  hTotal->GetListOfFunctions()->Add(new TLine(.5,25,5.5,25));
297 
298  UInt_t bad = 0;
299  for (UShort_t d = 1; d <= 3; d++) {
300  UShort_t nq = d/2+1;
301  for (UShort_t q = 0; q < nq; q++) {
302  Char_t r = q == 0 ? 'I' : 'O';
303  TString nam = Form("FMD%d%c", d, r);
304  TCollection* det = GetCollection(enf, nam);
305  if (!det) continue;
306 
307  TCollection* eld = GetCollection(det, "elossDists");
308  if (!eld) continue;
309 
310  Int_t nTotal = 0;
311  Int_t nOK = 0;
312  Int_t dist = 0;
313  Int_t max = 0;
314  for (Int_t bin = 1; bin <= eta.GetNbins(); bin++) {
315  TH1* dst = GetH1(eld, Form("%s_etabin%03d", nam.Data(), bin), false);
316  if (!dst) continue;
317  nTotal++;
318 
320  TList* fcs = dst->GetListOfFunctions();
321  TIter nxt(fcs);
322  TF1* fun = 0;
323  Int_t i = 0;
324  tmp.Clear();
325  while ((fun = static_cast<TF1*>(nxt()))) {
326  fit = new (tmp[i++]) AliFMDCorrELossFit::ELossFit(0,*fun);
327  fit->fDet = d;
328  fit->fRing = r;
329  fit->fBin = bin;
330  fit->CalculateQuality(mxc, mre, lwt);
331  }
332  // Sort them
333  tmp.Sort();
334  fit = static_cast<AliFMDCorrELossFit::ELossFit*>(tmp.At(i-1));
335  if (!fit) {
336  Warning("Trending2ELoss", "No fit found for %s %3d",
337  nam.Data(), bin);
338  continue;
339  }
340  corr->SetFit(d, r, bin, new AliFMDCorrELossFit::ELossFit(*fit));
342  nOK++;
343  max = TMath::Max(dist,max);
344  dist = 0;
345  }
346  else {
347  dist++;
348  }
349  } // for each bin
350  Double_t rate = Float_t(nOK)/nTotal;
351  Info("Trending2ELoss", "FMD%d%c [%d] %3d/%3d: %5.1f%% (max: %d)",
352  d, r, ir, nOK, nTotal, 100*rate, max);
353  if (rate < minRate) bad |= (1 << ir);
354  if (max > maxGap) bad |= (1 << ir);
355  if (r == 'I' && nTotal < 25) bad |= (1 << ir);
356  else if (nTotal < 15) bad |= (1 << ir);
357 
358  ir++;
359  hRate ->GetXaxis()->SetBinLabel(ir, Form("FMD%d%c",d,r));
360  hTotal->GetXaxis()->SetBinLabel(ir, Form("FMD%d%c",d,r));
361  hMax ->GetXaxis()->SetBinLabel(ir, Form("FMD%d%c",d,r));
362  hRate ->SetBinContent(ir, 100*rate);
363  hTotal->SetBinContent(ir, nTotal);
364  hMax ->SetBinContent(ir, max);
365  } // for each ring
366  } // for each detector
367  // corr->Print("R");
368 
369  TFile* diag = TFile::Open("diagnostics.root","RECREATE");
370  hRate ->SetMaximum(100); hRate ->SetMinimum(0); hRate ->Write();
371  hTotal->SetMaximum(35); hTotal->SetMinimum(0); hTotal->Write();
372  hMax ->SetMaximum(10); hTotal->SetMinimum(0); hMax ->Write();
373  diag->Write();
374  diag->Close();
375  Bool_t testBad = !corr->IsGood(true);
376  if (testBad != (bad>0)) {
377  Warning("Trending2ELoss", "Mismatch between this (%s) and test (%s)",
378  (bad>0) ? "bad" : "good", testBad ? "bad" : "good");
379  }
380  if (bad > 0)
381  return Fail(Form("%09d: One or more detectors are bad: 0x%x", run, bad));
382 
384  if (!man.Store(corr,run,sys,sNN,fld,mc,sat,"fmd_corrections.root"))
385  return Fail(Form("%09d: Failed to store correction", run));
386 
387  return true;
388 }
389 //
390 // EOF
391 //
392 
393 
void SetLowCut(Double_t cut)
Bool_t SetFit(UShort_t d, Char_t r, Double_t eta, Int_t quality, const TF1 &f)
TH1 * GetH1(TDirectory *dir, const char *name, Bool_t verbose=true)
double Double_t
Definition: External.C:58
TCollection * GetCollection(TDirectory *dir, const char *name, Bool_t verbose=true)
TString fileName
const TAxis & GetEtaAxis() const
char Char_t
Definition: External.C:18
Bool_t Trending2ELoss(const char *fileName="trending.root", Double_t minRate=.7, Int_t maxGap=3)
void CalculateQuality(Double_t maxChi2nu=fgMaxChi2nu, Double_t maxRelError=fgMaxRelError, Double_t leastWeight=fgLeastWeight)
TAxis * GetAxis(TDirectory *dir, const char *name, Bool_t verbose=true)
Bool_t GetBool(TDirectory *dir, const char *name, Bool_t def=false, Bool_t verbose=true)
Long_t GetLong(TDirectory *dir, const char *name, Long_t def=0, Bool_t verbose=true)
int Int_t
Definition: External.C:63
Definition: External.C:204
Bool_t IsGood(Bool_t verbose=true, Double_t minRate=.7, Int_t maxGap=3, Int_t minInner=25, Int_t minOuter=15, Int_t minQuality=kDefaultQuality)
unsigned int UInt_t
Definition: External.C:33
virtual Bool_t Store(TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN, Short_t field, Bool_t mc, Bool_t sat, const char *file, const char *meth="NEAR") const
float Float_t
Definition: External.C:68
Definition: External.C:212
TObject * GetObject(TDirectory *dir, const char *name, TClass *cls, Bool_t verbose=true)
Int_t GetInt(TDirectory *dir, const char *name, Int_t def=0, Bool_t verbose=true)
Double_t GetDouble(TDirectory *dir, const char *name, Double_t def=0, Bool_t verbose=true)
void SetEtaAxis(const TAxis &axis)
short Short_t
Definition: External.C:23
const char * fwd
Bool_t Fail(const char *msg)
TFile * file
TList with histograms for a given trigger.
unsigned short UShort_t
Definition: External.C:28
bool Bool_t
Definition: External.C:53
Bool_t CheckClass(TObject *o, const TObject *dir, TClass *cls, Bool_t verbose=true)
Definition: External.C:196
static AliForwardCorrectionManager & Instance()
TDirectoryFile * dir