]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerHV.cxx
Consolidation of AliMUONTrackerHV class
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerHV.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$
17
18 #include "AliMUONTrackerHV.h"
19
20 #include <algorithm>
21 #include <map>
22 #include <set>
23
24 #include "AliCDBManager.h"
25 #include "AliCDBEntry.h"
26 #include "AliDCSValue.h"
27 #include "AliGRPObject.h"
28 #include "AliMpDCSNamer.h"
29 #include "AliMpDEStore.h"
30 #include "AliMUONCalibrationData.h"
31 #include "AliMUONCDB.h"
32 #include "AliLog.h"
33
34 #include "TCanvas.h"
35 #include "TGraph.h"
36 #include "TH2.h"
37 #include "TLine.h"
38 #include "TMap.h"
39 #include "TMultiGraph.h"
40 #include "TObjArray.h"
41 #include "TObjString.h"
42 #include "TStyle.h"
43 #include "Riostream.h"
44
45 //
46 // Class to inspect the MUON TRACKER HV values
47 //
48 // With this class you can :
49 //
50 // a) get a list of trips (method ReportTrips)
51 // b) print the values for some (or all) HV channels (method Print)
52 // c) plot the values for some (or all) HV channels (method Plot)
53 // d) get a list of HV channels that are "OFF" (methods Scan and HVoff)
54 //
55 // Note that in this class, all the output (either text or canvas) or the
56 // channel *names* used are the same as in the DCS UI at Pt2
57 // Specifically the chamber ids start at 1, the slat numbers at 1 and
58 // the quad and sect number at 1 also. And not at zero like for the
59 // DCS *aliases*. On the contraty, the internal map, coming from the OCDB,
60 // only contains aliases, not names. Confusing ? It is.
61 //
62
63 ClassImp(AliMUONTrackerHV)
64
65 //______________________________________________________________________________
66 AliMUONTrackerHV::AliMUONTrackerHV(const char* runlist, const char* ocdbPath)
67 : TObject(), fRunList(), fOCDBPath(ocdbPath), fDCSNamer(0x0)
68 {
69   // ctor from a runlist (txt file)
70   SetRunList(runlist);
71 }
72
73 //______________________________________________________________________________
74 AliMUONTrackerHV::AliMUONTrackerHV(Int_t runNumber, const char* ocdbPath)
75 : TObject(), fRunList(), fOCDBPath(ocdbPath), fDCSNamer(0x0)
76 {
77   // ctor for a single run
78   SetRunList(runNumber);
79 }
80
81 //______________________________________________________________________________
82 AliMUONTrackerHV::~AliMUONTrackerHV()
83 {
84   // dtor
85   delete fDCSNamer;
86 }
87
88 //______________________________________________________________________________
89 void AliMUONTrackerHV::ReadIntegers(const char* filename, std::vector<int>& integers)
90 {
91   /// Read integers from filename, where integers are either
92   /// separated by "," or by return carriage
93   std::ifstream in(gSystem->ExpandPathName(filename));
94   int i;
95   
96   std::set<int> runset;
97   
98   char line[10000];
99   
100   in.getline(line,10000,'\n');
101   
102   TString sline(line);
103   
104   if (sline.Contains(","))
105   {
106     TObjArray* a = sline.Tokenize(",");
107     TIter next(a);
108     TObjString* s;
109     while ( ( s = static_cast<TObjString*>(next()) ) )
110     {
111       runset.insert(s->String().Atoi());
112     }
113     delete a;
114   }
115   else
116   {
117     runset.insert(sline.Atoi());
118     
119     while ( in >> i )
120     {
121       runset.insert(i);
122     }
123   }
124   
125   for ( std::set<int>::const_iterator it = runset.begin(); it != runset.end(); ++it )
126   {
127     integers.push_back((*it));
128   }
129   
130   std::sort(integers.begin(),integers.end());
131 }
132
133 //______________________________________________________________________________
134 AliMpDCSNamer*
135 AliMUONTrackerHV::DCSNamer() const
136 {
137   // return the dcs namer
138   if (!fDCSNamer)
139   {
140     if (!AliMpDEStore::Instance(false))
141     {
142       AliMUONCDB::LoadMapping();
143     }
144     fDCSNamer = new AliMpDCSNamer("TRACKER");
145   }
146   return fDCSNamer;
147 }
148
149 //______________________________________________________________________________
150 void AliMUONTrackerHV::SetRunList(Int_t runNumber)
151 {
152   // Make the runlist be a single run
153   fRunList.clear();
154   fRunList.push_back(runNumber);
155 }
156
157 //______________________________________________________________________________
158 void
159 AliMUONTrackerHV::SetRunList(const char* runlist)
160 {
161   // Read the runlist from an ASCII file or a comma separated list
162   // or a space separated list
163   
164   fRunList.clear();
165   
166   if ( TString(runlist).Contains(",") || TString(runlist).Contains(" ") )
167   {
168     TObjArray* runs = 0x0;
169     if ( TString(runlist).Contains(",") )
170     {
171       runs = TString(runlist).Tokenize(",");
172     }
173     else
174     {
175       runs = TString(runlist).Tokenize(" ");
176     }
177     TIter next(runs);
178     TObjString* s;
179     std::set<int> runset;
180     
181     while ( ( s = static_cast<TObjString*>(next()) ) )
182     {
183       runset.insert(s->String().Atoi());
184     }
185     
186     for ( std::set<int>::const_iterator it = runset.begin(); it != runset.end(); ++it )
187     {
188       fRunList.push_back((*it));
189     }
190     
191     std::sort(fRunList.begin(),fRunList.end());
192     
193     delete runs;
194   }
195   else
196   {
197     ReadIntegers(runlist,fRunList);
198   }
199 }
200
201
202 //______________________________________________________________________________
203 TGraph*
204 AliMUONTrackerHV::GraphValues(TMap* m, const char* dcsname)
205 {
206   // make a graph of HV channels' voltage values for a given dcs name (name, not
207   // alias)
208   
209   if ( TString(dcsname).Contains("sw") )
210   {
211     // do not graph switches
212     return 0x0;
213   }
214
215   
216   AliInfo(dcsname);
217   
218   TPair* p = static_cast<TPair*>(m->FindObject(DCSNamer()->DCSAliasFromName(dcsname).Data()));
219   
220   if (!p) return 0x0;
221   
222   TObjArray* a = static_cast<TObjArray*>(p->Value());
223   TIter n2(a);
224   AliDCSValue* val;
225   Int_t i(0);
226
227   TGraph* g = new TGraph(a->GetEntries());
228   while ( ( val = static_cast<AliDCSValue*>(n2()) ) )
229   {
230     g->SetPoint(i,val->GetTimeStamp(),val->GetFloat());
231     ++i;
232   }
233   return g;
234 }
235
236 //______________________________________________________________________________
237 void
238 AliMUONTrackerHV::Scan(Int_t verbose)
239 {
240   /// Retrieve HV values from OCDB for a given run list, and check whether
241   /// we have some issues with them...
242   /// If you pipe the results of this into a text file, you can then
243   /// feed it to the HVoff method for further investigations.
244   ///
245   
246   if ( fRunList.empty() )
247   {
248     AliError("No runs to process...");
249     return;    
250   }
251     
252   AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
253   
254   for ( std::vector<int>::size_type i = 0; i < fRunList.size(); ++i )
255   {
256     AliMUONCDB::CheckHV(fRunList[i],verbose);
257   }
258 }
259
260 //______________________________________________________________________________
261 void AliMUONTrackerHV::HVoff(const char* logfile, const char* outputBaseName)
262 {
263   /// Check the number of HV which have problem
264   /// the input is the output of e.g.
265   /// .L MUONTrackerHV.C+
266   /// ScanHV("lhc11de.list");>  lhc11de.log
267   ///
268   
269   gStyle->SetOptStat(0);
270   
271   char line[1024];
272   
273   std::ifstream in(logfile);
274   int run(-1),a,b,c,d,e,f,g,h,z,other;
275   std::map<int,std::string> results;
276   
277   std::string message;
278   const char* testProblem = "I-AliMUONCDB::CheckHV::CheckHV:      Problem at ";
279   
280   while ( in.getline(line,1023,'\n') )
281   {
282     TString sline(line);
283     if (sline.Contains("SUMMARY"))
284     {
285       AliInfo(line);
286       int r;
287       sscanf(line,"I-AliMUONCDB::CheckHV::CheckHV: RUN %09d HVchannel SUMMARY : # of cases A(%3d) B(%3d) C(%3d) D(%3d) E(%3d) F(%3d) G(%3d) H(%3d) Z(%3d) OTHER(%3d)",
288              &r,&a,&b,&c,&d,&e,&f,&g,&h,&z,&other);
289       if ( r != run )
290       {
291         if ( run == -1 )
292         {
293           AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
294           AliCDBManager::Instance()->SetRun(r);
295           AliMUONCDB::LoadMapping();
296         }
297         
298         if ( run > 0 )
299         {
300           results.insert(std::make_pair<int,std::string>(run,message));
301           
302         }
303         message = "";
304         run = r;
305       }          
306     }
307     else if ( sline.Contains(testProblem) )
308     {
309       message += "|";
310       message += sline(strlen(testProblem),sline.Length()-1).Data();
311     }
312   }
313   
314   results.insert(std::make_pair<int,std::string>(run,message));
315   
316   TH2* hvoff = new TH2I(outputBaseName,outputBaseName,1,0,1,1,0,1);
317   
318   std::map<int,std::string>::const_iterator it;
319   
320   for ( it = results.begin(); it != results.end(); ++it )
321   {
322     AliInfo(Form("%d -> %s",it->first,it->second.c_str()));
323     TObjArray* split = TString(it->second.c_str()).Tokenize("|");
324     TIter next(split);
325     TObjString* str;
326     while ( ( str = static_cast<TObjString*>(next()) ) )
327     {
328       TString s(str->String());
329       TObjArray* parts = s.Tokenize(":");
330       TString alias = (static_cast<TObjString*>(parts->At(0)))->String();
331       TString channel = DCSNamer()->DCSNameFromAlias(alias.Data());
332       channel.ReplaceAll(".actual.vMon","");
333       hvoff->Fill(Form("%6d",it->first),channel.Data(),1.0);
334       delete parts;
335     }
336     delete split;
337   }
338   
339   hvoff->LabelsDeflate("x");
340   hvoff->LabelsDeflate("y");
341   hvoff->LabelsOption("x","<");
342   hvoff->LabelsOption("y","<");
343   
344   TCanvas* c1 = new TCanvas;
345   c1->SetLeftMargin(0.35);
346   hvoff->Draw("text");
347   c1->Print(Form("%s.pdf",outputBaseName));
348   TCanvas* c2 = new TCanvas;
349   TH1* hx = hvoff->ProjectionX("hvoffperrun");
350   hx->Draw();
351   c2->Print(Form("%s-perrun.pdf",outputBaseName));
352   TCanvas* c3 = new TCanvas;
353   c3->SetBottomMargin(0.5);
354   TH1* perchannel = hvoff->ProjectionY("hvoffperchannel");
355   perchannel->GetXaxis()->SetBit(TAxis::kLabelsVert);
356   perchannel->GetXaxis()->LabelsOption(">");
357   perchannel->Draw();
358   c3->Print(Form("%s-perchannel.pdf",outputBaseName));
359 }
360
361 //______________________________________________________________________________
362 void AliMUONTrackerHV::TimeAxis(TMultiGraph* g)
363 {
364   g->GetXaxis()->SetTimeDisplay(1);
365 //  g->GetXaxis()->SetTimeFormat("%d/%m %H:%M%F2010-12-31 24:00:00");
366   g->GetXaxis()->SetTimeFormat("%d/%m %H:%M");
367   g->GetXaxis()->SetTimeOffset(0,"gmt");
368   g->GetXaxis()->SetNdivisions(505);
369 }
370
371 //______________________________________________________________________________
372 TMultiGraph*
373 AliMUONTrackerHV::GraphHV(TMap* m, const char* dcsname)
374 {
375   // Make a graph of the values matching dcsname
376   TIter next(m);
377   TObjString* s;
378   
379   TMultiGraph* mg = new TMultiGraph;
380
381   while ( ( s = static_cast<TObjString*>(next()) ) )
382   {
383     TString name(DCSNamer()->DCSNameFromAlias(s->String()));
384     
385     if ( dcsname && !name.Contains(dcsname)) continue;
386     
387     TGraph* g = GraphValues(m,name);
388     
389     if ( g ) 
390     {
391       g->SetMarkerSize(1.5);
392       g->SetMarkerStyle(2);
393       g->SetLineStyle(2);
394       mg->Add(g,"lp");
395       g->SetTitle(name.Data());
396     }
397   }  
398
399   return mg;
400 }
401
402 //______________________________________________________________________________
403 void
404 AliMUONTrackerHV::Print(Option_t* dcsname) const
405 {
406   /// Print HV values for a given dcs name (or all if dcsname=0)
407   
408   AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
409   TList messages;
410   messages.SetOwner(kTRUE);
411   
412   for ( std::vector<int>::size_type iRun = 0; iRun < fRunList.size(); ++iRun )
413   {
414     Int_t runNumber = fRunList[iRun];
415     
416     AliInfo("---------------------");
417     AliInfo(Form("RUN %09d",runNumber));
418     
419     messages.Delete();
420     
421     AliCDBManager::Instance()->SetRun(runNumber);
422     
423     TMap* m = AliMUONCalibrationData::CreateHV(runNumber,0x0,kFALSE,&messages,kTRUE);
424     
425     TIter next(m);
426     TObjString* s;
427     
428     while ( ( s = static_cast<TObjString*>(next()) ) )
429     {      
430       TString name(DCSNamer()->DCSNameFromAlias(s->String()));
431       
432       if ( dcsname && !name.Contains(dcsname)) continue;
433       
434       TPair* p = static_cast<TPair*>(m->FindObject(DCSNamer()->DCSAliasFromName(dcsname).Data()));
435       
436       if (!p) continue;
437       
438       TObjArray* a = static_cast<TObjArray*>(p->Value());
439       TIter n2(a);
440       AliDCSValue* val;
441       Int_t i(0);
442       
443       while ( ( val = static_cast<AliDCSValue*>(n2()) ) )
444       {
445         std::cout << Form("i=%5d ",i) << std::endl;
446         val->Print("");
447         ++i;
448       }
449     }
450   }
451 }
452
453 //______________________________________________________________________________
454 void
455 AliMUONTrackerHV::Plot(const char* dcsname, Bool_t withPatch)
456 {
457   /// Show HV values for a given dcs name (or all if dcsname=0)
458   /// Each canvas for each run will go to a separate PDF file
459   
460   AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
461   TList messages;
462   messages.SetOwner(kTRUE);
463   
464   for ( std::vector<int>::size_type i = 0; i < fRunList.size(); ++i )
465   {
466     Int_t runNumber = fRunList[i];
467   
468     messages.Delete();
469     
470     AliCDBManager::Instance()->SetRun(runNumber);
471     
472     TMap* m = AliMUONCalibrationData::CreateHV(runNumber,0x0,withPatch,&messages,kTRUE);
473     
474     TMultiGraph* mg = GraphHV(m,dcsname);
475     
476     if ( !mg ) continue;
477     
478     TString cname(Form("MCH_HV_RUN%09d",runNumber));
479     
480     if ( strlen(dcsname) > 0 )
481     {
482       TString s(dcsname);
483       s.ReplaceAll("/","_");
484       cname += Form("_dcsname_%s",s.Data());
485     }
486     
487     AliCDBEntry* e = AliCDBManager::Instance()->Get("GRP/GRP/Data",runNumber);
488     
489     TLine* startRunLine(0);
490     TLine* endRunLine(0);
491     time_t start(0);
492     time_t end(0);
493     
494     if ( e )
495     {
496       AliGRPObject* grp = static_cast<AliGRPObject*>(e->GetObject());
497       if (grp)
498       {
499         start = grp->GetTimeStart();
500         end = grp->GetTimeEnd();
501       }
502     }
503     
504     if ( end )
505     {
506       TGraph* g = new TGraph(1);
507       g->SetPoint(0,end,0);
508       mg->Add(g,"");
509     }
510     
511     TCanvas* c = new TCanvas(cname.Data(),cname.Data());
512     
513     c->Draw();
514     
515     mg->SetTitle(cname.Data());
516     
517     mg->Draw("AL");
518     
519     TimeAxis(mg);
520     
521     if ( start )
522     {
523       startRunLine = new TLine(start,mg->GetYaxis()->GetXmin(),start,mg->GetYaxis()->GetXmax());
524       startRunLine->SetLineColor(2);
525       startRunLine->SetLineWidth(4);
526     }
527     if  ( end )
528     {
529       endRunLine = new TLine(end,mg->GetYaxis()->GetXmin(),end,mg->GetYaxis()->GetXmax());
530       endRunLine->SetLineColor(2);
531       endRunLine->SetLineWidth(4);
532     }
533     
534     if ( startRunLine ) startRunLine->Draw();
535     if ( endRunLine ) endRunLine->Draw();
536     
537     c->SaveAs(Form("%s.pdf",cname.Data()));
538   }
539 }
540
541 //______________________________________________________________________________
542 void
543 AliMUONTrackerHV::ReportTrips(Bool_t includeLowOnes)
544 {
545   /// Report trips
546   /// if includeLowOnes is kTRUE we'll report also the trips which starts from non-operational voltage values
547   
548   AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
549   
550   TList messages;
551   messages.SetOwner(kTRUE);
552   TObjString* msg(0);
553
554   for ( std::vector<int>::size_type i = 0; i < fRunList.size(); ++i )
555   {
556     Int_t runNumber = fRunList[i];
557     
558     AliInfo("---------------------");
559     
560     Int_t ntrips(0);
561     
562     messages.Delete();
563     
564     AliCDBManager::Instance()->SetRun(runNumber);
565     
566     AliMUONCalibrationData::CreateHV(runNumber,0x0,kTRUE,&messages,kTRUE);
567     
568     if (!AliMpDEStore::Instance(false))
569     {
570       AliMUONCDB::LoadMapping();
571     }
572     
573     TIter next(&messages);
574
575     while ( ( msg = static_cast<TObjString*>(next())) )
576     {
577       if ( msg->String().Contains("TRIP") && ( includeLowOnes || !msg->String().Contains("LOWTRIP") ) )
578       {
579         ++ntrips;
580       }
581     }
582
583     AliInfo(Form("RUN %09d - %d trip%c",runNumber,ntrips,(ntrips>1 ? 's':' ')));
584     
585     next.Reset();
586     std::map<int,std::string> report;
587     
588     while ( ( msg = static_cast<TObjString*>(next())) )
589     {
590       if ( msg->String().Contains("TRIP") )
591       {
592         TObjArray* parts = msg->String().Tokenize(" ");
593         TString channelName(static_cast<TObjString*>(parts->At(0))->String());
594         
595         for ( Int_t ip = 0; ip <= parts->GetLast(); ++ip)
596         {
597           TString p(static_cast<TObjString*>(parts->At(ip))->String());
598           
599           if ( p.Contains("TRIP") )
600           {
601             if ( includeLowOnes || !p.Contains("LOWTRIP") )
602             {
603               TString ts(static_cast<TObjString*>(parts->At(ip+2))->String());
604           
605               ip += 3;
606           
607               Int_t index = ts.Index("TS:");
608           
609               UInt_t timeStamp = TString(ts(index+strlen("TS:"),ts.Length()-index)).Atoi();
610           
611               TString tmp(msg->String());
612               tmp.ReplaceAll(channelName.Data(),DCSNamer()->DCSNameFromAlias(channelName.Data()));
613               report[timeStamp] = tmp.Data();
614             }
615           }
616         }
617         delete parts;
618       }
619     }
620
621     for ( std::map<int,std::string>::const_iterator it = report.begin(); it != report.end(); ++it )
622     {
623       AliInfo(Form("%s %s",TTimeStamp(it->first).AsString("s"),it->second.c_str()));
624     }
625   }
626 }
627