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