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