ROOT logo
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
*                                                                        *
* Author: The ALICE Off-line Project.                                    *
* Contributors are mentioned in the code where appropriate.              *
*                                                                        *
* Permission to use, copy, modify and distribute this software and its   *
* documentation strictly for non-commercial purposes is hereby granted   *
* without fee, provided that the above copyright notice appears in all   *
* copies and that both the copyright notice and this permission notice   *
* appear in the supporting documentation. The authors make no claims     *
* about the suitability of this software for any purpose. It is          *
* provided "as is" without express or implied warranty.                  *
**************************************************************************/

/* $Id$ */

/// \ingroup macros
/// \file MUONOfflineShift.C
/// \brief Macro to be used to check raw data during MUON offline shifts.
///  
/// You NEED an access to the Grid to run this macro.
///
/// Basic usage is : 
///
/// MUONOfflineShift("path_to_raw_file","basename of output file"); > log
///
/// (the redirection to an output log file is recommended as the output from
/// this macro might be quite long...)
///
/// This will read the raw data and process it several times, varying what's done :
/// only decoding, decoding + zero-suppression, etc... (TBE)
///
/// Two outputs files will be created : 
///
/// - basename.root, containing AliMUONVTrackerData objects that can then 
///   be displayed using the mchview program (using mchview --use basename.root)
///
/// - basename.log, containing (for the moment) the occupancy numbers of the various detection elements
///
/// Unless you really know what you're doing, please use this macro together with 
/// the grid OCDB (i.e. don't set it to a local OCDB). There are now a lot of things
/// that are grabbed from the OCDB that are run dependent...
///
/// \author Laurent Aphecetche

#if !defined(__CINT__) || defined(__MAKECINT__)

#include "AliCDBManager.h"
#include "AliCodeTimer.h"
#include "AliMUONCDB.h"
#include "AliMUONPainterDataRegistry.h"
#include "AliMUONRecoParam.h"
#include "AliMUONTrackerDataMaker.h"
#include "AliMUONVTrackerData.h"
#include "AliMpCDB.h"
#include "AliMpConstants.h"
#include "AliMpDEIterator.h"
#include "AliRawReader.h"
#include <Riostream.h>
#include <TFile.h>
#include <TGrid.h>
#include <TStopwatch.h>
#include <TString.h>
#include <TSystem.h>

#endif

//______________________________________________________________________________
Int_t DataMakerReading(const char* input,
                       TStopwatch& timer,
                       const char* cdbPath="",
                       const char* calibMode="",
                       Bool_t histogram=kFALSE,
                       Double_t xmin = 0.0,
                       Double_t xmax = 4096.0)
{
  /// Run over the data and calibrate it if so required (if cdbPath != "")
  /// calibMode can be :
  /// - NOGAIN           : only zero-suppression will be done
  /// - GAINCONSTANTCAPA : zero-suppression + gain, but with a single capa value for all channels
  /// - GAIN             : zero-suppression + gain w/ individual capacitance per channel.
  
  TString fileName(gSystem->ExpandPathName(input));
  
  AliRawReader* rawReader = AliRawReader::Create(fileName.Data());
  
  if (!rawReader) return 0;
  
  AliMUONVTrackerDataMaker* dm(0x0);
  
  AliMUONRecoParam* recoParam = AliMUONCDB::LoadRecoParam();
  if (!recoParam) return 0;
  
  if ( strlen(cdbPath) > 0 ) 
  {
    dm = new AliMUONTrackerDataMaker(recoParam,rawReader,cdbPath,calibMode,histogram,xmin,xmax);
  }
  else  
  {
    dm = new AliMUONTrackerDataMaker(rawReader,histogram);
  }
  
  AliMUONPainterDataRegistry::Instance()->Register(dm);

  timer.Start(kTRUE);
  Int_t n(0);
  
  dm->SetRunning(kTRUE);
  
  while (dm->NextEvent())
  {
    ++n;
  }
  
  timer.Stop();
  
  return n;
}

