]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRKOCCda.cxx
Trigger scalers added to ESD header.
[u/mrichter/AliRoot.git] / MUON / MUONTRKOCCda.cxx
1 /*
2  MCH DA for online occupancy
3  
4  Contact: Laurent Aphecetche <laurent.aphecetche@subatech.in2p3.fr>, Jean-Luc Charvet <jean-luc.charvet@cea.fr>, Alberto Baldisseri <alberto.baldisseri@cea.fr>
5  Link: 
6  Run Type: PHYSICS STANDALONE
7  DA Type: MON
8  Number of events needed: all (or at least as much as possible...)
9  Input Files:
10  Output Files: mch.occupancy, to be exported to the DAQ FXS
11  Trigger types used: PHYSICS_EVENT
12 */
13
14 /**************************************************************************
15  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
16  *                                                                        *
17  * Author: The ALICE Off-line Project.                                    *
18  * Contributors are mentioned in the code where appropriate.              *
19  *                                                                        *
20  * Permission to use, copy, modify and distribute this software and its   *
21  * documentation strictly for non-commercial purposes is hereby granted   *
22  * without fee, provided that the above copyright notice appears in all   *
23  * copies and that both the copyright notice and this permission notice   *
24  * appear in the supporting documentation. The authors make no claims     *
25  * about the suitability of this software for any purpose. It is          *
26  * provided "as is" without express or implied warranty.                  *
27  **************************************************************************/
28
29 ///
30 /// MUON TRACKER DA to compute the hit count at manu level.
31 ///
32 /// In the end, this DA produces an ASCII file containing
33 /// the hit count of all the manus (that were seen in the data flow)
34 /// of the MUON Tracker (and the number of seen events, so we can
35 /// later on compute the occupancy)
36 ///
37 /// $Id$
38
39 #include "AliMUON2DMap.h"
40 #include "AliMUONCalibParamNI.h"
41 #include "AliMUONRawStreamTrackerHP.h"
42 #include "AliMpConstants.h"
43 #include "AliRawEventHeaderBase.h"
44 #include "AliRawReader.h"
45 #include "Riostream.h"
46 #include "TPluginManager.h"
47 #include "TROOT.h"
48 #include "TString.h"
49 #include "TTimeStamp.h"
50 #include "TStopwatch.h"
51 #include "daqDA.h"
52 #include "event.h"
53
54 #ifdef ALI_AMORE
55 #include <AmoreDA.h>
56 #endif
57
58 const char* OUTPUT_FILE = "mch.occupancy";
59 const char* DAVERSION = "MUONTRKOCCda v1.0";
60
61 //______________________________________________________________________________
62 void Add(AliMUONVStore& destStore, const AliMUONVStore& srcStore)
63 {
64   /// Add all elements from srcStore to destStore
65   /// Each element of srcStore is supposed to be an AliMUONCalibParamNI,
66   /// with ID0=busPatchId and ID1=manuId
67   
68   TIter next(srcStore.CreateIterator());
69   AliMUONVCalibParam* source;
70   
71   while ( ( source = static_cast<AliMUONVCalibParam*>(next()) ) )
72   {
73     AliMUONCalibParamNI* dest = static_cast<AliMUONCalibParamNI*>(destStore.FindObject(source->ID0(),source->ID1()));
74     if (!dest)
75     {
76       dest = static_cast<AliMUONCalibParamNI*>(source->Clone());
77       destStore.Add(dest);
78     }
79     else
80     {
81       for ( Int_t i = 0; i < source->Size(); ++i ) 
82       {
83         for ( Int_t j = 0; j  < source->Dimension(); ++j ) 
84         {
85           dest->SetValueAsInt(i,j,dest->ValueAsInt(i,j)+source->ValueAsInt(i,j));
86         }
87       }
88     }
89   }
90 }
91
92 //______________________________________________________________________________
93 void GenerateOutputFile(const AliMUONVStore& store, ostream& out, 
94                         Int_t runNumber, Int_t nevents)
95 {
96   /// Write the channel hit count (grouped by manu) in the output file.
97   
98   TIter next(store.CreateIterator());
99   AliMUONVCalibParam* manu;
100   
101   out << "//===========================================================================" << endl;
102   out << "//  Hit counter file calculated by " << __FILE__ << endl;
103   out << "//===========================================================================" << endl;
104   out << "//" << endl;
105   out << "//       * Run Number          : " << runNumber << endl;
106   out << "//       * File Creation Date  : " << TTimeStamp().AsString("l") << endl;
107   out << "//---------------------------------------------------------------------------" << endl;
108   out << "//  BP   MANU  SUM_N  NEVENTS" << endl;
109   out << "//---------------------------------------------------------------------------" << endl;
110   
111   while ( ( manu = static_cast<AliMUONVCalibParam*>(next()) ) )
112   {
113     Int_t sum(0);
114 //    Int_t nevents(0);
115     
116     for ( Int_t i = 0; i < manu->Size(); ++i ) 
117     {
118       sum += manu->ValueAsInt(i);
119       //      nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
120       // nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
121     }
122     
123     out << Form("%5d %5d %10d %10d",manu->ID0(),manu->ID1(),sum,nevents) << endl;
124   }
125 }
126   
127 //______________________________________________________________________________
128 int main(int argc, char **argv) 
129 {
130   /// Main method.
131   ///
132   /// We loop over all physics events.
133   /// For each event we store the channels that were hit for that event.
134   /// If the event is good, we then increment the list of channels hit for
135   /// the whole run.
136   /// We delete the store for a single event and move to next event.
137   ///
138   /// In the end we output an ASCII file with the necessary information 
139   /// to compute the occupancy later on, i.e. the number of times channels
140   /// were seen per manu, and the number of events.
141   ///
142   
143   TStopwatch timers;
144   timers.Start(kTRUE); 
145   
146   ios::sync_with_stdio();
147
148   cout << "Running " << DAVERSION << endl;
149   
150   if ( argc < 2 ) 
151   {
152     cout << "Wrong number of arguments" << endl;
153     cout << "Usage : " << argv[0] << " datasource1 [datasource2] ..." << endl;
154     return -1;
155   }
156   
157   // needed for streamer application
158   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
159                                         "*",
160                                         "TStreamerInfo",
161                                         "RIO",
162                                         "TStreamerInfo()");
163   
164   Int_t numberOfEvents(0);
165   Int_t numberOfPhysicsEvent(0);
166   Int_t numberOfBadEvents(0);
167   Int_t numberOfUsedEvents(0);
168   
169   AliMUON2DMap oneEventData(kTRUE);
170   AliMUON2DMap accumulatedData(kTRUE);
171   
172   UInt_t runNumber(0);
173
174   for ( Int_t i = 1; i < argc; ++i ) 
175   {    
176     AliRawReader* rawReader = AliRawReader::Create(argv[i]);
177
178     while ( rawReader->NextEvent() ) 
179     {
180       /* check shutdown condition */
181       if (daqDA_checkShutdown()) break;
182     
183       ++numberOfEvents;
184      
185       if ( rawReader->GetType() == AliRawEventHeaderBase::kEndOfRun )
186       {
187         printf("EOR event detected\n");
188         break;
189       }
190     
191       if ( rawReader->GetType() != AliRawEventHeaderBase::kPhysicsEvent ) continue;
192     
193       ++numberOfPhysicsEvent;
194       
195       if ( rawReader->GetRunNumber() != runNumber )
196       {
197         if ( runNumber != 0 ) 
198         {
199           cout << "Uh oh. That's bad... Changing of run number ???" << endl;
200           return -9999;
201         }
202         runNumber = rawReader->GetRunNumber();
203       }
204       
205       AliMUONRawStreamTrackerHP stream(rawReader);
206       
207       stream.DisableWarnings();
208       stream.EnabbleErrorLogger();
209       
210       oneEventData.Clear();
211       
212       Int_t buspatchId;
213       UShort_t  manuId;
214       UChar_t manuChannel;
215       UShort_t adc;
216       
217       stream.First();
218       
219       while ( stream.Next(buspatchId,manuId,manuChannel,adc,kTRUE) )
220       {    
221         AliMUONVCalibParam* one = static_cast<AliMUONVCalibParam*>(oneEventData.FindObject(buspatchId,manuId));
222         
223         if (!one)
224         {
225           one = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),buspatchId,manuId);
226           oneEventData.Add(one);
227         }
228         
229         one->SetValueAsInt(manuChannel,0,one->ValueAsInt(manuChannel,0)+1);
230       }
231       
232       Bool_t badEvent = stream.HasPaddingError() || stream.HasGlitchError();
233       
234       if ( !badEvent )
235       {
236         ++numberOfUsedEvents;
237         Add(accumulatedData,oneEventData);
238       }
239       else
240       {
241         ++numberOfBadEvents;
242       }
243     }
244     
245     delete rawReader;
246   }
247   
248   
249   cout << Form("%12d events processed : %12d physics %d used ones %d bad ones",
250                numberOfEvents,numberOfPhysicsEvent,numberOfUsedEvents,numberOfBadEvents) << endl;
251     
252   ofstream fout(OUTPUT_FILE);
253   
254   GenerateOutputFile(accumulatedData,fout,runNumber,numberOfUsedEvents);
255   
256   fout.close();
257   
258 #ifdef ALI_AMORE
259   
260   // Send occupancy store (as a big string) to the AMORE DB
261   
262   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
263   
264   ostringstream str;
265   
266   GenerateOutputFile(accumulatedData,str,runNumber,numberOfUsedEvents);
267   
268   str.close();
269   
270   TObjString occupancyAsString(str.str().c_str());
271   
272   Int_t status = amoreDA.Send("Occupancy",&occupanyAsString);
273   if ( status )
274   {
275     cerr << "ERROR : Failed to write occupancies in the AMORE database : " << status << endl;
276   } 
277
278 #endif
279   
280   /* store the result file on FXS */  
281   if (daqDA_FES_storeFile(OUTPUT_FILE,"OCCUPANCY")) return -9;
282
283   timers.Stop();
284   printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
285
286   return 0;
287 }