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