//______________________________________________________________________________
void Print(const char* method, TStopwatch& timer, Int_t n)
{
  /// Get the timing for a given method
  
  cout << Form("%20s %10d events. Total CPU time %7.2f seconds.",
               method,n,timer.CpuTime());
  
  Double_t cpu = timer.CpuTime()/n;
  Double_t real = timer.RealTime()/n;
  
  cout << Form(" ms real/event = %7.2f ms CPU/event = %7.2f",real*1E3,cpu*1E3)
    << endl;
}

//______________________________________________________________________________
void Occupancy(ostream& outfile)
{
  /// Write occupancy numbers to output text file
  
  outfile << "-----------------------------------------------------" << endl;
  outfile << "Occupancy numbers" << endl;
  outfile << "-----------------------------------------------------" << endl;
  
  const Int_t occIndex = 2;
  
  AliMUONPainterDataRegistry* reg = AliMUONPainterDataRegistry::Instance();

  Int_t nofDataSources = reg->NumberOfDataSources();

  outfile << Form("%11s|"," ");
  
  for ( Int_t ids = 0; ids < nofDataSources; ++ ids ) 
  {
    AliMUONVTrackerData* data = reg->DataSource(ids);
    outfile << Form(" %13s |",data->GetName());
  }

  outfile << endl;

  for ( Int_t chamberId = 0; chamberId < AliMpConstants::NofTrackingChambers(); ++chamberId ) 
  {
    Bool_t nonZero(kFALSE);
    for ( Int_t ids = 0; ids < nofDataSources && nonZero == kFALSE; ++ ids ) 
    {
      if ( reg->DataSource(ids)->Chamber(chamberId,occIndex) ) nonZero = kTRUE;
    }
    
    if ( !nonZero ) continue;
    
    outfile << Form("Chamber %2d |",chamberId);
    for ( Int_t ids = 0; ids < nofDataSources; ++ ids ) 
    {
      AliMUONVTrackerData* data = reg->DataSource(ids);
      outfile << Form("   %7.2f %%   |",100.0*data->Chamber(chamberId,occIndex));
    }
    outfile << endl;
    
    AliMpDEIterator it;
    it.First(chamberId);
    while (!it.IsDone())
    {
      Int_t detElemId = it.CurrentDEId();
      Bool_t nonZero(kFALSE);
      for ( Int_t ids = 0; ids < nofDataSources && nonZero == kFALSE; ++ ids ) 
      {
        AliMUONVTrackerData* data = reg->DataSource(ids);
        if ( data->DetectionElement(detElemId,occIndex) > 0 ) 
        {
          nonZero = kTRUE;
        }
      }
      
      if ( nonZero ) 
      {
        outfile << Form("   DE %04d |",detElemId);
        for ( Int_t ids = 0; ids < nofDataSources; ++ ids ) 
        {
          AliMUONVTrackerData* data = reg->DataSource(ids);
          outfile << Form("   %7.2f %%   |",100.0*data->DetectionElement(detElemId,occIndex));
        }
        outfile << endl;
      }
      it.Next();
    }
  }
}

