]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/TRD/info/AliTRDtrendingManager.cxx
107891f840a940b6f6838d56128ea4aa035b8a1e
[u/mrichter/AliRoot.git] / PWGPP / TRD / info / AliTRDtrendingManager.cxx
1 ////////////////////////////////////////////////////////////////////////////
2 //                                                                        //
3 //  Trend Value Manager                                                   //
4 //                                                                        //
5 //  Mediates interaction with DB (OCDB ?!)                                //                                                                      //                                                                        //
6 //  Authors:                                                              //
7 //    Alexandru Bercuci <A.Bercuci@gsi.de>                                //
8 //                                                                        //
9 ////////////////////////////////////////////////////////////////////////////
10
11 #include "TFile.h"
12 #include "TKey.h"
13 #include "TObjArray.h"
14 #include "TH1F.h"
15 #include "TAxis.h"
16 #include "TGraph.h"
17 #include "TCanvas.h"
18 #include "TString.h"
19
20 #include "AliLog.h"
21 #include "AliTRDtrendingManager.h"
22
23 ClassImp(AliTRDtrendingManager)
24
25 AliTRDtrendingManager* AliTRDtrendingManager::fgInstance=NULL;
26 Bool_t AliTRDtrendingManager::fgTerminated = kFALSE;
27
28 //____________________________________________
29 AliTRDtrendingManager* AliTRDtrendingManager::Instance()
30 {
31   //
32   // Singleton implementation
33   // Returns an instance of this class, it is created if neccessary
34   //
35   if (fgTerminated != kFALSE) return NULL;
36
37   if (!fgInstance) fgInstance = new AliTRDtrendingManager();
38
39   return fgInstance;
40 }
41
42 //____________________________________________
43 void AliTRDtrendingManager::Terminate()
44 {
45   //
46   // Singleton implementation
47   // Deletes the instance of this class and sets the terminated flag,
48   // instances cannot be requested anymore
49   // This function can be called several times.
50   //
51   
52   fgTerminated = kTRUE;
53   
54   if (fgInstance != NULL) {
55     if(TFile::Open("TRD.Trend.root", "RECREATE")){
56       fEntries->Write();
57       gFile->Close();
58     }
59     delete fgInstance; fgInstance = NULL;
60   }
61 }
62
63 //____________________________________________
64 AliTRDtrendingManager::AliTRDtrendingManager() 
65   : TObject()
66   ,fEntries(NULL)
67   ,fValue(NULL)
68 {
69 // Constructor
70 //  fRunRange[0] = 0; fRunRange[1] = AliCDBRunRange::Infinity();
71 }
72
73 //____________________________________________
74 AliTRDtrendingManager::~AliTRDtrendingManager()
75 {
76 // Destructor
77   if(fValue) delete fValue;
78   if(fEntries) delete fEntries;
79 }
80
81 //____________________________________________
82 void AliTRDtrendingManager::AddValue(
83    const Char_t *name
84   ,Double_t mean,Double_t sigm
85   ,const Char_t *title
86   ,const Char_t *responsible
87   ,const Char_t *notifiables
88   ,Char_t **messages
89   )
90 {
91 // Expert Function !!!
92 // Add a trend value to the map already loaded
93 // If no map loaded create a new one from scratch
94 //
95 // class_name : name of the performance task 
96 // name       : name of the value to be trended
97 // title      : description of the value to be trended
98 // messages   : array of alarm messages for each alarm level
99 // responsible: name and email of the responsible person. Format "name/email"
100 // notifiables: name and email of the notifiable persons. Format "name1/email1, name2/email2, etc"
101 //
102
103   if(!fEntries){ // if no trending map defined create one
104     AliDebug(1, "No trending map loaded. Create one from scratch.");
105     fEntries = new TObjArray(50);
106     fEntries->SetOwner();
107     fEntries->SetName("values");
108   }
109
110   if(!(fValue = GetValue(name))){
111     // create new trending value`
112     fValue = new AliTRDtrendValue(name, title?title:"");
113     fValue->Set(mean, sigm);
114     if(messages) for(Int_t ilevel(AliTRDtrendValue::kNlevels); ilevel--;) if(messages[ilevel]) fValue->SetAlarm(ilevel, messages[ilevel]);
115     TObjArray *r(NULL);
116     if(responsible){
117       TString s(responsible);
118       r=s.Tokenize("/");
119       if(r->GetEntriesFast()!=2){
120         AliWarning("Responsible name/email incorrectly formated.");
121       } else {
122         fValue->SetResponsible(((TObjString*)r->At(0))->String().Data(), ((TObjString*)r->At(1))->String().Data());
123       }
124     }
125     if(notifiables){
126       TString s(notifiables);
127       TObjArray *n=s.Tokenize(",");
128       for(Int_t in(0); in<TMath::Min(AliTRDtrendValue::kNnotifiable, n->GetEntriesFast()); in++){
129         TString ss(((TObjString*)n->At(in))->String());
130         r=ss.Tokenize("/");
131         if(r->GetEntriesFast()!=2){
132           AliWarning(Form("Notifiable person name/email incorrectly formated for [%s].", ss.Data()));
133         } else {
134           fValue->SetNotifiable(((TObjString*)r->At(0))->String().Data(), ((TObjString*)r->At(1))->String().Data());
135         }
136       }
137     }
138   }
139
140   fEntries->AddLast(new AliTRDtrendValue(*fValue));
141 }
142
143 //____________________________________________
144 AliTRDtrendValue* AliTRDtrendingManager::GetValue(const Char_t *name)
145 {
146 // Search trend value list by value "name" formatted according to "class_name"
147   if(!fEntries){
148     AliError("No trending map defined.");
149     return NULL;
150   }
151   fValue = (AliTRDtrendValue*)fEntries->FindObject(name);
152   return fValue;
153 }
154
155 //____________________________________________
156 Bool_t AliTRDtrendingManager::MakeTrends(const char *fileList)
157 {
158 // Make trends with reference to DB for all trend files in "fileList".
159 // The DB should be loaded
160   if(!fEntries){
161     AliWarning("Trending map undefined");
162     return kFALSE;
163   }
164   Int_t ntv(fEntries->GetEntries());
165
166   FILE *fp(NULL);
167   if(!(fp= fopen(fileList, "rt"))){
168     AliWarning(Form("Can not open file list \"%s\"", fileList));
169     return kFALSE;
170   }
171
172   TGraph **g = new TGraph*[ntv]; memset(g, 0, ntv*sizeof(TGraph*));
173   AliTRDtrendValue *TV(NULL), *tv(NULL);
174   TString sfp; Int_t run[10000], nr(0);
175   while(sfp.Gets(fp)){
176     // guess run no from path. Do we need a more robust approach ?
177     TObjArray *afp=sfp.Tokenize("/");
178     Int_t idx = afp->GetEntries()-2;
179     Int_t rno = ((TObjString*)(*afp)[idx])->GetString().Atoi();
180
181     if(!TFile::Open(sfp.Data())) continue;
182     run[nr] = rno;
183     for(Int_t it(0); it<ntv; it++){
184       if(!(TV = (AliTRDtrendValue*)fEntries->At(it))) continue;
185       if(!(tv = (AliTRDtrendValue*)gFile->Get(TV->GetName()))) {
186         AliWarning(Form("Missing %09d.%s", rno, TV->GetName()));
187         continue;
188       }
189       (*tv)/=(*TV);
190       if(!g[it]){
191         g[it] = new TGraph();
192         g[it]->SetNameTitle(TV->GetName(), TV->GetTitle());
193         g[it]->SetMarkerStyle(4);g[it]->SetMarkerSize(0.8);
194       }
195       g[it]->SetPoint(g[it]->GetN(), nr, tv->GetVal());
196     }
197     nr++;
198   }
199
200   // Draw
201   TH1 *hT = new TH1F("hT", ";#bf{RUN};", nr, -0.5, nr-0.5);
202   TAxis *ax = hT->GetXaxis(); ax->SetTitleOffset(2.6);ax->CenterTitle(); ax->SetBit(TAxis::kLabelsVert);
203   TAxis *ay = hT->GetYaxis(); ay->SetTitleOffset(0.4);ay->CenterTitle(); ay->SetAxisColor(kRed); ay->SetRangeUser(-5, 5);
204   for(Int_t ir(0); ir<nr; ir++) ax->SetBinLabel(ir+1, Form("%09d", run[ir]));
205
206   TCanvas *c = new TCanvas("c", "TRD Trend", 1, 1, 1200, 500);
207   c->SetLeftMargin(0.03666361);
208   c->SetRightMargin(0.005499542);
209   c->SetTopMargin(0.02542373);
210   c->SetBottomMargin(0.1758475);
211   for(Int_t it(0); it<ntv; it++){
212     c->Clear();
213     ay->SetTitle(Form("#bf{%s [#sigmau]}", g[it]->GetTitle())); hT->Draw("p");
214     g[it]->Draw("p");
215     c->Modified(); c->Update(); c->SaveAs(Form("Trend_%s.gif", g[it]->GetName()));
216     delete g[it];
217   }
218   delete [] g;
219   return kTRUE;
220 }
221
222 //____________________________________________
223 Bool_t AliTRDtrendingManager::ModifyValue(
224   const Char_t *name
225   ,const Char_t *title
226   ,Double_t mean, Double_t sgm
227   ,Char_t **messages
228   ,const Char_t *responsible
229   ,const Char_t *notifiables
230   )
231 {
232 // Expert Function !!!
233 // Modify a trend value in the map already loaded
234 // see function AddValue() for explanation of input format. 
235
236   if(!fEntries){
237     AliError("No trending map loaded.");
238     return kFALSE;
239   }
240   AliWarning("*** EXPERT FUNCTION *** This function is modifying one trending value to the current DB. Continue if you know what yout do!");
241
242   if(!GetValue(name)){
243     AliError(Form("Missing trending value %s", name));
244     return kFALSE;
245   }  
246   
247   fValue->SetTitle(title);
248   fValue->Set(mean, sgm);
249   if(messages){ 
250     for(Int_t ilevel(AliTRDtrendValue::kNlevels); ilevel--;) fValue->SetAlarm(ilevel, messages[ilevel]);
251   }
252   TString s;
253   TObjArray *r(NULL);
254   if(responsible){ 
255     s=responsible;
256     r=s.Tokenize("/");
257     if(r->GetEntriesFast()!=2){ 
258       AliWarning("Responsible name/email incorrectly formated.");
259     } else { 
260       fValue->SetResponsible(((TObjString*)r->At(0))->String().Data(), ((TObjString*)r->At(1))->String().Data());
261     }
262   }
263   if(notifiables){
264     s=notifiables;
265     TObjArray *n=s.Tokenize(",");
266     for(Int_t in(0); in<TMath::Min(AliTRDtrendValue::kNnotifiable, n->GetEntriesFast()); in++){
267       TString ss(((TObjString*)n->At(in))->String());
268       r=ss.Tokenize("/");
269       if(r->GetEntriesFast()!=2){ 
270         AliWarning(Form("Notifiable person name/email incorrectly formated for [%s].", ss.Data()));
271       } else { 
272         fValue->SetNotifiable(((TObjString*)r->At(0))->String().Data(), ((TObjString*)r->At(1))->String().Data());
273       }
274     }
275   }
276   return kTRUE;
277 }
278
279 //____________________________________________
280 void AliTRDtrendingManager::Print(Option_t *o) const
281 {
282 // Dump trend value list
283   if(!fEntries){
284     AliError("No trending map available.");
285     return;
286   }
287
288   for(Int_t iv(0); iv<fEntries->GetEntriesFast(); iv++){
289     ((AliTRDtrendValue*)fEntries->At(iv))->Print(o);
290   }
291 }
292
293 //____________________________________________
294 void AliTRDtrendingManager::Load(const char *fn)
295 {
296 // Load TRD trending DB from $ALICE_ROOT/PWGPP/TRD/data.
297
298   AliDebug(1, "Loading TRD trending ...");
299   if(!TFile::Open(fn)) return;
300
301   TList *tvList = gFile->GetListOfKeys(); TIterator *iter(tvList->MakeIterator()); AliTRDtrendValue *tv(NULL);
302   fEntries = new TObjArray(tvList->GetEntries());
303   fEntries->SetOwner();
304   TKey *ktv(NULL);
305   while((ktv = (TKey*)iter->Next())){
306     tv = (AliTRDtrendValue*)gFile->Get(ktv->GetName());
307     fEntries->AddLast(new AliTRDtrendValue(*tv));
308   }
309 }