]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRKOCCda.cxx
Adding data corruption recovery procedure for magnetic field = 0 runs. (http://savann...
[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: 09000094301009.10.raw
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 "AliRawReaderDate.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 #include "monitor.h"
54 #include "signal.h"
55
56 #ifdef ALI_AMORE
57 #include <AmoreDA.h>
58 #include "TObjString.h"
59 #include "TSystem.h"
60 #include <sstream>
61 #endif
62
63 const char* OUTPUT_FILE = "mch.occupancy";
64 const char* DAVERSION = "MUONTRKOCCda v1.4 ($Id$)";
65
66 //______________________________________________________________________________
67 void Add(AliMUONVStore& destStore, const AliMUONVStore& srcStore)
68 {
69   /// Add all elements from srcStore to destStore
70   /// Each element of srcStore is supposed to be an AliMUONCalibParamNI,
71   /// with ID0=busPatchId and ID1=manuId
72   
73   TIter next(srcStore.CreateIterator());
74   AliMUONVCalibParam* source;
75   
76   while ( ( source = static_cast<AliMUONVCalibParam*>(next()) ) )
77   {
78     AliMUONCalibParamNI* dest = static_cast<AliMUONCalibParamNI*>(destStore.FindObject(source->ID0(),source->ID1()));
79     if (!dest)
80     {
81       dest = static_cast<AliMUONCalibParamNI*>(source->Clone());
82       destStore.Add(dest);
83     }
84     else
85     {
86       for ( Int_t i = 0; i < source->Size(); ++i ) 
87       {
88         for ( Int_t j = 0; j  < source->Dimension(); ++j ) 
89         {
90           dest->SetValueAsIntFast(i,j,dest->ValueAsIntFast(i,j)+source->ValueAsIntFast(i,j));
91         }
92       }
93     }
94   }
95 }
96
97 //______________________________________________________________________________
98 void GenerateOutputFile(const AliMUONVStore& store, ostream& out, 
99                         Int_t runNumber, Int_t nevents)
100 {
101   /// Write the channel hit count (grouped by manu) in the output file.
102   
103   TIter next(store.CreateIterator());
104   AliMUONVCalibParam* manu;
105   
106   out << "//===========================================================================" << endl;
107   out << "//  Hit counter file calculated by " << __FILE__ << endl;
108   out << "//===========================================================================" << endl;
109   out << "//" << endl;
110   out << "//       * Run Number          : " << runNumber << endl;
111   out << "//       * File Creation Date  : " << TTimeStamp().AsString("l") << endl;
112   out << "//---------------------------------------------------------------------------" << endl;
113   out << "//  BP   MANU  SUM_N  NEVENTS" << endl;
114   out << "//---------------------------------------------------------------------------" << endl;
115   
116   while ( ( manu = static_cast<AliMUONVCalibParam*>(next()) ) )
117   {
118     Int_t sum(0);
119 //    Int_t nevents(0);
120     
121     for ( Int_t i = 0; i < manu->Size(); ++i ) 
122     {
123       sum += manu->ValueAsInt(i);
124       //      nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
125       // nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
126     }
127     
128     out << Form("%5d %5d %10d %10d",manu->ID0(),manu->ID1(),sum,nevents) << endl;
129   }
130 }
131   
132 //______________________________________________________________________________
133 int main(int argc, char **argv) 
134 {
135   /// Main method.
136   ///
137   /// We loop over all physics events.
138   /// For each event we store the channels that were hit for that event.
139   /// If the event is good, we then increment the list of channels hit for
140   /// the whole run.
141   /// We delete the store for a single event and move to next event.
142   ///
143   /// In the end we output an ASCII file with the necessary information 
144   /// to compute the occupancy later on, i.e. the number of times channels
145   /// were seen per manu, and the number of events.
146   ///
147   
148   signal(SIGSEGV,SIG_DFL); // to be able to get core dumps...
149   
150   TStopwatch timers;
151   timers.Start(kTRUE); 
152   
153   ios::sync_with_stdio();
154
155   cout << "Running " << DAVERSION << endl;
156   
157   if ( argc < 2 ) 
158   {
159     cout << "Wrong number of arguments" << endl;
160     cout << "Usage : " << argv[0] << " datasource1 [datasource2] ..." << endl;
161     return -1;
162   }
163   
164   // needed for streamer application
165   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
166                                         "*",
167                                         "TStreamerInfo",
168                                         "RIO",
169                                         "TStreamerInfo()");
170   
171   Int_t numberOfEvents(0);
172   Int_t numberOfPhysicsEvent(0);
173   Int_t numberOfBadEvents(0);
174   Int_t numberOfUsedEvents(0);
175   
176   AliMUON2DMap oneEventData(kTRUE);
177   AliMUON2DMap accumulatedData(kTRUE);
178   
179   UInt_t runNumber(0);
180
181   for ( Int_t i = 1; i < argc; ++i ) 
182   {    
183     int status;
184     AliRawReaderDate* rawReader(0x0);
185     
186 // define data source : 
187    status=monitorSetDataSource(argv[i]);
188     if (status!=0) 
189     {
190       printf("MCH Occupancy DA ERROR: monitorSetDataSource() failed: %s\n", monitorDecodeError(status));
191       return -1;
192     }
193     
194 // Declare monitoring program 
195     status=monitorDeclareMp("MUON_TRK_OCC");
196     if (status!=0) 
197     {
198       printf("MCH Occupancy DA ERROR: monitorDeclareMp() failed: %s\n", monitorDecodeError(status));
199       return -1;
200     }
201 // Define wait event timeout - 1s max
202     monitorSetNowait();
203     monitorSetNoWaitNetworkTimeout(1000);
204     
205     for(;;)
206     {
207       struct eventHeaderStruct *event(0x0);
208       eventTypeType eventT;
209       
210       status=monitorGetEventDynamic((void **)&event);
211       if (status!=0)
212       {
213         printf("MCH Occupancy DA ERROR: %s\n", monitorDecodeError(status));
214         delete event;
215         break;
216       }
217
218       /* check shutdown condition */
219       if (daqDA_checkShutdown())
220       {
221         delete event;
222         break;
223       }
224       
225       /* retry if got no event */
226       if (event==NULL) continue;
227
228       ++numberOfEvents;
229
230       eventT=event->eventType;
231       if ((eventT == END_OF_RUN)||(eventT == END_OF_RUN_FILES)) 
232       {
233         delete event;
234         break;
235       }
236       if (eventT != PHYSICS_EVENT) 
237       {
238         delete event;
239         continue;
240       }
241                  
242       ++numberOfPhysicsEvent;
243       
244       rawReader = new AliRawReaderDate((void*)event);
245       
246       if ( rawReader->GetRunNumber() != runNumber )
247       {
248         if ( runNumber != 0 ) 
249         {
250           cout << "Uh oh. That's bad... Changing of run number ???" << endl;
251           delete event;
252           delete rawReader;
253           return -9999;
254         }
255         runNumber = rawReader->GetRunNumber();
256       }
257       
258       AliMUONRawStreamTrackerHP stream(rawReader);
259       
260       stream.DisableWarnings();
261       
262       oneEventData.Clear();
263       
264       Int_t buspatchId;
265       UShort_t  manuId;
266       UChar_t manuChannel;
267       UShort_t adc;
268       
269       stream.First();
270             
271       while ( stream.Next(buspatchId,manuId,manuChannel,adc,kTRUE) )
272       {    
273         AliMUONVCalibParam* one = static_cast<AliMUONVCalibParam*>(oneEventData.FindObject(buspatchId,manuId));
274         
275         if (!one)
276         {
277           one = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),buspatchId,manuId);
278           oneEventData.Add(one);
279         }
280         
281         one->SetValueAsInt(manuChannel,0,one->ValueAsInt(manuChannel,0)+1);
282       }
283       
284       Bool_t badEvent = stream.HasPaddingError() || stream.HasGlitchError();
285       
286       if ( !badEvent )
287       {
288         ++numberOfUsedEvents;
289         Add(accumulatedData,oneEventData);
290       }
291       else
292       {
293         ++numberOfBadEvents;
294       }
295
296       delete event;
297       
298       delete rawReader;
299     }    
300   }
301   
302   
303   cout << Form("%12d events processed : %12d physics %d used ones %d bad ones",
304                numberOfEvents,numberOfPhysicsEvent,numberOfUsedEvents,numberOfBadEvents) << endl;
305     
306   ofstream fout(OUTPUT_FILE);
307   
308   GenerateOutputFile(accumulatedData,fout,runNumber,numberOfUsedEvents);
309   
310   fout.close();
311   
312 #ifdef ALI_AMORE
313   
314   if ( numberOfUsedEvents ) // do not update the AMORE pool with an empty object...
315   {
316     // Send occupancy store (as a big string) to the AMORE DB
317     amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
318     
319     ostringstream str;
320     
321     GenerateOutputFile(accumulatedData,str,runNumber,numberOfUsedEvents);
322     
323     TObjString occupancyAsString(str.str().c_str());
324     
325     Int_t status = amoreDA.Send("Occupancy",&occupancyAsString);
326     if ( status )
327     {
328       cerr << "ERROR : Failed to write occupancies in the AMORE database : " << status << endl;
329     } 
330   }
331
332 #endif
333   
334   /* store the result file on FXS */  
335   if (daqDA_FES_storeFile(OUTPUT_FILE,"OCCUPANCY")) return -9;
336
337   timers.Stop();
338   printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
339
340   return 0;
341 }