//______________________________________________________________________________
void MUONOfflineShift(const char* input="alien:///alice/data/2009/LHC09a/000067495/raw/09000067495031.10.root", 
                      const char* outputBase="67495031.10",
                      const char* ocdbPath="alien://folder=/alice/data/2009/OCDB")
{
  /// Entry point of the macro. 

  AliCodeTimer::Instance()->Reset();
  
  TGrid::Connect("alien://");
  
  AliRawReader* rawReader = AliRawReader::Create(input);
  
  rawReader->NextEvent();
  
  Int_t runNumber = rawReader->GetRunNumber();
    
  delete rawReader;
  
  AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
  AliCDBManager::Instance()->SetRun(runNumber);

  if (!AliMUONCDB::LoadMapping()) return;
  
  TStopwatch timer1;
  TStopwatch timer2;
  TStopwatch timer3;
  TStopwatch timer4;
  TStopwatch timer5;
  
  Int_t n1 = DataMakerReading(input,timer1,"","",kTRUE,0,0);

  Int_t n2 = DataMakerReading(input,timer2,ocdbPath,"NOGAIN");

  Int_t n3 = DataMakerReading(input,timer3,ocdbPath,"GAINCONSTANTCAPA");

  Int_t n4 = DataMakerReading(input,timer4,ocdbPath,"GAIN");

  Int_t n5 = DataMakerReading(input,timer5,ocdbPath,"INJECTIONGAIN");

  Print("DataMakerReading(HRAW)",timer1,n1);  
  Print("DataMakerReading(HCALZ)",timer2,n2);
  Print("DataMakerReading(HCALG)",timer3,n3);
  Print("DataMakerReading(HCALC)",timer4,n4);
  Print("DataMakerReading(HCALE)",timer5,n5);
  
  AliMUONPainterDataRegistry* reg = AliMUONPainterDataRegistry::Instance();
  
  TFile f(gSystem->ExpandPathName(Form("%s.root",outputBase)),"RECREATE");
  ofstream out(gSystem->ExpandPathName(Form("%s.log",outputBase)));

  Occupancy(out);

  for ( Int_t i = 0; i < reg->NumberOfDataSources(); ++i )
  {
    AliMUONVTrackerData* data = reg->DataSource(i);
    data->Write();
  }
  
  f.Close();
  
  AliCodeTimer::Instance()->Print();

}
 MUONOfflineShift.C:1
 MUONOfflineShift.C:2
 MUONOfflineShift.C:3
 MUONOfflineShift.C:4
 MUONOfflineShift.C:5
 MUONOfflineShift.C:6
 MUONOfflineShift.C:7
 MUONOfflineShift.C:8
 MUONOfflineShift.C:9
 MUONOfflineShift.C:10
 MUONOfflineShift.C:11
 MUONOfflineShift.C:12
 MUONOfflineShift.C:13
 MUONOfflineShift.C:14
 MUONOfflineShift.C:15
 MUONOfflineShift.C:16
 MUONOfflineShift.C:17
 MUONOfflineShift.C:18
 MUONOfflineShift.C:19
 MUONOfflineShift.C:20
 MUONOfflineShift.C:21
 MUONOfflineShift.C:22
 MUONOfflineShift.C:23
 MUONOfflineShift.C:24
 MUONOfflineShift.C:25
 MUONOfflineShift.C:26
 MUONOfflineShift.C:27
 MUONOfflineShift.C:28
 MUONOfflineShift.C:29
 MUONOfflineShift.C:30
 MUONOfflineShift.C:31
 MUONOfflineShift.C:32
 MUONOfflineShift.C:33
 MUONOfflineShift.C:34
 MUONOfflineShift.C:35
 MUONOfflineShift.C:36
 MUONOfflineShift.C:37
 MUONOfflineShift.C:38
 MUONOfflineShift.C:39
 MUONOfflineShift.C:40
 MUONOfflineShift.C:41
 MUONOfflineShift.C:42
 MUONOfflineShift.C:43
 MUONOfflineShift.C:44
 MUONOfflineShift.C:45
 MUONOfflineShift.C:46
 MUONOfflineShift.C:47
 MUONOfflineShift.C:48
 MUONOfflineShift.C:49
 MUONOfflineShift.C:50
 MUONOfflineShift.C:51
 MUONOfflineShift.C:52
 MUONOfflineShift.C:53
 MUONOfflineShift.C:54
 MUONOfflineShift.C:55
 MUONOfflineShift.C:56
 MUONOfflineShift.C:57
 MUONOfflineShift.C:58
 MUONOfflineShift.C:59
 MUONOfflineShift.C:60
 MUONOfflineShift.C:61
 MUONOfflineShift.C:62
 MUONOfflineShift.C:63
 MUONOfflineShift.C:64
 MUONOfflineShift.C:65
 MUONOfflineShift.C:66
 MUONOfflineShift.C:67
 MUONOfflineShift.C:68
 MUONOfflineShift.C:69
 MUONOfflineShift.C:70
 MUONOfflineShift.C:71
 MUONOfflineShift.C:72
 MUONOfflineShift.C:73
 MUONOfflineShift.C:74
 MUONOfflineShift.C:75
 MUONOfflineShift.C:76
 MUONOfflineShift.C:77
 MUONOfflineShift.C:78
 MUONOfflineShift.C:79
 MUONOfflineShift.C:80
 MUONOfflineShift.C:81
 MUONOfflineShift.C:82
 MUONOfflineShift.C:83
 MUONOfflineShift.C:84
 MUONOfflineShift.C:85
 MUONOfflineShift.C:86
 MUONOfflineShift.C:87
 MUONOfflineShift.C:88
 MUONOfflineShift.C:89
 MUONOfflineShift.C:90
 MUONOfflineShift.C:91
 MUONOfflineShift.C:92
 MUONOfflineShift.C:93
 MUONOfflineShift.C:94
 MUONOfflineShift.C:95
 MUONOfflineShift.C:96
 MUONOfflineShift.C:97
 MUONOfflineShift.C:98
 MUONOfflineShift.C:99
 MUONOfflineShift.C:100
 MUONOfflineShift.C:101
 MUONOfflineShift.C:102
 MUONOfflineShift.C:103
 MUONOfflineShift.C:104
 MUONOfflineShift.C:105
 MUONOfflineShift.C:106
 MUONOfflineShift.C:107
 MUONOfflineShift.C:108
 MUONOfflineShift.C:109
 MUONOfflineShift.C:110
 MUONOfflineShift.C:111
 MUONOfflineShift.C:112
 MUONOfflineShift.C:113
 MUONOfflineShift.C:114
 MUONOfflineShift.C:115
 MUONOfflineShift.C:116
 MUONOfflineShift.C:117
 MUONOfflineShift.C:118
 MUONOfflineShift.C:119
 MUONOfflineShift.C:120
 MUONOfflineShift.C:121
 MUONOfflineShift.C:122
 MUONOfflineShift.C:123
 MUONOfflineShift.C:124
 MUONOfflineShift.C:125
 MUONOfflineShift.C:126
 MUONOfflineShift.C:127
 MUONOfflineShift.C:128
 MUONOfflineShift.C:129
 MUONOfflineShift.C:130
 MUONOfflineShift.C:131
 MUONOfflineShift.C:132
 MUONOfflineShift.C:133
 MUONOfflineShift.C:134
 MUONOfflineShift.C:135
 MUONOfflineShift.C:136
 MUONOfflineShift.C:137
 MUONOfflineShift.C:138
 MUONOfflineShift.C:139
 MUONOfflineShift.C:140
 MUONOfflineShift.C:141
 MUONOfflineShift.C:142
 MUONOfflineShift.C:143
 MUONOfflineShift.C:144
 MUONOfflineShift.C:145
 MUONOfflineShift.C:146
 MUONOfflineShift.C:147
 MUONOfflineShift.C:148
 MUONOfflineShift.C:149
 MUONOfflineShift.C:150
 MUONOfflineShift.C:151
 MUONOfflineShift.C:152
 MUONOfflineShift.C:153
 MUONOfflineShift.C:154
 MUONOfflineShift.C:155
 MUONOfflineShift.C:156
 MUONOfflineShift.C:157
 MUONOfflineShift.C:158
 MUONOfflineShift.C:159
 MUONOfflineShift.C:160
 MUONOfflineShift.C:161
 MUONOfflineShift.C:162
 MUONOfflineShift.C:163
 MUONOfflineShift.C:164
 MUONOfflineShift.C:165
 MUONOfflineShift.C:166
 MUONOfflineShift.C:167
 MUONOfflineShift.C:168
 MUONOfflineShift.C:169
 MUONOfflineShift.C:170
 MUONOfflineShift.C:171
 MUONOfflineShift.C:172
 MUONOfflineShift.C:173
 MUONOfflineShift.C:174
 MUONOfflineShift.C:175
 MUONOfflineShift.C:176
 MUONOfflineShift.C:177
 MUONOfflineShift.C:178
 MUONOfflineShift.C:179
 MUONOfflineShift.C:180
 MUONOfflineShift.C:181
 MUONOfflineShift.C:182
 MUONOfflineShift.C:183
 MUONOfflineShift.C:184
 MUONOfflineShift.C:185
 MUONOfflineShift.C:186
 MUONOfflineShift.C:187
 MUONOfflineShift.C:188
 MUONOfflineShift.C:189
 MUONOfflineShift.C:190
 MUONOfflineShift.C:191
 MUONOfflineShift.C:192
 MUONOfflineShift.C:193
 MUONOfflineShift.C:194
 MUONOfflineShift.C:195
 MUONOfflineShift.C:196
 MUONOfflineShift.C:197
 MUONOfflineShift.C:198
 MUONOfflineShift.C:199
 MUONOfflineShift.C:200
 MUONOfflineShift.C:201
 MUONOfflineShift.C:202
 MUONOfflineShift.C:203
 MUONOfflineShift.C:204
 MUONOfflineShift.C:205
 MUONOfflineShift.C:206
 MUONOfflineShift.C:207
 MUONOfflineShift.C:208
 MUONOfflineShift.C:209
 MUONOfflineShift.C:210
 MUONOfflineShift.C:211
 MUONOfflineShift.C:212
 MUONOfflineShift.C:213
 MUONOfflineShift.C:214
 MUONOfflineShift.C:215
 MUONOfflineShift.C:216
 MUONOfflineShift.C:217
 MUONOfflineShift.C:218
 MUONOfflineShift.C:219
 MUONOfflineShift.C:220
 MUONOfflineShift.C:221
 MUONOfflineShift.C:222
 MUONOfflineShift.C:223
 MUONOfflineShift.C:224
 MUONOfflineShift.C:225
 MUONOfflineShift.C:226
 MUONOfflineShift.C:227
 MUONOfflineShift.C:228
 MUONOfflineShift.C:229
 MUONOfflineShift.C:230
 MUONOfflineShift.C:231
 MUONOfflineShift.C:232
 MUONOfflineShift.C:233
 MUONOfflineShift.C:234
 MUONOfflineShift.C:235
 MUONOfflineShift.C:236
 MUONOfflineShift.C:237
 MUONOfflineShift.C:238
 MUONOfflineShift.C:239
 MUONOfflineShift.C:240
 MUONOfflineShift.C:241
 MUONOfflineShift.C:242
 MUONOfflineShift.C:243
 MUONOfflineShift.C:244
 MUONOfflineShift.C:245
 MUONOfflineShift.C:246
 MUONOfflineShift.C:247
 MUONOfflineShift.C:248
 MUONOfflineShift.C:249
 MUONOfflineShift.C:250
 MUONOfflineShift.C:251
 MUONOfflineShift.C:252
 MUONOfflineShift.C:253
 MUONOfflineShift.C:254
 MUONOfflineShift.C:255
 MUONOfflineShift.C:256
 MUONOfflineShift.C:257
 MUONOfflineShift.C:258
 MUONOfflineShift.C:259
 MUONOfflineShift.C:260
 MUONOfflineShift.C:261
 MUONOfflineShift.C:262
 MUONOfflineShift.C:263
 MUONOfflineShift.C:264
 MUONOfflineShift.C:265
 MUONOfflineShift.C:266
 MUONOfflineShift.C:267
 MUONOfflineShift.C:268
 MUONOfflineShift.C:269
 MUONOfflineShift.C:270
 MUONOfflineShift.C:271
 MUONOfflineShift.C:272
 MUONOfflineShift.C:273